Search in sources :

Example 6 with SOAP11Factory

use of org.apache.axiom.soap.impl.dom.soap11.SOAP11Factory in project wso2-synapse by wso2.

the class DeferredMessageBuilder method getDocument.

public OMElement getDocument(MessageContext msgCtx, InputStream in) throws XMLStreamException, IOException {
    /**
     * HTTP Delete requests may contain entity body or not. Hence if the request is a HTTP DELETE, we have to verify
     * that the payload stream is empty or not.
     */
    if (HTTPConstants.HEADER_DELETE.equals(msgCtx.getProperty(Constants.Configuration.HTTP_METHOD)) && RelayUtils.isEmptyPayloadStream(in)) {
        msgCtx.setProperty(PassThroughConstants.NO_ENTITY_BODY, Boolean.TRUE);
        return TransportUtils.createSOAPEnvelope(null);
    }
    String contentType = (String) msgCtx.getProperty(Constants.Configuration.CONTENT_TYPE);
    String _contentType = getContentType(contentType, msgCtx);
    in = HTTPTransportUtils.handleGZip(msgCtx, in);
    AxisConfiguration configuration = msgCtx.getConfigurationContext().getAxisConfiguration();
    Parameter useFallbackParameter = configuration.getParameter(Constants.Configuration.USE_DEFAULT_FALLBACK_BUILDER);
    boolean useFallbackBuilder = false;
    if (useFallbackParameter != null) {
        useFallbackBuilder = JavaUtils.isTrueExplicitly(useFallbackParameter.getValue(), useFallbackBuilder);
    }
    Map transportHeaders = (Map) msgCtx.getProperty(MessageContext.TRANSPORT_HEADERS);
    String contentLength = null;
    String trasferEncoded = null;
    if (transportHeaders != null) {
        contentLength = (String) transportHeaders.get(HTTP.CONTENT_LEN);
        trasferEncoded = (String) transportHeaders.get(HTTP.TRANSFER_ENCODING);
        if (contentType.equals(PassThroughConstants.DEFAULT_CONTENT_TYPE) && (contentLength == null || Integer.valueOf(contentLength) == 0) && trasferEncoded == null) {
            msgCtx.setProperty(PassThroughConstants.NO_ENTITY_BODY, true);
            msgCtx.setProperty(Constants.Configuration.CONTENT_TYPE, "");
            msgCtx.setProperty(PassThroughConstants.RELAY_EARLY_BUILD, true);
            return new SOAP11Factory().getDefaultEnvelope();
        }
    }
    OMElement element = null;
    Builder builder;
    if (contentType != null) {
        // loading builder from externally..
        // builder = configuration.getMessageBuilder(_contentType,useFallbackBuilder);
        builder = MessageProcessorSelector.getMessageBuilder(_contentType, msgCtx);
        if (builder != null) {
            try {
                if (contentLength != null && "0".equals(contentLength) && !msgCtx.isDoingREST()) {
                    element = new org.apache.axiom.soap.impl.llom.soap11.SOAP11Factory().getDefaultEnvelope();
                    // since we are setting an empty envelop to achieve the empty body, we have to set a different
                    // content-type other than text/xml, application/soap+xml or any other content-type which will
                    // invoke the soap builder, otherwise soap builder will get hit and an empty envelope
                    // will be send out
                    msgCtx.setProperty(Constants.Configuration.MESSAGE_TYPE, "application/xml");
                } else {
                    element = builder.processDocument(in, contentType, msgCtx);
                }
            } catch (AxisFault axisFault) {
                log.error("Error building message", axisFault);
                throw axisFault;
            }
        }
    }
    if (element == null) {
        if (msgCtx.isDoingREST()) {
            try {
                element = BuilderUtil.getPOXBuilder(in, null).getDocumentElement();
            } catch (XMLStreamException e) {
                log.error("Error building message using POX Builder", e);
                throw e;
            }
        } else {
            // switch to default
            builder = new SOAPBuilder();
            try {
                if (contentLength != null && "0".equals(contentLength)) {
                    element = new SOAP11Factory().getDefaultEnvelope();
                    // since we are setting an empty envelop to achieve the empty body, we have to set a different
                    // content-type other than text/xml, application/soap+xml or any other content-type which will
                    // invoke the soap builder, otherwise soap builder will get hit and an empty envelope
                    // will be send out
                    msgCtx.setProperty(Constants.Configuration.MESSAGE_TYPE, "application/xml");
                } else {
                    element = builder.processDocument(in, contentType, msgCtx);
                }
            } catch (AxisFault axisFault) {
                log.error("Error building message using SOAP builder");
                throw axisFault;
            }
        }
    }
    // build the soap headers and body
    if (element instanceof SOAPEnvelope) {
        SOAPEnvelope env = (SOAPEnvelope) element;
        env.hasFault();
    }
    // setting up original contentType (resetting the content type)
    if (contentType != null && !contentType.isEmpty()) {
        msgCtx.setProperty(Constants.Configuration.CONTENT_TYPE, contentType);
    }
    return element;
}
Also used : AxisFault(org.apache.axis2.AxisFault) AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) OMElement(org.apache.axiom.om.OMElement) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) XMLStreamException(javax.xml.stream.XMLStreamException) SOAP11Factory(org.apache.axiom.soap.impl.dom.soap11.SOAP11Factory) Parameter(org.apache.axis2.description.Parameter) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with SOAP11Factory

