org.apache.http.client.config.RequestConfig.Builder Java Examples

The following examples show how to use org.apache.http.client.config.RequestConfig.Builder. 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 vote down vote up
/**
 * 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: ElasticSearchClientFactory.java    From ache with Apache License 2.0 6 votes vote down vote up
public static RestClient createClient(ElasticSearchConfig config) {

        HttpHost[] httpHosts = parseHostAddresses(config.getRestApiHosts());

        RestClient client = RestClient.builder(httpHosts)
                .setRequestConfigCallback(new RequestConfigCallback() {
                    @Override
                    public Builder customizeRequestConfig(
                            Builder requestConfigBuilder) {
                        return requestConfigBuilder
                                .setConnectTimeout(config.getRestConnectTimeout())
                                .setSocketTimeout(config.getRestSocketTimeout());
                    }
                })
                .setMaxRetryTimeoutMillis(config.getRestMaxRetryTimeoutMillis())
                .build();

        logger.info("Initialized Elasticsearch REST client for: " + Arrays.toString(httpHosts));
        return client;
    }
 
Example #3
Source File: FetchDataTest.java    From BUbiNG with Apache License 2.0 5 votes vote down vote up
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 #4
Source File: HttpClientUtil.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public static Builder createDefaultRequestConfigBuilder() {
  String cpolicy = cookiePolicy;
  Builder builder = RequestConfig.custom();

  builder.setSocketTimeout(DEFAULT_SO_TIMEOUT)
      .setConnectTimeout(DEFAULT_CONNECT_TIMEOUT)
      .setRedirectsEnabled(false)
      .setDecompressionEnabled(false); // we do our own compression / decompression
  if (cpolicy != null) {
    builder.setCookieSpec(cpolicy);
  }
  return builder;
}
 
Example #5
Source File: AbstractSingleCheckThread.java    From sitemonitoring-production with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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 #6
Source File: AbstractSingleCheckThread.java    From sitemonitoring-production with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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 #7
Source File: ConsulUtils.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private static HttpClient createHttpClient(String clientCert, String clientKey, String serverCert) throws Exception {
    SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
            .loadTrustMaterial(KeyStoreUtil.createTrustStore(serverCert), null)
            .loadKeyMaterial(KeyStoreUtil.createKeyStore(clientCert, clientKey), "consul".toCharArray())
            .build();
    try (PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(setupSchemeRegistry(sslContext))) {
        connectionManager.setMaxTotal(MAX_CONNECTION);
        connectionManager.setDefaultMaxPerRoute(MAX_ROUTE);
        Builder requestBuilder = RequestConfig.custom()
                .setConnectTimeout(DEFAULT_TIMEOUT_MS).setConnectionRequestTimeout(DEFAULT_TIMEOUT_MS).setSocketTimeout(DEFAULT_TIMEOUT_MS);
        return HttpClientBuilder.create().setConnectionManager(connectionManager).setDefaultRequestConfig(requestBuilder.build()).build();
    }
}
 
Example #8
Source File: LongPollChannel.java    From joynr with Apache License 2.0 5 votes vote down vote up
public void setChannelUrl(String channelUrl) {
    this.httpget = httpRequestFactory.createHttpGet(URI.create(channelUrl));
    Builder requestConfigBuilder = RequestConfig.copy(defaultRequestConfig);
    httpget.setConfig(requestConfigBuilder.build());
    httpget.setHeader(httpConstants.getHEADER_X_ATMOSPHERE_TRACKING_ID(), receiverId);
    if (channelUrl.length() > 15) {
        this.id = "..." + channelUrl.substring(channelUrl.length() - 15);
    } else {
        this.id = channelUrl;
    }

}
 
Example #9
Source File: HttpClientFactory.java    From vividus with Apache License 2.0 4 votes vote down vote up
@Override
public IHttpClient buildHttpClient(HttpClientConfig config)
{
    HttpClientBuilder builder = HttpClientBuilder.create();
    builder.setDefaultHeaders(config.createHeaders());
    if (config.hasCookieStore())
    {
        builder.setDefaultCookieStore(config.getCookieStore());
    }
    if (config.hasCredentials())
    {
        AuthScope authScope = config.hasAuthScope() ? config.getAuthScope()
                : ClientBuilderUtils.DEFAULT_AUTH_SCOPE;
        CredentialsProvider credProvider = ClientBuilderUtils.createCredentialsProvider(authScope,
                config.getCredentials());
        builder.setDefaultCredentialsProvider(credProvider);
    }

    sslContextFactory.getSslContext(SSLConnectionSocketFactory.SSL, !config.isSslCertificateCheckEnabled())
            .ifPresent(builder::setSSLContext);

    if (!config.isSslHostnameVerificationEnabled())
    {
        builder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE);
    }
    builder.setConnectionManager(config.getConnectionManager());
    builder.setMaxConnTotal(config.getMaxTotalConnections());
    builder.setMaxConnPerRoute(config.getMaxConnectionsPerRoute());
    builder.addInterceptorLast(config.getLastRequestInterceptor());
    builder.addInterceptorLast(config.getLastResponseInterceptor());
    builder.setRedirectStrategy(config.getRedirectStrategy());
    builder.setRetryHandler(config.getHttpRequestRetryHandler());
    Builder requestConfigBuilder = RequestConfig.custom();
    requestConfigBuilder.setConnectionRequestTimeout(config.getConnectionRequestTimeout());
    requestConfigBuilder.setConnectTimeout(config.getConnectTimeout());
    requestConfigBuilder.setCircularRedirectsAllowed(config.isCircularRedirectsAllowed());
    requestConfigBuilder.setSocketTimeout(config.getSocketTimeout());
    Optional.ofNullable(config.getCookieSpec()).ifPresent(requestConfigBuilder::setCookieSpec);
    builder.setDefaultRequestConfig(requestConfigBuilder.build());
    builder.setDefaultSocketConfig(SocketConfig.copy(SocketConfig.DEFAULT)
            .setSoTimeout(config.getSocketTimeout())
            .build());
    builder.setDnsResolver(config.getDnsResolver());

    HttpClient httpClient = new HttpClient();
    httpClient.setCloseableHttpClient(builder.build());
    if (config.hasBaseUrl())
    {
        httpClient.setHttpHost(HttpHost.create(config.getBaseUrl()));
    }
    httpClient.setSkipResponseEntity(config.isSkipResponseEntity());
    return httpClient;
}
 
Example #10
Source File: HttpClientAdapter.java    From uyuni with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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 #11
Source File: HttpClientUtil.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example #12
Source File: ElasticRestClientImpl.java    From jframe with Apache License 2.0 4 votes vote down vote up
@Override
public Builder customizeRequestConfig(Builder requestConfigBuilder) {
    int sTimtout = Integer.parseInt(Plugin.getConfig(HTTP_SOCKET_TIMEOUT, "30000"));
    int cTimeout = Integer.parseInt(Plugin.getConfig(HTTP_CONN_TIMEOUT, "1000"));
    return requestConfigBuilder.setSocketTimeout(sTimtout).setConnectTimeout(cTimeout);
}