* @param msgCtx the axis2 message context
* @throws AxisFault on error
*/
public void sendMessage(MessageContext msgCtx, String targetAddress,
OutTransportInfo outTransportInfo) throws AxisFault {
XMPPConnection xmppConnection = null;
XMPPOutTransportInfo xmppOutTransportInfo = null;
//if on client side,create connection to xmpp server
if(!msgCtx.isServerSide()){
connectUsingClientOptions(msgCtx);
}
Message message = new Message();
Options options = msgCtx.getOptions();
String serviceName = XMPPUtils.getServiceName(targetAddress);
if (targetAddress != null) {
xmppOutTransportInfo = new XMPPOutTransportInfo(targetAddress);
xmppOutTransportInfo.setConnectionFactory(connectionFactory);
} else if (msgCtx.getTo() != null &&
!msgCtx.getTo().hasAnonymousAddress()) {
//TODO
} else if (msgCtx.isServerSide()) {
xmppOutTransportInfo = (XMPPOutTransportInfo)
msgCtx.getProperty(Constants.OUT_TRANSPORT_INFO);
}
xmppConnection = xmppOutTransportInfo.getConnectionFactory().getXmppConnection();
if(msgCtx.isServerSide()){
message.setProperty(XMPPConstants.IS_SERVER_SIDE, new Boolean(false));
message.setProperty(XMPPConstants.IN_REPLY_TO, xmppOutTransportInfo.getInReplyTo());
}else{
//message is going to be processed on server side
message.setProperty(XMPPConstants.IS_SERVER_SIDE,new Boolean(true));
//we are sending a soap envelope as a message
message.setProperty(XMPPConstants.CONTAINS_SOAP_ENVELOPE, new Boolean(true));
message.setProperty(XMPPConstants.SERVICE_NAME, serviceName);
String action = options.getAction();
if (action == null) {
AxisOperation axisOperation = msgCtx.getAxisOperation();
if (axisOperation != null) {
action = axisOperation.getSoapAction();
}
}
if (action != null) {
message.setProperty(XMPPConstants.ACTION, action);
}
}
if(xmppConnection == null){
handleException("Connection to XMPP Server is not established.");
}
//initialize the chat manager using connection
ChatManager chatManager = xmppConnection.getChatManager();
Chat chat = chatManager.createChat(xmppOutTransportInfo.getDestinationAccount(), null);
try
{
boolean waitForResponse =
msgCtx.getOperationContext() != null &&
WSDL2Constants.MEP_URI_OUT_IN.equals(
msgCtx.getOperationContext().getAxisOperation().getMessageExchangePattern());
//int endOfXMLDeclaration = soapMessage.indexOf("?>");
//String modifiedSOAPMessage = soapMessage.substring(endOfXMLDeclaration+2);
OMElement msgElement;
String messageToBeSent = "";
//TODO : need to read from a constant
if("xmpp/text".equals(xmppOutTransportInfo.getContentType())){
//if request is received from a chat client, whole soap envelope
//should not be sent.
OMElement soapBodyEle = msgCtx.getEnvelope().getBody();
OMElement responseEle = soapBodyEle.getFirstElement();
if(responseEle != null){
msgElement = responseEle.getFirstElement();
}else{
msgElement = responseEle;
}
}else{
//if request received from a ws client whole soap envelope
//must be sent.
msgElement = msgCtx.getEnvelope();
}
messageToBeSent = msgElement.toString();
message.setBody(messageToBeSent);
XMPPClientSidePacketListener xmppClientSidePacketListener = null;
if(waitForResponse && !msgCtx.isServerSide()){
PacketFilter filter = new PacketTypeFilter(message.getClass());
xmppClientSidePacketListener = new XMPPClientSidePacketListener(msgCtx);
xmppConnection.addPacketListener(xmppClientSidePacketListener,filter);
}
chat.sendMessage(message);
log.debug("Sent message :"+message.toXML());
//If this is on client side, wait for the response from server.
//Is this the best way to do this?
if(waitForResponse && !msgCtx.isServerSide()){
//TODO : need to add a timeout
while(! xmppClientSidePacketListener.isResponseReceived()){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
log.debug("Sleep interrupted",e);
}
}
xmppConnection.disconnect();
}
} catch (XMPPException e) {
log.error("Error occurred while sending the message : "+message.toXML(),e);
handleException("Error occurred while sending the message : "+message.toXML(),e);
}finally{
if(!msgCtx.isServerSide()){
xmppConnection.disconnect();
}
}
}