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())); }
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); 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()); }
public static HttpClientBuilder getDefaultClientBuilder(int connectTimeoutInSeconds) { RequestConfig config = RequestConfig .custom() .setConnectTimeout(connectTimeoutInSeconds * 1000) .setConnectionRequestTimeout(connectTimeoutInSeconds * 1000) .build(); SystemDefaultRoutePlanner routePlanner = new SystemDefaultRoutePlanner(ProxySelector.getDefault()); return HttpClients .custom() .setDefaultRequestConfig(config) .setRoutePlanner(routePlanner); }
private CloseableHttpClient configureHttpClient() { final String userAgentString = UI.BUNDLE.getString("useragent"); final ProxySelector proxySelector = ProxySelector.getDefault(); LOGGER.debug("Using proxy selector: {}", proxySelector); final SystemDefaultRoutePlanner routePlanner = new SystemDefaultRoutePlanner(proxySelector); final CloseableHttpClient client = HttpClientBuilder .create() .setUserAgent(userAgentString) .setRoutePlanner(routePlanner) .build(); LOGGER.info("ComeOn! uses \"{}\" as User-Agent", userAgentString); return client; }
@Override public CloseableHttpClient createHttpClient(HttpHost httpHost, boolean acceptAnyCertificate, HttpRequestInterceptor requestInterceptor, HttpResponseInterceptor responseInterceptor) { 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); } //add an interceptor that replaces the invalid Content-Type //'none' by 'identity' builder.addInterceptorFirst(new ContentEncodingNoneInterceptor()); //add interceptors if (requestInterceptor != null) { builder.addInterceptorLast(requestInterceptor); } if (responseInterceptor != null) { builder.addInterceptorLast(responseInterceptor); } CloseableHttpClient client = builder.build(); return client; }
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); SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory( sslContext); httpClientBuilder.setSSLSocketFactory(sslSocketFactory); httpClientBuilder.setSslcontext(sslContext); } RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(options.getConnectionTimeout()) .setSocketTimeout(options.getReadTimeout()) .setAuthenticationEnabled(true) .build(); httpClientBuilder.setDefaultRequestConfig(requestConfig); // Support redirects httpClientBuilder.setRedirectStrategy(new LaxRedirectStrategy()); // Fix weird problem `ProtocolException: Content-Length header already present` from `org.apache.http.protocol.RequestContent` httpClientBuilder.addInterceptorFirst(new HttpRequestInterceptor() { @Override public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { if (request instanceof HttpEntityEnclosingRequest) { request.removeHeaders(HTTP.TRANSFER_ENCODING); request.removeHeaders(HTTP.CONTENT_LEN); } } }); return new HttpComponentsClientHttpRequestFactory(httpClientBuilder.build()); }
@Override public SystemDefaultRoutePlanner proxy() { // implement? return null; }
@Override public SystemDefaultRoutePlanner proxy() { return null; }
/** * Returns a HTTP client doing Basic Auth if needed. * @param url The URL to browse. * @param user The user. Maybe null. * @param password The password. Maybe null. * @return The HTTP client. */ public static CloseableHttpClient getClient( URL url, String user, String password ) { int timeout = DEFAULT_TIMEOUT; ApplicationSettings set = Config .getInstance() .getApplicationSettings(); String ts = set.getApplicationSetting("requestTimeout_s"); if (ts != null) { try { timeout = S_TO_MS * Integer.parseInt(ts); } catch (NumberFormatException nfe) { log.log(Level.SEVERE, nfe.getMessage()); } } // Use JVM proxy settings. SystemDefaultRoutePlanner routePlanner = new SystemDefaultRoutePlanner(ProxySelector.getDefault()); RequestConfig requestConfig = RequestConfig. custom(). setSocketTimeout(timeout).build(); HttpClientBuilder builder = HttpClientBuilder.create() .setDefaultRequestConfig(requestConfig); builder.setRetryHandler(new StandardHttpRequestRetryHandler(1, true)); builder.setRoutePlanner(routePlanner); builder.setUserAgent(getUserAgent()); if (user != null && password != null) { UsernamePasswordCredentials defaultCreds = new UsernamePasswordCredentials(user, password); BasicCredentialsProvider credsProv = new BasicCredentialsProvider(); credsProv.setCredentials( new AuthScope(url.getHost(), url.getPort()), defaultCreds); HttpClientContext context = HttpClientContext.create(); context.setCredentialsProvider(credsProv); BasicAuthCache authCache = new BasicAuthCache(); BasicScheme basicAuth = new BasicScheme(); HttpHost target = new HttpHost(url.getHost(), url.getPort()); authCache.put(target, basicAuth); context.setAuthCache(authCache); builder.setDefaultCredentialsProvider(credsProv); } return builder.build(); }
protected HttpRoutePlanner getRoutePlanner() { return new SystemDefaultRoutePlanner(ProxySelector.getDefault()); }
private static HttpRoutePlanner createProxyRoutePlanner() { // use the standard JRE ProxySelector to get proxy information Message.verbose("Using JRE standard ProxySelector for configuring HTTP proxy"); return new SystemDefaultRoutePlanner(ProxySelector.getDefault()); }
/** * Build an HttpClient * * @param customiser * * @return */ public CloseableHttpClient createHttpClient(final Consumer<HttpClientBuilder> customiser) { final HttpClientBuilder builder = HttpClientBuilder.create(); // By default set long call timeouts { RequestConfig.Builder requestBuilder = RequestConfig.custom(); requestBuilder.setConnectTimeout((int) connectionTimeout.getMilliseconds()) .setSocketTimeout((int) socketTimeout.getMilliseconds()); builder.setDefaultRequestConfig(requestBuilder.build()); } // Set the default keepalive setting if (noKeepalive) builder.setConnectionReuseStrategy(new NoConnectionReuseStrategy()); // By default share the common connection provider builder.setConnectionManager(connectionManager); // By default use the JRE default route planner for proxies builder.setRoutePlanner(new SystemDefaultRoutePlanner(ProxySelector.getDefault())); // If a correlation id is set locally then make sure we pass it along to the remote service // N.B. we use the value from the MDC because the correlation id could be for a internal task builder.addInterceptorFirst(new HttpRequestInterceptor() { @Override public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { final String traceId = MDC.get(LoggingMDCConstants.TRACE_ID); if (traceId != null) request.addHeader("X-Correlation-ID", traceId); } }); // Allow customisation if (customiser != null) customiser.accept(builder); return builder.build(); }