Example usage for org.apache.http.conn.routing HttpRoute getTargetHost

List of usage examples for org.apache.http.conn.routing HttpRoute getTargetHost

Introduction

In this page you can find the example usage for org.apache.http.conn.routing HttpRoute getTargetHost.

Prototype

public final HttpHost getTargetHost() 

Source Link

Usage

From source file:org.apache.synapse.transport.nhttp.HttpCoreNIOSender.java

/**
 * 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//from  ww  w  .  j  a  v  a  2s .c o  m
 */
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);
        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 {
            conn.setSocketTimeout(socketTimeout); // reinitialize timeouts for the pooled connection
            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);
    }
}

From source file:org.apache.synapse.transport.passthru.connections.TargetConnections.java

/**
 * Return a connection to the host:port pair. If a connection is not available
 * return <code>null</code>. If the particular host:port allows to create more connections
 * this method will try to connect asynchronously. If the connection is successful it will
 * be notified in a separate thread./*from w  w  w  . j a  v a 2  s  .c  o  m*/
 *
 * @param host host
 * @param port port
 * @return Either returns a connection if already available or returns null and notifies
 *         the delivery agent when the connection is available
 */
public NHttpClientConnection getConnection(HttpRoute route) {
    if (log.isDebugEnabled()) {
        log.debug("Trying to get a connection " + route);
    }

    HostConnections pool = getConnectionPool(route);

    // trying to get an existing connection
    NHttpClientConnection connection = pool.getConnection();
    if (connection == null) {
        if (pool.canHaveMoreConnections()) {
            HttpHost host = route.getProxyHost() != null ? route.getProxyHost() : route.getTargetHost();
            ioReactor.connect(new InetSocketAddress(host.getHostName(), host.getPort()), null, pool, callback);
        } else {
            log.warn("Connection pool reached maximum allowed connections for route " + route
                    + ". Target server may have become slow");
        }
    }

    return connection;
}