Example usage for org.apache.http.impl.client HttpClientBuilder setDefaultCookieSpecRegistry

List of usage examples for org.apache.http.impl.client HttpClientBuilder setDefaultCookieSpecRegistry

Introduction

In this page you can find the example usage for org.apache.http.impl.client HttpClientBuilder setDefaultCookieSpecRegistry.

Prototype

public final HttpClientBuilder setDefaultCookieSpecRegistry(
        final Lookup<CookieSpecProvider> cookieSpecRegistry) 

Source Link

Document

Assigns default org.apache.http.cookie.CookieSpec registry which will be used for request execution if not explicitly set in the client execution context.

Usage

From source file:org.jboss.as.test.http.util.TestHttpClientUtils.java

/**
 * Same as {@link TestHttpClientUtils#promiscuousCookieHttpClient()} but instead returns a builder that can be further configured.
 *
 * @return {@link HttpClientBuilder} of the http client that gives free cookies to everybody
 * @see TestHttpClientUtils#promiscuousCookieHttpClient()
 *//*from   w w w .  ja va  2 s  . c  o m*/
public static HttpClientBuilder promiscuousCookieHttpClientBuilder() {
    HttpClientBuilder builder = HttpClients.custom();

    RegistryBuilder<CookieSpecProvider> registryBuilder = CookieSpecRegistries.createDefaultBuilder();
    Registry<CookieSpecProvider> promiscuousCookieSpecRegistry = registryBuilder
            .register("promiscuous", new PromiscuousCookieSpecProvider()).build();
    builder.setDefaultCookieSpecRegistry(promiscuousCookieSpecRegistry);

    RequestConfig requestConfig = RequestConfig.custom().setCookieSpec("promiscuous").build();
    builder.setDefaultRequestConfig(requestConfig);

    builder.setDefaultCookieStore(new PromiscuousCookieStore());

    return builder;
}

From source file:com.mirth.connect.util.HttpUtil.java

/**
 * Applies global settings to any Apache HttpComponents HttpClientBuilder.<br/>
 * <br/>/*from www .  j  a  v  a 2 s  .  c  o m*/
 * As of version 4.5, the default cookie specifications used by Apache HttpClient are more
 * strict in order to abide by RFC 6265. As part of this change, domain parameters in cookies
 * are checked against a public ICANN suffix matcher before they are allowed to be added to
 * outgoing requests. However this may cause clients to fail to connect if they are using custom
 * hostnames like "mycustomhost". To prevent that, we're building our own CookieSpecProvider
 * registry, and setting that on the client. Look at CookieSpecRegistries to see how the default
 * registry is built. The only difference now is that DefaultCookieSpecProvider is being
 * constructed without a PublicSuffixMatcher.
 */
public static void configureClientBuilder(HttpClientBuilder clientBuilder) {
    PublicSuffixMatcher publicSuffixMatcher = PublicSuffixMatcherLoader.getDefault();
    clientBuilder.setPublicSuffixMatcher(publicSuffixMatcher);
    RegistryBuilder<CookieSpecProvider> cookieSpecBuilder = RegistryBuilder.<CookieSpecProvider>create();
    CookieSpecProvider defaultProvider = new DefaultCookieSpecProvider();
    CookieSpecProvider laxStandardProvider = new RFC6265CookieSpecProvider(
            RFC6265CookieSpecProvider.CompatibilityLevel.RELAXED, publicSuffixMatcher);
    CookieSpecProvider strictStandardProvider = new RFC6265CookieSpecProvider(
            RFC6265CookieSpecProvider.CompatibilityLevel.STRICT, publicSuffixMatcher);
    cookieSpecBuilder.register(CookieSpecs.DEFAULT, defaultProvider);
    cookieSpecBuilder.register("best-match", defaultProvider);
    cookieSpecBuilder.register("compatibility", defaultProvider);
    cookieSpecBuilder.register(CookieSpecs.STANDARD, laxStandardProvider);
    cookieSpecBuilder.register(CookieSpecs.STANDARD_STRICT, strictStandardProvider);
    cookieSpecBuilder.register(CookieSpecs.NETSCAPE, new NetscapeDraftSpecProvider());
    cookieSpecBuilder.register(CookieSpecs.IGNORE_COOKIES, new IgnoreSpecProvider());
    clientBuilder.setDefaultCookieSpecRegistry(cookieSpecBuilder.build());
}

