Example usage for org.apache.http.impl.client HttpClientBuilder setRoutePlanner

List of usage examples for org.apache.http.impl.client HttpClientBuilder setRoutePlanner

Introduction

In this page you can find the example usage for org.apache.http.impl.client HttpClientBuilder setRoutePlanner.

Prototype

public final HttpClientBuilder setRoutePlanner(final HttpRoutePlanner routePlanner) 

Source Link

Document

Assigns HttpRoutePlanner instance.

Usage

From source file:lh.api.showcase.server.util.HttpClientUtils.java

public static CloseableHttpClient createHttpClient(HasProxySettings proxySetting,
        PoolingHttpClientConnectionManager connectionManager) {
    // http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d5e475

    HttpClientBuilder clientBuilder = HttpClients.custom();
    if (proxySetting != null && proxySetting.isActive()) {
        logger.info("Set the http proxy (" + proxySetting.getHost() + ":" + proxySetting.getPort() + ")");
        CredentialsProvider credsProvider = Preconditions.checkNotNull(proxySetting.getCredentialsProvider());
        HttpHost proxy = new HttpHost(proxySetting.getHost(), proxySetting.getPort());
        DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);

        clientBuilder.setRoutePlanner(routePlanner).setDefaultCredentialsProvider(credsProvider);
    }//w w w.  java  2 s . c  o  m
    if (connectionManager != null) {
        clientBuilder.setConnectionManager(connectionManager);
    }
    return clientBuilder.build();
}

From source file:org.springframework.cloud.config.server.support.HttpClientSupport.java

public static HttpClientBuilder builder(HttpEnvironmentRepositoryProperties environmentProperties)
        throws GeneralSecurityException {
    SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
    HttpClientBuilder httpClientBuilder = HttpClients.custom();

    if (environmentProperties.isSkipSslValidation()) {
        sslContextBuilder.loadTrustMaterial(null, (certificate, authType) -> true);
        httpClientBuilder.setSSLHostnameVerifier(new NoopHostnameVerifier());
    }/*  ww  w.  ja v  a 2s. co  m*/

    if (!CollectionUtils.isEmpty(environmentProperties.getProxy())) {
        ProxyHostProperties httpsProxy = environmentProperties.getProxy()
                .get(ProxyHostProperties.ProxyForScheme.HTTPS);
        ProxyHostProperties httpProxy = environmentProperties.getProxy()
                .get(ProxyHostProperties.ProxyForScheme.HTTP);

        httpClientBuilder.setRoutePlanner(new SchemeBasedRoutePlanner(httpsProxy, httpProxy));
        httpClientBuilder
                .setDefaultCredentialsProvider(new ProxyHostCredentialsProvider(httpProxy, httpsProxy));
    } else {
        httpClientBuilder.setRoutePlanner(new SystemDefaultRoutePlanner(ProxySelector.getDefault()));
        httpClientBuilder.setDefaultCredentialsProvider(new SystemDefaultCredentialsProvider());
    }

    int timeout = environmentProperties.getTimeout() * 1000;
    return httpClientBuilder.setSSLContext(sslContextBuilder.build()).setDefaultRequestConfig(
            RequestConfig.custom().setSocketTimeout(timeout).setConnectTimeout(timeout).build());
}

From source file:de.undercouch.gradle.tasks.download.internal.DefaultHttpClientFactory.java

@Override
public CloseableHttpClient createHttpClient(HttpHost httpHost, boolean acceptAnyCertificate) {
    HttpClientBuilder builder = HttpClientBuilder.create();

    //configure proxy from system environment
    builder.setRoutePlanner(new SystemDefaultRoutePlanner(null));

    //accept any certificate if necessary
    if ("https".equals(httpHost.getSchemeName()) && acceptAnyCertificate) {
        SSLConnectionSocketFactory icsf = getInsecureSSLSocketFactory();
        builder.setSSLSocketFactory(icsf);
        Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("https", icsf).build();
        HttpClientConnectionManager cm = new BasicHttpClientConnectionManager(registry);
        builder.setConnectionManager(cm);
    }/*from w w w . j a  va 2 s.  c  o  m*/

    //add an interceptor that replaces the invalid Content-Type
    //'none' by 'identity'
    builder.addInterceptorFirst(new ContentEncodingNoneInterceptor());

    CloseableHttpClient client = builder.build();
    return client;
}

From source file:com.yahoo.sql4d.sql4ddriver.DruidNodeAccessor.java

public CloseableHttpClient getClient() {
    HttpClientBuilder builder = HttpClients.custom().setConnectionManager(pool);
    return (customRouterPlanner != null) ? builder.setRoutePlanner(customRouterPlanner).build()
            : builder.build();/*from   ww w  .j av  a2s  .  c  o  m*/
}

From source file:com.intuit.wasabi.export.rest.impl.DefaultRestDriver.java

