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

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

Introduction

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

Prototype

public final HttpClientBuilder setConnectionManager(final HttpClientConnectionManager connManager) 

Source Link

Document

Assigns HttpClientConnectionManager instance.

Usage

From source file:co.tuzza.clicksend4j.http.HttpClientUtils.java

public HttpClient getHttpClient() {

    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    httpClientBuilder.setUserAgent("ClickSend4J 0.0.1");
    httpClientBuilder.setConnectionManager(clientConnectionManager);
    httpClientBuilder.setDefaultRequestConfig(buildRequestConfig());

    return httpClientBuilder.build();

}

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 ww .ja va  2 s.  c om*/
    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:org.ambraproject.wombat.service.remote.AbstractRemoteService.java

private CloseableHttpClient createClient() {
    HttpClientBuilder clientBuilder = HttpClientBuilder.create();
    if (connectionManager.isPresent()) {
        clientBuilder = clientBuilder.setConnectionManager(connectionManager.get());
    }//from   w ww.  j a  v a  2  s.co m
    return clientBuilder.build();
}

From source file:ee.ria.xroad.proxy.serverproxy.HttpClientCreator.java

private void build() throws HttpClientCreatorException {
    RegistryBuilder<ConnectionSocketFactory> sfr = RegistryBuilder.create();
    sfr.register("http", PlainConnectionSocketFactory.INSTANCE);

    try {//from  w w  w . j a va2s  .  c  o  m
        sfr.register("https", createSSLSocketFactory());
    } catch (Exception e) {
        throw new HttpClientCreatorException("Creating SSL Socket Factory failed", e);
    }

    connectionManager = new PoolingHttpClientConnectionManager(sfr.build());
    connectionManager.setMaxTotal(CLIENT_MAX_TOTAL_CONNECTIONS);
    connectionManager.setDefaultMaxPerRoute(CLIENT_MAX_CONNECTIONS_PER_ROUTE);
    connectionManager.setDefaultSocketConfig(SocketConfig.custom().setTcpNoDelay(true).build());

    RequestConfig.Builder rb = RequestConfig.custom();
    rb.setConnectTimeout(CLIENT_TIMEOUT);
    rb.setConnectionRequestTimeout(CLIENT_TIMEOUT);
    rb.setSocketTimeout(CLIENT_TIMEOUT);

    HttpClientBuilder cb = HttpClients.custom();
    cb.setDefaultRequestConfig(rb.build());
    cb.setConnectionManager(connectionManager);

    // Disable request retry
    cb.setRetryHandler(new DefaultHttpRequestRetryHandler(0, false));

    httpClient = cb.build();
}

From source file:jp.co.ctc_g.jse.core.rest.springmvc.client.ProxyClientHttpRequestFactory.java

/**
 * ???????HttpClient????????//from www  .j a  va 2s.c  o  m
 * {@inheritDoc}
 */
@Override
public void afterPropertiesSet() throws Exception {

    Assert.notNull(proxyHost, "(proxyHost)???");
    Assert.notNull(proxyPort, "??(proxyPort)???");

    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
    connectionManager.setMaxTotal(maxTotal);
    connectionManager.setDefaultMaxPerRoute(defaultMaxPerRoute);

    HttpClientBuilder builder = HttpClients.custom();
    builder.setConnectionManager(connectionManager);
    if (authentication) {
        Assert.notNull(username,
                "??true???????(username)???");
        Assert.notNull(password,
                "??true?????(password)???");
        DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(
                new HttpHost(proxyHost, Integer.parseInt(proxyPort)));
        builder.setRoutePlanner(routePlanner);
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(proxyHost, Integer.parseInt(proxyPort)),
                new UsernamePasswordCredentials(username, password));
        builder.setDefaultCredentialsProvider(credsProvider);
    }
    builder.setDefaultRequestConfig(RequestConfig.custom().setSocketTimeout(readTimeout).build());
    CloseableHttpClient client = builder.build();
    setHttpClient(client);
}

From source file:co.tuzza.swipehq.transport.ManagedHttpTransport.java

