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

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

Introduction

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

Prototype

public final HttpClientBuilder setProxy(final HttpHost proxy) 

Source Link

Document

Assigns default proxy value.

Usage

From source file:com.threatconnect.app.playbooks.db.tcapi.ConnectionUtil.java

/**
 * Adds proxy information to an http client builder
 * //w ww  .j  a v a 2s. co  m
 * @param builder
 * the HttpClientBuilder builder to add the proxy information to
 * @param proxyHost
 * the host of the proxy server
 * @param proxyPort
 * the port of the proxy server
 * @param proxyUserName
 * the username to authenticate with the proxy server (optional)
 * @param proxyPassword
 * the password to authenticate with the proxy server (optional)
 */
public static void addProxy(final HttpClientBuilder builder, final String proxyHost, final Integer proxyPort,
        final String proxyUserName, final String proxyPassword) {
    // check to see if the the host or port are null
    if (proxyHost == null || proxyPort == null) {
        logger.warn("proxyHost and proxyPort are required to connect to a proxy");
    } else {
        // add the proxy information to the builder
        builder.setProxy(new HttpHost(proxyHost, proxyPort));

        // authentication required
        if (proxyUserName != null && proxyPassword != null) {
            // add the credentials to the proxy information
            Credentials credentials = new UsernamePasswordCredentials(proxyUserName, proxyPassword);
            AuthScope authScope = new AuthScope(proxyHost, proxyPort);
            CredentialsProvider credsProvider = new BasicCredentialsProvider();
            credsProvider.setCredentials(authScope, credentials);
            builder.setDefaultCredentialsProvider(credsProvider);
        }
    }
}

From source file:io.wcm.caravan.commons.httpclient.impl.HttpClientItem.java

private static CloseableHttpClient buildHttpClient(HttpClientConfig config,
        PoolingHttpClientConnectionManager connectionManager, CredentialsProvider credentialsProvider) {

    // prepare HTTPClient builder
    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create().setConnectionManager(connectionManager);

    // timeout settings
    httpClientBuilder.setDefaultRequestConfig(RequestConfig.custom()
            .setConnectTimeout(config.getConnectTimeout()).setSocketTimeout(config.getSocketTimeout()).build());

    httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);

    // optional proxy support
    if (StringUtils.isNotEmpty(config.getProxyHost())) {
        httpClientBuilder.setProxy(new HttpHost(config.getProxyHost(), config.getProxyPort()));

        // optional proxy authentication
        if (StringUtils.isNotEmpty(config.getProxyUser())) {
            httpClientBuilder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy());
        }//from www.  j a v a  2s . co  m
    }

    return httpClientBuilder.build();
}

From source file:lucee.commons.net.http.httpclient.HTTPEngine4Impl.java

public static void setProxy(HttpClientBuilder builder, HttpUriRequest request, ProxyData proxy) {
    // set Proxy/*  w  w  w. jav  a  2 s . co  m*/
    if (ProxyDataImpl.isValid(proxy)) {
        HttpHost host = new HttpHost(proxy.getServer(), proxy.getPort() == -1 ? 80 : proxy.getPort());
        builder.setProxy(host);

        // username/password
        if (!StringUtil.isEmpty(proxy.getUsername())) {
            CredentialsProvider cp = new BasicCredentialsProvider();
            builder.setDefaultCredentialsProvider(cp);
            cp.setCredentials(new AuthScope(proxy.getServer(), proxy.getPort()),
                    new UsernamePasswordCredentials(proxy.getUsername(), proxy.getPassword()));
        }
    }
}

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);
    }/*from   w w  w .java  2  s. co  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:com.github.tomakehurst.wiremock.http.HttpClientFactory.java

public static CloseableHttpClient createClient(int maxConnections, int timeoutMilliseconds,
        ProxySettings proxySettings, KeyStoreSettings trustStoreSettings) {

    HttpClientBuilder builder = HttpClientBuilder.create().disableAuthCaching().disableAutomaticRetries()
            .disableCookieManagement().disableRedirectHandling().disableContentCompression()
            .setMaxConnTotal(maxConnections)
            .setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(timeoutMilliseconds).build())
            .useSystemProperties().setHostnameVerifier(new AllowAllHostnameVerifier());

    if (proxySettings != NO_PROXY) {
        HttpHost proxyHost = new HttpHost(proxySettings.host(), proxySettings.port());
        builder.setProxy(proxyHost);
    }/*from  ww  w.j  av a 2s .c  o  m*/

    if (trustStoreSettings != NO_STORE) {
        builder.setSslcontext(buildSSLContextWithTrustStore(trustStoreSettings));
    } else {
        builder.setSslcontext(buildAllowAnythingSSLContext());
    }

    return builder.build();
}

From source file:org.glowroot.central.SyntheticMonitorService.java

