Search in sources :

Example 1 with CookieSpecProvider

use of org.apache.http.cookie.CookieSpecProvider in project fess-crawler by codelibs.

the class HcHttpClient method buildCookieSpecRegistry.

protected Lookup<CookieSpecProvider> buildCookieSpecRegistry() {
    if (cookieSpecRegistry != null) {
        return cookieSpecRegistry;
    }
    final PublicSuffixMatcher publicSuffixMatcher = PublicSuffixMatcherLoader.getDefault();
    final CookieSpecProvider defaultProvider = new DefaultCookieSpecProvider(CompatibilityLevel.DEFAULT, publicSuffixMatcher, cookieDatePatterns, false);
    final CookieSpecProvider laxStandardProvider = new RFC6265CookieSpecProvider(RFC6265CookieSpecProvider.CompatibilityLevel.RELAXED, publicSuffixMatcher);
    final CookieSpecProvider strictStandardProvider = new RFC6265CookieSpecProvider(RFC6265CookieSpecProvider.CompatibilityLevel.STRICT, publicSuffixMatcher);
    return // 
    RegistryBuilder.<CookieSpecProvider>create().register(CookieSpecs.DEFAULT, // 
    defaultProvider).register("best-match", // 
    defaultProvider).register("compatibility", // 
    defaultProvider).register(CookieSpecs.STANDARD, // 
    laxStandardProvider).register(CookieSpecs.STANDARD_STRICT, // 
    strictStandardProvider).register(CookieSpecs.NETSCAPE, // 
    new NetscapeDraftSpecProvider()).register(CookieSpecs.IGNORE_COOKIES, // 
    new IgnoreSpecProvider()).build();
}
Also used : RFC6265CookieSpecProvider(org.apache.http.impl.cookie.RFC6265CookieSpecProvider) IgnoreSpecProvider(org.apache.http.impl.cookie.IgnoreSpecProvider) CookieSpecProvider(org.apache.http.cookie.CookieSpecProvider) DefaultCookieSpecProvider(org.apache.http.impl.cookie.DefaultCookieSpecProvider) RFC6265CookieSpecProvider(org.apache.http.impl.cookie.RFC6265CookieSpecProvider) NetscapeDraftSpecProvider(org.apache.http.impl.cookie.NetscapeDraftSpecProvider) PublicSuffixMatcher(org.apache.http.conn.util.PublicSuffixMatcher) DefaultCookieSpecProvider(org.apache.http.impl.cookie.DefaultCookieSpecProvider)

Example 2 with CookieSpecProvider

use of org.apache.http.cookie.CookieSpecProvider in project gradle by gradle.

the class HttpClientConfigurer method configureCookieSpecRegistry.

private void configureCookieSpecRegistry(HttpClientBuilder builder) {
    PublicSuffixMatcher publicSuffixMatcher = PublicSuffixMatcherLoader.getDefault();
    builder.setPublicSuffixMatcher(publicSuffixMatcher);
    // Add more data patterns to the default configuration to work around https://github.com/gradle/gradle/issues/1596
    final CookieSpecProvider defaultProvider = new DefaultCookieSpecProvider(DefaultCookieSpecProvider.CompatibilityLevel.DEFAULT, publicSuffixMatcher, new String[] { // Netscape expires pattern
    "EEE, dd-MMM-yy HH:mm:ss z", DateUtils.PATTERN_RFC1036, DateUtils.PATTERN_ASCTIME, DateUtils.PATTERN_RFC1123 }, false);
    final CookieSpecProvider laxStandardProvider = new RFC6265CookieSpecProvider(RFC6265CookieSpecProvider.CompatibilityLevel.RELAXED, publicSuffixMatcher);
    final CookieSpecProvider strictStandardProvider = new RFC6265CookieSpecProvider(RFC6265CookieSpecProvider.CompatibilityLevel.STRICT, publicSuffixMatcher);
    builder.setDefaultCookieSpecRegistry(RegistryBuilder.<CookieSpecProvider>create().register(CookieSpecs.DEFAULT, defaultProvider).register("best-match", defaultProvider).register("compatibility", defaultProvider).register(CookieSpecs.STANDARD, laxStandardProvider).register(CookieSpecs.STANDARD_STRICT, strictStandardProvider).register(CookieSpecs.NETSCAPE, new NetscapeDraftSpecProvider()).register(CookieSpecs.IGNORE_COOKIES, new IgnoreSpecProvider()).build());
}
Also used : RFC6265CookieSpecProvider(org.apache.http.impl.cookie.RFC6265CookieSpecProvider) IgnoreSpecProvider(org.apache.http.impl.cookie.IgnoreSpecProvider) CookieSpecProvider(org.apache.http.cookie.CookieSpecProvider) DefaultCookieSpecProvider(org.apache.http.impl.cookie.DefaultCookieSpecProvider) RFC6265CookieSpecProvider(org.apache.http.impl.cookie.RFC6265CookieSpecProvider) NetscapeDraftSpecProvider(org.apache.http.impl.cookie.NetscapeDraftSpecProvider) PublicSuffixMatcher(org.apache.http.conn.util.PublicSuffixMatcher) DefaultCookieSpecProvider(org.apache.http.impl.cookie.DefaultCookieSpecProvider)

