Search in sources :

Example 46 with PoolingHttpClientConnectionManager

use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project fabric-sdk-java by hyperledger.

the class HFCAClient method httpGet.

JsonObject httpGet(String url, User registrar) throws Exception {
    String authHTTPCert = getHTTPAuthCertificate(registrar.getEnrollment(), "");
    url = getURL(url);
    HttpGet httpGet = new HttpGet(url);
    logger.debug(format("httpGet %s, authHTTPCert: %s", url, authHTTPCert));
    final HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    if (registry != null) {
        httpClientBuilder.setConnectionManager(new PoolingHttpClientConnectionManager(registry));
    }
    HttpClient client = httpClientBuilder.build();
    final HttpClientContext context = HttpClientContext.create();
    httpGet.addHeader("Authorization", authHTTPCert);
    HttpResponse response = client.execute(httpGet, context);
    return getResult(response, "", "GET");
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Example 47 with PoolingHttpClientConnectionManager

use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project cloudbreak by hortonworks.

the class ConsulUtils method createHttpClient.

private static HttpClient createHttpClient(String clientCert, String clientKey, String serverCert) throws Exception {
    SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom().loadTrustMaterial(KeyStoreUtil.createTrustStore(serverCert), null).loadKeyMaterial(KeyStoreUtil.createKeyStore(clientCert, clientKey), "consul".toCharArray()).build();
    try (PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(setupSchemeRegistry(sslContext))) {
        connectionManager.setMaxTotal(MAX_CONNECTION);
        connectionManager.setDefaultMaxPerRoute(MAX_ROUTE);
        Builder requestBuilder = RequestConfig.custom().setConnectTimeout(DEFAULT_TIMEOUT_MS).setConnectionRequestTimeout(DEFAULT_TIMEOUT_MS).setSocketTimeout(DEFAULT_TIMEOUT_MS);
        return HttpClientBuilder.create().setConnectionManager(connectionManager).setDefaultRequestConfig(requestBuilder.build()).build();
    }
}
Also used : RegistryBuilder(org.apache.http.config.RegistryBuilder) Builder(org.apache.http.client.config.RequestConfig.Builder) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) SSLContext(javax.net.ssl.SSLContext) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Example 48 with PoolingHttpClientConnectionManager

use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project scout.rt by eclipse.

the class HttpServiceTunnelTransportManager method interceptNewHttpTransport.

@Override
public void interceptNewHttpTransport(IHttpTransportBuilder builder0) {
    super.interceptNewHttpTransport(builder0);
    if (builder0 instanceof ApacheHttpTransportBuilder) {
        ApacheHttpTransportBuilder builder = (ApacheHttpTransportBuilder) builder0;
        if (builder.getConnectionManager() != null && builder.getConnectionManager() instanceof PoolingHttpClientConnectionManager) {
            @SuppressWarnings("resource") PoolingHttpClientConnectionManager cm = (PoolingHttpClientConnectionManager) builder.getConnectionManager();
            cm.setDefaultMaxPerRoute(CONFIG.getPropertyValue(HttpServiceTunnelTransportMaxConnectionsPerRouteProperty.class));
            cm.setMaxTotal(CONFIG.getPropertyValue(HttpServiceTunnelTransportMaxConnectionsTotalProperty.class));
        } else {
            builder.getBuilder().setMaxConnPerRoute(CONFIG.getPropertyValue(HttpServiceTunnelTransportMaxConnectionsPerRouteProperty.class));
            builder.getBuilder().setMaxConnTotal(CONFIG.getPropertyValue(HttpServiceTunnelTransportMaxConnectionsTotalProperty.class));
        }
    }
}
Also used : ApacheHttpTransportBuilder(org.eclipse.scout.rt.shared.http.ApacheHttpTransportFactory.ApacheHttpTransportBuilder) HttpServiceTunnelTransportMaxConnectionsTotalProperty(org.eclipse.scout.rt.shared.servicetunnel.http.HttpServiceTunnelConfigurationProperties.HttpServiceTunnelTransportMaxConnectionsTotalProperty) HttpServiceTunnelTransportMaxConnectionsPerRouteProperty(org.eclipse.scout.rt.shared.servicetunnel.http.HttpServiceTunnelConfigurationProperties.HttpServiceTunnelTransportMaxConnectionsPerRouteProperty) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Example 49 with PoolingHttpClientConnectionManager

use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project LogHub by fbacchella.

the class AbstractHttpSender method configure.

