Search in sources :

Example 11 with NHttpClientConnection

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

the class ConnectionPool method forget.

public void forget(NHttpClientConnection conn) {
    HttpContext ctx = conn.getContext();
    Axis2HttpRequest axis2Req = (Axis2HttpRequest) ctx.getAttribute(ClientHandler.AXIS2_HTTP_REQUEST);
    if (axis2Req != null) {
        HttpRoute route = axis2Req.getRoute();
        List<NHttpClientConnection> connections = (List<NHttpClientConnection>) connMap.get(route);
        if (connections != null) {
            synchronized (connections) {
                connections.remove(conn);
            }
        }
    }
}
Also used : HttpRoute(org.apache.http.conn.routing.HttpRoute) HttpContext(org.apache.http.protocol.HttpContext) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) NHttpClientConnection(org.apache.http.nio.NHttpClientConnection)

Example 12 with NHttpClientConnection

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

the class HttpCoreNIOSender method sendAsyncRequest.

/**
 * Send the request message asynchronously to the given EPR
 * @param epr the destination EPR for the message
 * @param msgContext the message being sent
 * @throws AxisFault on error
 */
private void sendAsyncRequest(EndpointReference epr, MessageContext msgContext) 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(NhttpConstants.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);
        }
        Axis2HttpRequest axis2Req = new Axis2HttpRequest(epr, route, msgContext);
        Object timeout = msgContext.getProperty(NhttpConstants.SEND_TIMEOUT);
        if (timeout != null && timeout instanceof Long) {
            axis2Req.setTimeout((int) ((Long) timeout).longValue());
        }
        NHttpClientConnection conn = connpool.getConnection(route);
        // Ensure MessageContext has a ClientConnectionDebug attached before we start streaming
        ServerConnectionDebug scd = (ServerConnectionDebug) msgContext.getProperty(ServerHandler.SERVER_CONNECTION_DEBUG);
        ClientConnectionDebug ccd;
        if (scd != null) {
            ccd = scd.getClientConnectionDebug();
            if (ccd == null) {
                ccd = new ClientConnectionDebug(scd);
                scd.setClientConnectionDebug(ccd);
            }
            ccd.recordRequestStartTime(conn, axis2Req);
            msgContext.setProperty(ClientHandler.CLIENT_CONNECTION_DEBUG, ccd);
        }
        if (conn == null) {
            HttpHost host = route.getProxyHost() != null ? route.getProxyHost() : route.getTargetHost();
            ioReactor.connect(new InetSocketAddress(host.getHostName(), host.getPort()), null, axis2Req, sessionRequestCallback);
            if (log.isDebugEnabled()) {
                log.debug("A new connection established to : " + route);
            }
        } else {
            // reinitialize timeouts for the pooled connection
            conn.setSocketTimeout(socketTimeout);
            try {
                handler.submitRequest(conn, axis2Req);
                if (log.isDebugEnabled()) {
                    log.debug("An existing connection reused to : " + hostname + ":" + port);
                }
            } catch (ConnectionClosedException e) {
                ioReactor.connect(new InetSocketAddress(hostname, port), null, axis2Req, sessionRequestCallback);
                if (log.isDebugEnabled()) {
                    log.debug("A new connection established to : " + hostname + ":" + port);
                }
            }
        }
        try {
            axis2Req.streamMessageContents();
        } catch (AxisFault af) {
            throw af;
        }
    } catch (MalformedURLException e) {
        handleException("Malformed destination EPR : " + epr.getAddress(), e);
    }
}
Also used : AxisFault(org.apache.axis2.AxisFault) MalformedURLException(java.net.MalformedURLException) ServerConnectionDebug(org.apache.synapse.transport.nhttp.debug.ServerConnectionDebug) InetSocketAddress(java.net.InetSocketAddress) ClientConnectionDebug(org.apache.synapse.transport.nhttp.debug.ClientConnectionDebug) ConnectionClosedException(org.apache.http.ConnectionClosedException) NHttpClientConnection(org.apache.http.nio.NHttpClientConnection) URL(java.net.URL) HttpRoute(org.apache.http.conn.routing.HttpRoute) HttpHost(org.apache.http.HttpHost)

