Java Code Examples for org.apache.http.client.config.RequestConfig.Builder#setCookieSpec()

The following examples show how to use org.apache.http.client.config.RequestConfig.Builder#setCookieSpec() . 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: 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 2
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;
}