Java Code Examples for org.apache.http.client.config.RequestConfig.Builder#build()
The following examples show how to use
org.apache.http.client.config.RequestConfig.Builder#build() .
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: CFMetricsFetcher.java From promregator with Apache License 2.0 | 6 votes |
/** * creates a new Metrics Fetcher by defining the target endpoint where the metrics can be read, the instance identifier * of the instance, which shall be queried. * Additional configuration options can be provided using the CFMetricsFetcherConfig reference. * @param endpointUrl the endpoint URL, which shall be used to query the CF app for the Prometheus metrics. * @param instanceId the instance Id in format <i>[app guid]:[instance number]</i>, which identifies the instance uniquely. * @param config additional configurations specifying additional properties for retrieving data. */ public CFMetricsFetcher(String endpointUrl, String instanceId, CFMetricsFetcherConfig config) { this.endpointUrl = endpointUrl; this.instanceId = instanceId; this.ae = config.getAuthenticationEnricher(); this.mfse = config.getMetricFamilySamplesEnricher(); this.mfm = config.getMetricsFetcherMetrics(); this.up = config.getUpChild(); this.promregatorUUID = config.getPromregatorInstanceIdentifier(); Builder requestConfigBuilder = RequestConfig.custom() .setRedirectsEnabled(true) .setCircularRedirectsAllowed(false) .setMaxRedirects(10) .setSocketTimeout(config.getSocketReadTimeoutInMillis()) .setConnectTimeout(config.getConnectionTimeoutInMillis()); if (config.getProxyHost() != null && config.getProxyPort() != 0) { requestConfigBuilder = requestConfigBuilder.setProxy(new HttpHost(config.getProxyHost(), config.getProxyPort(), "http")); } this.config = requestConfigBuilder.build(); }
Example 2
Source File: FetchDataTest.java From BUbiNG with Apache License 2.0 | 5 votes |
public static CloseableHttpClient getHttpClient(final HttpHost proxy, final boolean redirects, final CookieStore cookieStore) { final Builder builder = RequestConfig.custom() .setRedirectsEnabled(redirects) .setMaxRedirects(5); if (proxy != null) builder.setProxy(proxy); final RequestConfig requestConfig = builder.build(); return HttpClients.custom() .setDefaultRequestConfig(requestConfig) .setDefaultCookieStore(cookieStore) .setConnectionReuseStrategy(NoConnectionReuseStrategy.INSTANCE) .build(); }
Example 3
Source File: AbstractSingleCheckThread.java From sitemonitoring-production with BSD 3-Clause "New" or "Revised" License | 5 votes |
private CloseableHttpResponse doRequest(final HttpRequestBase request) throws IOException { if (log.isDebugEnabled()) { log.debug(request.getMethod() + " " + request.getURI()); } Builder requestConfigBuilder = RequestConfig.custom().setSocketTimeout(check.getSocketTimeout()).setConnectTimeout(check.getConnectionTimeout()); if (check.getHttpProxyServer() != null && !check.getHttpProxyServer().isEmpty()) { HttpHost httpProxy = new HttpHost(check.getHttpProxyServer(), check.getHttpProxyPort()); requestConfigBuilder.setProxy(httpProxy); } RequestConfig requestConfig = requestConfigBuilder.build(); request.setConfig(requestConfig); String header = check.getHeader(); if(null!=header && header.length()>0 && header.contains(":")) { log.info("header:" + header); String[] headerKV = header.split(":"); request.setHeader(headerKV[0],headerKV[1]); } CloseableHttpResponse response = null; try { request.setHeader("User-Agent", check.getUserAgent()); response = httpClient.execute(request); } catch (SSLHandshakeException ex) { // ignore ValidatorException -> thrown when Java cannot validate // certificate log.error("java could not validate certificate for URL: " + request.getURI(), ex); return null; } if (log.isDebugEnabled()) { log.debug("status: " + response.getStatusLine()); } return response; }
Example 4
Source File: AbstractSingleCheckThread.java From sitemonitoring-production with BSD 3-Clause "New" or "Revised" License | 5 votes |
private CloseableHttpResponse doRequest(final HttpRequestBase request) throws IOException { if (log.isDebugEnabled()) { log.debug(request.getMethod() + " " + request.getURI()); } Builder requestConfigBuilder = RequestConfig.custom().setSocketTimeout(check.getSocketTimeout()).setConnectTimeout(check.getConnectionTimeout()); if (check.getHttpProxyServer() != null && !check.getHttpProxyServer().isEmpty()) { HttpHost httpProxy = new HttpHost(check.getHttpProxyServer(), check.getHttpProxyPort()); requestConfigBuilder.setProxy(httpProxy); } RequestConfig requestConfig = requestConfigBuilder.build(); request.setConfig(requestConfig); String header = check.getHeader(); if(null!=header && header.length()>0 && header.contains(":")) { log.info("header:" + header); String[] headerKV = header.split(":"); request.setHeader(headerKV[0],headerKV[1]); } CloseableHttpResponse response = null; try { request.setHeader("User-Agent", check.getUserAgent()); response = httpClient.execute(request); } catch (SSLHandshakeException ex) { // ignore ValidatorException -> thrown when Java cannot validate // certificate log.error("java could not validate certificate for URL: " + request.getURI(), ex); return null; } if (log.isDebugEnabled()) { log.debug("status: " + response.getStatusLine()); } return response; }
Example 5
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 6
Source File: HttpClientUtil.java From lucene-solr with Apache License 2.0 | 4 votes |
private static HttpClientBuilder setupBuilder(HttpClientBuilder builder, SolrParams config) { Builder requestConfigBuilder = RequestConfig.custom() .setRedirectsEnabled(config.getBool(HttpClientUtil.PROP_FOLLOW_REDIRECTS, false)).setDecompressionEnabled(false) .setConnectTimeout(config.getInt(HttpClientUtil.PROP_CONNECTION_TIMEOUT, DEFAULT_CONNECT_TIMEOUT)) .setSocketTimeout(config.getInt(HttpClientUtil.PROP_SO_TIMEOUT, DEFAULT_SO_TIMEOUT)); String cpolicy = cookiePolicy; if (cpolicy != null) { requestConfigBuilder.setCookieSpec(cpolicy); } RequestConfig requestConfig = requestConfigBuilder.build(); HttpClientBuilder retBuilder = builder.setDefaultRequestConfig(requestConfig); if (config.getBool(HttpClientUtil.PROP_USE_RETRY, true)) { retBuilder = retBuilder.setRetryHandler(new SolrHttpRequestRetryHandler(Integer.getInteger("solr.httpclient.retries", 3))); } else { retBuilder = retBuilder.setRetryHandler(NO_RETRY); } final String basicAuthUser = config.get(HttpClientUtil.PROP_BASIC_AUTH_USER); final String basicAuthPass = config.get(HttpClientUtil.PROP_BASIC_AUTH_PASS); if (basicAuthUser != null && basicAuthPass != null) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(basicAuthUser, basicAuthPass)); retBuilder.setDefaultCredentialsProvider(credsProvider); } if (config.getBool(HttpClientUtil.PROP_ALLOW_COMPRESSION, false)) { retBuilder.addInterceptorFirst(new UseCompressionRequestInterceptor()); retBuilder.addInterceptorFirst(new UseCompressionResponseInterceptor()); } else { retBuilder.disableContentCompression(); } return retBuilder; }