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.jboss.as.test.integration.security.common.SSLTruststoreUtil.java

public static HttpClient getHttpClientWithSSL(File keyStoreFile, String keyStorePassword, File trustStoreFile,
        String trustStorePassword) {

    try {// w w w.jav a2 s .com
        final KeyStore truststore = loadKeyStore(trustStoreFile, trustStorePassword.toCharArray());
        final KeyStore keystore = keyStoreFile != null
                ? loadKeyStore(keyStoreFile, keyStorePassword.toCharArray())
                : null;
        SSLContextBuilder sslContextBuilder = SSLContexts.custom().useTLS().loadTrustMaterial(truststore);
        if (keyStoreFile != null) {
            sslContextBuilder.loadKeyMaterial(keystore, keyStorePassword.toCharArray());
        }
        SSLContext sslContext = sslContextBuilder.build();
        SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext,
                new AllowAllHostnameVerifier());

        Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", PlainConnectionSocketFactory.getSocketFactory())
                .register("https", socketFactory).build();

        return HttpClientBuilder.create().setSSLSocketFactory(socketFactory)
                .setHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)
                .setConnectionManager(new PoolingHttpClientConnectionManager(registry))
                .setSchemePortResolver(new DefaultSchemePortResolver()).build();

    } catch (Exception e) {
        LOGGER.error(
                "Creating HttpClient with customized SSL failed. We are returning the default one instead.", e);
        return HttpClients.createDefault();
    }
}

From source file:org.openo.sdnhub.osdriverservice.openstack.client.http.OpenStackHttpConnection.java

/**
 * Constructor<br>/*w w w.  j  a  v  a  2s  .com*/
 *
 * @param creds {@link OpenStackCredentials}
 * @throws OpenStackException
 * @since SDNHUB 0.5
 */
public OpenStackHttpConnection(OpenStackCredentials creds) throws OpenStackException {
    try {
        if (creds.isSecured()) {
            SSLContext sslContext = SSLContext.getInstance(SSLCONTEST_TLS);
            sslContext.init(null, new TrustManager[] { new TrustAllX509TrustManager() },
                    new java.security.SecureRandom());
            X509HostnameVerifier hostnameVerifier = new AllowAllHostnameVerifier();
            Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder
                    .<ConnectionSocketFactory>create()
                    .register("https", new SSLConnectionSocketFactory(sslContext, hostnameVerifier)).build();
            HttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(
                    socketFactoryRegistry);

            this.httpClient = HttpClients.custom().setConnectionManager(connManager)
                    .setRedirectStrategy(new LaxRedirectStrategy()).build();
        } else {
            this.httpClient = HttpClients.createDefault();
        }
    } catch (Exception e) {
        LOGGER.error("Excepton failed. ", e);
        throw new OpenStackException(e);
    }

    this.credentials = creds;
    this.regionCache = new OpenStackHttpConnection.RegionCache();
}

From source file:org.openo.sdnhub.overlayvpndriver.http.OverlayVpnDriverSsoProxy.java

private OverlayVpnDriverSsoProxy(final String acIp, final String acPort, final String acLoginName,
        String acLoginPassword) {
    this.acIp = acIp;
    this.acPort = acPort;
    this.acLoginName = acLoginName;
    this.acLoginPassword = acLoginPassword;
    try {/* w ww  .j a  v  a2 s. c  o m*/
        SSLContext sslcontext = SSLContext.getInstance(SSLCONTEST_TLS);
        sslcontext.init(null, new TrustManager[] { new X509TrustManager() {

            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }

            @Override
            public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
                // unimplemented
            }

            @Override
            public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
                // unimplemented
            }
        } }, new java.security.SecureRandom());

        X509HostnameVerifier hostnameVerifier = new AllowAllHostnameVerifier();
        Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder
                .<ConnectionSocketFactory>create()
                .register("https", new SSLConnectionSocketFactory(sslcontext, hostnameVerifier)).build();
        HttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);

        httpClient = HttpClients.custom().setConnectionManager(connManager)
                .setRedirectStrategy(new LaxRedirectStrategy()).build();
    } catch (Exception e) {
        LOGGER.error("ACSSOProxy: throw exception.", e);
    }
}

From source file:org.ovirt.engine.core.uutils.net.HttpClientBuilder.java

public CloseableHttpClient build() throws IOException, GeneralSecurityException {
    // Prepare the default configuration for all requests:
    RequestConfig requestConfig = RequestConfig.custom()
            .setConnectTimeout(connectTimeout != null ? connectTimeout : 0)
            .setSocketTimeout(readTimeout != null ? readTimeout : 0).build();

    // Configure the trust manager:
    TrustManager[] trustManager = null;
    if (verifyChain) {
        if (trustStore != null) {
            try (InputStream is = new FileInputStream(trustStore)) {
                KeyStore ks = KeyStore.getInstance(trustStoreType);
                ks.load(is, StringUtils.isEmpty(trustStorePassword) ? null : trustStorePassword.toCharArray());
                TrustManagerFactory tmf = TrustManagerFactory.getInstance(trustManagerAlgorithm);
                tmf.init(ks);//from w w w  .ja v  a  2s  .  c  o  m
                trustManager = tmf.getTrustManagers();
            }
        }
    } else {
        trustManager = new TrustManager[] { new X509TrustManager() {
            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[] {};
            }

            public void checkClientTrusted(X509Certificate[] certs, String authType) {
            }

            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        } };
    }

    // Create the SSL context:
    SSLContext sslContext = SSLContext.getInstance(tlsProtocol);
    sslContext.init(null, trustManager, null);

    // Create the SSL host name verifier:
    HostnameVerifier sslHostnameVerifier = null;
    if (!verifyHost) {
        sslHostnameVerifier = (hostname, session) -> true;
    }

    // Create the socket factory for HTTP:
    ConnectionSocketFactory httpSocketFactory = new PlainConnectionSocketFactory();

    // Create the socket factory for HTTPS:
    ConnectionSocketFactory httpsSocketFactory = new SSLConnectionSocketFactory(sslContext,
            sslHostnameVerifier);

    // Create the socket factory registry:
    Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", httpSocketFactory).register("https", httpsSocketFactory).build();

    // Create the connection manager:
    HttpClientConnectionManager connectionManager;
    if (poolSize != null) {
        PoolingHttpClientConnectionManager poolManager = new PoolingHttpClientConnectionManager(
                socketFactoryRegistry);
        poolManager.setDefaultMaxPerRoute(poolSize);
        poolManager.setMaxTotal(poolSize);
        connectionManager = poolManager;
    } else {
        connectionManager = new BasicHttpClientConnectionManager(socketFactoryRegistry);
    }

    // Create the client:
    return org.apache.http.impl.client.HttpClientBuilder.create().setDefaultRequestConfig(requestConfig)
            .setSSLHostnameVerifier(sslHostnameVerifier).setConnectionManager(connectionManager).build();
}