use of org.apache.axiom.soap.impl.dom.soap11.SOAP11Factory in project wso2-synapse by wso2.

the class ClientWorker method run.

public void run() {
    initTenantInfo();
    if (responseMsgCtx == null) {
        cleanup();
        return;
    }
    if (responseMsgCtx.getProperty(PassThroughConstants.PASS_THROUGH_SOURCE_CONNECTION) != null) {
        ((NHttpServerConnection) responseMsgCtx.getProperty(PassThroughConstants.PASS_THROUGH_SOURCE_CONNECTION)).getContext().setAttribute(PassThroughConstants.CLIENT_WORKER_START_TIME, System.currentTimeMillis());
    }
    try {
        if (expectEntityBody) {
            String cType = response.getHeader(HTTP.CONTENT_TYPE);
            if (cType == null) {
                cType = response.getHeader(HTTP.CONTENT_TYPE.toLowerCase());
            }
            String contentType;
            if (cType != null) {
                // This is the most common case - Most of the time servers send the Content-Type
                contentType = cType;
            } else {
                // Server hasn't sent the header - Try to infer the content type
                contentType = inferContentType();
            }
            responseMsgCtx.setProperty(Constants.Configuration.CONTENT_TYPE, contentType);
            String charSetEnc = BuilderUtil.getCharSetEncoding(contentType);
            if (charSetEnc == null) {
                charSetEnc = MessageContext.DEFAULT_CHAR_SET_ENCODING;
            }
            if (contentType != null) {
                responseMsgCtx.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, contentType.indexOf("charset") > 0 ? charSetEnc : MessageContext.DEFAULT_CHAR_SET_ENCODING);
                responseMsgCtx.removeProperty(PassThroughConstants.NO_ENTITY_BODY);
            }
            responseMsgCtx.setServerSide(false);
            SOAPFactory fac = OMAbstractFactory.getSOAP11Factory();
            SOAPEnvelope envelope = fac.getDefaultEnvelope();
            try {
                responseMsgCtx.setEnvelope(envelope);
            } catch (AxisFault axisFault) {
                log.error("Error setting SOAP envelope", axisFault);
            }
            responseMsgCtx.setServerSide(true);
        } else {
            // there is no response entity-body
            responseMsgCtx.setProperty(PassThroughConstants.NO_ENTITY_BODY, Boolean.TRUE);
            responseMsgCtx.setEnvelope(new SOAP11Factory().getDefaultEnvelope());
        }
        // copy the HTTP status code as a message context property with the key HTTP_SC to be
        // used at the sender to set the proper status code when passing the message
        int statusCode = this.response.getStatus();
        responseMsgCtx.setProperty(PassThroughConstants.HTTP_SC, statusCode);
        responseMsgCtx.setProperty(PassThroughConstants.HTTP_SC_DESC, response.getStatusLine());
        if (statusCode >= 400) {
            responseMsgCtx.setProperty(PassThroughConstants.FAULT_MESSAGE, PassThroughConstants.TRUE);
        }
        /*else if (statusCode == 202 && responseMsgCtx.getOperationContext().isComplete()) {
                // Handle out-only invocation scenario
                responseMsgCtx.setProperty(PassThroughConstants.MESSAGE_BUILDER_INVOKED, Boolean.TRUE);
            }*/
        responseMsgCtx.setProperty(PassThroughConstants.NON_BLOCKING_TRANSPORT, true);
        // process response received
        try {
            AxisEngine.receive(responseMsgCtx);
        } catch (AxisFault af) {
            log.error("Fault processing response message through Axis2", af);
            // This will be reached if an exception is thrown within an Axis2 handler
            String errorMessage = "Fault processing response message through Axis2: " + af.getMessage();
            responseMsgCtx.setProperty(NhttpConstants.SENDING_FAULT, Boolean.TRUE);
            responseMsgCtx.setProperty(NhttpConstants.ERROR_CODE, NhttpConstants.RESPONSE_PROCESSING_FAILURE);
            responseMsgCtx.setProperty(NhttpConstants.ERROR_MESSAGE, errorMessage.split("\n")[0]);
            responseMsgCtx.setProperty(NhttpConstants.ERROR_DETAIL, JavaUtils.stackToString(af));
            responseMsgCtx.setProperty(NhttpConstants.ERROR_EXCEPTION, af);
            responseMsgCtx.getAxisOperation().getMessageReceiver().receive(responseMsgCtx);
        }
    } catch (AxisFault af) {
        log.error("Fault creating response SOAP envelope", af);
    } finally {
        cleanup();
    }
}
Also used : AxisFault(org.apache.axis2.AxisFault) SOAP11Factory(org.apache.axiom.soap.impl.llom.soap11.SOAP11Factory) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) SOAPFactory(org.apache.axiom.soap.SOAPFactory)