private static SyncHttpClientHolder createSyncHttpClientHolder(String shortVersion,
        HttpProxyConfig httpProxyConfig) {
    HttpClientBuilder builder = HttpClients.custom().setUserAgent("GlowrootCentral" + shortVersion)
            .setDefaultHeaders(Arrays.asList(new BasicHeader("Glowroot-Transaction-Type", "Synthetic")));
    if (!httpProxyConfig.host().isEmpty()) {
        int proxyPort = MoreObjects.firstNonNull(httpProxyConfig.port(), 80);
        builder.setProxy(new HttpHost(httpProxyConfig.host(), proxyPort));
    }// ww w.ja  v a2 s.com
    CloseableHttpClient httpClient = builder.setMaxConnPerRoute(10) // increasing from default 2
            .setMaxConnTotal(1000) // increasing from default 20
            .build();
    return ImmutableSyncHttpClientHolder.of(httpClient, httpProxyConfig);
}

From source file:securitytools.common.http.HttpClientFactory.java

public static CloseableHttpClient build(ClientConfiguration clientConfiguration)
        throws NoSuchAlgorithmException {
    HttpClientBuilder builder = HttpClients.custom();

    // Certificate Validation
    if (clientConfiguration.isCertificateValidationEnabled()) {
        builder.setSSLSocketFactory(new SSLConnectionSocketFactory(SSLContext.getDefault(),
                SSLConnectionSocketFactory.STRICT_HOSTNAME_VERIFIER));
    } else {/*from w w  w  .  ja  v a2 s.c  om*/
        // Disable
        builder.setSSLSocketFactory(new TrustingSSLConnectionSocketFactory());
    }

    // Timeouts
    RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
    requestConfigBuilder.setConnectTimeout(clientConfiguration.getConnectionTimeout());
    requestConfigBuilder.setConnectionRequestTimeout(clientConfiguration.getConnectionTimeout());
    requestConfigBuilder.setSocketTimeout(clientConfiguration.getSocketTimeout());
    builder.setDefaultRequestConfig(requestConfigBuilder.build());

    // User Agent
    builder.setUserAgent(clientConfiguration.getUserAgent());

    // Proxy
    if (clientConfiguration.getProxyHost() != null) {
        builder.setProxy(clientConfiguration.getProxyHost());
    }

    return builder.build();
}

From source file:com.qdeve.oilprice.configuration.HttpConfiguration.java

private HttpClient httpclient() {
    HttpClientBuilder builder = HttpClientBuilder.create().useSystemProperties();
    if (appPropertries.getProxyHost() != null && appPropertries.getProxyPort() != null) {
        builder.setProxy(new HttpHost(appPropertries.getProxyHost(), appPropertries.getProxyPort()));
    }/*  www  .  j  av a  2 s  . co m*/
    return builder.build();
}

From source file:org.apache.nifi.processors.standard.util.HTTPUtils.java

public static void setProxy(final ProcessContext context, final HttpClientBuilder clientBuilder,
        final CredentialsProvider credentialsProvider) {
    // Set the proxy if specified
    final ProxyConfiguration proxyConfig = ProxyConfiguration.getConfiguration(context, () -> {
        if (context.getProperty(PROXY_HOST).isSet() && context.getProperty(PROXY_PORT).isSet()) {
            final ProxyConfiguration componentProxyConfig = new ProxyConfiguration();
            final String host = context.getProperty(PROXY_HOST).getValue();
            final int port = context.getProperty(PROXY_PORT).asInteger();
            componentProxyConfig.setProxyType(Proxy.Type.HTTP);
            componentProxyConfig.setProxyServerHost(host);
            componentProxyConfig.setProxyServerPort(port);
            return componentProxyConfig;
        }// w  w w  . ja v a 2s .com
        return ProxyConfiguration.DIRECT_CONFIGURATION;
    });

    if (Proxy.Type.HTTP.equals(proxyConfig.getProxyType())) {
        final String host = proxyConfig.getProxyServerHost();
        final int port = proxyConfig.getProxyServerPort();
        clientBuilder.setProxy(new HttpHost(host, port));

        if (proxyConfig.hasCredential()) {
            final AuthScope proxyAuthScope = new AuthScope(host, port);
            final UsernamePasswordCredentials proxyCredential = new UsernamePasswordCredentials(
                    proxyConfig.getProxyUserName(), proxyConfig.getProxyUserPassword());
            credentialsProvider.setCredentials(proxyAuthScope, proxyCredential);
        }
    }
}

From source file:org.opensextant.xtext.collectors.sharepoint.SharepointClient.java

/**
 * Sharepoint requires NTLM. This client requires a non-null user/passwd/domain.
 * /*ww w. ja  v  a  2  s .  c o m*/
 */
@Override
public HttpClient getClient() {

    if (currentConn != null) {
        return currentConn;
    }

    HttpClientBuilder clientHelper = HttpClientBuilder.create();

    if (proxyHost != null) {
        clientHelper.setProxy(proxyHost);
    }

    RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY)
            .build();

    CredentialsProvider creds = new BasicCredentialsProvider();
    creds.setCredentials(AuthScope.ANY, new NTCredentials(user, passwd, server, domain));
    clientHelper.setDefaultCredentialsProvider(creds);
    HttpClient httpClient = clientHelper.setDefaultRequestConfig(globalConfig).build();

    return httpClient;

}