Example usage for org.apache.http.impl.conn PoolingHttpClientConnectionManager PoolingHttpClientConnectionManager

List of usage examples for org.apache.http.impl.conn PoolingHttpClientConnectionManager PoolingHttpClientConnectionManager

Introduction

In this page you can find the example usage for org.apache.http.impl.conn PoolingHttpClientConnectionManager PoolingHttpClientConnectionManager.

Prototype

public PoolingHttpClientConnectionManager(
            final HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory) 

Source Link

Usage

From source file:org.geosamples.utilities.HTTPClient.java

/**
 * This method relaxes SSL constraints because geosamples does not yet
 * provide certificate./*from ww w.ja  va 2  s . c o  m*/
 *
 * @see <a href="http://literatejava.com/networks/ignore-ssl-certificate-errors-apache-httpclient-4-4/">Tom's Blog</a>
 * @return CloseableHttpClient
 * @throws java.security.NoSuchAlgorithmException
 * @throws java.security.KeyStoreException
 * @throws java.security.KeyManagementException
 */
public static CloseableHttpClient clientWithNoSecurityValidation()
        throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {

    HttpClientBuilder clientBuilder = HttpClientBuilder.create();

    // setup a Trust Strategy that allows all certificates.
    SSLContext sslContext = null;

    sslContext = new SSLContextBuilder().loadTrustMaterial(null, (X509Certificate[] arg0, String arg1) -> true)
            .build();

    clientBuilder.setSSLContext(sslContext);

    // don't check Hostnames, either.
    HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;

    // here's the special part:
    //      -- need to create an SSL Socket Factory, to use our weakened "trust strategy";
    //      -- and create a Registry, to register it.
    //
    SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
    Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", PlainConnectionSocketFactory.getSocketFactory())
            .register("https", sslSocketFactory).build();

    // now, we create connection-manager using our Registry.
    //      -- allows multi-threaded use
    PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
    clientBuilder.setConnectionManager(connMgr);

    CloseableHttpClient httpClient = clientBuilder.build();

    return httpClient;
}

From source file:slash.navigation.rest.ssl.SSLConnectionManagerFactory.java

public HttpClientConnectionManager createConnectionManager() throws CertificateException,
        NoSuchAlgorithmException, KeyStoreException, IOException, KeyManagementException {
    SSLContext sslContext = createSSLContext();
    SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext);
    return new PoolingHttpClientConnectionManager(RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", PlainConnectionSocketFactory.getSocketFactory())
            .register("https", sslSocketFactory).build());
}

From source file:com.jivesoftware.os.routing.bird.http.client.HttpClientFactoryProvider.java

public HttpClientFactory createHttpClientFactory(Collection<HttpClientConfiguration> configurations,
        boolean latentClient) {

    HttpClientConfig httpClientConfig = locateConfig(configurations, HttpClientConfig.class,
            HttpClientConfig.newBuilder().build());
    HttpClientSSLConfig sslConfig = locateConfig(configurations, HttpClientSSLConfig.class, null);

    String scheme;/* ww  w .j a v  a2  s.c  o  m*/
    PoolingHttpClientConnectionManager poolingHttpClientConnectionManager;
    if (sslConfig != null && sslConfig.isUseSsl()) {
        LayeredConnectionSocketFactory sslSocketFactory;
        if (sslConfig.getCustomSSLSocketFactory() != null) {
            sslSocketFactory = sslConfig.getCustomSSLSocketFactory();
        } else {
            sslSocketFactory = SSLConnectionSocketFactory.getSocketFactory();
        }

        scheme = "https";
        poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager(
                RegistryBuilder.<ConnectionSocketFactory>create()
                        .register("http", PlainConnectionSocketFactory.getSocketFactory())
                        .register("https", sslSocketFactory).build());
    } else {
        scheme = "http";
        poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager();
    }

    if (httpClientConfig.getMaxConnections() > 0) {
        poolingHttpClientConnectionManager.setMaxTotal(httpClientConfig.getMaxConnections());
    } else {
        poolingHttpClientConnectionManager.setMaxTotal(Integer.MAX_VALUE);
    }

    if (httpClientConfig.getMaxConnectionsPerHost() > 0) {
        poolingHttpClientConnectionManager.setDefaultMaxPerRoute(httpClientConfig.getMaxConnectionsPerHost());
    } else {
        poolingHttpClientConnectionManager.setDefaultMaxPerRoute(Integer.MAX_VALUE);
    }

    poolingHttpClientConnectionManager.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(
            httpClientConfig.getSocketTimeoutInMillis() > 0 ? httpClientConfig.getSocketTimeoutInMillis() : 0)
            .build());

    Closeable closeable;
    HttpClientConnectionManager clientConnectionManager;
    clientConnectionManager = poolingHttpClientConnectionManager;
    closeable = poolingHttpClientConnectionManager;

    return (OAuthSigner signer, String host, int port) -> {
        HttpClientBuilder httpClientBuilder = HttpClients.custom()
                .setConnectionManager(clientConnectionManager);

        CloseableHttpClient client = httpClientBuilder.build();
        HttpClient httpClient = new ApacheHttpClient441BackedHttpClient(scheme, host, port, signer, client,
                closeable, httpClientConfig.getCopyOfHeadersForEveryRequest());

        if (latentClient) {
            httpClient = new LatentHttpClient(httpClient);
        }
        return httpClient;
    };
}

