Search in sources :

Example 1 with HTTPClientImpl

use of core.framework.impl.http.HTTPClientImpl in project core-ng-project by neowu.

the class HTTPClientBuilder method build.

public HTTPClient build() {
    StopWatch watch = new StopWatch();
    try {
        HttpClientBuilder builder = HttpClients.custom();
        builder.setUserAgent(userAgent);
        builder.setKeepAliveStrategy((response, context) -> keepAliveTimeout.toMillis());
        builder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).setSSLContext(new SSLContextBuilder().loadTrustMaterial(TrustSelfSignedStrategy.INSTANCE).build());
        // builder use PoolingHttpClientConnectionManager by default, and connTimeToLive will be set by keepAlive value
        builder.setDefaultSocketConfig(SocketConfig.custom().setSoKeepAlive(true).build());
        builder.setDefaultRequestConfig(RequestConfig.custom().setSocketTimeout((int) timeout.toMillis()).setConnectionRequestTimeout((int) timeout.toMillis()).setConnectTimeout((int) timeout.toMillis()).build());
        builder.setMaxConnPerRoute(maxConnections).setMaxConnTotal(maxConnections);
        builder.disableAuthCaching();
        builder.disableConnectionState();
        // retry should be handled in framework level with better trace log
        builder.disableAutomaticRetries();
        if (!enableRedirect)
            builder.disableRedirectHandling();
        if (!enableCookie)
            builder.disableCookieManagement();
        CloseableHttpClient httpClient = builder.build();
        return new HTTPClientImpl(httpClient, userAgent, slowOperationThreshold);
    } catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {
        throw new Error(e);
    } finally {
        logger.info("create http client, elapsedTime={}", watch.elapsedTime());
    }
}
Also used : HTTPClientImpl(core.framework.impl.http.HTTPClientImpl) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) KeyManagementException(java.security.KeyManagementException) StopWatch(core.framework.util.StopWatch)

Aggregations

HTTPClientImpl (core.framework.impl.http.HTTPClientImpl)1 StopWatch (core.framework.util.StopWatch)1 KeyManagementException (java.security.KeyManagementException)1 KeyStoreException (java.security.KeyStoreException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)1 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)1 SSLContextBuilder (org.apache.http.ssl.SSLContextBuilder)1