Search in sources :

Example 1 with HttpInetConnection

use of org.apache.http.HttpInetConnection in project XobotOS by xamarin.

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 2 with HttpInetConnection

use of org.apache.http.HttpInetConnection in project oap by oaplatform.

the class BlockingHandlerAdapter method handle.

@Override
public void handle(final HttpRequest httpRequest, final HttpResponse httpResponse, final HttpContext httpContext) throws IOException {
    log.trace("Handling [{}]", httpRequest);
    final HttpInetConnection connection = (HttpInetConnection) httpContext.getAttribute(HTTP_CONNECTION);
    final InetAddress remoteAddress = connection.getRemoteAddress();
    final String httpContextProtocol = String.valueOf(httpContext.getAttribute("protocol"));
    final Request request = new Request(httpRequest, new Context(location, remoteAddress, httpContextProtocol));
    RequestCors cors = corsPolicy.getCors(request);
    final Response response = new Response(httpResponse, cors);
    if (Protocol.LOCAL.equals(this.protocol) && !Inet.isLocalAddress(remoteAddress)) {
        response.respond(HTTP_FORBIDDEN);
    } else if (cors.autoOptions && request.httpMethod == Request.HttpMethod.OPTIONS) {
        response.respond(NO_CONTENT);
    } else {
        handler.handle(request, response);
    }
}
Also used : HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) RequestCors(oap.http.cors.RequestCors) HttpRequest(org.apache.http.HttpRequest) HttpInetConnection(org.apache.http.HttpInetConnection) InetAddress(java.net.InetAddress)

Example 3 with HttpInetConnection

use of org.apache.http.HttpInetConnection in project oap by oaplatform.

the class NioHandlerAdapter method handle.

@Override
public void handle(final HttpRequest httpRequest, final HttpAsyncExchange httpAsyncExchange, final HttpContext httpContext) throws HttpException, IOException {
    LOGGER.trace("handling [{}]", httpRequest);
    final HttpInetConnection connection = (HttpInetConnection) httpContext.getAttribute(HttpCoreContext.HTTP_CONNECTION);
    final InetAddress remoteAddress = connection.getRemoteAddress();
    final HttpResponse response = httpAsyncExchange.getResponse();
    final String httpContextProtocol = String.valueOf(httpContext.getAttribute("protocol"));
    if (Protocol.LOCAL.equals(this.protocol) && !Inet.isLocalAddress(remoteAddress)) {
        response.setStatusCode(HTTP_FORBIDDEN);
    } else {
        Request request = new Request(httpRequest, new Context(location, remoteAddress, httpContextProtocol));
        handler.handle(request, new Response(response, corsPolicy.getCors(request)));
    }
    httpAsyncExchange.submitResponse();
}
Also used : Context(oap.http.Context) HttpCoreContext(org.apache.http.protocol.HttpCoreContext) HttpContext(org.apache.http.protocol.HttpContext) Response(oap.http.Response) HttpResponse(org.apache.http.HttpResponse) HttpRequest(org.apache.http.HttpRequest) Request(oap.http.Request) HttpResponse(org.apache.http.HttpResponse) HttpInetConnection(org.apache.http.HttpInetConnection) InetAddress(java.net.InetAddress)

Example 4 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 5 with HttpInetConnection

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

the class ServerWorker method createMessageContext.

/**
 * Create an Axis2 message context for the given http request. The request may be in the
 * process of being streamed
 *
 * @param request the http request to be used to create the corresponding Axis2 message context
 * @return the Axis2 message context created
 */