private HttpClient getHttpClient() {
    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    httpClientBuilder.setUserAgent("SwipeHQClient " + SwipeHQClient.VERSION);
    httpClientBuilder.setConnectionManager(clientConnectionManager);
    httpClientBuilder.setDefaultRequestConfig(getRequestConfig());
    httpClientBuilder.setSSLContext(sslContext);

    return httpClientBuilder.build();

}

From source file:net.sf.jasperreports.phantomjs.ProcessConnection.java

public ProcessConnection(ProcessDirector director, PhantomJSProcess process) {
    this.process = process;

    HttpClientBuilder clientBuilder = HttpClients.custom();

    // single connection
    BasicHttpClientConnectionManager connManager = new BasicHttpClientConnectionManager();
    clientBuilder.setConnectionManager(connManager);

    RequestConfig requestConfig = RequestConfig.custom()
            // ignore cookies for now
            .setCookieSpec(CookieSpecs.IGNORE_COOKIES).setSocketTimeout(director.getRequestTimeout()).build();
    clientBuilder.setDefaultRequestConfig(requestConfig);

    this.httpClient = clientBuilder.build();
}

From source file:org.bimserver.client.AbstractBimServerClientFactory.java

public void initHttpClient() {
    HttpClientBuilder builder = HttpClientBuilder.create();

    HttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
    builder.setConnectionManager(connManager);

    //      builder.addInterceptorFirst(new HttpRequestInterceptor() {
    //         public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    //            if (!request.containsHeader("Accept-Encoding")) {
    //               request.addHeader("Accept-Encoding", "gzip");
    //            }
    //         }/*w  w  w.  j a  v  a2s .  com*/
    //      });
    //
    //      builder.addInterceptorFirst(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;
    //                     }
    //                  }
    //               }
    //            }
    //         }
    //      });

    httpClient = builder.build();
}

From source file:cn.org.once.cstack.utils.JSONClient.java

public CloseableHttpClient buildSecureHttpClient() throws IOException {
    if (isUnixSocket) {
        HttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(
                getUnixSocketFactoryRegistry());
        HttpClientBuilder builder = HttpClients.custom();
        builder.setConnectionManager(manager);
        return builder.build();
    } else if (certPathDirectory != null && !certPathDirectory.isEmpty()) {
        org.apache.http.impl.client.HttpClientBuilder builder = HttpClients.custom();
        HttpClientConnectionManager manager = getConnectionFactory(this.certPathDirectory, 10);
        builder.setConnectionManager(manager);
        return builder.build();
    } else {// w ww.  jav  a2s  . co m
        return HttpClients.createDefault();
    }
}

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

private void testPrepareHttpClientBuilderHttpRequest(boolean isSSL, boolean isProxy) throws Exception {
    // Setup/*from  www. j a  va 2  s.c  om*/
    HttpClientBuilder builder = underTest.prepare(plan -> plan.setUserAgentBase(EXPECTED_USER_AGENT));
    builder.setConnectionManager(null);
    if (isProxy) {
        builder.setProxy(new HttpHost(proxyServer.getHostName(), proxyServer.getPort()));
    }
    if (isSSL) {
        setSSL(builder);
    }

    String url;
    if (isSSL) {
        url = "https://" + targetServerSSL.getUrl().getHost() + ":" + targetServerSSL.getPort();
    } else {
        url = "http://" + targetServer.getUrl().getHost() + ":" + targetServer.getPort();
    }

    CloseableHttpResponse resp = null;
    // Execute
    try (CloseableHttpClient client = builder.build()) {
        resp = client.execute(new HttpGet(new URI(url)));
    } finally {
        if (resp != null) {
            resp.close();
        }
    }

    // Verify
    assertThat(resp.getStatusLine().getStatusCode(), equalTo(HttpStatus.OK));
    if (isSSL) {
        assertThat(httpsValidatingBehaviour.getSuccessCount(), equalTo(1));
    } else {
        assertThat(httpValidatingBehaviour.getSuccessCount(), equalTo(1));
    }
    if (isProxy) {
        if (isSSL) {
            // Only one filterable request in SSL (CONNECT) without using MITM
            assertThat(proxyServer.getSuccessCount(), equalTo(1));
        } else {
            // Two filterable requests in non-SSL (initiate and real request)
            assertThat(proxyServer.getSuccessCount(), equalTo(2));
        }
    }
}