Example 3 with CookieSpecProvider

use of org.apache.http.cookie.CookieSpecProvider in project lucene-solr by apache.

the class Krb5HttpClientBuilder method getBuilder.

public SolrHttpClientBuilder getBuilder(SolrHttpClientBuilder builder) {
    if (System.getProperty(LOGIN_CONFIG_PROP) != null) {
        String configValue = System.getProperty(LOGIN_CONFIG_PROP);
        if (configValue != null) {
            logger.info("Setting up SPNego auth with config: " + configValue);
            final String useSubjectCredsProp = "javax.security.auth.useSubjectCredsOnly";
            String useSubjectCredsVal = System.getProperty(useSubjectCredsProp);
            // authentication mechanism can load the credentials from the JAAS configuration.
            if (useSubjectCredsVal == null) {
                System.setProperty(useSubjectCredsProp, "false");
            } else if (!useSubjectCredsVal.toLowerCase(Locale.ROOT).equals("false")) {
                // Don't overwrite the prop value if it's already been written to something else,
                // but log because it is likely the Credentials won't be loaded correctly.
                logger.warn("System Property: " + useSubjectCredsProp + " set to: " + useSubjectCredsVal + " not false.  SPNego authentication may not be successful.");
            }
            javax.security.auth.login.Configuration.setConfiguration(jaasConfig);
            //Enable only SPNEGO authentication scheme.
            builder.setAuthSchemeRegistryProvider(() -> {
                Lookup<AuthSchemeProvider> authProviders = RegistryBuilder.<AuthSchemeProvider>create().register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory(true, false)).build();
                return authProviders;
            });
            // Get the credentials from the JAAS configuration rather than here
            Credentials useJaasCreds = new Credentials() {

                public String getPassword() {
                    return null;
                }

                public Principal getUserPrincipal() {
                    return null;
                }
            };
            HttpClientUtil.setCookiePolicy(SolrPortAwareCookieSpecFactory.POLICY_NAME);
            builder.setCookieSpecRegistryProvider(() -> {
                SolrPortAwareCookieSpecFactory cookieFactory = new SolrPortAwareCookieSpecFactory();
                Lookup<CookieSpecProvider> cookieRegistry = RegistryBuilder.<CookieSpecProvider>create().register(SolrPortAwareCookieSpecFactory.POLICY_NAME, cookieFactory).build();
                return cookieRegistry;
            });
            builder.setDefaultCredentialsProvider(() -> {
                CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
                credentialsProvider.setCredentials(AuthScope.ANY, useJaasCreds);
                return credentialsProvider;
            });
            HttpClientUtil.addRequestInterceptor(bufferedEntityInterceptor);
        }
    } else {
        logger.warn("{} is configured without specifying system property '{}'", getClass().getName(), LOGIN_CONFIG_PROP);
    }
    return builder;
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CookieSpecProvider(org.apache.http.cookie.CookieSpecProvider) AuthSchemeProvider(org.apache.http.auth.AuthSchemeProvider) SPNegoSchemeFactory(org.apache.http.impl.auth.SPNegoSchemeFactory) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) Credentials(org.apache.http.auth.Credentials)

Example 4 with CookieSpecProvider

use of org.apache.http.cookie.CookieSpecProvider in project wildfly by wildfly.

the class TestHttpClientUtils method promiscuousCookieHttpClientBuilder.

/**
 * 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()
 */
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;
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CookieSpecProvider(org.apache.http.cookie.CookieSpecProvider) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder)

Example 5 with CookieSpecProvider

use of org.apache.http.cookie.CookieSpecProvider in project jmeter by apache.

the class HTTPHC4Impl method setupClient.

