Search in sources :

Example 1 with NHttpClientConnection

use of org.apache.http.nio.NHttpClientConnection in project wso2-synapse by wso2.

the class TargetHandlerTest method testRequestReady.

/**
 * Testing whether request-ready connection is processed
 *
 * @throws Exception
 */
@Test
public void testRequestReady() throws Exception {
    DeliveryAgent deliveryAgent = mock(DeliveryAgent.class);
    ClientConnFactory connFactory = mock(ClientConnFactory.class);
    TargetConfiguration configuration = mock(TargetConfiguration.class);
    TargetHandler targetHandler = new TargetHandler(deliveryAgent, connFactory, configuration);
    NHttpClientConnection conn = mock(NHttpClientConnection.class, Mockito.RETURNS_DEEP_STUBS);
    HttpContext context = mock(HttpContext.class);
    when(conn.getContext()).thenReturn(context);
    mockStatic(TargetContext.class);
    when(TargetContext.getState(any(NHttpClientConnection.class))).thenReturn(ProtocolState.REQUEST_READY);
    targetHandler.requestReady(conn);
}
Also used : TargetConfiguration(org.apache.synapse.transport.passthru.config.TargetConfiguration) HttpContext(org.apache.http.protocol.HttpContext) ClientConnFactory(org.apache.synapse.transport.http.conn.ClientConnFactory) NHttpClientConnection(org.apache.http.nio.NHttpClientConnection) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with NHttpClientConnection

use of org.apache.http.nio.NHttpClientConnection in project wso2-synapse by wso2.

the class TargetResponseTest method testTrue.

/**
 * Testing the starting of target response when response body is expected
 *
 * @throws Exception
 */
@Test
public void testTrue() throws Exception {
    ConfigurationContext configurationContext = new ConfigurationContext(new AxisConfiguration());
    WorkerPool workerPool = new NativeWorkerPool(3, 4, 5, 5, "name", "id");
    PassThroughTransportMetricsCollector metrics = new PassThroughTransportMetricsCollector(true, "testScheme");
    TargetConfiguration targetConfiguration = new TargetConfiguration(configurationContext, null, workerPool, metrics, null);
    targetConfiguration.build();
    HttpResponse response = PowerMockito.mock(HttpResponse.class, Mockito.RETURNS_DEEP_STUBS);
    NHttpClientConnection conn = PowerMockito.mock(NHttpClientConnection.class, Mockito.RETURNS_DEEP_STUBS);
    PowerMockito.mockStatic(TargetContext.class);
    TargetContext cntxt = new TargetContext(targetConfiguration);
    PowerMockito.when(TargetContext.get(any(NHttpClientConnection.class))).thenReturn(cntxt);
    TargetResponse targetResponse = new TargetResponse(targetConfiguration, response, conn, true, false);
    try {
        targetResponse.start(conn);
    } catch (Exception e) {
        logger.error(e);
        Assert.fail("Unable to start the target response!");
    }
}
Also used : ConfigurationContext(org.apache.axis2.context.ConfigurationContext) AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) WorkerPool(org.apache.axis2.transport.base.threads.WorkerPool) NativeWorkerPool(org.apache.axis2.transport.base.threads.NativeWorkerPool) TargetConfiguration(org.apache.synapse.transport.passthru.config.TargetConfiguration) PassThroughTransportMetricsCollector(org.apache.synapse.transport.passthru.jmx.PassThroughTransportMetricsCollector) NativeWorkerPool(org.apache.axis2.transport.base.threads.NativeWorkerPool) HttpResponse(org.apache.http.HttpResponse) NHttpClientConnection(org.apache.http.nio.NHttpClientConnection) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 3 with NHttpClientConnection

use of org.apache.http.nio.NHttpClientConnection in project wso2-synapse by wso2.

the class TargetResponseTest method testFalse.

/**
 * Testing the starting of target response when response body is not expected
 *
 * @throws Exception
 */