@Override
public boolean configure(Properties properties) {
    endPoints = Helpers.stringsToUrl(destinations, port, protocol, logger);
    if (endPoints.length == 0) {
        return false;
    }
    // Create the senders threads and the common queue
    batch = new Batch();
    threads = new Thread[publisherThreads];
    for (int i = 1; i <= publisherThreads; i++) {
        String tname = getPublishName() + "Publisher" + i;
        threads[i - 1] = new Thread(publisher) {

            {
                setDaemon(false);
                setName(tname);
                start();
            }
        };
    }
    // The HTTP connection management
    HttpClientBuilder builder = HttpClientBuilder.create();
    builder.setUserAgent(VersionInfo.getUserAgent("LogHub-HttpClient", "org.apache.http.client", HttpClientBuilder.class));
    // Set the Configuration manager
    Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", new SSLConnectionSocketFactory(properties.ssl)).build();
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
    cm.setDefaultMaxPerRoute(2);
    cm.setMaxTotal(2 * publisherThreads);
    cm.setValidateAfterInactivity(timeout * 1000);
    builder.setConnectionManager(cm);
    if (properties.ssl != null) {
        builder.setSSLContext(properties.ssl);
    }
    builder.setDefaultRequestConfig(RequestConfig.custom().setConnectionRequestTimeout(timeout * 1000).setConnectTimeout(timeout * 1000).setSocketTimeout(timeout * 1000).build());
    builder.setDefaultSocketConfig(SocketConfig.custom().setTcpNoDelay(true).setSoKeepAlive(true).setSoTimeout(timeout * 1000).build());
    builder.setDefaultConnectionConfig(ConnectionConfig.custom().build());
    builder.disableCookieManagement();
    builder.setRetryHandler(new HttpRequestRetryHandler() {

        @Override
        public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
            return false;
        }
    });
    client = builder.build();
    if (user != null && password != null) {
        credsProvider = new BasicCredentialsProvider();
        for (URL i : endPoints) {
            credsProvider.setCredentials(new AuthScope(i.getHost(), i.getPort()), new UsernamePasswordCredentials(user, password));
        }
    }
    // Schedule a task to flush every 5 seconds
    Runnable flush = () -> {
        synchronized (publisher) {
            long now = new Date().getTime();
            if ((now - lastFlush) > 5000) {
                batches.add(batch);
                batch = new Batch();
                publisher.notify();
            }
        }
    };
    properties.registerScheduledTask(getPublishName() + "Flusher", flush, 5000);
    return true;
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpContext(org.apache.http.protocol.HttpContext) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) URL(java.net.URL) Date(java.util.Date) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) AuthScope(org.apache.http.auth.AuthScope) HttpRequestRetryHandler(org.apache.http.client.HttpRequestRetryHandler)

Example 50 with PoolingHttpClientConnectionManager

use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project LogHub by fbacchella.

the class TestHttpSsl method TestHcComplex.

@Test
public void TestHcComplex() throws NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException, KeyManagementException {
    CloseableHttpClient client = null;
    ;
    try {
        int publisherThreads = 1;
        int timeout = 2;
        // The HTTP connection management
        HttpClientBuilder builder = HttpClientBuilder.create();
        builder.setUserAgent(VersionInfo.getUserAgent("LogHub-HttpClient", "org.apache.http.client", HttpClientBuilder.class));
        Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", new SSLConnectionSocketFactory(getContext.get(), localhostVerifier)).build();
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
        cm.setDefaultMaxPerRoute(2);
        cm.setMaxTotal(2 * publisherThreads);
        cm.setValidateAfterInactivity(timeout * 1000);
        builder.setConnectionManager(cm);
        builder.setDefaultRequestConfig(RequestConfig.custom().setConnectionRequestTimeout(timeout * 1000).setConnectTimeout(timeout * 1000).setSocketTimeout(timeout * 1000).build());
        builder.setDefaultSocketConfig(SocketConfig.custom().setTcpNoDelay(true).setSoKeepAlive(true).setSoTimeout(timeout * 1000).build());
        builder.setDefaultConnectionConfig(ConnectionConfig.custom().setCharset(Charset.forName("UTF-8")).build());
        builder.disableCookieManagement();
        client = builder.build();
        HttpClientContext context = HttpClientContext.create();
        HttpHost host;
        RequestLine requestLine = new BasicRequestLine("GET", theurl.getPath(), HttpVersion.HTTP_1_1);
        BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest(requestLine);
        host = new HttpHost(theurl.getHost(), theurl.getPort(), "https");
        CloseableHttpResponse response = client.execute(host, request, context);
        Assert.assertEquals(200, response.getStatusLine().getStatusCode());
        try {
            HttpEntity entity = response.getEntity();
            EntityUtils.consume(entity);
        } finally {
            response.close();
        }
    } finally {
        if (client != null) {
            client.close();
        }
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpEntity(org.apache.http.HttpEntity) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) RequestLine(org.apache.http.RequestLine) BasicRequestLine(org.apache.http.message.BasicRequestLine) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) HttpHost(org.apache.http.HttpHost) BasicRequestLine(org.apache.http.message.BasicRequestLine) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BasicHttpEntityEnclosingRequest(org.apache.http.message.BasicHttpEntityEnclosingRequest) Test(org.junit.Test)

Aggregations

PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)180 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)63 ConnectionSocketFactory (org.apache.http.conn.socket.ConnectionSocketFactory)62 PlainConnectionSocketFactory (org.apache.http.conn.socket.PlainConnectionSocketFactory)54 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)52 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)42 SSLContext (javax.net.ssl.SSLContext)36 RequestConfig (org.apache.http.client.config.RequestConfig)31 IOException (java.io.IOException)29 Test (org.junit.Test)27 HttpHost (org.apache.http.HttpHost)21 HttpGet (org.apache.http.client.methods.HttpGet)18 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)17 AuthScope (org.apache.http.auth.AuthScope)16 SSLContextBuilder (org.apache.http.ssl.SSLContextBuilder)16 HttpResponse (org.apache.http.HttpResponse)15 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)14 CredentialsProvider (org.apache.http.client.CredentialsProvider)14 HostnameVerifier (javax.net.ssl.HostnameVerifier)13 HttpClient (org.apache.http.client.HttpClient)12