Search in sources :

Example 6 with HttpInetConnection

use of org.apache.http.HttpInetConnection in project platform_external_apache-http by android.

the class RequestTargetHost method process.

public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    if (request == null) {
        throw new IllegalArgumentException("HTTP request may not be null");
    }
    if (context == null) {
        throw new IllegalArgumentException("HTTP context may not be null");
    }
    if (!request.containsHeader(HTTP.TARGET_HOST)) {
        HttpHost targethost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        if (targethost == null) {
            HttpConnection conn = (HttpConnection) context.getAttribute(ExecutionContext.HTTP_CONNECTION);
            if (conn instanceof HttpInetConnection) {
                // Populate the context with a default HTTP host based on the
                // inet address of the target host
                InetAddress address = ((HttpInetConnection) conn).getRemoteAddress();
                int port = ((HttpInetConnection) conn).getRemotePort();
                if (address != null) {
                    targethost = new HttpHost(address.getHostName(), port);
                }
            }
            if (targethost == null) {
                ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
                if (ver.lessEquals(HttpVersion.HTTP_1_0)) {
                    return;
                } else {
                    throw new ProtocolException("Target host missing");
                }
            }
        }
        request.addHeader(HTTP.TARGET_HOST, targethost.toHostString());
    }
}
Also used : ProtocolException(org.apache.http.ProtocolException) HttpConnection(org.apache.http.HttpConnection) HttpHost(org.apache.http.HttpHost) HttpInetConnection(org.apache.http.HttpInetConnection) ProtocolVersion(org.apache.http.ProtocolVersion) InetAddress(java.net.InetAddress)

Example 7 with HttpInetConnection

use of org.apache.http.HttpInetConnection in project robovm by robovm.

the class RequestTargetHost method process.

public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    if (request == null) {
        throw new IllegalArgumentException("HTTP request may not be null");
    }
    if (context == null) {
        throw new IllegalArgumentException("HTTP context may not be null");
    }
    if (!request.containsHeader(HTTP.TARGET_HOST)) {
        HttpHost targethost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        if (targethost == null) {
            HttpConnection conn = (HttpConnection) context.getAttribute(ExecutionContext.HTTP_CONNECTION);
            if (conn instanceof HttpInetConnection) {
                // Populate the context with a default HTTP host based on the 
                // inet address of the target host
                InetAddress address = ((HttpInetConnection) conn).getRemoteAddress();
                int port = ((HttpInetConnection) conn).getRemotePort();
                if (address != null) {
                    targethost = new HttpHost(address.getHostName(), port);
                }
            }
            if (targethost == null) {
                ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
                if (ver.lessEquals(HttpVersion.HTTP_1_0)) {
                    return;
                } else {
                    throw new ProtocolException("Target host missing");
                }
            }
        }
        request.addHeader(HTTP.TARGET_HOST, targethost.toHostString());
    }
}
Also used : ProtocolException(org.apache.http.ProtocolException) HttpConnection(org.apache.http.HttpConnection) HttpHost(org.apache.http.HttpHost) HttpInetConnection(org.apache.http.HttpInetConnection) ProtocolVersion(org.apache.http.ProtocolVersion) InetAddress(java.net.InetAddress)

Example 8 with HttpInetConnection

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

the class ClientHandler method shutdownConnection.

/**
 * Shutdown the connection ignoring any IO errors during the process
 *
 * @param conn     the connection to be shutdown
 * @param isError  whether shutdown is due to an error
 * @param errorMsg error message if shutdown happens on error
 */
private void shutdownConnection(final NHttpClientConnection conn, boolean isError, String errorMsg) {
    if (conn instanceof HttpInetConnection) {
        HttpInetConnection inetConnection = (HttpInetConnection) conn;
        if (log.isWarnEnabled() && (isError || log.isDebugEnabled())) {
            String msg = "Connection from local address : " + inetConnection.getLocalAddress() + ":" + inetConnection.getLocalPort() + " to remote address : " + inetConnection.getRemoteAddress() + ":" + inetConnection.getRemotePort() + " is closed!" + (errorMsg != null ? " - On error : " + errorMsg : "");
            if (isError) {
                log.warn(msg);
            } else {
                log.debug(msg);
            }
        }
        if (countConnections) {
            removeConnectionRecord(inetConnection);
        }
    }
    HttpContext context = conn.getContext();
    NhttpSharedOutputBuffer outputBuffer = (NhttpSharedOutputBuffer) context.getAttribute(REQUEST_SOURCE_BUFFER);
    if (outputBuffer != null) {
        outputBuffer.close();
    }
    SharedInputBuffer inputBuffer = (SharedInputBuffer) context.getAttribute(RESPONSE_SINK_BUFFER);
    if (inputBuffer != null) {
        inputBuffer.close();
    }
    try {
        conn.shutdown();
    } catch (IOException ignore) {
    }
    context.removeAttribute(RESPONSE_SINK_BUFFER);
    context.removeAttribute(REQUEST_SOURCE_BUFFER);
    context.removeAttribute(CLIENT_CONNECTION_DEBUG);
    context.removeAttribute(CONNECTION_CREATION_TIME);
}
Also used : SharedInputBuffer(org.apache.http.nio.util.SharedInputBuffer) HttpContext(org.apache.http.protocol.HttpContext) HttpInetConnection(org.apache.http.HttpInetConnection) IOException(java.io.IOException)