private CloseableHttpClient createCloseableHttpClientWithProxy(final HttpClientBuilder httpClientBuilder,
        final String proxyHost, final Integer proxyPort) {
    HttpHost proxy = new HttpHost(proxyHost, proxyPort);
    DefaultProxyRoutePlanner defaultProxyRoutePlanner = new DefaultProxyRoutePlanner(proxy);

    httpClientBuilder.setRoutePlanner(defaultProxyRoutePlanner);

    return httpClientBuilder.build();
}

From source file:com.facebook.presto.jdbc.QueryExecutor.java

private QueryExecutor(String userAgent, ObjectMapper mapper, HttpHost proxy) {
    checkNotNull(userAgent, "userAgent is null");
    checkNotNull(mapper, "mapper is null");

    this.userAgent = userAgent;
    this.mapper = mapper;

    HttpClientBuilder builder = HttpClients.custom();
    HttpAsyncClientBuilder asyncBuilder = HttpAsyncClients.custom();

    if (proxy != null) {
        DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
        builder.setRoutePlanner(routePlanner);
        asyncBuilder.setRoutePlanner(routePlanner);
    }/* ww  w  .j a va2s. c o m*/

    this.httpClient = asyncBuilder.build();
    this.httpClient.start();
}

From source file:org.dasein.cloud.utils.requester.AbstractDaseinRequestExecutor.java

protected HttpClientBuilder setProxyIfRequired(HttpClientBuilder httpClientBuilder) {
    if (this.httpProxyConfig != null) {
        HttpHost proxy = new HttpHost(httpProxyConfig.getHost(), httpProxyConfig.getPort());
        DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
        httpClientBuilder.setRoutePlanner(routePlanner);
    }/*  www.j av  a 2s .  co m*/

    return httpClientBuilder;
}

From source file:com.ksc.http.apache.client.impl.ApacheHttpClientFactory.java

private void addProxyConfig(HttpClientBuilder builder, HttpClientSettings settings) {
    if (isProxyEnabled(settings)) {

        LOG.info("Configuring Proxy. Proxy Host: " + settings.getProxyHost() + " " + "Proxy Port: "
                + settings.getProxyPort());

        builder.setRoutePlanner(new SdkProxyRoutePlanner(settings.getProxyHost(), settings.getProxyPort(),
                settings.getNonProxyHosts()));

        if (isAuthenticatedProxy(settings)) {
            builder.setDefaultCredentialsProvider(ApacheUtils.newProxyCredentialsProvider(settings));
        }//from  w  ww  . j a  v a 2  s  . c o m
    }
}

From source file:org.eclipse.cft.server.core.internal.client.RestUtils.java

public static ClientHttpRequestFactory createRequestFactory(HttpProxyConfiguration httpProxyConfiguration,
        boolean trustSelfSignedCerts, boolean disableRedirectHandling) {
    HttpClientBuilder httpClientBuilder = HttpClients.custom().useSystemProperties();

    if (trustSelfSignedCerts) {
        httpClientBuilder.setSslcontext(buildSslContext());
        httpClientBuilder.setHostnameVerifier(BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
    }//w  ww  .  j  av  a  2  s. c  o  m

    if (disableRedirectHandling) {
        httpClientBuilder.disableRedirectHandling();
    }

    if (httpProxyConfiguration != null) {
        HttpHost proxy = new HttpHost(httpProxyConfiguration.getProxyHost(),
                httpProxyConfiguration.getProxyPort());
        httpClientBuilder.setProxy(proxy);

        if (httpProxyConfiguration.isAuthRequired()) {
            BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(
                    new AuthScope(httpProxyConfiguration.getProxyHost(), httpProxyConfiguration.getProxyPort()),
                    new UsernamePasswordCredentials(httpProxyConfiguration.getUsername(),
                            httpProxyConfiguration.getPassword()));
            httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
        }

        HttpRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
        httpClientBuilder.setRoutePlanner(routePlanner);
    }

    HttpClient httpClient = httpClientBuilder.build();
    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(
            httpClient);

    return requestFactory;
}

From source file:net.siegmar.japtproxy.fetcher.HttpClientConfigurer.java

protected void configureProxy(final HttpClientBuilder httpClientBuilder, final String proxy)
        throws InitializationException {
    final URL proxyUrl;
    try {/*ww  w.  j  a  v  a2s .co  m*/
        proxyUrl = new URL(proxy);
    } catch (final MalformedURLException e) {
        throw new InitializationException("Invalid proxy url", e);
    }

    final String proxyHost = proxyUrl.getHost();
    final int proxyPort = proxyUrl.getPort() != -1 ? proxyUrl.getPort() : proxyUrl.getDefaultPort();

    LOG.info("Set proxy server to '{}:{}'", proxyHost, proxyPort);
    httpClientBuilder.setRoutePlanner(new DefaultProxyRoutePlanner(new HttpHost(proxyHost, proxyPort)));

    final String userInfo = proxyUrl.getUserInfo();
    if (userInfo != null) {
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(new AuthScope(proxyHost, proxyPort), buildCredentials(userInfo));

        httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
    }
}