use of org.apache.axis2.context.MessageContext in project wso2-axis2-transports by wso2.
the class AxisRequestResponseTestClient method sendMessage.
public IncomingMessage<AxisMessage> sendMessage(ClientOptions options, ContentType contentType, AxisMessage message) throws Exception {
MessageContext responseMsgContext = send(options, message, ServiceClient.ANON_OUT_IN_OP, true, WSDLConstants.MESSAGE_LABEL_IN_VALUE);
Assert.assertFalse(responseMsgContext.isServerSide());
for (MessageContextValidator validator : validators) {
validator.validate(responseMsgContext, true);
}
return new IncomingMessage<AxisMessage>(null, new AxisMessage(responseMsgContext));
}
use of org.apache.axis2.context.MessageContext in project wso2-axis2-transports by wso2.
the class UDPSender method waitForReply.
private void waitForReply(MessageContext messageContext, DatagramSocket datagramSocket, String contentType) throws IOException {
// message context in asnych model
if (!(messageContext.getAxisOperation() instanceof OutInAxisOperation) && messageContext.getProperty(org.apache.axis2.Constants.PIGGYBACK_MESSAGE) == null) {
return;
}
// TODO set the maximum size parameter
byte[] inputBuffer = new byte[4096];
DatagramPacket packet = new DatagramPacket(inputBuffer, inputBuffer.length);
datagramSocket.receive(packet);
// create the soap envelope
try {
MessageContext respMessageContext = messageContext.getOperationContext().getMessageContext(WSDL2Constants.MESSAGE_LABEL_IN);
InputStream inputStream = new ByteArrayInputStream(inputBuffer, 0, packet.getLength());
SOAPEnvelope envelope = TransportUtils.createSOAPMessage(respMessageContext, inputStream, contentType);
respMessageContext.setEnvelope(envelope);
} catch (XMLStreamException e) {
throw new AxisFault("Can not build the soap message ", e);
}
}
use of org.apache.axis2.context.MessageContext in project wso2-axis2-transports by wso2.
the class UDPSender method sendMessage.
@Override
public void sendMessage(MessageContext msgContext, String targetEPR, OutTransportInfo outTransportInfo) throws AxisFault {
UDPOutTransportInfo udpOutInfo;
if ((targetEPR == null) && (outTransportInfo != null)) {
// this can happen only at the server side and send the message using back chanel
udpOutInfo = (UDPOutTransportInfo) outTransportInfo;
} else {
udpOutInfo = new UDPOutTransportInfo(targetEPR);
}
MessageFormatter messageFormatter = MessageProcessorSelector.getMessageFormatter(msgContext);
OMOutputFormat format = BaseUtils.getOMOutputFormat(msgContext);
format.setContentType(udpOutInfo.getContentType());
byte[] payload = messageFormatter.getBytes(msgContext, format);
try {
DatagramSocket socket = new DatagramSocket();
if (log.isDebugEnabled()) {
log.debug("Sending " + payload.length + " bytes to " + udpOutInfo.getAddress());
}
try {
socket.send(new DatagramPacket(payload, payload.length, udpOutInfo.getAddress()));
if (!msgContext.getOptions().isUseSeparateListener() && !msgContext.isServerSide()) {
waitForReply(msgContext, socket, udpOutInfo.getContentType());
}
} finally {
socket.close();
}
} catch (IOException ex) {
throw new AxisFault("Unable to send packet", ex);
}
}
use of org.apache.axis2.context.MessageContext in project wso2-axis2-transports by wso2.
the class XMPPPacketListener method createSOAPEnvelopeForRawMessage.
/**
* Creates a SOAP envelope using details found in chat message.
* @param msgCtx
* @param chatMessage
* @return
*/
private SOAPEnvelope createSOAPEnvelopeForRawMessage(MessageContext msgCtx, String chatMessage) throws AxisFault {
// TODO : need to add error handling logic
String callRemoved = chatMessage.replaceFirst("call", "");
// extract Service name
String serviceName = callRemoved.trim().substring(0, callRemoved.indexOf(":") - 1);
String operationName = callRemoved.trim().substring(callRemoved.indexOf(":"), callRemoved.indexOf("(") - 1);
// Extract parameters from IM message
String parameterList = callRemoved.trim().substring(callRemoved.indexOf("("), callRemoved.trim().length() - 1);
StringTokenizer st = new StringTokenizer(parameterList, ",");
MultipleEntryHashMap parameterMap = new MultipleEntryHashMap();
while (st.hasMoreTokens()) {
String token = st.nextToken();
String name = token.substring(0, token.indexOf("="));
String value = token.substring(token.indexOf("=") + 1);
parameterMap.put(name, value);
}
SOAPEnvelope envelope = null;
try {
msgCtx.setProperty(XMPPConstants.CONTAINS_SOAP_ENVELOPE, new Boolean(true));
if (serviceName != null && serviceName.trim().length() > 0) {
AxisService axisService = msgCtx.getConfigurationContext().getAxisConfiguration().getService(serviceName);
msgCtx.setAxisService(axisService);
AxisOperation axisOperation = axisService.getOperationBySOAPAction("urn:" + operationName);
if (axisOperation != null) {
msgCtx.setAxisOperation(axisOperation);
}
}
if (operationName != null && operationName.trim().length() > 0) {
msgCtx.setSoapAction("urn:" + operationName);
}
XMPPOutTransportInfo xmppOutTransportInfo = (XMPPOutTransportInfo) msgCtx.getProperty(org.apache.axis2.Constants.OUT_TRANSPORT_INFO);
// This should be only set for messages received via chat.
// TODO : need to read from a constant
xmppOutTransportInfo.setContentType("xmpp/text");
msgCtx.setServerSide(true);
// TODO : need to support SOAP12 as well
SOAPFactory soapFactory = new SOAP11Factory();
envelope = BuilderUtil.buildsoapMessage(msgCtx, parameterMap, soapFactory);
// TODO : improve error handling & messages
} catch (AxisFault e) {
throw new AxisFault(e.getMessage());
} catch (OMException e) {
throw new AxisFault(e.getMessage());
} catch (FactoryConfigurationError e) {
throw new AxisFault(e.getMessage());
}
return envelope;
}
use of org.apache.axis2.context.MessageContext in project wso2-axis2-transports by wso2.
the class XMPPPacketListener method createMessageContext.
/**
* Creates message context using values received in XMPP packet
* @param packet
* @return MessageContext
* @throws AxisFault
*/
private MessageContext createMessageContext(Packet packet) throws AxisFault {
Message message = (Message) packet;
Boolean isServerSide = (Boolean) message.getProperty(XMPPConstants.IS_SERVER_SIDE);
String serviceName = (String) message.getProperty(XMPPConstants.SERVICE_NAME);
String action = (String) message.getProperty(XMPPConstants.ACTION);
MessageContext msgContext = null;
TransportInDescription transportIn = configurationContext.getAxisConfiguration().getTransportIn("xmpp");
TransportOutDescription transportOut = configurationContext.getAxisConfiguration().getTransportOut("xmpp");
if ((transportIn != null) && (transportOut != null)) {
msgContext = configurationContext.createMessageContext();
msgContext.setTransportIn(transportIn);
msgContext.setTransportOut(transportOut);
if (isServerSide != null) {
msgContext.setServerSide(isServerSide.booleanValue());
}
msgContext.setProperty(CONTENT_TYPE, "text/xml");
msgContext.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, "UTF-8");
msgContext.setIncomingTransportName("xmpp");
Map services = configurationContext.getAxisConfiguration().getServices();
AxisService axisService = (AxisService) services.get(serviceName);
msgContext.setAxisService(axisService);
msgContext.setSoapAction(action);
// pass the configurationFactory to transport sender
msgContext.setProperty("XMPPConfigurationFactory", this.xmppConnectionFactory);
if (packet.getFrom() != null) {
msgContext.setFrom(new EndpointReference(packet.getFrom()));
}
if (packet.getTo() != null) {
msgContext.setTo(new EndpointReference(packet.getTo()));
}
XMPPOutTransportInfo xmppOutTransportInfo = new XMPPOutTransportInfo();
xmppOutTransportInfo.setConnectionFactory(this.xmppConnectionFactory);
String packetFrom = packet.getFrom();
if (packetFrom != null) {
EndpointReference fromEPR = new EndpointReference(packetFrom);
xmppOutTransportInfo.setFrom(fromEPR);
xmppOutTransportInfo.setDestinationAccount(packetFrom);
}
// Save Message-Id to set as In-Reply-To on reply
String xmppMessageId = packet.getPacketID();
if (xmppMessageId != null) {
xmppOutTransportInfo.setInReplyTo(xmppMessageId);
}
xmppOutTransportInfo.setSequenceID((String) message.getProperty(XMPPConstants.SEQUENCE_ID));
msgContext.setProperty(org.apache.axis2.Constants.OUT_TRANSPORT_INFO, xmppOutTransportInfo);
buildSOAPEnvelope(packet, msgContext);
} else {
throw new AxisFault("Either transport in or transport out is null");
}
return msgContext;
}
Aggregations