Java Code Examples for org.apache.http.impl.client.HttpClientBuilder#setRoutePlanner()
The following examples show how to use
org.apache.http.impl.client.HttpClientBuilder#setRoutePlanner() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProxyClientHttpRequestFactory.java From sinavi-jfw with Apache License 2.0 | 7 votes |
/** * プロパティの設定が終了したあとにHttpClientのインスタンスを生成し、プロキシの設定を行います。 * {@inheritDoc} */ @Override public void afterPropertiesSet() throws Exception { Assert.notNull(proxyHost, "プロキシホスト(proxyHost)は必須です。"); Assert.notNull(proxyPort, "プロキシポート番号(proxyPort)は必須です。"); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); connectionManager.setMaxTotal(maxTotal); connectionManager.setDefaultMaxPerRoute(defaultMaxPerRoute); HttpClientBuilder builder = HttpClients.custom(); builder.setConnectionManager(connectionManager); if (authentication) { Assert.notNull(username, "ユーザ認証がtrueに設定された場合、ユーザ名(username)は必須です。"); Assert.notNull(password, "ユーザ認証がtrueに設定された場合、パスワード(password)は必須です。"); DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(new HttpHost(proxyHost, Integer.parseInt(proxyPort))); builder.setRoutePlanner(routePlanner); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(proxyHost, Integer.parseInt(proxyPort)), new UsernamePasswordCredentials(username, password)); builder.setDefaultCredentialsProvider(credsProvider); } builder.setDefaultRequestConfig(RequestConfig.custom().setSocketTimeout(readTimeout).build()); CloseableHttpClient client = builder.build(); setHttpClient(client); }
Example 2
Source File: DefaultTokenManager.java From ibm-cos-sdk-java with Apache License 2.0 | 6 votes |
/** * Add a proxy to the http request if it has been set within settings * * @param builder * Builder used to create the http client * @param settings * Settings which contain any proxy configuration details */ public static void addProxyConfig(HttpClientBuilder builder, HttpClientSettings settings) { if (settings.isProxyEnabled()) { log.info("Configuring Proxy. Proxy Host: " + settings.getProxyHost() + " " + "Proxy Port: " + settings.getProxyPort()); builder.setRoutePlanner(new SdkProxyRoutePlanner( settings.getProxyHost(), settings.getProxyPort(), settings.getNonProxyHosts())); if (settings.isAuthenticatedProxy()) { builder.setDefaultCredentialsProvider(ApacheUtils.newProxyCredentialsProvider(settings)); } } }
Example 3
Source File: PiwikTracker.java From matomo-java-tracker with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * Get a HTTP client. With proxy if a proxy is provided in the constructor. * @return a HTTP client */ protected HttpClient getHttpClient(){ HttpClientBuilder builder = HttpClientBuilder.create(); if(proxyHost != null && proxyPort != 0) { HttpHost proxy = new HttpHost(proxyHost, proxyPort); DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy); builder.setRoutePlanner(routePlanner); } RequestConfig.Builder config = RequestConfig.custom() .setConnectTimeout(timeout) .setConnectionRequestTimeout(timeout) .setSocketTimeout(timeout); builder.setDefaultRequestConfig(config.build()); return builder.build(); }
Example 4
Source File: HttpManagement.java From gerbil with GNU Affero General Public License v3.0 | 6 votes |
/** * Creates a HttpClientBuilder with the default settings of GERBIL. * * @return a HttpClientBuilder with the default settings of GERBIL. */ public HttpClientBuilder generateHttpClientBuilder() { HttpClientBuilder builder = HttpClientBuilder.create(); builder.setUserAgent(userAgent); String proxyHost = GerbilConfiguration.getInstance().getString(PROXY_HOST_KEY); int proxyPort = GerbilConfiguration.getInstance().getInt(PROXY_PORT_KEY, DEFAULT_PROXY_PORT); if (proxyHost != null) { HttpHost proxy = new HttpHost(proxyHost, proxyPort); DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy); builder.setRoutePlanner(routePlanner); } // Use a redirect strategy that allows the "direct redirect" of POST requests // without creating a completely new request builder.setRedirectStrategy(new SimpleRedirectStrategy()).build(); return builder; }
Example 5
Source File: PrerenderConfig.java From prerender-java with MIT License | 5 votes |
private HttpClientBuilder configureProxy(HttpClientBuilder builder) { final String proxy = config.get("proxy"); if (isNotBlank(proxy)) { final int proxyPort = Integer.parseInt(config.get("proxyPort")); DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(new HttpHost(proxy, proxyPort)); builder.setRoutePlanner(routePlanner); } return builder; }
Example 6
Source File: HttpUtil.java From document-management-software with GNU Lesser General Public License v3.0 | 5 votes |
public static CloseableHttpClient getNotValidatingClient(int timeout, String proxyServer, Integer proxyPort, String proxyUser, String proxyPassword) { try { HttpClientBuilder clientBuilder = HttpClients.custom(); RequestConfig.Builder requestBuilder = RequestConfig.custom().setConnectTimeout(timeout * 1000) .setSocketTimeout(timeout * 1000).setConnectionRequestTimeout(timeout * 1000) .setRedirectsEnabled(true); if (StringUtils.isNotEmpty(proxyServer)) { HttpHost proxyHost = new HttpHost(proxyServer, proxyPort); requestBuilder.setProxy(proxyHost); DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxyHost); clientBuilder.setRoutePlanner(routePlanner); if (StringUtils.isNotEmpty(proxyUser)) { CredentialsProvider credentialProvider = new BasicCredentialsProvider(); credentialProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(proxyUser, proxyPassword)); clientBuilder.setRoutePlanner(routePlanner); } } RequestConfig requestConfig = requestBuilder.build(); CloseableHttpClient httpclient = clientBuilder.setHostnameVerifier(new AllowAllHostnameVerifier()) .setSslcontext(new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() { public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { return true; } }).build()).setDefaultRequestConfig(requestConfig).build(); return httpclient; } catch (Throwable t) { return null; } }
Example 7
Source File: RestUtil.java From cf-java-client-sap with Apache License 2.0 | 5 votes |
public ClientHttpRequestFactory createRequestFactory(HttpProxyConfiguration httpProxyConfiguration, boolean trustSelfSignedCerts) { HttpClientBuilder httpClientBuilder = HttpClients.custom() .useSystemProperties(); if (trustSelfSignedCerts) { httpClientBuilder.setSslcontext(buildSslContext()); httpClientBuilder.setHostnameVerifier(BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); } 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(); return new HttpComponentsClientHttpRequestFactory(httpClient); }
Example 8
Source File: VaultConfig.java From cloudbreak with Apache License 2.0 | 5 votes |
private ClientHttpRequestFactory usingHttpComponents(ClientOptions options, SslConfiguration sslConfiguration) throws GeneralSecurityException, IOException { HttpClientBuilder httpClientBuilder = HttpClients.custom(); httpClientBuilder.setRoutePlanner(new SystemDefaultRoutePlanner( DefaultSchemePortResolver.INSTANCE, ProxySelector.getDefault())); if (isNoneEmpty(httpsProxyUser, httpsProxyPassword)) { UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(httpsProxyUser, httpsProxyPassword); CredentialsProvider provider = new BasicCredentialsProvider(); provider.setCredentials(AuthScope.ANY, credentials); httpClientBuilder.setDefaultCredentialsProvider(provider); } if (hasSslConfiguration(sslConfiguration)) { SSLContext sslContext = getSSLContext(sslConfiguration, getTrustManagers(sslConfiguration)); SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory( sslContext); httpClientBuilder.setSSLSocketFactory(sslSocketFactory); httpClientBuilder.setSSLContext(sslContext); } RequestConfig requestConfig = RequestConfig .custom() .setConnectTimeout(Math.toIntExact(options.getConnectionTimeout().toMillis())) .setSocketTimeout(Math.toIntExact(options.getReadTimeout().toMillis())) .setAuthenticationEnabled(true) .build(); httpClientBuilder.setDefaultRequestConfig(requestConfig); httpClientBuilder.setRedirectStrategy(new LaxRedirectStrategy()); return new HttpComponentsClientHttpRequestFactory(httpClientBuilder.build()); }
Example 9
Source File: UDTProxyConfigurator.java From cyberduck with GNU General Public License v3.0 | 5 votes |
@Override public HttpClientBuilder build(final Proxy proxy, final TranscriptListener listener, final LoginCallback prompt) { final HttpClientBuilder builder = super.build(proxy, listener, prompt); // Add filter to inject custom headers to authenticate with proxy builder.setRequestExecutor( new CustomHeaderHttpRequestExecutor(headers) ); // Set proxy router planer builder.setRoutePlanner(new DefaultProxyRoutePlanner( new HttpHost(this.proxy.getHostname(), this.proxy.getPort(), this.proxy.getProtocol().getScheme().name()), new DefaultSchemePortResolver())); return builder; }
Example 10
Source File: ApacheHttpClientFactory.java From ibm-cos-sdk-java with Apache License 2.0 | 5 votes |
private void addProxyConfig(HttpClientBuilder builder, HttpClientSettings settings) { if (settings.isProxyEnabled()) { LOG.info("Configuring Proxy. Proxy Host: " + settings.getProxyHost() + " " + "Proxy Port: " + settings.getProxyPort()); builder.setRoutePlanner(new SdkProxyRoutePlanner( settings.getProxyHost(), settings.getProxyPort(), settings.getNonProxyHosts())); if (settings.isAuthenticatedProxy()) { builder.setDefaultCredentialsProvider(ApacheUtils.newProxyCredentialsProvider(settings)); } } }
Example 11
Source File: ClientHttpRequestFactoryFactory.java From spring-vault with Apache License 2.0 | 5 votes |
static ClientHttpRequestFactory usingHttpComponents(ClientOptions options, SslConfiguration sslConfiguration) throws GeneralSecurityException, IOException { HttpClientBuilder httpClientBuilder = HttpClients.custom(); httpClientBuilder.setRoutePlanner( new SystemDefaultRoutePlanner(DefaultSchemePortResolver.INSTANCE, ProxySelector.getDefault())); if (hasSslConfiguration(sslConfiguration)) { SSLContext sslContext = getSSLContext(sslConfiguration, getTrustManagers(sslConfiguration)); SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext); httpClientBuilder.setSSLSocketFactory(sslSocketFactory); httpClientBuilder.setSSLContext(sslContext); } RequestConfig requestConfig = RequestConfig.custom() // .setConnectTimeout(Math.toIntExact(options.getConnectionTimeout().toMillis())) // .setSocketTimeout(Math.toIntExact(options.getReadTimeout().toMillis())) // .setAuthenticationEnabled(true) // .build(); httpClientBuilder.setDefaultRequestConfig(requestConfig); // Support redirects httpClientBuilder.setRedirectStrategy(new LaxRedirectStrategy()); return new HttpComponentsClientHttpRequestFactory(httpClientBuilder.build()); }
Example 12
Source File: Proxy.java From verano-http with MIT License | 5 votes |
@Override public final HttpClientBuilder apply(final HttpClientBuilder builder) { return builder.setRoutePlanner( new DefaultProxyRoutePlanner( new HttpHost(this.host, this.port, this.scheme) ) ); }
Example 13
Source File: StandardMattermostService.java From jenkins-mattermost-plugin with MIT License | 5 votes |
private RequestConfig.Builder setupProxy(ProxyConfiguration proxy, HttpClientBuilder clientBuilder, RequestConfig.Builder reqconfigconbuilder) throws MalformedURLException { HttpHost proxyHost = new HttpHost(proxy.name, proxy.port); DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxyHost); clientBuilder.setRoutePlanner(routePlanner); reqconfigconbuilder.setProxy(proxyHost); setupProxyAuth(proxy, clientBuilder, proxyHost); return reqconfigconbuilder; }
Example 14
Source File: HttpClientSupport.java From spring-cloud-config with Apache License 2.0 | 5 votes |
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()); } 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()); }
Example 15
Source File: VespaHttpClientBuilder.java From vespa with Apache License 2.0 | 4 votes |
private static void addHttpsRewritingRoutePlanner(HttpClientBuilder builder) { if (TransportSecurityUtils.isTransportSecurityEnabled() && TransportSecurityUtils.getInsecureMixedMode() != MixedMode.PLAINTEXT_CLIENT_MIXED_SERVER) { builder.setRoutePlanner(new HttpToHttpsRoutePlanner()); } }
Example 16
Source File: CommonsDataLoader.java From dss with GNU Lesser General Public License v2.1 | 4 votes |
/** * Configure the proxy with the required credential if needed * * @param httpClientBuilder * @param credentialsProvider * @param url * @return {@link HttpClientBuilder} */ private HttpClientBuilder configureProxy(HttpClientBuilder httpClientBuilder, CredentialsProvider credentialsProvider, String url) { if (proxyConfig == null) { return httpClientBuilder; } final String protocol = getURL(url).getProtocol(); final boolean proxyHTTPS = Protocol.isHttps(protocol) && (proxyConfig.getHttpsProperties() != null); final boolean proxyHTTP = Protocol.isHttp(protocol) && (proxyConfig.getHttpProperties() != null); ProxyProperties proxyProps = null; if (proxyHTTPS) { LOG.debug("Use proxy https parameters"); proxyProps = proxyConfig.getHttpsProperties(); } else if (proxyHTTP) { LOG.debug("Use proxy http parameters"); proxyProps = proxyConfig.getHttpProperties(); } else { return httpClientBuilder; } String proxyHost = proxyProps.getHost(); int proxyPort = proxyProps.getPort(); String proxyUser = proxyProps.getUser(); String proxyPassword = proxyProps.getPassword(); String proxyExcludedHosts = proxyProps.getExcludedHosts(); if (Utils.isStringNotEmpty(proxyUser) && Utils.isStringNotEmpty(proxyPassword)) { AuthScope proxyAuth = new AuthScope(proxyHost, proxyPort); UsernamePasswordCredentials proxyCredentials = new UsernamePasswordCredentials(proxyUser, proxyPassword); credentialsProvider.setCredentials(proxyAuth, proxyCredentials); } LOG.debug("proxy host/port: {}:{}", proxyHost, proxyPort); // TODO SSL peer shut down incorrectly when protocol is https final HttpHost proxy = new HttpHost(proxyHost, proxyPort, Protocol.HTTP.getName()); if (Utils.isStringNotEmpty(proxyExcludedHosts)) { final String[] hosts = proxyExcludedHosts.split("[,; ]"); HttpRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy) { @Override public HttpRoute determineRoute(final HttpHost host, final HttpRequest request, final HttpContext context) throws HttpException { String hostname = (host != null ? host.getHostName() : null); if ((hosts != null) && (hostname != null)) { for (String h : hosts) { if (hostname.equalsIgnoreCase(h)) { // bypass proxy for that hostname return new HttpRoute(host); } } } return super.determineRoute(host, request, context); } }; httpClientBuilder.setRoutePlanner(routePlanner); } return httpClientBuilder.setProxy(proxy); }
Example 17
Source File: HttpClient.java From ats-framework with Apache License 2.0 | 4 votes |
protected void initialzeInternalClient() { if (!needsInternalClientInialization) { // internal client is already initialized return; } // release any resources if this client was already used close(); // rebuild the client HttpClientBuilder httpClientBuilder = HttpClients.custom(); // Add this interceptor to get the values of all HTTP headers in the request. // Some of them are provided by the user while others are generated by Apache HTTP Components. httpClientBuilder.addInterceptorLast(new HttpRequestInterceptor() { @Override public void process( HttpRequest request, HttpContext context ) throws HttpException, IOException { Header[] requestHeaders = request.getAllHeaders(); actualRequestHeaders = new ArrayList<HttpHeader>(); for (Header header : requestHeaders) { addHeaderToList(actualRequestHeaders, header.getName(), header.getValue()); } if (debugLevel != HttpDebugLevel.NONE) { logHTTPRequest(requestHeaders, request); } } }); // connect and read timeouts httpClientBuilder.setDefaultRequestConfig(RequestConfig.custom() .setConnectTimeout(connectTimeoutSeconds * 1000) .setSocketTimeout(readTimeoutSeconds * 1000) .build()); // socket buffer size if (this.socketBufferSize > 0) { httpClientBuilder.setDefaultSocketConfig(SocketConfig.custom() .setRcvBufSize(this.socketBufferSize) .setSndBufSize(this.socketBufferSize) .build()); } // SSL if (isOverSsl) { setupSSL(httpClientBuilder); } // setup authentication if (!StringUtils.isNullOrEmpty(username)) { setupAuthentication(httpClientBuilder); } // set proxy if (AtsSystemProperties.SYSTEM_HTTP_PROXY_HOST != null && AtsSystemProperties.SYSTEM_HTTP_PROXY_PORT != null) { HttpHost proxy = new HttpHost(AtsSystemProperties.SYSTEM_HTTP_PROXY_HOST, Integer.parseInt(AtsSystemProperties.SYSTEM_HTTP_PROXY_PORT)); DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy); httpClientBuilder.setRoutePlanner(routePlanner); } // now build the client after we have already set everything needed on the client builder httpClient = httpClientBuilder.build(); // do not come here again until not needed needsInternalClientInialization = false; }
Example 18
Source File: HttpClientAdapter.java From uyuni with GNU General Public License v2.0 | 4 votes |
/** * Initialize an {@link HttpClient} for performing requests. Proxy settings will * be read from the configuration and applied transparently. */ public HttpClientAdapter() { Optional<SSLConnectionSocketFactory> sslSocketFactory = Optional.empty(); try { SSLContext sslContext = SSLContext.getDefault(); sslSocketFactory = Optional.of(new SSLConnectionSocketFactory( sslContext, new String[]{"TLSv1", "TLSv1.1", "TLSv1.2"}, null, SSLConnectionSocketFactory.getDefaultHostnameVerifier())); } catch (NoSuchAlgorithmException e) { log.warn("No such algorithm. Using default context", e); } HttpClientBuilder clientBuilder = HttpClientBuilder.create(); sslSocketFactory.ifPresent(sf -> clientBuilder.setSSLSocketFactory(sf)); clientBuilder.setDefaultCredentialsProvider(credentialsProvider); Builder requestConfigBuilder = RequestConfig.custom() .setConnectTimeout(Config.get().getInt(HTTP_CONNECTION_TIMEOUT, 5) * TO_MILLISECONDS) .setSocketTimeout(Config.get().getInt(HTTP_SOCKET_TIMEOUT, 5 * 60) * TO_MILLISECONDS) .setCookieSpec(CookieSpecs.IGNORE_COOKIES); // Store the proxy settings String proxyHostname = ConfigDefaults.get().getProxyHost(); if (!StringUtils.isBlank(proxyHostname)) { int proxyPort = ConfigDefaults.get().getProxyPort(); proxyHost = new HttpHost(proxyHostname, proxyPort); clientBuilder.setProxy(proxyHost); String proxyUsername = ConfigDefaults.get().getProxyUsername(); String proxyPassword = ConfigDefaults.get().getProxyPassword(); if (!StringUtils.isBlank(proxyUsername) && !StringUtils.isBlank(proxyPassword)) { Credentials proxyCredentials = new UsernamePasswordCredentials( proxyUsername, proxyPassword); credentialsProvider.setCredentials(new AuthScope(proxyHostname, proxyPort), proxyCredentials); } // Explicitly exclude the NTLM authentication scheme requestConfigBuilder = requestConfigBuilder.setProxyPreferredAuthSchemes( Arrays.asList(AuthSchemes.DIGEST, AuthSchemes.BASIC)); clientBuilder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy()); clientBuilder.setRoutePlanner(new CustomProxyRoutePlanner(proxyHost)); } // Read proxy exceptions from the "no_proxy" config option String noProxy = Config.get().getString(NO_PROXY); if (!StringUtils.isBlank(noProxy)) { for (String domain : Arrays.asList(noProxy.split(","))) { noProxyDomains.add(domain.toLowerCase().trim()); } } requestConfig = requestConfigBuilder.build(); clientBuilder.setMaxConnPerRoute(Config.get().getInt(MAX_CONNCECTIONS, 1)); clientBuilder.setMaxConnTotal(Config.get().getInt(MAX_CONNCECTIONS, 1)); httpClient = clientBuilder.build(); }
Example 19
Source File: PGPKeysServerClient.java From pgpverify-maven-plugin with Apache License 2.0 | 2 votes |
/** * Build an HTTP client with the given router planer. * * @param planer * The router planer for http client, used for load balancing * * @return The new HTTP client instance. */ private CloseableHttpClient buildClient(HttpRoutePlanner planer) { final HttpClientBuilder clientBuilder = this.createClientBuilder(); this.applyTimeouts(clientBuilder); clientBuilder.setRoutePlanner(planer); return clientBuilder.build(); }