private MutableTriple<CloseableHttpClient, AuthState, PoolingHttpClientConnectionManager> setupClient(HttpClientKey key, JMeterVariables jMeterVariables, HttpClientContext clientContext) throws GeneralSecurityException {
    Map<HttpClientKey, MutableTriple<CloseableHttpClient, AuthState, PoolingHttpClientConnectionManager>> mapHttpClientPerHttpClientKey = HTTPCLIENTS_CACHE_PER_THREAD_AND_HTTPCLIENTKEY.get();
    clientContext.setAttribute(CONTEXT_ATTRIBUTE_CLIENT_KEY, key);
    CloseableHttpClient httpClient = null;
    MutableTriple<CloseableHttpClient, AuthState, PoolingHttpClientConnectionManager> triple = null;
    boolean concurrentDwn = this.testElement.isConcurrentDwn();
    Map<String, Object> samplerContext = JMeterContextService.getContext().getSamplerContext();
    if (concurrentDwn) {
        triple = (MutableTriple<CloseableHttpClient, AuthState, PoolingHttpClientConnectionManager>) samplerContext.get(CONTEXT_ATTRIBUTE_PARENT_SAMPLE_CLIENT_STATE);
    }
    if (triple == null) {
        triple = mapHttpClientPerHttpClientKey.get(key);
    }
    if (triple != null) {
        httpClient = triple.getLeft();
    }
    setupProxyAuthState(triple, clientContext);
    resetStateIfNeeded(triple, jMeterVariables, clientContext, mapHttpClientPerHttpClientKey);
    if (httpClient == null) {
        // One-time init for this client
        DnsResolver resolver = this.testElement.getDNSResolver();
        if (resolver == null) {
            resolver = SystemDefaultDnsResolver.INSTANCE;
        }
        Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("https", new LazyLayeredConnectionSocketFactory()).register("http", CONNECTION_SOCKET_FACTORY).build();
        // Modern browsers use more connections per host than the current httpclient default (2)
        // when using parallel download the httpclient and connection manager are shared by the downloads threads
        // to be realistic JMeter must set an higher value to DefaultMaxPerRoute
        PoolingHttpClientConnectionManager pHCCM = new PoolingHttpClientConnectionManager(new JMeterDefaultHttpClientConnectionOperator(registry, null, resolver), null, TIME_TO_LIVE, TimeUnit.MILLISECONDS);
        pHCCM.setValidateAfterInactivity(VALIDITY_AFTER_INACTIVITY_TIMEOUT);
        if (concurrentDwn) {
            try {
                int maxConcurrentDownloads = Integer.parseInt(this.testElement.getConcurrentPool());
                pHCCM.setDefaultMaxPerRoute(Math.max(maxConcurrentDownloads, pHCCM.getDefaultMaxPerRoute()));
            } catch (NumberFormatException nfe) {
            // no need to log -> will be done by the sampler
            }
        }
        CookieSpecProvider cookieSpecProvider = new IgnoreSpecProvider();
        Lookup<CookieSpecProvider> cookieSpecRegistry = RegistryBuilder.<CookieSpecProvider>create().register(CookieSpecs.IGNORE_COOKIES, cookieSpecProvider).build();
        HttpClientBuilder builder = HttpClients.custom().setConnectionManager(pHCCM).setSchemePortResolver(new DefaultSchemePortResolver()).setDnsResolver(resolver).setRequestExecutor(REQUEST_EXECUTOR).setSSLSocketFactory(new LazyLayeredConnectionSocketFactory()).setDefaultCookieSpecRegistry(cookieSpecRegistry).setDefaultSocketConfig(SocketConfig.DEFAULT).setRedirectStrategy(new LaxRedirectStrategy()).setConnectionTimeToLive(TIME_TO_LIVE, TimeUnit.MILLISECONDS).setRetryHandler(new StandardHttpRequestRetryHandler(RETRY_COUNT, REQUEST_SENT_RETRY_ENABLED)).setConnectionReuseStrategy(DefaultClientConnectionReuseStrategy.INSTANCE).setProxyAuthenticationStrategy(getProxyAuthStrategy());
        if (DISABLE_DEFAULT_UA) {
            builder.disableDefaultUserAgent();
        }
        Lookup<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create().register(AuthSchemes.BASIC, new BasicSchemeFactory()).register(AuthSchemes.DIGEST, new DigestSchemeFactory()).register(AuthSchemes.NTLM, new NTLMSchemeFactory()).register(AuthSchemes.SPNEGO, new DynamicSPNegoSchemeFactory(AuthManager.STRIP_PORT, AuthManager.USE_CANONICAL_HOST_NAME)).register(AuthSchemes.KERBEROS, new DynamicKerberosSchemeFactory(AuthManager.STRIP_PORT, AuthManager.USE_CANONICAL_HOST_NAME)).build();
        builder.setDefaultAuthSchemeRegistry(authSchemeRegistry);
        if (IDLE_TIMEOUT > 0) {
            builder.setKeepAliveStrategy(IDLE_STRATEGY);
        }
        // Set up proxy details
        AuthScope proxyAuthScope = null;
        NTCredentials proxyCredentials = null;
        if (key.hasProxy) {
            HttpHost proxy = new HttpHost(key.proxyHost, key.proxyPort, key.proxyScheme);
            builder.setProxy(proxy);
            CredentialsProvider credsProvider = new BasicCredentialsProvider();
            if (!key.proxyUser.isEmpty()) {
                proxyAuthScope = new AuthScope(key.proxyHost, key.proxyPort);
                proxyCredentials = new NTCredentials(key.proxyUser, key.proxyPass, LOCALHOST, PROXY_DOMAIN);
                credsProvider.setCredentials(proxyAuthScope, proxyCredentials);
            }
            builder.setDefaultCredentialsProvider(credsProvider);
        }
        builder.disableContentCompression().addInterceptorLast(RESPONSE_CONTENT_ENCODING);
        if (BASIC_AUTH_PREEMPTIVE) {
            builder.addInterceptorFirst(PREEMPTIVE_AUTH_INTERCEPTOR);
        } else {
            builder.setDefaultCredentialsProvider(new ManagedCredentialsProvider(getAuthManager(), proxyAuthScope, proxyCredentials));
        }
        httpClient = builder.build();
        if (log.isDebugEnabled()) {
            log.debug("Created new HttpClient: @{} {}", System.identityHashCode(httpClient), key);
        }
        triple = MutableTriple.of(httpClient, null, pHCCM);
        // save the agent for next time round
        mapHttpClientPerHttpClientKey.put(key, triple);
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Reusing the HttpClient: @{} {}", System.identityHashCode(httpClient), key);
        }
    }
    if (concurrentDwn) {
        samplerContext.put(CONTEXT_ATTRIBUTE_PARENT_SAMPLE_CLIENT_STATE, triple);
    }
    return triple;
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) BasicSchemeFactory(org.apache.http.impl.auth.BasicSchemeFactory) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) NTCredentials(org.apache.http.auth.NTCredentials) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) LazyLayeredConnectionSocketFactory(org.apache.jmeter.protocol.http.sampler.hc.LazyLayeredConnectionSocketFactory) SlowHCPlainConnectionSocketFactory(org.apache.jmeter.protocol.http.util.SlowHCPlainConnectionSocketFactory) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) IgnoreSpecProvider(org.apache.http.impl.cookie.IgnoreSpecProvider) HttpHost(org.apache.http.HttpHost) DigestSchemeFactory(org.apache.http.impl.auth.DigestSchemeFactory) LaxRedirectStrategy(org.apache.http.impl.client.LaxRedirectStrategy) MutableTriple(org.apache.commons.lang3.tuple.MutableTriple) DynamicSPNegoSchemeFactory(org.apache.jmeter.protocol.http.control.DynamicSPNegoSchemeFactory) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) SystemDefaultDnsResolver(org.apache.http.impl.conn.SystemDefaultDnsResolver) DnsResolver(org.apache.http.conn.DnsResolver) LazyLayeredConnectionSocketFactory(org.apache.jmeter.protocol.http.sampler.hc.LazyLayeredConnectionSocketFactory) DynamicKerberosSchemeFactory(org.apache.jmeter.protocol.http.control.DynamicKerberosSchemeFactory) CredentialsProvider(org.apache.http.client.CredentialsProvider) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) StandardHttpRequestRetryHandler(org.apache.http.impl.client.StandardHttpRequestRetryHandler) AuthState(org.apache.http.auth.AuthState) DefaultSchemePortResolver(org.apache.http.impl.conn.DefaultSchemePortResolver) AuthScope(org.apache.http.auth.AuthScope) CookieSpecProvider(org.apache.http.cookie.CookieSpecProvider) AuthSchemeProvider(org.apache.http.auth.AuthSchemeProvider) NTLMSchemeFactory(org.apache.http.impl.auth.NTLMSchemeFactory)