public MessageContext createMessageContext(MessageContext msgContext, SourceRequest request) {
    Map excessHeaders = request.getExcessHeaders();
    ConfigurationContext cfgCtx = sourceConfiguration.getConfigurationContext();
    NHttpServerConnection conn = request.getConnection();
    Object systemGeneratedCorrelationLog = conn.getContext().getAttribute(CorrelationConstants.SYSTEM_GENERATED_CORRELATION_ID);
    Object correlationId = conn.getContext().getAttribute(CorrelationConstants.CORRELATION_ID);
    if (msgContext == null) {
        msgContext = new MessageContext();
    }
    String messageId = null;
    if (systemGeneratedCorrelationLog instanceof Boolean && (Boolean) systemGeneratedCorrelationLog) {
        if (correlationId instanceof String && StringUtils.isNotEmpty((String) correlationId)) {
            messageId = "urn:uuid:" + correlationId;
        }
    }
    if (StringUtils.isEmpty(messageId)) {
        messageId = UIDGenerator.generateURNString();
    }
    msgContext.setMessageID(messageId);
    // Axis2 spawns a new threads to send a message if this is TRUE - and it has to
    // be the other way
    msgContext.setProperty(MessageContext.CLIENT_API_NON_BLOCKING, Boolean.FALSE);
    msgContext.setConfigurationContext(cfgCtx);
    // msgContext.setTransportOut(cfgCtx.getAxisConfiguration()
    // .getTransportOut(Constants.TRANSPORT_HTTP));
    // msgContext.setTransportIn(cfgCtx.getAxisConfiguration()
    // .getTransportIn(Constants.TRANSPORT_HTTP));
    // msgContext.setIncomingTransportName(Constants.TRANSPORT_HTTP);
    // msgContext.setProperty(Constants.OUT_TRANSPORT_INFO, this);
    // propagate correlation logging related properties
    msgContext.setProperty(CorrelationConstants.CORRELATION_ID, correlationId);
    // propagate transaction property
    msgContext.setProperty(BaseConstants.INTERNAL_TRANSACTION_COUNTED, conn.getContext().getAttribute(BaseConstants.INTERNAL_TRANSACTION_COUNTED));
    if (sourceConfiguration.getScheme().isSSL()) {
        msgContext.setTransportOut(cfgCtx.getAxisConfiguration().getTransportOut(Constants.TRANSPORT_HTTPS));
        msgContext.setTransportIn(cfgCtx.getAxisConfiguration().getTransportIn(Constants.TRANSPORT_HTTPS));
        msgContext.setIncomingTransportName(sourceConfiguration.getInDescription() != null ? sourceConfiguration.getInDescription().getName() : Constants.TRANSPORT_HTTPS);
        SSLIOSession ssliosession = (SSLIOSession) (conn.getContext()).getAttribute(SSLIOSession.SESSION_KEY);
        // set SSL certificates to message context if SSLVerifyClient parameter is set
        if (ssliosession != null && msgContext.getTransportIn() != null && msgContext.getTransportIn().getParameter(NhttpConstants.SSL_VERIFY_CLIENT) != null) {
            try {
                msgContext.setProperty(NhttpConstants.SSL_CLIENT_AUTH_CERT_X509, ssliosession.getSSLSession().getPeerCertificateChain());
            } catch (SSLPeerUnverifiedException e) {
                // Peer Certificate Chain may not be available always.(in case of Mutual SSL is not enabled)
                if (log.isTraceEnabled()) {
                    log.trace("Peer certificate chain is not available for MsgContext " + msgContext.getMessageID());
                }
            }
        }
    } else {
        msgContext.setTransportOut(cfgCtx.getAxisConfiguration().getTransportOut(Constants.TRANSPORT_HTTP));
        msgContext.setTransportIn(cfgCtx.getAxisConfiguration().getTransportIn(Constants.TRANSPORT_HTTP));
        msgContext.setIncomingTransportName(sourceConfiguration.getInDescription() != null ? sourceConfiguration.getInDescription().getName() : Constants.TRANSPORT_HTTP);
    }
    msgContext.setProperty(Constants.OUT_TRANSPORT_INFO, this);
    msgContext.setServerSide(true);
    msgContext.setProperty(Constants.Configuration.TRANSPORT_IN_URL, request.getUri());
    // http transport header names are case insensitive
    Map<String, String> headers = new TreeMap<String, String>(new Comparator<String>() {

        public int compare(String o1, String o2) {
            return o1.compareToIgnoreCase(o2);
        }
    });
    Set<Map.Entry<String, String>> entries = request.getHeaders().entrySet();
    for (Map.Entry<String, String> entry : entries) {
        headers.put(entry.getKey(), entry.getValue());
    }
    msgContext.setProperty(MessageContext.TRANSPORT_HEADERS, headers);
    msgContext.setProperty(NhttpConstants.EXCESS_TRANSPORT_HEADERS, excessHeaders);
    if (conn instanceof HttpInetConnection) {
        HttpInetConnection netConn = (HttpInetConnection) conn;
        InetAddress remoteAddress = netConn.getRemoteAddress();
        if (remoteAddress != null) {
            msgContext.setProperty(MessageContext.REMOTE_ADDR, remoteAddress.getHostAddress());
            msgContext.setProperty(NhttpConstants.REMOTE_HOST, NhttpUtil.getHostName(remoteAddress));
        }
    }
    msgContext.setProperty(RequestResponseTransport.TRANSPORT_CONTROL, new HttpCoreRequestResponseTransport(msgContext));
    msgContext.setProperty(PassThroughConstants.TRANSPORT_MESSAGE_HANDLER, new PassThroughMessageHandler());
    return msgContext;
}
Also used : PassThroughMessageHandler(org.apache.synapse.transport.util.PassThroughMessageHandler) ConfigurationContext(org.apache.axis2.context.ConfigurationContext) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) HttpInetConnection(org.apache.http.HttpInetConnection) TreeMap(java.util.TreeMap) HttpCoreRequestResponseTransport(org.apache.synapse.transport.nhttp.HttpCoreRequestResponseTransport) NHttpServerConnection(org.apache.http.nio.NHttpServerConnection) SSLIOSession(org.apache.http.nio.reactor.ssl.SSLIOSession) MessageContext(org.apache.axis2.context.MessageContext) Map(java.util.Map) TreeMap(java.util.TreeMap) InetAddress(java.net.InetAddress)

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