Example usage for org.apache.http.nio NHttpConnection getContext

List of usage examples for org.apache.http.nio NHttpConnection getContext

Introduction

In this page you can find the example usage for org.apache.http.nio NHttpConnection getContext.

Prototype

HttpContext getContext();

Source Link

Document

Returns an HTTP execution context associated with this connection.

Usage

From source file:com.google.acre.script.NHttpClient.java

public NHttpClient(int max_connections) {
    _max_connections = max_connections;//w w w .  j  av  a  2s  . c  o m
    _costCollector = CostCollector.getInstance();

    BasicHttpProcessor httpproc = new BasicHttpProcessor();
    httpproc.addInterceptor(new RequestContent());
    httpproc.addInterceptor(new RequestTargetHost());
    httpproc.addInterceptor(new RequestConnControl());
    httpproc.addInterceptor(new RequestUserAgent());
    httpproc.addInterceptor(new RequestExpectContinue());

    BufferingHttpClientHandler handler = new BufferingHttpClientHandler(httpproc,
            new NHttpRequestExecutionHandler(), new DefaultConnectionReuseStrategy(), DEFAULT_HTTP_PARAMS);

    handler.setEventListener(new EventListener() {
        private final static String REQUEST_CLOSURE = "request-closure";

        public void connectionClosed(NHttpConnection conn) {
            // pass (should we be logging this?)
        }

        public void connectionOpen(NHttpConnection conn) {
            // pass (should we be logging this?)
        }

        public void connectionTimeout(NHttpConnection conn) {
            noteException(null, conn);
        }

        void noteException(Exception e, NHttpConnection conn) {
            HttpContext context = conn.getContext();
            NHttpClientClosure closure = (NHttpClientClosure) context.getAttribute(REQUEST_CLOSURE);
            if (closure != null)
                closure.exceptions().add(e);
        }

        public void fatalIOException(IOException e, NHttpConnection conn) {
            noteException(e, conn);
        }

        public void fatalProtocolException(HttpException e, NHttpConnection conn) {
            noteException(e, conn);
        }
    });

    try {
        SSLContext sctx = SSLContext.getInstance("SSL");
        sctx.init(null, null, null);
        _dispatch = new NHttpAdaptableSensibleAndLogicalIOEventDispatch(handler, sctx, DEFAULT_HTTP_PARAMS);
    } catch (java.security.KeyManagementException e) {
        throw new RuntimeException(e);
    } catch (java.security.NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }

    _requests = new ArrayList<NHttpClientClosure>();
    _connection_lock = new Semaphore(_max_connections);

}

From source file:org.apache.synapse.transport.passthru.TargetContext.java

public static void updateState(NHttpConnection conn, ProtocolState state) {

    TargetContext targetContext = (TargetContext) conn.getContext().getAttribute(CONNECTION_INFORMATION);
    if (targetContext != null) {
        targetContext.setState(state);/*from w w w.jav  a 2  s . c  om*/
        if (targetContext.getTargetConfiguration().isCorrelationLoggingEnabled()
                && conn.getContext().getAttribute(PassThroughConstants.CORRELATION_ID) != null) {
            long lastStateUpdateTime = targetContext.getLastStateUpdatedTime();
            String url = "", method = "";
            if (targetContext.getRequest() != null) {
                url = targetContext.getRequest().getUrl().toString();
                method = targetContext.getRequest().getMethod();
            } else {
                HttpRequest httpRequest = conn.getHttpRequest();
                if (httpRequest != null) {
                    url = httpRequest.getRequestLine().getUri();
                    method = httpRequest.getRequestLine().getMethod();
                }
            }
            if ((method.length() != 0) && (url.length() != 0)) {
                MDC.put(PassThroughConstants.CORRELATION_MDC_PROPERTY,
                        conn.getContext().getAttribute(PassThroughConstants.CORRELATION_ID).toString());
                correlationLog.info((targetContext.updateLastStateUpdatedTime() - lastStateUpdateTime)
                        + "|HTTP State Transition|" + conn.getContext().getAttribute("http.connection") + "|"
                        + method + "|" + url + "|" + state.name());
                MDC.remove(PassThroughConstants.CORRELATION_MDC_PROPERTY);
            }
        }
    } else {
        throw new IllegalStateException("Connection information should be present");
    }
}