@Test
public void testFalse() throws Exception {
    ConfigurationContext configurationContext = new ConfigurationContext(new AxisConfiguration());
    WorkerPool workerPool = new NativeWorkerPool(3, 4, 5, 5, "name", "id");
    PassThroughTransportMetricsCollector metrics = new PassThroughTransportMetricsCollector(true, "testScheme");
    TargetConfiguration targetConfiguration = new TargetConfiguration(configurationContext, null, workerPool, metrics, null);
    HttpResponse response = PowerMockito.mock(HttpResponse.class, Mockito.RETURNS_DEEP_STUBS);
    NHttpClientConnection conn = PowerMockito.mock(NHttpClientConnection.class, Mockito.RETURNS_DEEP_STUBS);
    TargetConnections connections = PowerMockito.mock(TargetConnections.class);
    targetConfiguration.setConnections(connections);
    PowerMockito.mockStatic(TargetContext.class);
    TargetResponse targetResponse = new TargetResponse(targetConfiguration, response, conn, false, false);
    try {
        targetResponse.start(conn);
    } catch (Exception e) {
        logger.error(e);
        Assert.fail("Unable to start the target response!");
    }
}
Also used : ConfigurationContext(org.apache.axis2.context.ConfigurationContext) AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) WorkerPool(org.apache.axis2.transport.base.threads.WorkerPool) NativeWorkerPool(org.apache.axis2.transport.base.threads.NativeWorkerPool) TargetConfiguration(org.apache.synapse.transport.passthru.config.TargetConfiguration) PassThroughTransportMetricsCollector(org.apache.synapse.transport.passthru.jmx.PassThroughTransportMetricsCollector) NativeWorkerPool(org.apache.axis2.transport.base.threads.NativeWorkerPool) HttpResponse(org.apache.http.HttpResponse) TargetConnections(org.apache.synapse.transport.passthru.connections.TargetConnections) NHttpClientConnection(org.apache.http.nio.NHttpClientConnection) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 4 with NHttpClientConnection

use of org.apache.http.nio.NHttpClientConnection in project wso2-synapse by wso2.

the class TargetResponseTest method testRead.

/**
 * Testing reading from a pipe
 * @throws Exception
 */
@Test
public void testRead() throws Exception {
    ConfigurationContext configurationContext = new ConfigurationContext(new AxisConfiguration());
    WorkerPool workerPool = new NativeWorkerPool(3, 4, 5, 5, "name", "id");
    PassThroughTransportMetricsCollector metrics = new PassThroughTransportMetricsCollector(true, "testScheme");
    TargetConfiguration targetConfiguration = new TargetConfiguration(configurationContext, null, workerPool, metrics, null);
    targetConfiguration.build();
    HttpResponse response = PowerMockito.mock(HttpResponse.class, Mockito.RETURNS_DEEP_STUBS);
    NHttpClientConnection conn = PowerMockito.mock(NHttpClientConnection.class, Mockito.RETURNS_DEEP_STUBS);
    ContentDecoder decoder = PowerMockito.mock(ContentDecoder.class);
    TargetConnections connections = PowerMockito.mock(TargetConnections.class);
    targetConfiguration.setConnections(connections);
    PowerMockito.mockStatic(TargetContext.class);
    TargetContext cntxt = new TargetContext(targetConfiguration);
    PowerMockito.when(TargetContext.get(any(NHttpClientConnection.class))).thenReturn(cntxt);
    PowerMockito.when(decoder.read(any(ByteBuffer.class))).thenReturn(12);
    PowerMockito.when(decoder.isCompleted()).thenReturn(true);
    TargetResponse targetResponse = new TargetResponse(targetConfiguration, response, conn, true, false);
    targetResponse.start(conn);
    int result = targetResponse.read(conn, decoder);
    Assert.assertEquals(12, result);
}
Also used : ConfigurationContext(org.apache.axis2.context.ConfigurationContext) AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) TargetConfiguration(org.apache.synapse.transport.passthru.config.TargetConfiguration) PassThroughTransportMetricsCollector(org.apache.synapse.transport.passthru.jmx.PassThroughTransportMetricsCollector) NativeWorkerPool(org.apache.axis2.transport.base.threads.NativeWorkerPool) ContentDecoder(org.apache.http.nio.ContentDecoder) HttpResponse(org.apache.http.HttpResponse) NHttpClientConnection(org.apache.http.nio.NHttpClientConnection) ByteBuffer(java.nio.ByteBuffer) WorkerPool(org.apache.axis2.transport.base.threads.WorkerPool) NativeWorkerPool(org.apache.axis2.transport.base.threads.NativeWorkerPool) TargetConnections(org.apache.synapse.transport.passthru.connections.TargetConnections) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 5 with NHttpClientConnection

