Search in sources :

Example 36 with Builder

use of org.apache.axis2.builder.Builder in project wso2-synapse by wso2.

the class DeferredMessageBuilder method getDocument.

public OMElement getDocument(MessageContext msgCtx, InputStream in) throws XMLStreamException, IOException {
    // that the payload stream is empty or not.
    if (HTTPConstants.HEADER_DELETE.equals(msgCtx.getProperty(Constants.Configuration.HTTP_METHOD)) && MessageUtils.isEmptyPayloadStream(in)) {
        msgCtx.setProperty(BridgeConstants.NO_ENTITY_BODY, Boolean.TRUE);
        return TransportUtils.createSOAPEnvelope(null);
    }
    String contentType = (String) msgCtx.getProperty(Constants.Configuration.CONTENT_TYPE);
    String contentType1 = getContentType(contentType, msgCtx);
    Map transportHeaders = (Map) msgCtx.getProperty(MessageContext.TRANSPORT_HEADERS);
    String contentLength = null;
    String transferEncoded;
    if (transportHeaders != null) {
        contentLength = (String) transportHeaders.get(BridgeConstants.CONTENT_LEN);
        transferEncoded = (String) transportHeaders.get(BridgeConstants.TRANSFER_ENCODING);
        if (contentType.equals(BridgeConstants.CONTENT_TYPE_APPLICATION_OCTET_STREAM) && (contentLength == null || Integer.parseInt(contentLength) == 0) && transferEncoded == null) {
            msgCtx.setProperty(BridgeConstants.NO_ENTITY_BODY, true);
            msgCtx.setProperty(Constants.Configuration.CONTENT_TYPE, "");
            return new SOAP11Factory().getDefaultEnvelope();
        }
    }
    OMElement element = null;
    Builder builder;
    if (contentType != null) {
        // loading builder from externally..
        // builder = configuration.getMessageBuilder(_contentType,useFallbackBuilder);
        builder = MessageProcessorSelector.getMessageBuilder(contentType1, msgCtx);
        if (builder != null) {
            try {
                if ("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", 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 ("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) XMLStreamException(javax.xml.stream.XMLStreamException) SOAP11Factory(org.apache.axiom.soap.impl.llom.soap11.SOAP11Factory) SOAPBuilder(org.apache.axis2.builder.SOAPBuilder) MTOMBuilder(org.apache.axis2.builder.MTOMBuilder) ApplicationXMLBuilder(org.apache.axis2.builder.ApplicationXMLBuilder) Builder(org.apache.axis2.builder.Builder) MIMEBuilder(org.apache.axis2.builder.MIMEBuilder) XFormURLEncodedBuilder(org.apache.axis2.builder.XFormURLEncodedBuilder) OMElement(org.apache.axiom.om.OMElement) SOAPBuilder(org.apache.axis2.builder.SOAPBuilder) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) HashMap(java.util.HashMap) Map(java.util.Map)

Example 37 with Builder

use of org.apache.axis2.builder.Builder in project wso2-synapse by wso2.

the class MessageUtils method getMessageFormatter.

/**
 * This selects the formatter for a given message format based on the the content type of the received message.
 * content-type to builder mapping can be specified through the Axis2.xml.
 *
 * @param msgContext axis2 MessageContext
 * @return the formatter registered against the given content-type
 */
public static MessageFormatter getMessageFormatter(MessageContext msgContext) {
    MessageFormatter messageFormatter = null;
    String messageFormatString = getMessageFormatterProperty(msgContext);
    messageFormatString = getContentTypeForFormatterSelection(messageFormatString, msgContext);
    if (messageFormatString != null) {
        messageFormatter = msgContext.getConfigurationContext().getAxisConfiguration().getMessageFormatter(messageFormatString);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Message format is: " + messageFormatString + "; message formatter returned by AxisConfiguration: " + messageFormatter);
        }
    }
    if (messageFormatter == null) {
        messageFormatter = (MessageFormatter) msgContext.getProperty(Constants.Configuration.MESSAGE_FORMATTER);
        if (messageFormatter != null) {
            return messageFormatter;
        }
    }
    if (messageFormatter == null) {
        // If we are doing rest better default to Application/xml formatter
        if (msgContext.isDoingREST()) {
            String httpMethod = (String) msgContext.getProperty(Constants.Configuration.HTTP_METHOD);
            if (Constants.Configuration.HTTP_METHOD_GET.equals(httpMethod) || Constants.Configuration.HTTP_METHOD_DELETE.equals(httpMethod)) {
                return new XFormURLEncodedFormatter();
            }
            return new ApplicationXMLFormatter();
        } else {
            // Lets default to SOAP formatter
            messageFormatter = new SOAPMessageFormatter();
        }
    }
    return messageFormatter;
}
Also used : XFormURLEncodedFormatter(org.apache.axis2.transport.http.XFormURLEncodedFormatter) SOAPMessageFormatter(org.apache.axis2.transport.http.SOAPMessageFormatter) MessageFormatter(org.apache.axis2.transport.MessageFormatter) SOAPMessageFormatter(org.apache.axis2.transport.http.SOAPMessageFormatter) ApplicationXMLFormatter(org.apache.axis2.transport.http.ApplicationXMLFormatter)

Example 38 with Builder

use of org.apache.axis2.builder.Builder in project wso2-synapse by wso2.

the class MessageConverter method toMessageContext.

/**
 * Converts a message read from the message store to a Synapse Message Context object.
 * @param message Message from the message store
 * @param axis2Ctx  Final Axis2 Message Context
 * @param synCtx Final Synapse message Context
 * @return Final Synapse Message Context
 */
public static MessageContext toMessageContext(StorableMessage message, org.apache.axis2.context.MessageContext axis2Ctx, MessageContext synCtx) {
    if (message == null) {
        logger.error("Cannot create Message Context. Message is null.");
        return null;
    }
    AxisConfiguration axisConfig = axis2Ctx.getConfigurationContext().getAxisConfiguration();
    if (axisConfig == null) {
        logger.warn("Cannot create AxisConfiguration. AxisConfiguration is null.");
        return null;
    }
    Axis2Message axis2Msg = message.getAxis2message();
    try {
        SOAPEnvelope envelope = getSoapEnvelope(axis2Msg.getSoapEnvelope());
        axis2Ctx.setEnvelope(envelope);
        // set the RMSMessageDto properties
        axis2Ctx.getOptions().setAction(axis2Msg.getAction());
        if (axis2Msg.getRelatesToMessageId() != null) {
            axis2Ctx.addRelatesTo(new RelatesTo(axis2Msg.getRelatesToMessageId()));
        }
        axis2Ctx.setMessageID(axis2Msg.getMessageID());
        axis2Ctx.getOptions().setAction(axis2Msg.getAction());
        axis2Ctx.setDoingREST(axis2Msg.isDoingPOX());
        axis2Ctx.setDoingMTOM(axis2Msg.isDoingMTOM());
        axis2Ctx.setDoingSwA(axis2Msg.isDoingSWA());
        AxisService axisService;
        if (axis2Msg.getService() != null && (axisService = axisConfig.getServiceForActivation(axis2Msg.getService())) != null) {
            AxisOperation axisOperation = axisService.getOperation(axis2Msg.getOperationName());
            axis2Ctx.setFLOW(axis2Msg.getFLOW());
            ArrayList executionChain = new ArrayList();
            if (axis2Msg.getFLOW() == org.apache.axis2.context.MessageContext.OUT_FLOW) {
                executionChain.addAll(axisOperation.getPhasesOutFlow());
                executionChain.addAll(axisConfig.getOutFlowPhases());
            } else if (axis2Msg.getFLOW() == org.apache.axis2.context.MessageContext.OUT_FAULT_FLOW) {
                executionChain.addAll(axisOperation.getPhasesOutFaultFlow());
                executionChain.addAll(axisConfig.getOutFlowPhases());
            }
            axis2Ctx.setExecutionChain(executionChain);
            ConfigurationContext configurationContext = axis2Ctx.getConfigurationContext();
            axis2Ctx.setAxisService(axisService);
            ServiceGroupContext serviceGroupContext = configurationContext.createServiceGroupContext(axisService.getAxisServiceGroup());
            ServiceContext serviceContext = serviceGroupContext.getServiceContext(axisService);
            OperationContext operationContext = serviceContext.createOperationContext(axis2Msg.getOperationName());
            axis2Ctx.setServiceContext(serviceContext);
            axis2Ctx.setOperationContext(operationContext);
            axis2Ctx.setAxisService(axisService);
            axis2Ctx.setAxisOperation(axisOperation);
        } else if (axis2Ctx.getOperationContext() == null) {
            axis2Ctx.setOperationContext(new OperationContext(new InOutAxisOperation(), new ServiceContext()));
        }
        if (axis2Msg.getReplyToAddress() != null) {
            axis2Ctx.setReplyTo(new EndpointReference(axis2Msg.getReplyToAddress().trim()));
        }
        if (axis2Msg.getFaultToAddress() != null) {
            axis2Ctx.setFaultTo(new EndpointReference(axis2Msg.getFaultToAddress().trim()));
        }
        if (axis2Msg.getFromAddress() != null) {
            axis2Ctx.setFrom(new EndpointReference(axis2Msg.getFromAddress().trim()));
        }
        if (axis2Msg.getToAddress() != null) {
            axis2Ctx.getOptions().setTo(new EndpointReference(axis2Msg.getToAddress().trim()));
        }
        Object map = axis2Msg.getProperties().get(ABSTRACT_MC_PROPERTIES);
        axis2Msg.getProperties().remove(ABSTRACT_MC_PROPERTIES);
        axis2Ctx.setProperties(axis2Msg.getProperties());
        axis2Ctx.setTransportIn(axisConfig.getTransportIn(axis2Msg.getTransportInName()));
        axis2Ctx.setTransportOut(axisConfig.getTransportOut(axis2Msg.getTransportOutName()));
        Object headers = axis2Msg.getProperties().get(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
        if (headers instanceof Map) {
            setTransportHeaders(axis2Ctx, (Map) headers);
        }
        if (map instanceof Map) {
            Map<String, Object> abstractMCProperties = (Map) map;
            Iterator<String> properties = abstractMCProperties.keySet().iterator();
            while (properties.hasNext()) {
                String property = properties.next();
                Object value = abstractMCProperties.get(property);
                axis2Ctx.setProperty(property, value);
            }
        }
        // That is to get the existing headers into the new envelope.
        if (axis2Msg.getJsonStream() != null) {
            JsonUtil.getNewJsonPayload(axis2Ctx, new ByteArrayInputStream(axis2Msg.getJsonStream()), true, true);
        }
        SynapseMessage synMsg = message.getSynapseMessage();
        synCtx.setTracingState(synMsg.getTracingState());
        synCtx.setMessageFlowTracingState(synMsg.getMessageFlowTracingState());
        Iterator<String> properties = synMsg.getProperties().keySet().iterator();
        while (properties.hasNext()) {
            String key = properties.next();
            Object value = synMsg.getProperties().get(key);
            synCtx.setProperty(key, value);
        }
        Iterator<String> propertyObjects = synMsg.getPropertyObjects().keySet().iterator();
        while (propertyObjects.hasNext()) {
            String key = propertyObjects.next();
            Object value = synMsg.getPropertyObjects().get(key);
            if (key.startsWith(OM_ELEMENT_PREFIX)) {
                String originalKey = key.substring(OM_ELEMENT_PREFIX.length(), key.length());
                ByteArrayInputStream is = new ByteArrayInputStream((byte[]) value);
                StAXOMBuilder builder = new StAXOMBuilder(is);
                OMElement omElement = builder.getDocumentElement();
                synCtx.setProperty(originalKey, omElement);
            }
        }
        synCtx.setFaultResponse(synMsg.isFaultResponse());
        synCtx.setResponse(synMsg.isResponse());
        return synCtx;
    } catch (Exception e) {
        logger.error("Cannot create Message Context. Error:" + e.getLocalizedMessage(), e);
        return null;
    }
}
Also used : OperationContext(org.apache.axis2.context.OperationContext) AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) ConfigurationContext(org.apache.axis2.context.ConfigurationContext) InOutAxisOperation(org.apache.axis2.description.InOutAxisOperation) AxisOperation(org.apache.axis2.description.AxisOperation) ServiceContext(org.apache.axis2.context.ServiceContext) AxisService(org.apache.axis2.description.AxisService) ArrayList(java.util.ArrayList) OMElement(org.apache.axiom.om.OMElement) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) RelatesTo(org.apache.axis2.addressing.RelatesTo) XMLStreamException(javax.xml.stream.XMLStreamException) SynapseException(org.apache.synapse.SynapseException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) EndpointReference(org.apache.axis2.addressing.EndpointReference) ServiceGroupContext(org.apache.axis2.context.ServiceGroupContext) ByteArrayInputStream(java.io.ByteArrayInputStream) StAXOMBuilder(org.apache.axiom.om.impl.builder.StAXOMBuilder) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) InOutAxisOperation(org.apache.axis2.description.InOutAxisOperation)

Example 39 with Builder

use of org.apache.axis2.builder.Builder in project wso2-synapse by wso2.

the class TestMessageContextBuilder method build.

/**
 * Build the test message context.
 * This method returns a new (and independent) instance on every invocation.
 *
 * @return
 * @throws Exception
 */
public MessageContext build() throws Exception {
    SynapseConfiguration testConfig = new SynapseConfiguration();
    // TODO: check whether we need a SynapseEnvironment in all cases
    SynapseEnvironment synEnv = new Axis2SynapseEnvironment(new ConfigurationContext(new AxisConfiguration()), testConfig);
    MessageContext synCtx;
    if (requireAxis2MessageContext) {
        synCtx = new Axis2MessageContext(new org.apache.axis2.context.MessageContext(), testConfig, synEnv);
    } else {
        synCtx = new TestMessageContext();
        synCtx.setEnvironment(synEnv);
        synCtx.setConfiguration(testConfig);
    }
    for (Map.Entry<String, Entry> mapEntry : entries.entrySet()) {
        testConfig.addEntry(mapEntry.getKey(), mapEntry.getValue());
    }
    XMLStreamReader parser = null;
    if (contentString != null) {
        parser = StAXUtils.createXMLStreamReader(new StringReader(contentString));
    } else if (contentFile != null) {
        parser = StAXUtils.createXMLStreamReader(new FileInputStream(contentFile));
    } else if (contentStringJson != null) {
        // synCtx = new Axis2MessageContext(null, testConfig, synEnv);
        SOAPEnvelope envelope = OMAbstractFactory.getSOAP11Factory().getDefaultEnvelope();
        synCtx.setEnvelope(envelope);
        JsonUtil.getNewJsonPayload(((Axis2MessageContext) synCtx).getAxis2MessageContext(), contentStringJson, true, true);
        return synCtx;
    }
    SOAPEnvelope envelope;
    if (parser != null) {
        if (contentIsEnvelope) {
            envelope = new StAXSOAPModelBuilder(parser).getSOAPEnvelope();
        } else {
            envelope = OMAbstractFactory.getSOAP11Factory().getDefaultEnvelope();
            // TODO: don't know why this is here, but without it some unit tests fail...
            OMDocument omDoc = OMAbstractFactory.getSOAP11Factory().createOMDocument();
            omDoc.addChild(envelope);
            SOAPBody body = envelope.getBody();
            StAXOMBuilder builder = new StAXOMBuilder(parser);
            OMElement bodyElement = builder.getDocumentElement();
            if (addTextAroundBody) {
                OMFactory fac = OMAbstractFactory.getOMFactory();
                body.addChild(fac.createOMText("\n"));
                body.addChild(bodyElement);
                body.addChild(fac.createOMText("\n"));
            } else {
                body.addChild(bodyElement);
            }
        }
    } else {
        envelope = OMAbstractFactory.getSOAP11Factory().getDefaultEnvelope();
    }
    synCtx.setEnvelope(envelope);
    return synCtx;
}
Also used : ConfigurationContext(org.apache.axis2.context.ConfigurationContext) AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) XMLStreamReader(javax.xml.stream.XMLStreamReader) Axis2SynapseEnvironment(org.apache.synapse.core.axis2.Axis2SynapseEnvironment) SynapseEnvironment(org.apache.synapse.core.SynapseEnvironment) OMElement(org.apache.axiom.om.OMElement) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) FileInputStream(java.io.FileInputStream) OMDocument(org.apache.axiom.om.OMDocument) OMFactory(org.apache.axiom.om.OMFactory) Axis2SynapseEnvironment(org.apache.synapse.core.axis2.Axis2SynapseEnvironment) Entry(org.apache.synapse.config.Entry) SOAPBody(org.apache.axiom.soap.SOAPBody) StAXSOAPModelBuilder(org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder) StringReader(java.io.StringReader) StAXOMBuilder(org.apache.axiom.om.impl.builder.StAXOMBuilder) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) HashMap(java.util.HashMap) Map(java.util.Map) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 40 with Builder

