Example usage for org.apache.http.impl.client HttpClientBuilder addInterceptorLast

List of usage examples for org.apache.http.impl.client HttpClientBuilder addInterceptorLast

Introduction

In this page you can find the example usage for org.apache.http.impl.client HttpClientBuilder addInterceptorLast.

Prototype

public final HttpClientBuilder addInterceptorLast(final HttpRequestInterceptor itcp) 

Source Link

Document

Adds this protocol interceptor to the tail of the protocol processing list.

Usage

From source file:org.apache.solr.client.solrj.impl.HttpClientUtil.java

/**
 * Creates new http client by using the provided configuration.
 * //from w  w w. j a v a 2s .c o m
 */
public static CloseableHttpClient createClient(final SolrParams params, PoolingHttpClientConnectionManager cm,
        boolean sharedConnectionManager) {
    final ModifiableSolrParams config = new ModifiableSolrParams(params);
    if (logger.isDebugEnabled()) {
        logger.debug("Creating new http client, config:" + config);
    }

    cm.setMaxTotal(params.getInt(HttpClientUtil.PROP_MAX_CONNECTIONS, 10000));
    cm.setDefaultMaxPerRoute(params.getInt(HttpClientUtil.PROP_MAX_CONNECTIONS_PER_HOST, 10000));
    cm.setValidateAfterInactivity(
            Integer.getInteger(VALIDATE_AFTER_INACTIVITY, VALIDATE_AFTER_INACTIVITY_DEFAULT));

    HttpClientBuilder newHttpClientBuilder = HttpClientBuilder.create();

    if (sharedConnectionManager) {
        newHttpClientBuilder.setConnectionManagerShared(true);
    } else {
        newHttpClientBuilder.setConnectionManagerShared(false);
    }

    ConnectionKeepAliveStrategy keepAliveStrat = new ConnectionKeepAliveStrategy() {
        @Override
        public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
            // we only close connections based on idle time, not ttl expiration
            return -1;
        }
    };

    if (httpClientBuilder.getAuthSchemeRegistryProvider() != null) {
        newHttpClientBuilder.setDefaultAuthSchemeRegistry(
                httpClientBuilder.getAuthSchemeRegistryProvider().getAuthSchemeRegistry());
    }
    if (httpClientBuilder.getCookieSpecRegistryProvider() != null) {
        newHttpClientBuilder.setDefaultCookieSpecRegistry(
                httpClientBuilder.getCookieSpecRegistryProvider().getCookieSpecRegistry());
    }
    if (httpClientBuilder.getCredentialsProviderProvider() != null) {
        newHttpClientBuilder.setDefaultCredentialsProvider(
                httpClientBuilder.getCredentialsProviderProvider().getCredentialsProvider());
    }

    newHttpClientBuilder.addInterceptorLast(new DynamicInterceptor());

    newHttpClientBuilder = newHttpClientBuilder.setKeepAliveStrategy(keepAliveStrat).evictIdleConnections(
            (long) Integer.getInteger(EVICT_IDLE_CONNECTIONS, EVICT_IDLE_CONNECTIONS_DEFAULT),
            TimeUnit.MILLISECONDS);

    HttpClientBuilder builder = setupBuilder(newHttpClientBuilder, params);

    HttpClient httpClient = builder.setConnectionManager(cm).build();

    assert ObjectReleaseTracker.track(httpClient);
    return (CloseableHttpClient) httpClient;
}

From source file:com.helger.httpclient.HttpClientFactory.java