use of org.apache.http.nio.NHttpClientConnection in project wso2-synapse by wso2.

the class DeliveryAgent method submit.

/**
 * This method queues the message for delivery. If a connection is already existing for
 * the destination epr, the message will be delivered immediately. Otherwise message has
 * to wait until a connection is established. In this case this method will inform the
 * system about the need for a connection.
 *
 * @param msgContext the message context to be sent
 * @param epr the endpoint to which the message should be sent
 * @throws AxisFault if an error occurs
 */
public void submit(MessageContext msgContext, EndpointReference epr) throws AxisFault {
    try {
        URL url = new URL(epr.getAddress());
        String scheme = url.getProtocol() != null ? url.getProtocol() : "http";
        String hostname = url.getHost();
        int port = url.getPort();
        if (port == -1) {
            // use default
            if ("http".equals(scheme)) {
                port = 80;
            } else if ("https".equals(scheme)) {
                port = 443;
            }
        }
        HttpHost target = new HttpHost(hostname, port, scheme);
        boolean secure = "https".equalsIgnoreCase(target.getSchemeName());
        HttpHost proxy = proxyConfig.selectProxy(target);
        msgContext.setProperty(PassThroughConstants.PROXY_PROFILE_TARGET_HOST, target.getHostName());
        HttpRoute route;
        if (proxy != null) {
            route = new HttpRoute(target, null, proxy, secure);
        } else {
            route = new HttpRoute(target, null, secure);
        }
        // first we queue the message
        Queue<MessageContext> queue = null;
        lock.lock();
        try {
            queue = waitingMessages.get(route);
            if (queue == null) {
                queue = new ConcurrentLinkedQueue<MessageContext>();
                waitingMessages.put(route, queue);
            }
            if (queue.size() == maxWaitingMessages) {
                MessageContext msgCtx = queue.poll();
                targetErrorHandler.handleError(msgCtx, ErrorCodes.CONNECTION_TIMEOUT, "Error connecting to the back end", null, ProtocolState.REQUEST_READY);
            }
            queue.add(msgContext);
        } finally {
            lock.unlock();
        }
        NHttpClientConnection conn = targetConnections.getConnection(route);
        if (conn != null) {
            conn.resetInput();
            conn.resetOutput();
            MessageContext messageContext = queue.poll();
            if (messageContext != null) {
                tryNextMessage(messageContext, route, conn);
            }
        }
    } catch (MalformedURLException e) {
        handleException("Malformed URL in the target EPR", e);
    }
}
Also used : HttpRoute(org.apache.http.conn.routing.HttpRoute) MalformedURLException(java.net.MalformedURLException) HttpHost(org.apache.http.HttpHost) MessageContext(org.apache.axis2.context.MessageContext) NHttpClientConnection(org.apache.http.nio.NHttpClientConnection) URL(java.net.URL)

Aggregations

NHttpClientConnection (org.apache.http.nio.NHttpClientConnection)14 HttpRoute (org.apache.http.conn.routing.HttpRoute)6 TargetConfiguration (org.apache.synapse.transport.passthru.config.TargetConfiguration)6 Test (org.junit.Test)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)5 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)5 NativeWorkerPool (org.apache.axis2.transport.base.threads.NativeWorkerPool)5 WorkerPool (org.apache.axis2.transport.base.threads.WorkerPool)5 PassThroughTransportMetricsCollector (org.apache.synapse.transport.passthru.jmx.PassThroughTransportMetricsCollector)5 MessageContext (org.apache.axis2.context.MessageContext)3 HttpHost (org.apache.http.HttpHost)3 HttpResponse (org.apache.http.HttpResponse)3 HttpContext (org.apache.http.protocol.HttpContext)3 ClientConnFactory (org.apache.synapse.transport.http.conn.ClientConnFactory)3 InetSocketAddress (java.net.InetSocketAddress)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2