Example 9 with HttpInetConnection

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

the class ServerWorker method processHttpRequestUri.

/**
 * Get Uri of underlying SourceRequest and  calculate service prefix and add to message context
 * create response buffers for  HTTP GET, DELETE, OPTION and HEAD methods
 * @param msgContext Axis2MessageContext of the request
 * @param method HTTP Method of the request
 */
public void processHttpRequestUri(MessageContext msgContext, String method) {
    String servicePrefixIndex = "://";
    ConfigurationContext cfgCtx = sourceConfiguration.getConfigurationContext();
    msgContext.setProperty(Constants.Configuration.HTTP_METHOD, request.getMethod());
    // String uri = request.getUri();
    String oriUri = request.getUri();
    String restUrlPostfix = NhttpUtil.getRestUrlPostfix(oriUri, cfgCtx.getServicePath());
    String servicePrefix = oriUri.substring(0, oriUri.indexOf(restUrlPostfix));
    if (servicePrefix.indexOf(servicePrefixIndex) == -1) {
        HttpInetConnection inetConn = (HttpInetConnection) request.getConnection();
        InetAddress localAddr = inetConn.getLocalAddress();
        if (localAddr != null) {
            servicePrefix = sourceConfiguration.getScheme().getName() + servicePrefixIndex + localAddr.getHostAddress() + ":" + inetConn.getLocalPort() + servicePrefix;
        }
    }
    msgContext.setProperty(PassThroughConstants.SERVICE_PREFIX, servicePrefix);
    msgContext.setTo(new EndpointReference(restUrlPostfix));
    msgContext.setProperty(PassThroughConstants.REST_URL_POSTFIX, restUrlPostfix);
    if (PassThroughConstants.HTTP_GET.equals(method) || PassThroughConstants.HTTP_HEAD.equals(method) || PassThroughConstants.HTTP_OPTIONS.equals(method)) {
        HttpResponse response = sourceConfiguration.getResponseFactory().newHttpResponse(request.getVersion(), HttpStatus.SC_OK, request.getConnection().getContext());
        // create a basic HttpEntity using the source channel of the response pipe
        BasicHttpEntity entity = new BasicHttpEntity();
        if (request.getVersion().greaterEquals(HttpVersion.HTTP_1_1)) {
            entity.setChunked(true);
        }
        response.setEntity(entity);
        httpGetRequestProcessor.process(request.getRequest(), response, msgContext, request.getConnection(), os, isRestDispatching);
    }
}
Also used : ConfigurationContext(org.apache.axis2.context.ConfigurationContext) HttpResponse(org.apache.http.HttpResponse) HttpInetConnection(org.apache.http.HttpInetConnection) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) InetAddress(java.net.InetAddress) EndpointReference(org.apache.axis2.addressing.EndpointReference)

Aggregations

HttpInetConnection (org.apache.http.HttpInetConnection)9 InetAddress (java.net.InetAddress)7 HttpConnection (org.apache.http.HttpConnection)3 HttpHost (org.apache.http.HttpHost)3 HttpResponse (org.apache.http.HttpResponse)3 ProtocolException (org.apache.http.ProtocolException)3 ProtocolVersion (org.apache.http.ProtocolVersion)3 HttpContext (org.apache.http.protocol.HttpContext)3 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)2 HttpRequest (org.apache.http.HttpRequest)2 IOException (java.io.IOException)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 SSLPeerUnverifiedException (javax.net.ssl.SSLPeerUnverifiedException)1 Context (oap.http.Context)1 Request (oap.http.Request)1 Response (oap.http.Response)1 RequestCors (oap.http.cors.RequestCors)1 EndpointReference (org.apache.axis2.addressing.EndpointReference)1