@Nonnull
public HttpClientBuilder createHttpClientBuilder() {
    final LayeredConnectionSocketFactory aSSLFactory = createSSLFactory();
    if (aSSLFactory == null)
        throw new IllegalStateException("Failed to create SSL SocketFactory");

    final HttpClientConnectionManager aConnMgr = createConnectionManager(aSSLFactory);
    final RequestConfig aRequestConfig = createRequestConfig();
    final HttpHost aProxyHost = getProxyHost();
    final CredentialsProvider aCredentialsProvider = createCredentialsProvider();

    final HttpClientBuilder aHCB = HttpClients.custom().setConnectionManager(aConnMgr)
            .setDefaultRequestConfig(aRequestConfig).setProxy(aProxyHost)
            .setDefaultCredentialsProvider(aCredentialsProvider);

    // Allow gzip,compress
    aHCB.addInterceptorLast(new RequestAcceptEncoding());
    // Add cookies
    aHCB.addInterceptorLast(new RequestAddCookies());
    // Un-gzip or uncompress
    aHCB.addInterceptorLast(new ResponseContentEncoding());

    // Enable usage of Java networking system properties
    if (m_bUseSystemProperties)
        aHCB.useSystemProperties();/*from   w  w w.j a  v a 2 s .c o  m*/

    // Set retry handler (if needed)
    if (m_nRetries > 0)
        aHCB.setRetryHandler(createRequestRetryHandler(m_nRetries, m_eRetryMode));

    return aHCB;
}

From source file:org.sonatype.nexus.plugins.crowd.client.rest.RestClient.java

RestClient(CrowdPluginConfiguration config) throws URISyntaxException {
    crowdServer = new URI(config.getCrowdServerUrl()).resolve("rest/usermanagement/1/");

    crowdCreds = new UsernamePasswordCredentials(config.getApplicationName(), config.getApplicationPassword());

    // configure the http client
    RequestConfig.Builder reqConfigBuilder = RequestConfig.custom().setAuthenticationEnabled(true)
            .setConnectTimeout(config.getHttpTimeout()).setSocketTimeout(config.getHttpTimeout());

    cm = new PoolingHttpClientConnectionManager();
    cm.setMaxTotal(config.getHttpMaxConnections());
    cm.setDefaultMaxPerRoute(config.getHttpMaxConnections());

    // proxy settings
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    if (StringUtils.isNotBlank(config.getHttpProxyHost()) && config.getHttpProxyPort() > 0) {
        HttpHost proxy = new HttpHost(config.getHttpProxyHost(), config.getHttpProxyPort());
        reqConfigBuilder.setProxy(proxy);

        if (config.getHttpProxyUsername() != null && config.getHttpProxyPassword() != null) {
            credsProvider.setCredentials(new AuthScope(proxy), new UsernamePasswordCredentials(
                    config.getHttpProxyUsername(), config.getHttpProxyPassword()));
        }//from   w  w w. j  ava  2  s .c o m
    }

    RequestConfig reqConfig = reqConfigBuilder.build();
    HttpClientBuilder hcBuilder = HttpClients.custom().setMaxConnPerRoute(config.getHttpMaxConnections())
            .setMaxConnTotal(config.getHttpMaxConnections()).setConnectionManager(cm)
            .setDefaultCredentialsProvider(credsProvider).setDefaultRequestConfig(reqConfig);

    // handling of compressed responses
    hcBuilder.addInterceptorLast(new CompressedHttpResponseInterceptor());

    client = hcBuilder.build();

    if (LOG.isDebugEnabled()) {
        LOG.debug("HTTP Client config");
        LOG.debug(config.getCrowdServerUrl());
        LOG.debug("PROPERTY_THREADPOOL_SIZE:" + cm.getMaxTotal());
        LOG.debug("PROPERTY_READ_TIMEOUT:" + reqConfig.getSocketTimeout());
        LOG.debug("PROPERTY_CONNECT_TIMEOUT:" + reqConfig.getConnectTimeout());
        if (reqConfig.getProxy() != null) {
            LOG.debug("PROPERTY_PROXY_URI:" + reqConfig.getProxy().toString());
        }
        LOG.debug("Crowd application name:" + config.getApplicationName());
    }
}

From source file:net.yacy.cora.protocol.http.HTTPClient.java

