Search in sources :

Example 1 with SystemDefaultRoutePlanner

use of org.apache.http.impl.conn.SystemDefaultRoutePlanner in project dropwizard by dropwizard.

the class JerseyClientBuilderTest method usesACustomHttpRoutePlanner.

@Test
public void usesACustomHttpRoutePlanner() {
    final HttpRoutePlanner customHttpRoutePlanner = new SystemDefaultRoutePlanner(new ProxySelector() {

        @Override
        public List<Proxy> select(URI uri) {
            return ImmutableList.of(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.168.53.12", 8080)));
        }

        @Override
        public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
        }
    });
    builder.using(customHttpRoutePlanner);
    verify(apacheHttpClientBuilder).using(customHttpRoutePlanner);
}
Also used : ProxySelector(java.net.ProxySelector) Proxy(java.net.Proxy) HttpRoutePlanner(org.apache.http.conn.routing.HttpRoutePlanner) InetSocketAddress(java.net.InetSocketAddress) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) URI(java.net.URI) SystemDefaultRoutePlanner(org.apache.http.impl.conn.SystemDefaultRoutePlanner) Test(org.junit.Test)

Example 2 with SystemDefaultRoutePlanner

use of org.apache.http.impl.conn.SystemDefaultRoutePlanner 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 3 with SystemDefaultRoutePlanner

use of org.apache.http.impl.conn.SystemDefaultRoutePlanner in project dropwizard by dropwizard.

the class HttpClientBuilderTest method usesACustomRoutePlanner.

@Test
public void usesACustomRoutePlanner() throws Exception {
    final HttpRoutePlanner routePlanner = new SystemDefaultRoutePlanner(new ProxySelector() {

        @Override
        public List<Proxy> select(URI uri) {
            return ImmutableList.of(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.168.52.1", 8080)));
        }

        @Override
        public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
        }
    });
    final CloseableHttpClient httpClient = builder.using(configuration).using(routePlanner).createClient(apacheBuilder, connectionManager, "test").getClient();
    assertThat(httpClient).isNotNull();
    assertThat(spyHttpClientBuilderField("routePlanner", apacheBuilder)).isSameAs(routePlanner);
    assertThat(spyHttpClientField("routePlanner", httpClient)).isSameAs(routePlanner);
}
Also used : ProxySelector(java.net.ProxySelector) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Proxy(java.net.Proxy) HttpRoutePlanner(org.apache.http.conn.routing.HttpRoutePlanner) InetSocketAddress(java.net.InetSocketAddress) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) URI(java.net.URI) SystemDefaultRoutePlanner(org.apache.http.impl.conn.SystemDefaultRoutePlanner) Test(org.junit.Test)

Example 4 with SystemDefaultRoutePlanner

use of org.apache.http.impl.conn.SystemDefaultRoutePlanner in project gradle by gradle.

the class HttpClientConfigurer method configureProxy.

private void configureProxy(HttpClientBuilder builder, CredentialsProvider credentialsProvider, HttpSettings httpSettings) {
    HttpProxySettings.HttpProxy httpProxy = httpSettings.getProxySettings().getProxy();
    HttpProxySettings.HttpProxy httpsProxy = httpSettings.getSecureProxySettings().getProxy();
    for (HttpProxySettings.HttpProxy proxy : Lists.newArrayList(httpProxy, httpsProxy)) {
        if (proxy != null) {
            if (proxy.credentials != null) {
                useCredentials(credentialsProvider, proxy.host, proxy.port, Collections.singleton(new AllSchemesAuthentication(proxy.credentials)));
            }
        }
    }
    builder.setRoutePlanner(new SystemDefaultRoutePlanner(ProxySelector.getDefault()));
}
Also used : AllSchemesAuthentication(org.gradle.internal.authentication.AllSchemesAuthentication) SystemDefaultRoutePlanner(org.apache.http.impl.conn.SystemDefaultRoutePlanner)

Aggregations

SystemDefaultRoutePlanner (org.apache.http.impl.conn.SystemDefaultRoutePlanner)4 ImmutableList (com.google.common.collect.ImmutableList)2 IOException (java.io.IOException)2 InetSocketAddress (java.net.InetSocketAddress)2 Proxy (java.net.Proxy)2 ProxySelector (java.net.ProxySelector)2 SocketAddress (java.net.SocketAddress)2 URI (java.net.URI)2 List (java.util.List)2 HttpRoutePlanner (org.apache.http.conn.routing.HttpRoutePlanner)2 Test (org.junit.Test)2 HttpRequestInterceptor (org.apache.http.HttpRequestInterceptor)1 HttpResponseInterceptor (org.apache.http.HttpResponseInterceptor)1 RequestConfig (org.apache.http.client.config.RequestConfig)1 NoConnectionReuseStrategy (org.apache.http.impl.NoConnectionReuseStrategy)1 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)1 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)1 LaxRedirectStrategy (org.apache.http.impl.client.LaxRedirectStrategy)1 AllSchemesAuthentication (org.gradle.internal.authentication.AllSchemesAuthentication)1