Search in sources :

Example 1 with DefaultAuthenticationStrategy

use of org.apache.hc.client5.http.impl.DefaultAuthenticationStrategy in project wiremock by wiremock.

the class HttpClientFactory method createClient.

public static CloseableHttpClient createClient(int maxConnections, int timeoutMilliseconds, ProxySettings proxySettings, KeyStoreSettings trustStoreSettings, boolean trustSelfSignedCertificates, final List<String> trustedHosts, boolean useSystemProperties) {
    HttpClientBuilder builder = HttpClientBuilder.create().disableAuthCaching().disableAutomaticRetries().disableCookieManagement().disableRedirectHandling().disableContentCompression().setConnectionManager(PoolingHttpClientConnectionManagerBuilder.create().setMaxConnPerRoute(maxConnections).setMaxConnTotal(maxConnections).setValidateAfterInactivity(// TODO Verify duration
    TimeValue.ofSeconds(5)).setConnectionFactory(new ManagedHttpClientConnectionFactory(null, CharCodingConfig.custom().setCharset(UTF_8).build(), null)).build()).setDefaultRequestConfig(RequestConfig.custom().setResponseTimeout(Timeout.ofMilliseconds(timeoutMilliseconds)).build()).setConnectionReuseStrategy((request, response, context) -> false).setKeepAliveStrategy((response, context) -> TimeValue.ZERO_MILLISECONDS);
    if (useSystemProperties) {
        builder.useSystemProperties();
    }
    if (proxySettings != NO_PROXY) {
        HttpHost proxyHost = new HttpHost(proxySettings.host(), proxySettings.port());
        builder.setProxy(proxyHost);
        if (!isEmpty(proxySettings.getUsername()) && !isEmpty(proxySettings.getPassword())) {
            // TODO Verify
            builder.setProxyAuthenticationStrategy(new DefaultAuthenticationStrategy());
            BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(new AuthScope(proxySettings.host(), proxySettings.port()), new UsernamePasswordCredentials(proxySettings.getUsername(), proxySettings.getPassword().toCharArray()));
            builder.setDefaultCredentialsProvider(credentialsProvider);
        }
    }
    final SSLContext sslContext = buildSslContext(trustStoreSettings, trustSelfSignedCertificates, trustedHosts);
    LayeredConnectionSocketFactory sslSocketFactory = buildSslConnectionSocketFactory(sslContext);
    PoolingHttpClientConnectionManager connectionManager = PoolingHttpClientConnectionManagerBuilder.create().setSSLSocketFactory(sslSocketFactory).build();
    builder.setConnectionManager(connectionManager);
    return builder.build();
}
Also used : SSLContext(javax.net.ssl.SSLContext) AuthScope(org.apache.hc.client5.http.auth.AuthScope) Enumeration(java.util.Enumeration) CharCodingConfig(org.apache.hc.core5.http.config.CharCodingConfig) LayeredConnectionSocketFactory(org.apache.hc.client5.http.socket.LayeredConnectionSocketFactory) Exceptions.throwUnchecked(com.github.tomakehurst.wiremock.common.Exceptions.throwUnchecked) BasicCredentialsProvider(org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider) TextUtils(org.apache.hc.core5.util.TextUtils) ManagedHttpClientConnectionFactory(org.apache.hc.client5.http.impl.io.ManagedHttpClientConnectionFactory) PoolingHttpClientConnectionManagerBuilder(org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder) LocalNotifier.notifier(com.github.tomakehurst.wiremock.common.LocalNotifier.notifier) org.apache.hc.client5.http.classic.methods(org.apache.hc.client5.http.classic.methods) URI(java.net.URI) RequestConfig(org.apache.hc.client5.http.config.RequestConfig) StringUtils.isEmpty(org.apache.commons.lang3.StringUtils.isEmpty) java.security(java.security) HttpClientBuilder(org.apache.hc.client5.http.impl.classic.HttpClientBuilder) TimeValue(org.apache.hc.core5.util.TimeValue) UsernamePasswordCredentials(org.apache.hc.client5.http.auth.UsernamePasswordCredentials) UTF_8(java.nio.charset.StandardCharsets.UTF_8) DefaultAuthenticationStrategy(org.apache.hc.client5.http.impl.DefaultAuthenticationStrategy) NoopHostnameVerifier(org.apache.hc.client5.http.ssl.NoopHostnameVerifier) PoolingHttpClientConnectionManager(org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager) Timeout(org.apache.hc.core5.util.Timeout) SSLConnectionSocketFactory(org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory) com.github.tomakehurst.wiremock.http.ssl(com.github.tomakehurst.wiremock.http.ssl) List(java.util.List) HttpHost(org.apache.hc.core5.http.HttpHost) NO_STORE(com.github.tomakehurst.wiremock.common.ssl.KeyStoreSettings.NO_STORE) RequestMethod(com.github.tomakehurst.wiremock.http.RequestMethod) KeyStoreSettings(com.github.tomakehurst.wiremock.common.ssl.KeyStoreSettings) CloseableHttpClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient) ProxySettings(com.github.tomakehurst.wiremock.common.ProxySettings) Collections(java.util.Collections) NO_PROXY(com.github.tomakehurst.wiremock.common.ProxySettings.NO_PROXY) BasicCredentialsProvider(org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider) LayeredConnectionSocketFactory(org.apache.hc.client5.http.socket.LayeredConnectionSocketFactory) HttpHost(org.apache.hc.core5.http.HttpHost) AuthScope(org.apache.hc.client5.http.auth.AuthScope) ManagedHttpClientConnectionFactory(org.apache.hc.client5.http.impl.io.ManagedHttpClientConnectionFactory) HttpClientBuilder(org.apache.hc.client5.http.impl.classic.HttpClientBuilder) DefaultAuthenticationStrategy(org.apache.hc.client5.http.impl.DefaultAuthenticationStrategy) SSLContext(javax.net.ssl.SSLContext) UsernamePasswordCredentials(org.apache.hc.client5.http.auth.UsernamePasswordCredentials) PoolingHttpClientConnectionManager(org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager)

Aggregations

Exceptions.throwUnchecked (com.github.tomakehurst.wiremock.common.Exceptions.throwUnchecked)1 LocalNotifier.notifier (com.github.tomakehurst.wiremock.common.LocalNotifier.notifier)1 ProxySettings (com.github.tomakehurst.wiremock.common.ProxySettings)1 NO_PROXY (com.github.tomakehurst.wiremock.common.ProxySettings.NO_PROXY)1 KeyStoreSettings (com.github.tomakehurst.wiremock.common.ssl.KeyStoreSettings)1 NO_STORE (com.github.tomakehurst.wiremock.common.ssl.KeyStoreSettings.NO_STORE)1 RequestMethod (com.github.tomakehurst.wiremock.http.RequestMethod)1 com.github.tomakehurst.wiremock.http.ssl (com.github.tomakehurst.wiremock.http.ssl)1 URI (java.net.URI)1 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)1 java.security (java.security)1 Collections (java.util.Collections)1 Enumeration (java.util.Enumeration)1 List (java.util.List)1 SSLContext (javax.net.ssl.SSLContext)1 StringUtils.isEmpty (org.apache.commons.lang3.StringUtils.isEmpty)1 AuthScope (org.apache.hc.client5.http.auth.AuthScope)1 UsernamePasswordCredentials (org.apache.hc.client5.http.auth.UsernamePasswordCredentials)1 org.apache.hc.client5.http.classic.methods (org.apache.hc.client5.http.classic.methods)1 RequestConfig (org.apache.hc.client5.http.config.RequestConfig)1