Search in sources :

Example 6 with ServiceContext

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

the class OAuthUtilsTest method createMessageContext.

/**
 * Create a empty message context
 *
 * @return A context with empty message
 * @throws AxisFault on an error creating a context
 */
private static 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"));
    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 7 with ServiceContext

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

the class DynamicLoadBalanceEndpointTest 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) EndpointReference(org.apache.axis2.addressing.EndpointReference) 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 8 with ServiceContext

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

the class NHttpTransportListenerTest method testRequestAndResponse.

/**
 * Test the Source Handler respond to client.
 * Send a message to http listener and get the same request message as a response using PassThroughHttpSender
 */
@Test
public void testRequestAndResponse() throws Exception {
    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);
            ServiceContext svcCtx = new ServiceContext();
            OperationContext opCtx = new OperationContext(new InOutAxisOperation(), svcCtx);
            axis2MessageContext.setServiceContext(svcCtx);
            axis2MessageContext.setOperationContext(opCtx);
            axis2MessageContext.getOperationContext().setProperty(org.apache.axis2.Constants.RESPONSE_WRITTEN, "SKIP");
            axis2MessageContext.setTo(null);
            axis2MessageContext.setProperty("synapse.isresponse", true);
            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</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);
}
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) 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)

Example 9 with ServiceContext

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

the class RequestResponseTest method testRequestAndResponse.

/**
 * Test the Source Handler respond to client.
 * Send a message to http listener and get the same request message as a response using PassThroughHttpSender
 */
@Test
public void testRequestAndResponse() throws Exception {
    PowerMockito.mockStatic(AxisEngine.class);
    PowerMockito.doAnswer(new Answer<Void>() {

        public Void answer(InvocationOnMock invocation) throws Exception {
            MessageContext axis2MessageContext = invocation.getArgument(0);
            axis2MessageContext.setTo(null);
            if (axis2MessageContext.getServiceContext() == null) {
                ServiceContext svcCtx = new ServiceContext();
                OperationContext opCtx = new OperationContext(new InOutAxisOperation(), svcCtx);
                axis2MessageContext.setServiceContext(svcCtx);
                axis2MessageContext.setOperationContext(opCtx);
            }
            axis2MessageContext.getOperationContext().setProperty(org.apache.axis2.Constants.RESPONSE_WRITTEN, "SKIP");
            ServiceUtils.receive(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</msg>", "application/xml", "UTF-8");
    method.setRequestEntity(stringRequestEntity);
    int responseCode = client.executeMethod(method);
    String response = method.getResponseBodyAsString();
    Assert.assertEquals("Response code mismatched", 200, responseCode);
    Assert.assertEquals("Response", "<msg>hello</msg>", response);
}
Also used : OperationContext(org.apache.axis2.context.OperationContext) Answer(org.mockito.stubbing.Answer) StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) PostMethod(org.apache.commons.httpclient.methods.PostMethod) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ServiceContext(org.apache.axis2.context.ServiceContext) HttpClient(org.apache.commons.httpclient.HttpClient) MessageContext(org.apache.axis2.context.MessageContext) InOutAxisOperation(org.apache.axis2.description.InOutAxisOperation) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 10 with ServiceContext

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

the class ServiceUtils method receive.

/**
 * This will provide the mocking functionality of AxisEngine.receive() method.
 * when AxisEngine is mocked, this method can be used with doAnswer
 *
 * @param axis2MessageContext
 * @throws IOException
 * @throws HttpException
 */
public static void receive(MessageContext axis2MessageContext) throws IOException, HttpException {
    if (axis2MessageContext.getServiceContext() == null) {
        ServiceContext svcCtx = new ServiceContext();
        OperationContext opCtx = new OperationContext(new InOutAxisOperation(), svcCtx);
        axis2MessageContext.setServiceContext(svcCtx);
        axis2MessageContext.setOperationContext(opCtx);
    }
    PassThroughHttpSender sender = new PassThroughHttpSender();
    ConfigurationContext cfgCtx = new ConfigurationContext(new AxisConfiguration());
    sender.init(cfgCtx, new TransportOutDescription("http"));
    sender.invoke(axis2MessageContext);
}
Also used : OperationContext(org.apache.axis2.context.OperationContext) ConfigurationContext(org.apache.axis2.context.ConfigurationContext) AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) PassThroughHttpSender(org.apache.synapse.transport.passthru.PassThroughHttpSender) ServiceContext(org.apache.axis2.context.ServiceContext) InOutAxisOperation(org.apache.axis2.description.InOutAxisOperation) TransportOutDescription(org.apache.axis2.description.TransportOutDescription)

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