Search in sources :

Example 11 with MessageContext

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));
}
Also used : IncomingMessage(org.apache.axis2.transport.testkit.message.IncomingMessage) MessageContext(org.apache.axis2.context.MessageContext) MessageContextValidator(org.apache.axis2.transport.testkit.axis2.MessageContextValidator) AxisMessage(org.apache.axis2.transport.testkit.message.AxisMessage)

Example 12 with MessageContext

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);
    }
}
Also used : AxisFault(org.apache.axis2.AxisFault) XMLStreamException(javax.xml.stream.XMLStreamException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DatagramPacket(java.net.DatagramPacket) MessageContext(org.apache.axis2.context.MessageContext) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) OutInAxisOperation(org.apache.axis2.description.OutInAxisOperation)

Example 13 with MessageContext

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);
    }
}
Also used : AxisFault(org.apache.axis2.AxisFault) DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) MessageFormatter(org.apache.axis2.transport.MessageFormatter) IOException(java.io.IOException) OMOutputFormat(org.apache.axiom.om.OMOutputFormat)

Example 14 with MessageContext

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;
}
Also used : AxisFault(org.apache.axis2.AxisFault) AxisOperation(org.apache.axis2.description.AxisOperation) AxisService(org.apache.axis2.description.AxisService) MultipleEntryHashMap(org.apache.axis2.util.MultipleEntryHashMap) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) SOAPFactory(org.apache.axiom.soap.SOAPFactory) StringTokenizer(java.util.StringTokenizer) SOAP11Factory(org.apache.axiom.soap.impl.llom.soap11.SOAP11Factory) OMException(org.apache.axiom.om.OMException) FactoryConfigurationError(javax.xml.parsers.FactoryConfigurationError)

Example 15 with MessageContext

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;
}
Also used : AxisFault(org.apache.axis2.AxisFault) Message(org.jivesoftware.smack.packet.Message) AxisService(org.apache.axis2.description.AxisService) MessageContext(org.apache.axis2.context.MessageContext) TransportInDescription(org.apache.axis2.description.TransportInDescription) Map(java.util.Map) MultipleEntryHashMap(org.apache.axis2.util.MultipleEntryHashMap) TransportOutDescription(org.apache.axis2.description.TransportOutDescription) EndpointReference(org.apache.axis2.addressing.EndpointReference)

Aggregations

MessageContext (org.apache.axis2.context.MessageContext)35 AxisFault (org.apache.axis2.AxisFault)25 OMElement (org.apache.axiom.om.OMElement)13 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)12 IOException (java.io.IOException)11 OMOutputFormat (org.apache.axiom.om.OMOutputFormat)11 XMLStreamException (javax.xml.stream.XMLStreamException)9 QName (javax.xml.namespace.QName)8 MessageFormatter (org.apache.axis2.transport.MessageFormatter)8 AxisOperation (org.apache.axis2.description.AxisOperation)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 InputStream (java.io.InputStream)5 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)5 SOAPFactory (org.apache.axiom.soap.SOAPFactory)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 OMFactory (org.apache.axiom.om.OMFactory)4 Options (org.apache.axis2.client.Options)4 Message (org.jivesoftware.smack.packet.Message)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 OutputStream (java.io.OutputStream)3