Example 13 with NHttpClientConnection

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

the class TargetHandlerTest method testOutputReady.

/**
 * Testing whether output-ready connection is processed
 *
 * @throws Exception
 */
@Test
public void testOutputReady() throws Exception {
    DeliveryAgent deliveryAgent = mock(DeliveryAgent.class);
    ClientConnFactory connFactory = mock(ClientConnFactory.class);
    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);
    TargetContext targetContext = new TargetContext(targetConfiguration);
    MessageContext messageContext = new MessageContext();
    targetContext.setRequestMsgCtx(messageContext);
    TargetHandler targetHandler = new TargetHandler(deliveryAgent, connFactory, targetConfiguration);
    TargetRequest request = mock(TargetRequest.class);
    NHttpClientConnection conn = mock(NHttpClientConnection.class, Mockito.RETURNS_DEEP_STUBS);
    ContentEncoder encoder = mock(ContentEncoder.class);
    mockStatic(TargetContext.class);
    when(TargetContext.get(conn)).thenReturn(targetContext);
    when(TargetContext.getState(conn)).thenReturn(ProtocolState.REQUEST_HEAD);
    when(TargetContext.getRequest(conn)).thenReturn(request);
    when(request.hasEntityBody()).thenReturn(true);
    when(request.write(conn, encoder)).thenReturn(12);
    when(encoder.isCompleted()).thenReturn(true);
    targetHandler.outputReady(conn, encoder);
}
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) ContentEncoder(org.apache.http.nio.ContentEncoder) NativeWorkerPool(org.apache.axis2.transport.base.threads.NativeWorkerPool) NHttpClientConnection(org.apache.http.nio.NHttpClientConnection) WorkerPool(org.apache.axis2.transport.base.threads.WorkerPool) NativeWorkerPool(org.apache.axis2.transport.base.threads.NativeWorkerPool) ClientConnFactory(org.apache.synapse.transport.http.conn.ClientConnFactory) MessageContext(org.apache.axis2.context.MessageContext) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 14 with NHttpClientConnection

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

the class TargetHandlerTest method testInputReady.

/**
 * Testing whether input-ready connection is processed
 *
 * @throws Exception
 */
@Test
public void testInputReady() throws Exception {
    DeliveryAgent deliveryAgent = mock(DeliveryAgent.class);
    ClientConnFactory connFactory = mock(ClientConnFactory.class);
    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);
    TargetContext targetContext = new TargetContext(targetConfiguration);
    MessageContext messageContext = new MessageContext();
    targetContext.setRequestMsgCtx(messageContext);
    TargetHandler targetHandler = new TargetHandler(deliveryAgent, connFactory, targetConfiguration);
    TargetResponse response = mock(TargetResponse.class);
    NHttpClientConnection conn = mock(NHttpClientConnection.class, Mockito.RETURNS_DEEP_STUBS);
    ContentDecoder decoder = mock(ContentDecoder.class);
    mockStatic(TargetContext.class);
    when(TargetContext.get(conn)).thenReturn(targetContext);
    when(TargetContext.getState(conn)).thenReturn(ProtocolState.RESPONSE_HEAD);
    when(TargetContext.getResponse(conn)).thenReturn(response);
    when(decoder.isCompleted()).thenReturn(true);
    targetHandler.inputReady(conn, decoder);
}
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) NHttpClientConnection(org.apache.http.nio.NHttpClientConnection) WorkerPool(org.apache.axis2.transport.base.threads.WorkerPool) NativeWorkerPool(org.apache.axis2.transport.base.threads.NativeWorkerPool) ClientConnFactory(org.apache.synapse.transport.http.conn.ClientConnFactory) MessageContext(org.apache.axis2.context.MessageContext) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

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