Search in sources :

Example 1 with NoConnectionReuseStrategy

use of org.apache.http.impl.NoConnectionReuseStrategy in project opennms by OpenNMS.

the class HttpClientWrapper method getClient.

public CloseableHttpClient getClient() {
    if (m_httpClient == null) {
        final HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        final RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
        if (!m_reuseConnections) {
            httpClientBuilder.setConnectionReuseStrategy(new NoConnectionReuseStrategy());
        }
        if (m_usePreemptiveAuth) {
            enablePreemptiveAuth(httpClientBuilder);
        }
        if (m_useSystemProxySettings) {
            httpClientBuilder.setRoutePlanner(new SystemDefaultRoutePlanner(ProxySelector.getDefault()));
        }
        if (!isEmpty(m_cookieSpec)) {
            requestConfigBuilder.setCookieSpec(m_cookieSpec);
        }
        if (m_cookieStore != null) {
            httpClientBuilder.setDefaultCookieStore(m_cookieStore);
        }
        if (m_username != null) {
            setCredentials(httpClientBuilder, m_username, m_password);
        }
        if (m_socketTimeout != null) {
            requestConfigBuilder.setSocketTimeout(m_socketTimeout);
        }
        if (m_connectionTimeout != null) {
            requestConfigBuilder.setConnectTimeout(m_connectionTimeout);
        }
        if (m_retries != null) {
            httpClientBuilder.setRetryHandler(new HttpRequestRetryOnExceptionHandler(m_retries, false));
        }
        if (m_sslContext.size() != 0) {
            configureSSLContext(httpClientBuilder);
        }
        for (final HttpRequestInterceptor interceptor : m_requestInterceptors) {
            httpClientBuilder.addInterceptorLast(interceptor);
        }
        for (final HttpResponseInterceptor interceptor : m_responseInterceptors) {
            httpClientBuilder.addInterceptorLast(interceptor);
        }
        if (m_useLaxRedirect) {
            httpClientBuilder.setRedirectStrategy(new LaxRedirectStrategy());
        }
        httpClientBuilder.setDefaultRequestConfig(requestConfigBuilder.build());
        m_httpClient = httpClientBuilder.build();
    }
    return m_httpClient;
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) NoConnectionReuseStrategy(org.apache.http.impl.NoConnectionReuseStrategy) HttpRequestInterceptor(org.apache.http.HttpRequestInterceptor) HttpResponseInterceptor(org.apache.http.HttpResponseInterceptor) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) LaxRedirectStrategy(org.apache.http.impl.client.LaxRedirectStrategy) SystemDefaultRoutePlanner(org.apache.http.impl.conn.SystemDefaultRoutePlanner)

Example 2 with NoConnectionReuseStrategy

use of org.apache.http.impl.NoConnectionReuseStrategy in project dropwizard by dropwizard.

the class HttpClientBuilder method createClient.

/**
     * Map the parameters in {@link HttpClientConfiguration} to configuration on a
     * {@link org.apache.http.impl.client.HttpClientBuilder} instance
     *
     * @param builder
     * @param manager
     * @param name
     * @return the configured {@link CloseableHttpClient}
     */