Example 8 with SOAP11Factory

use of org.apache.axiom.soap.impl.dom.soap11.SOAP11Factory in project wso2-synapse by wso2.

the class RESTUtil method processURLRequest.

/**
 * Processes the HTTP GET request and builds the SOAP info-set of the REST message
 *
 * @param msgContext The MessageContext of the Request Message
 * @param out        The output stream of the response
 * @param soapAction SoapAction of the request
 * @param requestURI The URL that the request came to
 * @throws AxisFault - Thrown in case a fault occurs
 */
public static void processURLRequest(MessageContext msgContext, OutputStream out, String soapAction, String requestURI) throws AxisFault {
    if ((soapAction != null) && soapAction.startsWith("\"") && soapAction.endsWith("\"")) {
        soapAction = soapAction.substring(1, soapAction.length() - 1);
    }
    msgContext.setSoapAction(soapAction);
    msgContext.setTo(new EndpointReference(requestURI));
    msgContext.setProperty(MessageContext.TRANSPORT_OUT, out);
    msgContext.setServerSide(true);
    msgContext.setDoingREST(true);
    msgContext.setEnvelope(new SOAP11Factory().getDefaultEnvelope());
    msgContext.setProperty(NhttpConstants.NO_ENTITY_BODY, Boolean.TRUE);
    AxisEngine.receive(msgContext);
}
Also used : SOAP11Factory(org.apache.axiom.soap.impl.llom.soap11.SOAP11Factory) EndpointReference(org.apache.axis2.addressing.EndpointReference)

Example 9 with SOAP11Factory

use of org.apache.axiom.soap.impl.dom.soap11.SOAP11Factory in project wso2-synapse by wso2.

the class FIXMessageBuilder method processDocument.