From source file:org.apache.solr.client.solrj.impl.HttpClientUtil.java

/**
 * Creates new http client by using the provided configuration.
 * // ww w .  j a  va2  s  . c o  m
 */
public static CloseableHttpClient createClient(final SolrParams params, PoolingHttpClientConnectionManager cm,
        boolean sharedConnectionManager) {
    final ModifiableSolrParams config = new ModifiableSolrParams(params);
    if (logger.isDebugEnabled()) {
        logger.debug("Creating new http client, config:" + config);
    }

    cm.setMaxTotal(params.getInt(HttpClientUtil.PROP_MAX_CONNECTIONS, 10000));
    cm.setDefaultMaxPerRoute(params.getInt(HttpClientUtil.PROP_MAX_CONNECTIONS_PER_HOST, 10000));
    cm.setValidateAfterInactivity(
            Integer.getInteger(VALIDATE_AFTER_INACTIVITY, VALIDATE_AFTER_INACTIVITY_DEFAULT));

    HttpClientBuilder newHttpClientBuilder = HttpClientBuilder.create();

    if (sharedConnectionManager) {
        newHttpClientBuilder.setConnectionManagerShared(true);
    } else {
        newHttpClientBuilder.setConnectionManagerShared(false);
    }

    ConnectionKeepAliveStrategy keepAliveStrat = new ConnectionKeepAliveStrategy() {
        @Override
        public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
            // we only close connections based on idle time, not ttl expiration
            return -1;
        }
    };

    if (httpClientBuilder.getAuthSchemeRegistryProvider() != null) {
        newHttpClientBuilder.setDefaultAuthSchemeRegistry(
                httpClientBuilder.getAuthSchemeRegistryProvider().getAuthSchemeRegistry());
    }
    if (httpClientBuilder.getCookieSpecRegistryProvider() != null) {
        newHttpClientBuilder.setDefaultCookieSpecRegistry(
                httpClientBuilder.getCookieSpecRegistryProvider().getCookieSpecRegistry());
    }
    if (httpClientBuilder.getCredentialsProviderProvider() != null) {
        newHttpClientBuilder.setDefaultCredentialsProvider(
                httpClientBuilder.getCredentialsProviderProvider().getCredentialsProvider());
    }

    newHttpClientBuilder.addInterceptorLast(new DynamicInterceptor());

    newHttpClientBuilder = newHttpClientBuilder.setKeepAliveStrategy(keepAliveStrat).evictIdleConnections(
            (long) Integer.getInteger(EVICT_IDLE_CONNECTIONS, EVICT_IDLE_CONNECTIONS_DEFAULT),
            TimeUnit.MILLISECONDS);

    HttpClientBuilder builder = setupBuilder(newHttpClientBuilder, params);

    HttpClient httpClient = builder.setConnectionManager(cm).build();

    assert ObjectReleaseTracker.track(httpClient);
    return (CloseableHttpClient) httpClient;
}

From source file:com.gargoylesoftware.htmlunit.HttpWebConnection.java

/**
 * Lazily initializes the internal HTTP client.
 *
 * @return the initialized HTTP client/*from  w w w . j  a  v a 2  s  .c  o  m*/
 */
protected HttpClientBuilder getHttpClientBuilder() {
    HttpClientBuilder builder = httpClientBuilder_.get();
    if (builder == null) {
        builder = createHttpClient();

        // this factory is required later
        // to be sure this is done, we do it outside the createHttpClient() call
        final RegistryBuilder<CookieSpecProvider> registeryBuilder = RegistryBuilder
                .<CookieSpecProvider>create().register(HACKED_COOKIE_POLICY, htmlUnitCookieSpecProvider_);
        builder.setDefaultCookieSpecRegistry(registeryBuilder.build());

        builder.setDefaultCookieStore(new HtmlUnitCookieStore(webClient_.getCookieManager()));
        httpClientBuilder_.set(builder);
    }

    return builder;
}