From source file:guru.mmp.common.http.SecureHttpClientBuilder.java

/**
 * Constructs a new <code>SecureHttpClientBuilder</code>.
 *
 * @param serverValidationEnabled should the connection to the remote server be validated
 *//*from w ww.java  2  s . c o m*/
public SecureHttpClientBuilder(boolean serverValidationEnabled) {
    this.serverValidationEnabled = serverValidationEnabled;

    SSLConnectionSocketFactory sslConnectionSocketFactory = getSSLConnectionSocketFactory();

    Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("https", sslConnectionSocketFactory).register("http", new PlainConnectionSocketFactory())
            .build();

    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(
            socketFactoryRegistry);

    setConnectionManager(connectionManager);
}

From source file:org.apache.solr.update.UpdateShardHandler.java

public UpdateShardHandler(UpdateShardHandlerConfig cfg) {
    clientConnectionManager = new PoolingHttpClientConnectionManager(
            HttpClientUtil.getSchemaRegisteryProvider().getSchemaRegistry());
    if (cfg != null) {
        clientConnectionManager.setMaxTotal(cfg.getMaxUpdateConnections());
        clientConnectionManager.setDefaultMaxPerRoute(cfg.getMaxUpdateConnectionsPerHost());
    }/*w  w w .j a  v a 2s .  c om*/

    ModifiableSolrParams clientParams = new ModifiableSolrParams();
    if (cfg != null) {
        clientParams.set(HttpClientUtil.PROP_SO_TIMEOUT, cfg.getDistributedSocketTimeout());
        clientParams.set(HttpClientUtil.PROP_CONNECTION_TIMEOUT, cfg.getDistributedConnectionTimeout());
    }
    client = HttpClientUtil.createClient(clientParams, clientConnectionManager);

    // following is done only for logging complete configuration.
    // The maxConnections and maxConnectionsPerHost have already been specified on the connection manager
    if (cfg != null) {
        clientParams.set(HttpClientUtil.PROP_MAX_CONNECTIONS, cfg.getMaxUpdateConnections());
        clientParams.set(HttpClientUtil.PROP_MAX_CONNECTIONS_PER_HOST, cfg.getMaxUpdateConnectionsPerHost());
    }
    log.debug("Created UpdateShardHandler HTTP client with params: {}", clientParams);
}

From source file:com.ibm.connectors.splunklog.SplunkHttpConnection.java

public void createHttpClient() {
    PlainConnectionSocketFactory sf = PlainConnectionSocketFactory.getSocketFactory();
    SSLContext sslContext = SSLContexts.createSystemDefault();
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext,
            NoopHostnameVerifier.INSTANCE);

    Registry<ConnectionSocketFactory> r = RegistryBuilder.<ConnectionSocketFactory>create().register("http", sf)
            .register("https", sslsf).build();

    this.cm = new PoolingHttpClientConnectionManager(r);
}

From source file:utils.HttpClientGenerator.java