private static HttpClientBuilder initClientBuilder() {
    final HttpClientBuilder builder = HttpClientBuilder.create();

    builder.setConnectionManager(CONNECTION_MANAGER);
    builder.setDefaultRequestConfig(dfltReqConf);

    // UserAgent/*from w w w.  j  av a2  s  .  c o m*/
    builder.setUserAgent(ClientIdentification.yacyInternetCrawlerAgent.userAgent);

    // remove retries; we expect connections to fail; therefore we should not retry
    //builder.disableAutomaticRetries();
    // disable the cookiestore, cause this may cause segfaults and is not needed
    builder.setDefaultCookieStore(null);
    builder.disableCookieManagement();

    // add custom keep alive strategy
    builder.setKeepAliveStrategy(customKeepAliveStrategy());

    // ask for gzip
    builder.addInterceptorLast(new GzipRequestInterceptor());
    // uncompress gzip
    builder.addInterceptorLast(new GzipResponseInterceptor());
    // Proxy
    builder.setRoutePlanner(ProxySettings.RoutePlanner);
    builder.setDefaultCredentialsProvider(ProxySettings.CredsProvider);

    return builder;
}

From source file:com.normalexception.app.rx8club.html.LoginFactory.java

/**
 * Initialize the client, cookie store, and context
 *//*from ww w .  j a v  a 2  s. c om*/
private void initializeClientInformation() {
    Log.d(TAG, "Initializing Client...");

    /*
    Log.d(TAG, "Creating Custom Cache Configuration");
    CacheConfig cacheConfig = CacheConfig.custom()
      .setMaxCacheEntries(1000)
      .setMaxObjectSize(8192)
      .build();
    */

    Log.d(TAG, "Creating Custom Request Configuration");
    RequestConfig rConfig = RequestConfig.custom().setCircularRedirectsAllowed(true)
            .setConnectionRequestTimeout(TIMEOUT).setSocketTimeout(TIMEOUT).build();

    cookieStore = new BasicCookieStore();
    httpContext = new BasicHttpContext();

    Log.d(TAG, "Building Custom HTTP Client");
    HttpClientBuilder httpclientbuilder = HttpClients.custom();
    //httpclientbuilder.setCacheConfig(cacheConfig);
    httpclientbuilder.setDefaultRequestConfig(rConfig);
    httpclientbuilder.setDefaultCookieStore(cookieStore);

    Log.d(TAG, "Connection Manager Initializing");
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    cm.setMaxTotal(200);
    cm.setDefaultMaxPerRoute(20);
    httpclientbuilder.setConnectionManager(cm);

    Log.d(TAG, "Enable GZIP Compression");
    httpclientbuilder.addInterceptorLast(new HttpRequestInterceptor() {

        public void process(final HttpRequest request, final HttpContext context)
                throws HttpException, IOException {
            if (!request.containsHeader("Accept-Encoding")) {
                request.addHeader("Accept-Encoding", "gzip");
            }
        }

    });
    httpclientbuilder.addInterceptorLast(new HttpResponseInterceptor() {

        public void process(final HttpResponse response, final HttpContext context)
                throws HttpException, IOException {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                Header ceheader = entity.getContentEncoding();
                if (ceheader != null) {
                    HeaderElement[] codecs = ceheader.getElements();
                    for (int i = 0; i < codecs.length; i++) {
                        if (codecs[i].getName().equalsIgnoreCase("gzip")) {
                            response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                            return;
                        }
                    }
                }
            }
        }

    });

    // Follow Redirects
    Log.d(TAG, "Registering Redirect Strategy");
    httpclientbuilder.setRedirectStrategy(new RedirectStrategy());

    // Setup retry handler
    Log.d(TAG, "Registering Retry Handler");
    httpclientbuilder.setRetryHandler(new RetryHandler());

    // Setup KAS
    Log.d(TAG, "Registering Keep Alive Strategy");
    httpclientbuilder.setKeepAliveStrategy(new KeepAliveStrategy());

    httpclient = httpclientbuilder.build();

    //httpclient.log.enableDebug(
    //      MainApplication.isHttpClientLogEnabled());

    isInitialized = true;
}

From source file:de.comlineag.snc.webcrawler.fetcher.PageFetcher.java