Aggregations

CookieSpecProvider (org.apache.http.cookie.CookieSpecProvider)6 AuthSchemeProvider (org.apache.http.auth.AuthSchemeProvider)3 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)3 HttpHost (org.apache.http.HttpHost)2 AuthScope (org.apache.http.auth.AuthScope)2 Credentials (org.apache.http.auth.Credentials)2 CredentialsProvider (org.apache.http.client.CredentialsProvider)2 RequestConfig (org.apache.http.client.config.RequestConfig)2 PublicSuffixMatcher (org.apache.http.conn.util.PublicSuffixMatcher)2 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)2 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)2 DefaultCookieSpecProvider (org.apache.http.impl.cookie.DefaultCookieSpecProvider)2 IgnoreSpecProvider (org.apache.http.impl.cookie.IgnoreSpecProvider)2 NetscapeDraftSpecProvider (org.apache.http.impl.cookie.NetscapeDraftSpecProvider)2 RFC6265CookieSpecProvider (org.apache.http.impl.cookie.RFC6265CookieSpecProvider)2 IOException (java.io.IOException)1 ConnectException (java.net.ConnectException)1 MalformedURLException (java.net.MalformedURLException)1 NoRouteToHostException (java.net.NoRouteToHostException)1 SocketException (java.net.SocketException)1