Example usage for org.apache.http.protocol BasicHttpProcessor addInterceptor

List of usage examples for org.apache.http.protocol BasicHttpProcessor addInterceptor

Introduction

In this page you can find the example usage for org.apache.http.protocol BasicHttpProcessor addInterceptor.

Prototype

public final void addInterceptor(HttpResponseInterceptor httpResponseInterceptor) 

Source Link

Usage

From source file:org.apache.axis2.transport.http.server.HttpFactory.java

public HttpProcessor newHttpProcessor() {
    BasicHttpProcessor httpProcessor = new BasicHttpProcessor();
    httpProcessor.addInterceptor(new RequestSessionCookie());
    httpProcessor.addInterceptor(new ResponseDate());
    httpProcessor.addInterceptor(new ResponseServer());
    httpProcessor.addInterceptor(new ResponseContent());
    httpProcessor.addInterceptor(new ResponseConnControl());
    httpProcessor.addInterceptor(new ResponseSessionCookie());
    return httpProcessor;
}

From source file:org.frameworkset.spi.remote.http.HttpServer.java

protected NHttpServiceHandler createHttpServiceHandler(HttpRequestHandler requestHandler,
        HttpExpectationVerifier expectationVerifier, EventListener eventListener) {

    BasicHttpProcessor httpproc = new BasicHttpProcessor();
    httpproc.addInterceptor(new ResponseDate());
    httpproc.addInterceptor(new ResponseServer());
    httpproc.addInterceptor(new ResponseContent());
    httpproc.addInterceptor(new ResponseConnControl());

    BufferingHttpServiceHandler serviceHandler = new BufferingHttpServiceHandler(httpproc,
            new DefaultHttpResponseFactory(), new DefaultConnectionReuseStrategy(), this.serverParams);

    serviceHandler.setHandlerResolver(new SimpleHttpRequestHandlerResolver(requestHandler));
    serviceHandler.setExpectationVerifier(expectationVerifier);
    serviceHandler.setEventListener(eventListener);

    return serviceHandler;
}

From source file:com.asakusafw.yaess.jobqueue.client.HttpJobClientTest.java

/**
 * Initializes the test.// ww w .j  av  a  2s .c om
 * @throws Exception if some errors were occurred
 */
@Before
public void setUp() throws Exception {
    BasicHttpProcessor proc = new BasicHttpProcessor();
    proc.addInterceptor(new ResponseDate());
    proc.addInterceptor(new ResponseServer());
    proc.addInterceptor(new ResponseContent());
    proc.addInterceptor(new ResponseConnControl());
    proc.addInterceptor(new RequestBasicAuth());
    proc.addInterceptor(new ResponseBasicUnauthorized());
    server = new LocalTestServer(proc, null);
    server.start();
    InetSocketAddress address = server.getServiceAddress();
    baseUrl = new URL("http", address.getHostName(), address.getPort(), "/").toExternalForm();
}

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

public NHttpClient(int max_connections) {
    _max_connections = max_connections;// w w w  .j ava2  s.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:com.soundcloud.playerapi.ApiWrapper.java

/** @return The HttpClient instance used to make the calls */
public HttpClient getHttpClient() {
    if (httpClient == null) {
        final HttpParams params = getParams();
        HttpClientParams.setRedirecting(params, false);
        HttpProtocolParams.setUserAgent(params, getUserAgent());

        final SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", getSocketFactory(), 80));
        final SSLSocketFactory sslFactory = getSSLSocketFactory();
        registry.register(new Scheme("https", sslFactory, 443));
        httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager(params, registry), params) {
            {/* ww w  .  ja v a2 s. c o  m*/
                setKeepAliveStrategy(new ConnectionKeepAliveStrategy() {
                    @Override
                    public long getKeepAliveDuration(HttpResponse httpResponse, HttpContext httpContext) {
                        return KEEPALIVE_TIMEOUT;
                    }
                });

                getCredentialsProvider().setCredentials(
                        new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, CloudAPI.REALM, OAUTH_SCHEME),
                        OAuth2Scheme.EmptyCredentials.INSTANCE);

                getAuthSchemes().register(CloudAPI.OAUTH_SCHEME, new OAuth2Scheme.Factory(ApiWrapper.this));

                addResponseInterceptor(new HttpResponseInterceptor() {
                    @Override
                    public void process(HttpResponse response, HttpContext context)
                            throws HttpException, IOException {
                        if (response == null || response.getEntity() == null)
                            return;

                        HttpEntity entity = response.getEntity();
                        Header header = entity.getContentEncoding();
                        if (header != null) {
                            for (HeaderElement codec : header.getElements()) {
                                if (codec.getName().equalsIgnoreCase("gzip")) {
                                    response.setEntity(new GzipDecompressingEntity(entity));
                                    break;
                                }
                            }
                        }
                    }
                });
            }

            @Override
            protected HttpContext createHttpContext() {
                HttpContext ctxt = super.createHttpContext();
                ctxt.setAttribute(ClientContext.AUTH_SCHEME_PREF,
                        Arrays.asList(CloudAPI.OAUTH_SCHEME, "digest", "basic"));
                return ctxt;
            }

            @Override
            protected BasicHttpProcessor createHttpProcessor() {
                BasicHttpProcessor processor = super.createHttpProcessor();
                processor.addInterceptor(new OAuth2HttpRequestInterceptor());
                return processor;
            }

            // for testability only
            @Override
            protected RequestDirector createClientRequestDirector(HttpRequestExecutor requestExec,
                    ClientConnectionManager conman, ConnectionReuseStrategy reustrat,
                    ConnectionKeepAliveStrategy kastrat, HttpRoutePlanner rouplan, HttpProcessor httpProcessor,
                    HttpRequestRetryHandler retryHandler, RedirectHandler redirectHandler,
                    AuthenticationHandler targetAuthHandler, AuthenticationHandler proxyAuthHandler,
                    UserTokenHandler stateHandler, HttpParams params) {
                return getRequestDirector(requestExec, conman, reustrat, kastrat, rouplan, httpProcessor,
                        retryHandler, redirectHandler, targetAuthHandler, proxyAuthHandler, stateHandler,
                        params);
            }
        };
    }
    return httpClient;
}