protected ConfiguredCloseableHttpClient createClient(final org.apache.http.impl.client.HttpClientBuilder builder, final InstrumentedHttpClientConnectionManager manager, final String name) {
    final String cookiePolicy = configuration.isCookiesEnabled() ? CookieSpecs.DEFAULT : CookieSpecs.IGNORE_COOKIES;
    final Integer timeout = (int) configuration.getTimeout().toMilliseconds();
    final Integer connectionTimeout = (int) configuration.getConnectionTimeout().toMilliseconds();
    final Integer connectionRequestTimeout = (int) configuration.getConnectionRequestTimeout().toMilliseconds();
    final long keepAlive = configuration.getKeepAlive().toMilliseconds();
    final ConnectionReuseStrategy reuseStrategy = keepAlive == 0 ? new NoConnectionReuseStrategy() : new DefaultConnectionReuseStrategy();
    final HttpRequestRetryHandler retryHandler = configuration.getRetries() == 0 ? NO_RETRIES : (httpRequestRetryHandler == null ? new DefaultHttpRequestRetryHandler(configuration.getRetries(), false) : httpRequestRetryHandler);
    final RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(cookiePolicy).setSocketTimeout(timeout).setConnectTimeout(connectionTimeout).setConnectionRequestTimeout(connectionRequestTimeout).build();
    final SocketConfig socketConfig = SocketConfig.custom().setTcpNoDelay(true).setSoTimeout(timeout).build();
    customizeBuilder(builder).setRequestExecutor(new InstrumentedHttpRequestExecutor(metricRegistry, metricNameStrategy, name)).setConnectionManager(manager).setDefaultRequestConfig(requestConfig).setDefaultSocketConfig(socketConfig).setConnectionReuseStrategy(reuseStrategy).setRetryHandler(retryHandler).setUserAgent(createUserAgent(name));
    if (keepAlive != 0) {
        // either keep alive based on response header Keep-Alive,
        // or if the server can keep a persistent connection (-1), then override based on client's configuration
        builder.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy() {

            @Override
            public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
                final long duration = super.getKeepAliveDuration(response, context);
                return (duration == -1) ? keepAlive : duration;
            }
        });
    }
    // create a tunnel through a proxy host if it's specified in the config
    final ProxyConfiguration proxy = configuration.getProxyConfiguration();
    if (proxy != null) {
        final HttpHost httpHost = new HttpHost(proxy.getHost(), proxy.getPort(), proxy.getScheme());
        builder.setRoutePlanner(new NonProxyListProxyRoutePlanner(httpHost, proxy.getNonProxyHosts()));
        // if the proxy host requires authentication then add the host credentials to the credentials provider
        final AuthConfiguration auth = proxy.getAuth();
        if (auth != null) {
            if (credentialsProvider == null) {
                credentialsProvider = new BasicCredentialsProvider();
            }
            credentialsProvider.setCredentials(new AuthScope(httpHost), new UsernamePasswordCredentials(auth.getUsername(), auth.getPassword()));
        }
    }
    if (credentialsProvider != null) {
        builder.setDefaultCredentialsProvider(credentialsProvider);
    }
    if (routePlanner != null) {
        builder.setRoutePlanner(routePlanner);
    }
    if (disableContentCompression) {
        builder.disableContentCompression();
    }
    if (redirectStrategy != null) {
        builder.setRedirectStrategy(redirectStrategy);
    }
    if (defaultHeaders != null) {
        builder.setDefaultHeaders(defaultHeaders);
    }
    if (verifier != null) {
        builder.setSSLHostnameVerifier(verifier);
    }
    if (httpProcessor != null) {
        builder.setHttpProcessor(httpProcessor);
    }
    return new ConfiguredCloseableHttpClient(builder.build(), requestConfig);
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) SocketConfig(org.apache.http.config.SocketConfig) NonProxyListProxyRoutePlanner(io.dropwizard.client.proxy.NonProxyListProxyRoutePlanner) DefaultConnectionReuseStrategy(org.apache.http.impl.DefaultConnectionReuseStrategy) DefaultHttpRequestRetryHandler(org.apache.http.impl.client.DefaultHttpRequestRetryHandler) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) AuthConfiguration(io.dropwizard.client.proxy.AuthConfiguration) InstrumentedHttpRequestExecutor(com.codahale.metrics.httpclient.InstrumentedHttpRequestExecutor) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) NoConnectionReuseStrategy(org.apache.http.impl.NoConnectionReuseStrategy) ProxyConfiguration(io.dropwizard.client.proxy.ProxyConfiguration) DefaultConnectionKeepAliveStrategy(org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) NoConnectionReuseStrategy(org.apache.http.impl.NoConnectionReuseStrategy) DefaultConnectionReuseStrategy(org.apache.http.impl.DefaultConnectionReuseStrategy) ConnectionReuseStrategy(org.apache.http.ConnectionReuseStrategy) DefaultHttpRequestRetryHandler(org.apache.http.impl.client.DefaultHttpRequestRetryHandler) HttpRequestRetryHandler(org.apache.http.client.HttpRequestRetryHandler)

Aggregations

RequestConfig (org.apache.http.client.config.RequestConfig)2 NoConnectionReuseStrategy (org.apache.http.impl.NoConnectionReuseStrategy)2 InstrumentedHttpRequestExecutor (com.codahale.metrics.httpclient.InstrumentedHttpRequestExecutor)1 AuthConfiguration (io.dropwizard.client.proxy.AuthConfiguration)1 NonProxyListProxyRoutePlanner (io.dropwizard.client.proxy.NonProxyListProxyRoutePlanner)1 ProxyConfiguration (io.dropwizard.client.proxy.ProxyConfiguration)1 ConnectionReuseStrategy (org.apache.http.ConnectionReuseStrategy)1 HttpHost (org.apache.http.HttpHost)1 HttpRequestInterceptor (org.apache.http.HttpRequestInterceptor)1 HttpResponse (org.apache.http.HttpResponse)1 HttpResponseInterceptor (org.apache.http.HttpResponseInterceptor)1 AuthScope (org.apache.http.auth.AuthScope)1 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)1 HttpRequestRetryHandler (org.apache.http.client.HttpRequestRetryHandler)1 SocketConfig (org.apache.http.config.SocketConfig)1 DefaultConnectionReuseStrategy (org.apache.http.impl.DefaultConnectionReuseStrategy)1 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)1 DefaultConnectionKeepAliveStrategy (org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy)1 DefaultHttpRequestRetryHandler (org.apache.http.impl.client.DefaultHttpRequestRetryHandler)1 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)1