Search in sources :

Example 6 with ProxyConfiguration

use of io.dropwizard.client.proxy.ProxyConfiguration in project dropwizard by dropwizard.

the class HttpClientBuilderTest method usesProxyWithBasicAuth.

@Test
void usesProxyWithBasicAuth() throws Exception {
    HttpClientConfiguration config = new HttpClientConfiguration();
    AuthConfiguration auth = new AuthConfiguration("secret", "stuff");
    ProxyConfiguration proxy = new ProxyConfiguration("192.168.52.11", 8080, "http", auth);
    config.setProxyConfiguration(proxy);
    CloseableHttpClient httpClient = checkProxy(config, new HttpHost("dropwizard.io", 80), new HttpHost("192.168.52.11", 8080, "http"));
    CredentialsProvider credentialsProvider = (CredentialsProvider) getInaccessibleField(httpClient.getClass(), "credentialsProvider").get(httpClient);
    assertThat(credentialsProvider.getCredentials(new AuthScope("192.168.52.11", 8080))).isEqualTo(new UsernamePasswordCredentials("secret", "stuff"));
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) ProxyConfiguration(io.dropwizard.client.proxy.ProxyConfiguration) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) AuthConfiguration(io.dropwizard.client.proxy.AuthConfiguration) CredentialsProvider(org.apache.http.client.CredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Test(org.junit.jupiter.api.Test)

Example 7 with ProxyConfiguration

use of io.dropwizard.client.proxy.ProxyConfiguration in project dropwizard by dropwizard.

the class HttpClientBuilderTest method usesProxyWithNonProxyHosts.

@Test
void usesProxyWithNonProxyHosts() throws Exception {
    HttpClientConfiguration config = new HttpClientConfiguration();
    ProxyConfiguration proxy = new ProxyConfiguration("192.168.52.11", 8080);
    proxy.setNonProxyHosts(Collections.singletonList("*.example.com"));
    config.setProxyConfiguration(proxy);
    checkProxy(config, new HttpHost("host.example.com", 80), null);
}
Also used : ProxyConfiguration(io.dropwizard.client.proxy.ProxyConfiguration) HttpHost(org.apache.http.HttpHost) Test(org.junit.jupiter.api.Test)

Example 8 with ProxyConfiguration

use of io.dropwizard.client.proxy.ProxyConfiguration 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 boolean normalizeUri = configuration.isNormalizeUriEnabled();
    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).setNormalizeUri(normalizeUri).build();
    final SocketConfig socketConfig = SocketConfig.custom().setTcpNoDelay(true).setSoTimeout(timeout).build();
    builder.setRequestExecutor(createRequestExecutor(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();
            }
            // set the AuthScope
            AuthScope authScope = new AuthScope(httpHost, auth.getRealm(), auth.getAuthScheme());
            // set the credentials type
            Credentials credentials = configureCredentials(auth);
            credentialsProvider.setCredentials(authScope, credentials);
        }
    }
    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);
    }
    if (serviceUnavailableRetryStrategy != null) {
        builder.setServiceUnavailableRetryStrategy(serviceUnavailableRetryStrategy);
    }
    customizeBuilder(builder);
    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) 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) NTCredentials(org.apache.http.auth.NTCredentials) Credentials(org.apache.http.auth.Credentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Aggregations

ProxyConfiguration (io.dropwizard.client.proxy.ProxyConfiguration)8 HttpHost (org.apache.http.HttpHost)8 Test (org.junit.jupiter.api.Test)6 AuthConfiguration (io.dropwizard.client.proxy.AuthConfiguration)4 AuthScope (org.apache.http.auth.AuthScope)4 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)4 CredentialsProvider (org.apache.http.client.CredentialsProvider)3 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)3 Credentials (org.apache.http.auth.Credentials)2 NTCredentials (org.apache.http.auth.NTCredentials)2 NonProxyListProxyRoutePlanner (io.dropwizard.client.proxy.NonProxyListProxyRoutePlanner)1 ConnectionReuseStrategy (org.apache.http.ConnectionReuseStrategy)1 HttpResponse (org.apache.http.HttpResponse)1 HttpRequestRetryHandler (org.apache.http.client.HttpRequestRetryHandler)1 RequestConfig (org.apache.http.client.config.RequestConfig)1 SocketConfig (org.apache.http.config.SocketConfig)1 DefaultConnectionReuseStrategy (org.apache.http.impl.DefaultConnectionReuseStrategy)1 NoConnectionReuseStrategy (org.apache.http.impl.NoConnectionReuseStrategy)1 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)1 DefaultConnectionKeepAliveStrategy (org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy)1