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:edu.harvard.hms.dbmi.bd2k.irct.ri.i2b2.I2B2XMLResourceImplementation.java

private HttpClientBuilder ignoreCertificate() throws NoSuchAlgorithmException, KeyManagementException {
    System.setProperty("jsse.enableSNIExtension", "false");

    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }//from   ww  w  . j a v  a  2  s .c  o m

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

        public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
        }
    } };

    SSLContext sslContext;

    sslContext = SSLContext.getInstance("SSL");
    sslContext.init(null, trustAllCerts, new java.security.SecureRandom());

    HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());

    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext,
            NoopHostnameVerifier.INSTANCE);

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

    HttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(r);

    return HttpClients.custom().setConnectionManager(cm);
}

From source file:com.gargoylesoftware.htmlunit.HttpWebConnection.java

/**
 * Has the exact logic in HttpClientBuilder, but with the ability to configure
 * <code>socketFactory</code>.
 *//*  w w w . j  a v a 2 s.c om*/
private PoolingHttpClientConnectionManager createConnectionManager(final HttpClientBuilder builder) {
    final ConnectionSocketFactory socketFactory = new SocksConnectionSocketFactory();

    LayeredConnectionSocketFactory sslSocketFactory;
    try {
        sslSocketFactory = (LayeredConnectionSocketFactory) FieldUtils.readDeclaredField(builder,
                "sslSocketFactory", true);
        final SocketConfig defaultSocketConfig = (SocketConfig) FieldUtils.readDeclaredField(builder,
                "defaultSocketConfig", true);
        final ConnectionConfig defaultConnectionConfig = (ConnectionConfig) FieldUtils
                .readDeclaredField(builder, "defaultConnectionConfig", true);
        final boolean systemProperties = (Boolean) FieldUtils.readDeclaredField(builder, "systemProperties",
                true);
        final int maxConnTotal = (Integer) FieldUtils.readDeclaredField(builder, "maxConnTotal", true);
        final int maxConnPerRoute = (Integer) FieldUtils.readDeclaredField(builder, "maxConnPerRoute", true);
        HostnameVerifier hostnameVerifier = (HostnameVerifier) FieldUtils.readDeclaredField(builder,
                "hostnameVerifier", true);
        final SSLContext sslcontext = (SSLContext) FieldUtils.readDeclaredField(builder, "sslContext", true);

        if (sslSocketFactory == null) {
            final String[] supportedProtocols = systemProperties
                    ? StringUtils.split(System.getProperty("https.protocols"), ',')
                    : null;
            final String[] supportedCipherSuites = systemProperties
                    ? StringUtils.split(System.getProperty("https.cipherSuites"), ',')
                    : null;
            if (hostnameVerifier == null) {
                hostnameVerifier = SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER;
            }
            if (sslcontext != null) {
                sslSocketFactory = new SSLConnectionSocketFactory(sslcontext, supportedProtocols,
                        supportedCipherSuites, hostnameVerifier);
            } else {
                if (systemProperties) {
                    sslSocketFactory = new SSLConnectionSocketFactory(
                            (SSLSocketFactory) SSLSocketFactory.getDefault(), supportedProtocols,
                            supportedCipherSuites, hostnameVerifier);
                } else {
                    sslSocketFactory = new SSLConnectionSocketFactory(SSLContexts.createDefault(),
                            hostnameVerifier);
                }
            }
        }

        final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(
                RegistryBuilder.<ConnectionSocketFactory>create().register("http", socketFactory)
                        .register("https", sslSocketFactory).build());
        if (defaultSocketConfig != null) {
            connectionManager.setDefaultSocketConfig(defaultSocketConfig);
        }
        if (defaultConnectionConfig != null) {
            connectionManager.setDefaultConnectionConfig(defaultConnectionConfig);
        }
        if (systemProperties) {
            String s = System.getProperty("http.keepAlive", "true");
            if ("true".equalsIgnoreCase(s)) {
                s = System.getProperty("http.maxConnections", "5");
                final int max = Integer.parseInt(s);
                connectionManager.setDefaultMaxPerRoute(max);
                connectionManager.setMaxTotal(2 * max);
            }
        }
        if (maxConnTotal > 0) {
            connectionManager.setMaxTotal(maxConnTotal);
        }
        if (maxConnPerRoute > 0) {
            connectionManager.setDefaultMaxPerRoute(maxConnPerRoute);
        }
        return connectionManager;
    } catch (final IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}

From source file:crawlercommons.fetcher.http.SimpleHttpFetcher.java

private void init() {
    if (_httpClient == null) {
        synchronized (SimpleHttpFetcher.class) {
            if (_httpClient != null)
                return;

            final HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
            final RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();

            // Set the socket and connection timeout to be something
            // reasonable.
            requestConfigBuilder.setSocketTimeout(_socketTimeout);
            requestConfigBuilder.setConnectTimeout(_connectionTimeout);
            requestConfigBuilder.setConnectionRequestTimeout(_connectionRequestTimeout);

            /*//from  w ww . ja  va2 s.  c  o  m
             * CoreConnectionPNames.TCP_NODELAY='http.tcp.nodelay':
             * determines whether Nagle's algorithm is to be used. Nagle's
             * algorithm tries to conserve bandwidth by minimizing the
             * number of segments that are sent. When applications wish to
             * decrease network latency and increase performance, they can
             * disable Nagle's algorithm (that is enable TCP_NODELAY. Data
             * will be sent earlier, at the cost of an increase in bandwidth
             * consumption. This parameter expects a value of type
             * java.lang.Boolean. If this parameter is not set, TCP_NODELAY
             * will be enabled (no delay).
             */
            // FIXME Could not find this parameter in http-client version
            // 4.5
            // HttpConnectionParams.setTcpNoDelay(params, true);
            // HttpProtocolParams.setVersion(params, _httpVersion);

            httpClientBuilder.setUserAgent(_userAgent.getUserAgentString());

            // HttpProtocolParams.setContentCharset(params, "UTF-8");
            // HttpProtocolParams.setHttpElementCharset(params, "UTF-8");

            /*
             * CoreProtocolPNames.USE_EXPECT_CONTINUE=
             * 'http.protocol.expect-continue': activates the Expect:
             * 100-Continue handshake for the entity enclosing methods. The
             * purpose of the Expect: 100-Continue handshake is to allow the
             * client that is sending a request message with a request body
             * to determine if the origin server is willing to accept the
             * request (based on the request headers) before the client
             * sends the request body. The use of the Expect: 100-continue
             * handshake can result in a noticeable performance improvement
             * for entity enclosing requests (such as POST and PUT) that
             * require the target server's authentication. The Expect:
             * 100-continue handshake should be used with caution, as it may
             * cause problems with HTTP servers and proxies that do not
             * support HTTP/1.1 protocol. This parameter expects a value of
             * type java.lang.Boolean. If this parameter is not set,
             * HttpClient will not attempt to use the handshake.
             */
            requestConfigBuilder.setExpectContinueEnabled(true);

            /*
             * CoreProtocolPNames.WAIT_FOR_CONTINUE=
             * 'http.protocol.wait-for-continue': defines the maximum period
             * of time in milliseconds the client should spend waiting for a
             * 100-continue response. This parameter expects a value of type
             * java.lang.Integer. If this parameter is not set HttpClient
             * will wait 3 seconds for a confirmation before resuming the
             * transmission of the request body.
             */
            // FIXME Could not find this parameter in http-client version
            // 4.5
            // params.setIntParameter(CoreProtocolPNames.WAIT_FOR_CONTINUE,
            // 5000);

            // FIXME Could not find this parameter in http-client version
            // 4.5
            // CookieSpecParamBean cookieParams = new
            // CookieSpecParamBean(params);
            // cookieParams.setSingleHeader(false);

            // Create and initialize connection socket factory registry
            RegistryBuilder<ConnectionSocketFactory> registry = RegistryBuilder
                    .<ConnectionSocketFactory>create();
            registry.register("http", PlainConnectionSocketFactory.getSocketFactory());
            SSLConnectionSocketFactory sf = createSSLConnectionSocketFactory();
            if (sf != null) {
                registry.register("https", sf);
            } else {
                LOGGER.warn("No valid SSLContext found for https");
            }

            _connectionManager = new PoolingHttpClientConnectionManager(registry.build());
            _connectionManager.setMaxTotal(_maxThreads);
            _connectionManager.setDefaultMaxPerRoute(getMaxConnectionsPerHost());

            /*
             * CoreConnectionPNames.STALE_CONNECTION_CHECK=
             * 'http.connection.stalecheck': determines whether stale
             * connection check is to be used. Disabling stale connection
             * check may result in a noticeable performance improvement (the
             * check can cause up to 30 millisecond overhead per request) at
             * the risk of getting an I/O error when executing a request
             * over a connection that has been closed at the server side.
             * This parameter expects a value of type java.lang.Boolean. For
             * performance critical operations the check should be disabled.
             * If this parameter is not set, the stale connection check will
             * be performed before each request execution.
             * 
             * We don't need I/O exceptions in case if Server doesn't
             * support Kee-Alive option; our client by default always tries
             * keep-alive.
             */
            // Even with stale checking enabled, a connection can "go stale"
            // between the check and the next request. So we still need to
            // handle the case of a closed socket (from the server side),
            // and disabling this check improves performance.
            // Stale connections will be checked in a separate monitor
            // thread
            _connectionManager.setValidateAfterInactivity(-1);

            httpClientBuilder.setConnectionManager(_connectionManager);
            httpClientBuilder.setRetryHandler(new MyRequestRetryHandler(_maxRetryCount));
            httpClientBuilder.setRedirectStrategy(new MyRedirectStrategy(getRedirectMode()));
            httpClientBuilder.setRequestExecutor(new MyHttpRequestExecutor());

            // FUTURE KKr - support authentication
            // FIXME Could not find this parameter in http-client version
            // 4.5
            // HttpClientParams.setAuthenticating(params, false);

            requestConfigBuilder.setCookieSpec(CookieSpecs.DEFAULT);

            if (getMaxRedirects() == 0) {
                requestConfigBuilder.setRedirectsEnabled(false);
            } else {
                requestConfigBuilder.setRedirectsEnabled(true);
                requestConfigBuilder.setMaxRedirects(getMaxRedirects());
            }

            // Set up default headers. This helps us get back from servers
            // what we want.
            HashSet<Header> defaultHeaders = new HashSet<Header>();
            defaultHeaders.add(new BasicHeader(HttpHeaders.ACCEPT_LANGUAGE, getAcceptLanguage()));
            defaultHeaders.add(new BasicHeader(HttpHeaders.ACCEPT_CHARSET, DEFAULT_ACCEPT_CHARSET));
            defaultHeaders.add(new BasicHeader(HttpHeaders.ACCEPT_ENCODING, DEFAULT_ACCEPT_ENCODING));
            defaultHeaders.add(new BasicHeader(HttpHeaders.ACCEPT, DEFAULT_ACCEPT));

            httpClientBuilder.setDefaultHeaders(defaultHeaders);

            httpClientBuilder.setKeepAliveStrategy(new MyConnectionKeepAliveStrategy());

            monitor = new IdleConnectionMonitorThread(_connectionManager);
            monitor.start();

            httpClientBuilder.setDefaultRequestConfig(requestConfigBuilder.build());
            _httpClient = httpClientBuilder.build();
        }
    }

}

From source file:org.hyperledger.fabric_ca.sdk.HFCAClient.java

/**
 * Http Post Request./*from w  w w .  ja  v a2  s.  c  o  m*/
 *
 * @param url         Target URL to POST to.
 * @param body        Body to be sent with the post.
 * @param credentials Credentials to use for basic auth.
 * @return Body of post returned.
 * @throws Exception
 */
String httpPost(String url, String body, UsernamePasswordCredentials credentials) throws Exception {
    logger.debug(format("httpPost %s, body:%s", url, body));

    final HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    CredentialsProvider provider = null;
    if (credentials != null) {
        provider = new BasicCredentialsProvider();

        provider.setCredentials(AuthScope.ANY, credentials);
        httpClientBuilder.setDefaultCredentialsProvider(provider);
    }

    if (registry != null) {

        httpClientBuilder.setConnectionManager(new PoolingHttpClientConnectionManager(registry));

    }

    HttpClient client = httpClientBuilder.build();

    HttpPost httpPost = new HttpPost(url);
    httpPost.setConfig(getRequestConfig());

    AuthCache authCache = new BasicAuthCache();

    HttpHost targetHost = new HttpHost(httpPost.getURI().getHost(), httpPost.getURI().getPort());

    if (credentials != null) {
        authCache.put(targetHost, new

        BasicScheme());

    }

    final HttpClientContext context = HttpClientContext.create();

    if (null != provider) {
        context.setCredentialsProvider(provider);
    }

    if (credentials != null) {
        context.setAuthCache(authCache);
    }

    httpPost.setEntity(new StringEntity(body));
    if (credentials != null) {
        httpPost.addHeader(new

        BasicScheme().

                authenticate(credentials, httpPost, context));
    }

    HttpResponse response = client.execute(httpPost, context);
    int status = response.getStatusLine().getStatusCode();

    HttpEntity entity = response.getEntity();
    logger.trace(format("httpPost %s  sending...", url));
    String responseBody = entity != null ? EntityUtils.toString(entity) : null;
    logger.trace(format("httpPost %s  responseBody %s", url, responseBody));

    if (status >= 400) {

        Exception e = new Exception(format(
                "POST request to %s  with request body: %s, " + "failed with status code: %d. Response: %s",
                url, body, status, responseBody));
        logger.error(e.getMessage());
        throw e;
    }
    logger.debug(format("httpPost Status: %d returning: %s ", status, responseBody));

    return responseBody;
}

From source file:org.hyperledger.fabric_ca.sdk.HFCAClient.java

JsonObject post(String url, String body, String authHTTPCert) throws Exception {
    url = addCAToURL(url);// ww  w  .  j a  v a 2s  . co m
    HttpPost httpPost = new HttpPost(url);
    httpPost.setConfig(getRequestConfig());
    logger.debug(format("httpPost %s, body:%s, authHTTPCert: %s", url, body, authHTTPCert));

    final HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    if (registry != null) {
        httpClientBuilder.setConnectionManager(new PoolingHttpClientConnectionManager(registry));
    }
    HttpClient client = httpClientBuilder.build();

    final HttpClientContext context = HttpClientContext.create();
    httpPost.setEntity(new StringEntity(body));
    httpPost.addHeader("Authorization", authHTTPCert);

    HttpResponse response = client.execute(httpPost, context);

    return getResult(response, body, "POST");
}

From source file:org.hyperledger.fabric_ca.sdk.HFCAClient.java

JsonObject httpGet(String url, User registrar, Map<String, String> queryMap) throws Exception {
    String getURL = getURL(url, queryMap);
    String authHTTPCert = getHTTPAuthCertificate(registrar.getEnrollment(), "GET", getURL, "");
    HttpGet httpGet = new HttpGet(getURL);
    httpGet.setConfig(getRequestConfig());
    logger.debug(format("httpGet %s, authHTTPCert: %s", url, authHTTPCert));

    final HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    if (registry != null) {
        httpClientBuilder.setConnectionManager(new PoolingHttpClientConnectionManager(registry));
    }//from w  w  w .  j a v a2s  .co  m
    HttpClient client = httpClientBuilder.build();

    final HttpClientContext context = HttpClientContext.create();
    httpGet.addHeader("Authorization", authHTTPCert);

    HttpResponse response = client.execute(httpGet, context);

    return getResult(response, "", "GET");
}

From source file:org.hyperledger.fabric_ca.sdk.HFCAClient.java

JsonObject httpPut(String url, String body, User registrar) throws Exception {
    String authHTTPCert = getHTTPAuthCertificate(registrar.getEnrollment(), "PUT", url, body);
    String putURL = addCAToURL(url);
    HttpPut httpPut = new HttpPut(putURL);
    httpPut.setConfig(getRequestConfig());
    logger.debug(format("httpPutt %s, body:%s, authHTTPCert: %s", url, body, authHTTPCert));

    final HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    if (registry != null) {
        httpClientBuilder.setConnectionManager(new PoolingHttpClientConnectionManager(registry));
    }//from  w ww.ja v a  2  s .  c o m
    HttpClient client = httpClientBuilder.build();

    final HttpClientContext context = HttpClientContext.create();
    httpPut.setEntity(new StringEntity(body));
    httpPut.addHeader("Authorization", authHTTPCert);

    HttpResponse response = client.execute(httpPut, context);

    return getResult(response, body, "PUT");
}

From source file:org.hyperledger.fabric_ca.sdk.HFCAClient.java

JsonObject httpDelete(String url, User registrar) throws Exception {
    String authHTTPCert = getHTTPAuthCertificate(registrar.getEnrollment(), "DELETE", url, "");
    String deleteURL = addCAToURL(url);
    HttpDelete httpDelete = new HttpDelete(deleteURL);
    httpDelete.setConfig(getRequestConfig());
    logger.debug(format("httpPut %s, authHTTPCert: %s", url, authHTTPCert));

    final HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    if (registry != null) {
        httpClientBuilder.setConnectionManager(new PoolingHttpClientConnectionManager(registry));
    }//from   www.j a  v  a  2 s . c o m
    HttpClient client = httpClientBuilder.build();

    final HttpClientContext context = HttpClientContext.create();
    httpDelete.addHeader("Authorization", authHTTPCert);

    HttpResponse response = client.execute(httpDelete, context);

    return getResult(response, "", "DELETE");
}

From source file:org.apache.marmotta.client.util.HTTPUtil.java

public static HttpClient createClient(ClientConfiguration config, String context) {

    final HttpClientBuilder httpClientBuilder = HttpClients.custom();

    httpClientBuilder.setUserAgent("Marmotta Client Library/" + MetaUtil.getVersion());
    httpClientBuilder.setRedirectStrategy(new MarmottaRedirectStrategy());
    httpClientBuilder.setRetryHandler(new MarmottaHttpRequestRetryHandler());

    final RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
    requestConfigBuilder.setSocketTimeout(config.getSoTimeout());
    requestConfigBuilder.setConnectTimeout(config.getConnectionTimeout());
    requestConfigBuilder.setRedirectsEnabled(true);
    requestConfigBuilder.setMaxRedirects(3);
    httpClientBuilder.setDefaultRequestConfig(requestConfigBuilder.build());

    if (config.getConectionManager() != null) {
        httpClientBuilder.setConnectionManager(config.getConectionManager());
    } else {//  w ww  . j a  v  a2s  .  c  om
        final Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", PlainConnectionSocketFactory.getSocketFactory())
                //.register("https", )
                .build();

        final PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
        cm.setMaxTotal(100);
        httpClientBuilder.setConnectionManager(cm);
    }

    return httpClientBuilder.build();
}

From source file:org.eclipse.php.composer.api.packages.AsyncDownloader.java

protected void init() {
    super.init();

    try {/*from  w w w .  j a v a 2s. c  o  m*/
        SSLContextBuilder builder = new SSLContextBuilder();
        builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());

        SSLConnectionSocketFactory ssf = new SSLConnectionSocketFactory(builder.build());
        Registry<ConnectionSocketFactory> r = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", ssf) //$NON-NLS-1$//$NON-NLS-2$
                .build();
        connectionManager = new PoolingHttpClientConnectionManager(r);
    } catch (NoSuchAlgorithmException e) {
        log.error("Exception during init", e); //$NON-NLS-1$
    } catch (KeyManagementException e) {
        log.error("Exception during init", e); //$NON-NLS-1$
    } catch (KeyStoreException e) {
        log.error("Exception during init", e); //$NON-NLS-1$
    }

}