public PageFetcher(CrawlConfig config) {
    super(config);

    RequestConfig requestConfig = RequestConfig.custom().setExpectContinueEnabled(false)
            .setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY).setRedirectsEnabled(false)
            .setSocketTimeout(config.getSocketTimeout()).setConnectTimeout(config.getConnectionTimeout())
            .build();//  w  ww.java  2 s. com

    RegistryBuilder<ConnectionSocketFactory> connRegistryBuilder = RegistryBuilder.create();
    connRegistryBuilder.register("http", PlainConnectionSocketFactory.INSTANCE);
    if (config.isIncludeHttpsPages()) {
        try { // Fixing: https://code.google.com/p/crawler4j/issues/detail?id=174
            // By always trusting the ssl certificate
            SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
                @Override
                public boolean isTrusted(final X509Certificate[] chain, String authType) {
                    return true;
                }
            }).build();
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext,
                    SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            connRegistryBuilder.register("https", sslsf);
        } catch (Exception e) {
            logger.debug("Exception thrown while trying to register https:", e);
        }
    }

    Registry<ConnectionSocketFactory> connRegistry = connRegistryBuilder.build();
    connectionManager = new PoolingHttpClientConnectionManager(connRegistry);
    connectionManager.setMaxTotal(config.getMaxTotalConnections());
    connectionManager.setDefaultMaxPerRoute(config.getMaxConnectionsPerHost());

    HttpClientBuilder clientBuilder = HttpClientBuilder.create();
    clientBuilder.setDefaultRequestConfig(requestConfig);
    clientBuilder.setConnectionManager(connectionManager);
    clientBuilder.setUserAgent(config.getUserAgentString());
    if (config.getProxyHost() != null) {

        if (config.getProxyUsername() != null) {
            BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(new AuthScope(config.getProxyHost(), config.getProxyPort()),
                    new UsernamePasswordCredentials(config.getProxyUsername(), config.getProxyPassword()));
            clientBuilder.setDefaultCredentialsProvider(credentialsProvider);
        }

        HttpHost proxy = new HttpHost(config.getProxyHost(), config.getProxyPort());
        clientBuilder.setProxy(proxy);
    }
    clientBuilder.addInterceptorLast(new HttpResponseInterceptor() {
        @Override
        public void process(final HttpResponse response, final HttpContext context)
                throws HttpException, IOException {
            HttpEntity entity = response.getEntity();
            Header contentEncoding = entity.getContentEncoding();
            if (contentEncoding != null) {
                HeaderElement[] codecs = contentEncoding.getElements();
                for (HeaderElement codec : codecs) {
                    if (codec.getName().equalsIgnoreCase("gzip")) {
                        response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                        return;
                    }
                }
            }
        }
    });

    httpClient = clientBuilder.build();

    if (connectionMonitorThread == null) {
        connectionMonitorThread = new IdleConnectionMonitorThread(connectionManager);
    }
    connectionMonitorThread.start();
}

From source file:com.wudaosoft.net.httpclient.Request.java

private CloseableHttpClient create() {

    CookieStore cookieStore = null;

    try {//from   w w w  .  java  2s  . c o  m
        if (defaultCookieStoreClass != null)
            cookieStore = defaultCookieStoreClass.newInstance();
    } catch (InstantiationException e) {
    } catch (IllegalAccessException e) {
    }

    HttpClientBuilder builder = HttpClients.custom().setConnectionManager(connManager)
            .setDefaultRequestConfig(hostConfig.getRequestConfig()).setRetryHandler(retryHandler)
            .setDefaultCookieStore(cookieStore);

    if (isKeepAlive) {
        builder.setKeepAliveStrategy(keepAliveStrategy);
    } else {
        builder.setConnectionReuseStrategy(NoConnectionReuseStrategy.INSTANCE);
    }

    if (requestInterceptor != null) {
        builder.addInterceptorLast(requestInterceptor);
    }

    CloseableHttpClient httpClient = builder.build();

    return httpClient;
}

From source file:org.sonatype.nexus.internal.httpclient.HttpClientManagerImpl.java