public static CloseableHttpClient getHttpClient(boolean checkCert) {

    if (checkCert == false) {
        HttpClientBuilder b = HttpClientBuilder.create();

        // setup a Trust Strategy that allows all certificates.
        SSLContext sslContext = null;
        try {/*from   w  w  w.j a  va 2s  .  c  o  m*/
            sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
                public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
                    return true;
                }
            }).build();
        } catch (NoSuchAlgorithmException e) {
            String err = "error occurred while creating SSL disables hhtp client";
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (KeyStoreException e) {
            e.printStackTrace();
        }
        b.setSslcontext(sslContext);

        // not to check Hostnames
        HostnameVerifier hostnameVerifier = SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;

        //       create an SSL Socket Factory, to use weakened "trust strategy";
        //       and create a Registry, to register it.
        SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext,
                (X509HostnameVerifier) hostnameVerifier);
        Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder
                .<ConnectionSocketFactory>create()
                .register("http", PlainConnectionSocketFactory.getSocketFactory())
                .register("https", sslSocketFactory).build();

        // creating connection-manager using our Registry.
        //      -- allows multi-threaded use
        PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(
                socketFactoryRegistry);
        connMgr.setDefaultMaxPerRoute(20);
        // Increase max connections for localhost:80 to 50
        HttpHost localhost = new HttpHost("localhost", 9443);
        connMgr.setMaxPerRoute(new HttpRoute(localhost), 10);
        b.setConnectionManager(connMgr);

        // finally, build the HttpClient;
        CloseableHttpClient client = b.build();
        return client;
    } else {
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
        // Increase default max connection per route to 20
        cm.setDefaultMaxPerRoute(20);
        // Increase max connections for localhost:80 to 50
        HttpHost localhost = new HttpHost("localhost", 9443);
        cm.setMaxPerRoute(new HttpRoute(localhost), 10);
        CloseableHttpClient client = HttpClients.custom().setConnectionManager(cm).build();
        return client;
    }
}

From source file:org.sonatype.spice.zapper.client.hc4.Hc4ClientBuilder.java

public Hc4Client build() {
    final Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", PlainConnectionSocketFactory.getSocketFactory())
            .register("https", SSLConnectionSocketFactory.getSystemSocketFactory()).build();

    final PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
    cm.setMaxTotal(200);/*from w ww .  j a  v  a 2  s.  c  om*/
    cm.setDefaultMaxPerRoute(parameters.getMaximumTrackCount());

    final HttpClientBuilder httpClientBuilder = HttpClientBuilder.create().setConnectionManager(cm)
            .setUserAgent("Zapper/1.0-HC4");

    if (proxyServer != null) {
        httpClientBuilder.setProxy(proxyServer);
    }
    if (credentialsProvider != null) {
        httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
    }

    return new Hc4Client(parameters, remoteUrl, httpClientBuilder.build(),
            preemptiveAuth ? credentialsProvider : null);
}

From source file:io.fabric8.maven.docker.access.hc.http.HttpClientBuilder.java

private static HttpClientConnectionManager getPooledConnectionFactory(String certPath, int maxConnections)
        throws IOException {
    PoolingHttpClientConnectionManager ret = certPath != null
            ? new PoolingHttpClientConnectionManager(getSslFactoryRegistry(certPath))
            : new PoolingHttpClientConnectionManager();
    ret.setDefaultMaxPerRoute(maxConnections);
    ret.setMaxTotal(maxConnections);//w  w  w .  j  a va 2s  .  c o m
    return ret;
}

From source file:com.adobe.ags.curly.ConnectionManager.java

private void createNewConnectionManager() {
    try {/* w ww  .  j  a  v a  2 s. c  om*/
        SSLContextBuilder builder = new SSLContextBuilder();
        builder.loadTrustMaterial(new TrustSelfSignedStrategy());

        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build(),
                NoopHostnameVerifier.INSTANCE);
        Registry<ConnectionSocketFactory> r = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", new PlainConnectionSocketFactory()).register("https", sslsf).build();
        connectionManager = new PoolingHttpClientConnectionManager(r);
        connectionManager.setValidateAfterInactivity(500);
        sharedContext = ThreadLocal.withInitial(HttpClientContext::new);
    } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException ex) {
        Logger.getLogger(ConnectionManager.class.getName()).log(Level.SEVERE, null, ex);
    }

}