Search in sources :

Example 21 with ServiceContext

use of org.apache.axis2.context.ServiceContext 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 22 with ServiceContext

use of org.apache.axis2.context.ServiceContext in project wso2-synapse by wso2.

the class RecipientListEndpointTest method createMessageContext.

/**
 * Create a empty message context
 *
 * @return A context with empty message
 * @throws AxisFault on an error creating a context
 */
private MessageContext createMessageContext() throws AxisFault {
    Axis2SynapseEnvironment synapseEnvironment = new Axis2SynapseEnvironment(new SynapseConfiguration());
    org.apache.axis2.context.MessageContext axis2MC = new org.apache.axis2.context.MessageContext();
    axis2MC.setConfigurationContext(new ConfigurationContext(new AxisConfiguration()));
    ServiceContext svcCtx = new ServiceContext();
    OperationContext opCtx = new OperationContext(new InOutAxisOperation(), svcCtx);
    axis2MC.setServiceContext(svcCtx);
    axis2MC.setOperationContext(opCtx);
    MessageContext mc = new Axis2MessageContext(axis2MC, new SynapseConfiguration(), synapseEnvironment);
    mc.setMessageID(UIDGenerator.generateURNString());
    mc.setEnvelope(OMAbstractFactory.getSOAP12Factory().createSOAPEnvelope());
    mc.getEnvelope().addChild(OMAbstractFactory.getSOAP12Factory().createSOAPBody());
    return mc;
}
Also used : OperationContext(org.apache.axis2.context.OperationContext) ConfigurationContext(org.apache.axis2.context.ConfigurationContext) AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) ServiceContext(org.apache.axis2.context.ServiceContext) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) Axis2SynapseEnvironment(org.apache.synapse.core.axis2.Axis2SynapseEnvironment) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) InOutAxisOperation(org.apache.axis2.description.InOutAxisOperation) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 23 with ServiceContext

use of org.apache.axis2.context.ServiceContext in project wso2-synapse by wso2.

the class ProxyServiceMessageReceiverTest method createMessageContext.

/**
 * Create a empty message context.
 *
 * @return A context with empty message
 * @throws AxisFault on an error creating a context
 */
private MessageContext createMessageContext() throws AxisFault {
    Axis2SynapseEnvironment synapseEnvironment = new Axis2SynapseEnvironment(new SynapseConfiguration());
    org.apache.axis2.context.MessageContext axis2MC = new org.apache.axis2.context.MessageContext();
    axis2MC.setConfigurationContext(new ConfigurationContext(new AxisConfiguration()));
    ServiceContext svcCtx = new ServiceContext();
    OperationContext opCtx = new OperationContext(new InOutAxisOperation(), svcCtx);
    axis2MC.setServiceContext(svcCtx);
    axis2MC.setOperationContext(opCtx);
    MessageContext mc = new Axis2MessageContext(axis2MC, new SynapseConfiguration(), synapseEnvironment);
    mc.setMessageID(UIDGenerator.generateURNString());
    mc.setEnvelope(OMAbstractFactory.getSOAP12Factory().createSOAPEnvelope());
    mc.getEnvelope().addChild(OMAbstractFactory.getSOAP12Factory().createSOAPBody());
    return mc;
}
Also used : OperationContext(org.apache.axis2.context.OperationContext) ConfigurationContext(org.apache.axis2.context.ConfigurationContext) AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) ServiceContext(org.apache.axis2.context.ServiceContext) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) MessageContext(org.apache.synapse.MessageContext) InOutAxisOperation(org.apache.axis2.description.InOutAxisOperation)

Example 24 with ServiceContext

use of org.apache.axis2.context.ServiceContext in project wso2-synapse by wso2.

the class HttpEndpointTest method createMessageContext.

/**
 * Create a empty message context
 *
 * @return A context with empty message
 * @throws AxisFault on an error creating a context
 */
private MessageContext createMessageContext() throws AxisFault {
    Axis2SynapseEnvironment synapseEnvironment = new Axis2SynapseEnvironment(new SynapseConfiguration());
    org.apache.axis2.context.MessageContext axis2MC = new org.apache.axis2.context.MessageContext();
    axis2MC.setConfigurationContext(new ConfigurationContext(new AxisConfiguration()));
    ServiceContext svcCtx = new ServiceContext();
    OperationContext opCtx = new OperationContext(new InOutAxisOperation(), svcCtx);
    axis2MC.setServiceContext(svcCtx);
    axis2MC.setOperationContext(opCtx);
    axis2MC.setTransportIn(new TransportInDescription("http"));
    // axis2MC.setTo(new EndpointReference("http://localhost:9000/services/SimpleStockQuoteService"));
    MessageContext mc = new Axis2MessageContext(axis2MC, new SynapseConfiguration(), synapseEnvironment);
    mc.setMessageID(UIDGenerator.generateURNString());
    mc.setEnvelope(OMAbstractFactory.getSOAP12Factory().createSOAPEnvelope());
    mc.getEnvelope().addChild(OMAbstractFactory.getSOAP12Factory().createSOAPBody());
    return mc;
}
Also used : OperationContext(org.apache.axis2.context.OperationContext) ConfigurationContext(org.apache.axis2.context.ConfigurationContext) AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) ServiceContext(org.apache.axis2.context.ServiceContext) TransportInDescription(org.apache.axis2.description.TransportInDescription) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) Axis2SynapseEnvironment(org.apache.synapse.core.axis2.Axis2SynapseEnvironment) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) InOutAxisOperation(org.apache.axis2.description.InOutAxisOperation) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 25 with ServiceContext

