Search in sources :

Example 1 with RequestURIBasedDispatcher

use of org.apache.axis2.dispatchers.RequestURIBasedDispatcher 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 2 with RequestURIBasedDispatcher

use of org.apache.axis2.dispatchers.RequestURIBasedDispatcher in project wso2-synapse by wso2.

the class ServerWorker method handleRESTUrlPost.

/**
 * Method will setup the necessary parameters for the rest url post action
 *
 * @param
 * @return
 * @throws FactoryConfigurationError
 */
public SOAPEnvelope handleRESTUrlPost(String contentTypeHdr) throws FactoryConfigurationError {
    SOAPEnvelope soapEnvelope = null;
    String contentType = contentTypeHdr != null ? TransportUtils.getContentType(contentTypeHdr, msgContext) : null;
    // recipient should consider it as application/octet-stream (rfc2616)
    if (contentType == null || contentType.isEmpty()) {
        contentType = PassThroughConstants.APPLICATION_OCTET_STREAM;
        // Temp fix for https://github.com/wso2/product-ei/issues/2001
        if (HTTPConstants.HTTP_METHOD_GET.equals(msgContext.getProperty(HTTP_METHOD)) || "DELETE".equals(msgContext.getProperty(HTTP_METHOD))) {
            contentType = HTTPConstants.MEDIA_TYPE_X_WWW_FORM;
        }
    }
    if (HTTPConstants.MEDIA_TYPE_X_WWW_FORM.equals(contentType) || (PassThroughConstants.APPLICATION_OCTET_STREAM.equals(contentType) && contentTypeHdr == null)) {
        msgContext.setTo(new EndpointReference(request.getRequest().getRequestLine().getUri()));
        String charSetEncoding;
        if (contentTypeHdr != null) {
            msgContext.setProperty(Constants.Configuration.CONTENT_TYPE, contentTypeHdr);
            charSetEncoding = BuilderUtil.getCharSetEncoding(contentTypeHdr);
        } else {
            msgContext.setProperty(Constants.Configuration.CONTENT_TYPE, contentType);
            charSetEncoding = BuilderUtil.getCharSetEncoding(contentType);
        }
        msgContext.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, charSetEncoding);
        try {
            RESTUtil.dispatchAndVerify(msgContext);
        } catch (AxisFault e1) {
            log.error("Error while building message for REST_URL request", e1);
        }
        try {
            /**
             * This reverseProxyMode was introduce to avoid the LB exposing
             * it's own web service when REST call was initiated
             */
            boolean reverseProxyMode = PassThroughConfiguration.getInstance().isReverseProxyMode();
            AxisService axisService = null;
            if (!reverseProxyMode) {
                RequestURIBasedDispatcher requestDispatcher = new RequestURIBasedDispatcher();
                axisService = requestDispatcher.findService(msgContext);
            }
            // the logic determines which service dispatcher to get invoke,
            // this will be determine
            // based on parameter defines at disableRestServiceDispatching,
            // and if super tenant invoke, with isTenantRequest
            // identifies whether the request to be dispatch to custom REST
            // Dispatcher Service.
            boolean isCustomRESTDispatcher = false;
            String requestURI = request.getRequest().getRequestLine().getUri();
            if (requestURI.matches(PassThroughConfiguration.getInstance().getRestUriApiRegex()) || requestURI.matches(PassThroughConfiguration.getInstance().getRestUriProxyRegex())) {
                isCustomRESTDispatcher = true;
            }
            if (!isCustomRESTDispatcher) {
                if (axisService == null) {
                    String defaultSvcName = PassThroughConfiguration.getInstance().getPassThroughDefaultServiceName();
                    axisService = msgContext.getConfigurationContext().getAxisConfiguration().getService(defaultSvcName);
                    msgContext.setAxisService(axisService);
                }
            } else {
                String multiTenantDispatchService = PassThroughConfiguration.getInstance().getRESTDispatchService();
                axisService = msgContext.getConfigurationContext().getAxisConfiguration().getService(multiTenantDispatchService);
                msgContext.setAxisService(axisService);
            }
        } catch (AxisFault e) {
            handleException("Error processing " + request.getMethod() + " request for : " + request.getUri(), e);
        }
        try {
            soapEnvelope = TransportUtils.createSOAPMessage(msgContext, null, contentType);
        } catch (Exception e) {
            log.error("Error while building message for REST_URL request");
        }
        // msgContext.setProperty(Constants.Configuration.CONTENT_TYPE,"application/xml");
        msgContext.setProperty(Constants.Configuration.MESSAGE_TYPE, HTTPConstants.MEDIA_TYPE_APPLICATION_XML);
    }
    return soapEnvelope;
}
Also used : AxisFault(org.apache.axis2.AxisFault) RequestURIBasedDispatcher(org.apache.axis2.dispatchers.RequestURIBasedDispatcher) AxisService(org.apache.axis2.description.AxisService) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) EndpointReference(org.apache.axis2.addressing.EndpointReference)

Example 3 with RequestURIBasedDispatcher

use of org.apache.axis2.dispatchers.RequestURIBasedDispatcher 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

AxisService (org.apache.axis2.description.AxisService)3 RequestURIBasedDispatcher (org.apache.axis2.dispatchers.RequestURIBasedDispatcher)3 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)2 MessageContext (org.apache.axis2.context.MessageContext)2 OperationContext (org.apache.axis2.context.OperationContext)2 ServiceContext (org.apache.axis2.context.ServiceContext)2 InOutAxisOperation (org.apache.axis2.description.InOutAxisOperation)2 TransportOutDescription (org.apache.axis2.description.TransportOutDescription)2 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)2 HttpClient (org.apache.commons.httpclient.HttpClient)2 PostMethod (org.apache.commons.httpclient.methods.PostMethod)2 StringRequestEntity (org.apache.commons.httpclient.methods.StringRequestEntity)2 Test (org.junit.Test)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Answer (org.mockito.stubbing.Answer)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 SSLPeerUnverifiedException (javax.net.ssl.SSLPeerUnverifiedException)1 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)1 AxisFault (org.apache.axis2.AxisFault)1 EndpointReference (org.apache.axis2.addressing.EndpointReference)1