public OMElement processDocument(InputStream inputStream, String contentType, MessageContext messageContext) throws AxisFault {
    Reader reader = null;
    StringBuilder messageString = new StringBuilder();
    quickfix.Message message = null;
    try {
        String charSetEncoding = (String) messageContext.getProperty(Constants.Configuration.CHARACTER_SET_ENCODING);
        if (charSetEncoding == null) {
            charSetEncoding = MessageContext.DEFAULT_CHAR_SET_ENCODING;
        }
        reader = new InputStreamReader(inputStream, charSetEncoding);
        try {
            int data = reader.read();
            while (data != -1) {
                char dataChar = (char) data;
                data = reader.read();
                messageString.append(dataChar);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            log.error("Error In creating FIX SOAP envelope ...", e);
            throw new AxisFault(e.getMessage());
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        log.error("Error In creating FIX SOAP envelope ...", e);
        throw new AxisFault(e.getMessage());
    }
    try {
        DefaultDataDictionaryProvider dataDictionary = new DefaultDataDictionaryProvider();
        String beginString = MessageUtils.getStringField(messageString.toString(), BeginString.FIELD);
        DataDictionary dataDic = dataDictionary.getSessionDataDictionary(beginString);
        message = new quickfix.Message(messageString.toString(), null, false);
    } catch (InvalidMessage e) {
        // TODO Auto-generated catch block
        log.error("Error In creating FIX SOAP envelope ...", e);
        throw new AxisFault(e.getMessage());
    }
    if (log.isDebugEnabled()) {
        log.debug("Creating SOAP envelope for FIX message...");
    }
    SOAPFactory soapFactory = new SOAP11Factory();
    OMElement msg = soapFactory.createOMElement(FIXConstants.FIX_MESSAGE, null);
    msg.addAttribute(soapFactory.createOMAttribute(FIXConstants.FIX_MESSAGE_INCOMING_SESSION, null, ""));
    msg.addAttribute(soapFactory.createOMAttribute(FIXConstants.FIX_MESSAGE_COUNTER, null, String.valueOf("-1")));
    OMElement header = soapFactory.createOMElement(FIXConstants.FIX_HEADER, null);
    OMElement body = soapFactory.createOMElement(FIXConstants.FIX_BODY, null);
    OMElement trailer = soapFactory.createOMElement(FIXConstants.FIX_TRAILER, null);
    // process FIX header
    Iterator<quickfix.Field<?>> iter = message.getHeader().iterator();
    if (iter != null) {
        while (iter.hasNext()) {
            quickfix.Field<?> field = iter.next();
            OMElement msgField = soapFactory.createOMElement(FIXConstants.FIX_FIELD, null);
            msgField.addAttribute(soapFactory.createOMAttribute(FIXConstants.FIX_FIELD_ID, null, String.valueOf(field.getTag())));
            Object value = field.getObject();
            if (value instanceof byte[]) {
                DataSource dataSource = new ByteArrayDataSource((byte[]) value);
                DataHandler dataHandler = new DataHandler(dataSource);
                String contentID = messageContext.addAttachment(dataHandler);
                OMElement binaryData = soapFactory.createOMElement(FIXConstants.FIX_BINARY_FIELD, null);
                String binaryCID = "cid:" + contentID;
                binaryData.addAttribute(FIXConstants.FIX_MESSAGE_REFERENCE, binaryCID, null);
                msgField.addChild(binaryData);
            } else {
                soapFactory.createOMText(msgField, value.toString(), OMElement.CDATA_SECTION_NODE);
            }
            header.addChild(msgField);
        }
    }
    // process FIX body
    convertFIXBodyToXML(message, body, soapFactory, messageContext);
    // process FIX trailer
    iter = message.getTrailer().iterator();
    if (iter != null) {
        while (iter.hasNext()) {
            quickfix.Field<?> field = iter.next();
            OMElement msgField = soapFactory.createOMElement(FIXConstants.FIX_FIELD, null);
            msgField.addAttribute(soapFactory.createOMAttribute(FIXConstants.FIX_FIELD_ID, null, String.valueOf(field.getTag())));
            Object value = field.getObject();
            if (value instanceof byte[]) {
                DataSource dataSource = new ByteArrayDataSource((byte[]) value);
                DataHandler dataHandler = new DataHandler(dataSource);
                String contentID = messageContext.addAttachment(dataHandler);
                OMElement binaryData = soapFactory.createOMElement(FIXConstants.FIX_BINARY_FIELD, null);
                String binaryCID = "cid:" + contentID;
                binaryData.addAttribute(FIXConstants.FIX_MESSAGE_REFERENCE, binaryCID, null);
                msgField.addChild(binaryData);
            } else {
                soapFactory.createOMText(msgField, value.toString(), OMElement.CDATA_SECTION_NODE);
            }
            trailer.addChild(msgField);
        }
    }
    msg.addChild(header);
    msg.addChild(body);
    msg.addChild(trailer);
    SOAPEnvelope envelope = soapFactory.getDefaultEnvelope();
    envelope.getBody().addChild(msg);
    messageContext.setEnvelope(envelope);
    return msg;
}
Also used : AxisFault(org.apache.axis2.AxisFault) InputStreamReader(java.io.InputStreamReader) DefaultDataDictionaryProvider(quickfix.DefaultDataDictionaryProvider) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) OMElement(org.apache.axiom.om.OMElement) BeginString(quickfix.field.BeginString) InvalidMessage(quickfix.InvalidMessage) DataHandler(javax.activation.DataHandler) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) DataDictionary(quickfix.DataDictionary) SOAPFactory(org.apache.axiom.soap.SOAPFactory) ByteArrayDataSource(org.apache.axiom.attachments.ByteArrayDataSource) DataSource(javax.activation.DataSource) SOAP11Factory(org.apache.axiom.soap.impl.llom.soap11.SOAP11Factory) ByteArrayDataSource(org.apache.axiom.attachments.ByteArrayDataSource)

Example 10 with SOAP11Factory

use of org.apache.axiom.soap.impl.dom.soap11.SOAP11Factory 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)

Aggregations

SOAP11Factory (org.apache.axiom.soap.impl.llom.soap11.SOAP11Factory)14 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)11 AxisFault (org.apache.axis2.AxisFault)8 OMElement (org.apache.axiom.om.OMElement)7 HashMap (java.util.HashMap)4 SOAPFactory (org.apache.axiom.soap.SOAPFactory)4 Test (org.junit.Test)4 XMLStreamException (javax.xml.stream.XMLStreamException)3 Map (java.util.Map)2 DataHandler (javax.activation.DataHandler)2 DataSource (javax.activation.DataSource)2 ByteArrayDataSource (org.apache.axiom.attachments.ByteArrayDataSource)2 OMException (org.apache.axiom.om.OMException)2 EndpointReference (org.apache.axis2.addressing.EndpointReference)2 Header (org.apache.http.Header)2 MessageContext (org.apache.synapse.MessageContext)2 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)2 SynapseEnvironment (org.apache.synapse.core.SynapseEnvironment)2 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)2 Axis2SynapseEnvironment (org.apache.synapse.core.axis2.Axis2SynapseEnvironment)2