@Override
@Guarded(by = STARTED)//from w  w  w  .j  a v a  2s  . co  m
public HttpClientBuilder prepare(@Nullable final Customizer customizer) {
    final HttpClientPlan plan = httpClientPlan();

    // attach connection manager early, so customizer has chance to replace it if needed
    plan.getClient().setConnectionManager(sharedConnectionManager);

    // apply defaults
    defaultsCustomizer.customize(plan);

    // apply globals
    new ConfigurationCustomizer(getConfigurationInternal()).customize(plan);

    // apply instance customization
    if (customizer != null) {
        customizer.customize(plan);
    }

    // apply plan to builder
    HttpClientBuilder builder = plan.getClient();
    // User agent must be set here to apply to all apache http requests, including over proxies
    String userAgent = plan.getUserAgent();
    if (userAgent != null) {
        setUserAgent(builder, userAgent);
    }
    builder.setDefaultConnectionConfig(plan.getConnection().build());
    builder.setDefaultSocketConfig(plan.getSocket().build());
    builder.setDefaultRequestConfig(plan.getRequest().build());
    builder.setDefaultCredentialsProvider(plan.getCredentials());

    builder.addInterceptorFirst((HttpRequest request, HttpContext context) -> {
        // add custom http-context attributes
        for (Entry<String, Object> entry : plan.getAttributes().entrySet()) {
            // only set context attribute if not already set, to allow per request overrides
            if (context.getAttribute(entry.getKey()) == null) {
                context.setAttribute(entry.getKey(), entry.getValue());
            }
        }

        // add custom http-request headers
        for (Entry<String, String> entry : plan.getHeaders().entrySet()) {
            request.addHeader(entry.getKey(), entry.getValue());
        }
    });
    builder.addInterceptorLast((HttpRequest httpRequest, HttpContext httpContext) -> {
        if (outboundLog.isDebugEnabled()) {
            httpContext.setAttribute(CTX_REQ_STOPWATCH, Stopwatch.createStarted());
            httpContext.setAttribute(CTX_REQ_URI, getRequestURI(httpContext));
            outboundLog.debug("{} > {}", httpContext.getAttribute(CTX_REQ_URI), httpRequest.getRequestLine());
        }
    });
    builder.addInterceptorLast((HttpResponse httpResponse, HttpContext httpContext) -> {
        Stopwatch stopwatch = (Stopwatch) httpContext.getAttribute(CTX_REQ_STOPWATCH);
        if (stopwatch != null) {
            outboundLog.debug("{} < {} @ {}", httpContext.getAttribute(CTX_REQ_URI),
                    httpResponse.getStatusLine(), stopwatch);
        }
    });

    return builder;
}

From source file:com.urswolfer.gerrit.client.rest.http.GerritRestClient.java

private HttpClientBuilder getHttpClient(HttpContext httpContext) {
    HttpClientBuilder client = HttpClients.custom();

    client.useSystemProperties(); // see also: com.intellij.util.net.ssl.CertificateManager

    OkHttpClient c = new OkHttpClient();
    c.setFollowRedirects(true);// w  ww.  j  a v a 2s  .co  m
    // we need to get redirected result after login (which is done with POST) for extracting xGerritAuth
    client.setRedirectStrategy(new LaxRedirectStrategy());

    c.setCookieHandler(cookieManager);

    c.setConnectTimeout(CONNECTION_TIMEOUT_MS, TimeUnit.MILLISECONDS);
    c.setReadTimeout(CONNECTION_TIMEOUT_MS, TimeUnit.MILLISECONDS);
    c.setWriteTimeout(CONNECTION_TIMEOUT_MS, TimeUnit.MILLISECONDS);

    CredentialsProvider credentialsProvider = getCredentialsProvider();
    client.setDefaultCredentialsProvider(credentialsProvider);

    if (authData.isLoginAndPasswordAvailable()) {
        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(authData.getLogin(), authData.getPassword()));

        BasicScheme basicAuth = new BasicScheme();
        httpContext.setAttribute(PREEMPTIVE_AUTH, basicAuth);
        client.addInterceptorFirst(new PreemptiveAuthHttpRequestInterceptor(authData));
    }

    client.addInterceptorLast(new UserAgentHttpRequestInterceptor());

    for (HttpClientBuilderExtension httpClientBuilderExtension : httpClientBuilderExtensions) {
        client = httpClientBuilderExtension.extend(client, authData);
        credentialsProvider = httpClientBuilderExtension.extendCredentialProvider(client, credentialsProvider,
                authData);
    }

    return client;
}