use of org.apache.axis2.builder.Builder in project wso2-synapse by wso2.

the class FIXMessageFromatterTest method testWriteTo.

public void testWriteTo() {
    String input = "8=FIX.4.0\u00019=105\u000135=D\u000134=2\u000149=BANZAI\u000152=20080711-06:42:26\u000156=SYNAPSE\u0001" + "11=1215758546278\u000121=1\u000138=90000000\u000140=1\u000154=1\u000155=DEL\u000159=0\u000110=121\u0001";
    MessageContext msgCtx = new MessageContext();
    FIXMessageBuilder builder = new FIXMessageBuilder();
    OMElement element = null;
    try {
        element = builder.processDocument(new ByteArrayInputStream(input.getBytes()), "fix/j", msgCtx);
        Assert.assertNotNull(element);
    // System.out.println(element);
    } catch (AxisFault e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    FIXMessageFromatter fixMessageFromatter = new FIXMessageFromatter();
    // msgCtx = new MessageContext();
    // msgCtx.seten
    OutputStream output = new ByteArrayOutputStream();
    SOAPFactory factory = OMAbstractFactory.getSOAP12Factory();
    SOAPEnvelope env = factory.getDefaultEnvelope();
    env.getBody().addChild(element);
    try {
        msgCtx.setEnvelope(env);
    } catch (AxisFault e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    OMOutputFormat myOutputFormat = new OMOutputFormat();
    try {
        fixMessageFromatter.writeTo(msgCtx, myOutputFormat, output, false);
        assertTrue(output.toString().length() > 0);
    } catch (AxisFault e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : AxisFault(org.apache.axis2.AxisFault) ByteArrayInputStream(java.io.ByteArrayInputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OMElement(org.apache.axiom.om.OMElement) MessageContext(org.apache.axis2.context.MessageContext) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) OMOutputFormat(org.apache.axiom.om.OMOutputFormat) SOAPFactory(org.apache.axiom.soap.SOAPFactory)

Aggregations

OMElement (org.apache.axiom.om.OMElement)24 MessageContext (org.apache.axis2.context.MessageContext)15 Builder (org.apache.axis2.builder.Builder)13 AxisFault (org.apache.axis2.AxisFault)11 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 SOAPBuilder (org.apache.axis2.builder.SOAPBuilder)8 InputStream (java.io.InputStream)7 HashMap (java.util.HashMap)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 EndpointReference (org.apache.axis2.addressing.EndpointReference)5 ContentType (javax.mail.internet.ContentType)4 ParseException (javax.mail.internet.ParseException)4 XMLStreamException (javax.xml.stream.XMLStreamException)4 Parameter (org.apache.axis2.description.Parameter)4 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)4 MessageFormatter (org.apache.axis2.transport.MessageFormatter)4 ManagedTestSuite (org.apache.axis2.transport.testkit.ManagedTestSuite)4 TransportTestSuiteBuilder (org.apache.axis2.transport.testkit.TransportTestSuiteBuilder)4 AxisAsyncTestClient (org.apache.axis2.transport.testkit.axis2.client.AxisAsyncTestClient)4