use of org.apache.axis2.context.ServiceContext in project wso2-synapse by wso2.

the class NHttpTransportListenerTest method testBackendResponse.

/**
 * Test the Source Handler respond to client.
 * Send a message to http listener and get the response from backend server.
 */
@Test
public void testBackendResponse() throws Exception {
    final SimpleHttpServer helloServer = new SimpleHttpServer();
    try {
        helloServer.start();
        RequestURIBasedDispatcher requestURIBasedDispatcher = Mockito.mock(RequestURIBasedDispatcher.class);
        Mockito.when(requestURIBasedDispatcher.findService(any(MessageContext.class))).thenReturn(new AxisService("myservice"));
        PowerMockito.whenNew(RequestURIBasedDispatcher.class).withNoArguments().thenReturn(requestURIBasedDispatcher);
        PowerMockito.mockStatic(AxisEngine.class);
        PowerMockito.doAnswer(new Answer<Void>() {

            public Void answer(InvocationOnMock invocation) throws Exception {
                MessageContext axis2MessageContext = invocation.getArgument(0);
                if (axis2MessageContext.getServiceContext() == null) {
                    // request path
                    ServiceContext svcCtx = new ServiceContext();
                    OperationContext opCtx = new OperationContext(new InOutAxisOperation(), svcCtx);
                    axis2MessageContext.setServiceContext(svcCtx);
                    axis2MessageContext.setOperationContext(opCtx);
                    axis2MessageContext.setProperty("TransportURL", helloServer.getServerUrl());
                    inMsgContext = axis2MessageContext;
                } else {
                    // response path
                    axis2MessageContext.setTo(null);
                    axis2MessageContext.setProperty("synapse.isresponse", true);
                    axis2MessageContext.removeProperty("TransportURL");
                    axis2MessageContext.setProperty(org.apache.axis2.Constants.OUT_TRANSPORT_INFO, inMsgContext.getProperty(org.apache.axis2.Constants.OUT_TRANSPORT_INFO));
                    axis2MessageContext.getOperationContext().setProperty(org.apache.axis2.Constants.RESPONSE_WRITTEN, "SKIP");
                }
                HttpCoreNIOSender sender = new HttpCoreNIOSender();
                ConfigurationContext cfgCtx = new ConfigurationContext(new AxisConfiguration());
                sender.init(cfgCtx, new TransportOutDescription("http"));
                sender.invoke(axis2MessageContext);
                return null;
            }
        }).when(AxisEngine.class, "receive", any(MessageContext.class));
        HttpClient client = new HttpClient();
        PostMethod method = new PostMethod(ServiceUtils.getServiceEndpoint("myservice", HOST, PORT));
        method.setRequestHeader("Content-Type", "application/xml");
        StringRequestEntity stringRequestEntity = new StringRequestEntity("<msg>hello server</msg>", "application/xml", "UTF-8");
        method.setRequestEntity(stringRequestEntity);
        int responseCode = client.executeMethod(method);
        Assert.assertEquals("Response code mismatched", 200, responseCode);
        String response = method.getResponseBodyAsString();
        Assert.assertEquals("Response", "<msg>hello</msg>", response);
    } finally {
        helloServer.stop();
    }
}
Also used : OperationContext(org.apache.axis2.context.OperationContext) ConfigurationContext(org.apache.axis2.context.ConfigurationContext) AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) RequestURIBasedDispatcher(org.apache.axis2.dispatchers.RequestURIBasedDispatcher) PostMethod(org.apache.commons.httpclient.methods.PostMethod) ServiceContext(org.apache.axis2.context.ServiceContext) AxisService(org.apache.axis2.description.AxisService) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) SimpleHttpServer(org.apache.synapse.transport.utils.http.server.SimpleHttpServer) HttpClient(org.apache.commons.httpclient.HttpClient) MessageContext(org.apache.axis2.context.MessageContext) InOutAxisOperation(org.apache.axis2.description.InOutAxisOperation) TransportOutDescription(org.apache.axis2.description.TransportOutDescription) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

ServiceContext (org.apache.axis2.context.ServiceContext)24 OperationContext (org.apache.axis2.context.OperationContext)15 InOutAxisOperation (org.apache.axis2.description.InOutAxisOperation)14 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)12 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)11 AxisService (org.apache.axis2.description.AxisService)10 MessageContext (org.apache.synapse.MessageContext)10 ServiceGroupContext (org.apache.axis2.context.ServiceGroupContext)9 AxisOperation (org.apache.axis2.description.AxisOperation)8 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)8 EndpointReference (org.apache.axis2.addressing.EndpointReference)6 Options (org.apache.axis2.client.Options)6 SynapseException (org.apache.synapse.SynapseException)6 MessageContext (org.apache.axis2.context.MessageContext)5 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)5 Axis2SynapseEnvironment (org.apache.synapse.core.axis2.Axis2SynapseEnvironment)5 TransportOutDescription (org.apache.axis2.description.TransportOutDescription)4 Test (org.junit.Test)4 Map (java.util.Map)3 QName (javax.xml.namespace.QName)3