org.apache.http.cookie.CookieSpecProvider Java Examples

The following examples show how to use org.apache.http.cookie.CookieSpecProvider. 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: HttpWebConnection.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Lazily initializes the internal HTTP client.
 *
 * @return the initialized HTTP client
 */
protected HttpClientBuilder getHttpClientBuilder() {
    final Thread currentThread = Thread.currentThread();
    HttpClientBuilder builder = httpClientBuilder_.get(currentThread);
    if (builder == null) {
        builder = createHttpClientBuilder();

        // 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()));
        builder.setUserAgent(webClient_.getBrowserVersion().getUserAgent());
        httpClientBuilder_.put(currentThread, builder);
    }

    return builder;
}
 
Example #2
Source File: StatefulHttpClient.java    From BigData-In-Practice with Apache License 2.0 5 votes vote down vote up
private void initCookieStore() {
    PublicSuffixMatcher publicSuffixMatcher = PublicSuffixMatcherLoader.getDefault();
    Registry<CookieSpecProvider> cookieSpecReg = RegistryBuilder.<CookieSpecProvider>create()
            .register(CookieSpecs.DEFAULT, new DefaultCookieSpecProvider(publicSuffixMatcher))
            .register(CookieSpecs.STANDARD, new RFC6265CookieSpecProvider(publicSuffixMatcher)).build();
    CookieStore cookieStore = new BasicCookieStore();

    context = HttpClientContext.create();
    context.setCookieSpecRegistry(cookieSpecReg);
    context.setCookieStore(cookieStore);
}
 
Example #3
Source File: HttpUtil.java    From ZhihuSpider with MIT License 5 votes vote down vote up
public static HttpClientContext getHttpClientContext() {
    HttpClientContext context = null;
    context = HttpClientContext.create();
    Registry<CookieSpecProvider> registry = RegistryBuilder
            .<CookieSpecProvider>create()
            .register(CookieSpecs.BEST_MATCH, new BestMatchSpecFactory())
            .register(CookieSpecs.BROWSER_COMPATIBILITY,
                    new BrowserCompatSpecFactory()).build();
    context.setCookieSpecRegistry(registry);
    return context;
}
 
Example #4
Source File: KerberosHttpClientBuilder.java    From nifi with Apache License 2.0 5 votes vote down vote up
public SolrHttpClientBuilder getBuilder(SolrHttpClientBuilder builder) {

        //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);
        return builder;
    }
 
Example #5
Source File: HttpClientUtils.java    From turbo-rpc with Apache License 2.0 4 votes vote down vote up
public static CloseableHttpClient createHttpClient(int concurrency) {
	HttpClientBuilder builder = HttpClientBuilder.create();

	PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
	connManager.setDefaultMaxPerRoute(concurrency);
	connManager.setMaxTotal(concurrency);

	RequestConfig requestConfig = RequestConfig.custom()//
			.setAuthenticationEnabled(true)//
			.setSocketTimeout(SOCKET_TIMEOUT)//
			.setConnectionRequestTimeout(CONNECTION_REQUEST_TIMEOUT)//
			.setConnectTimeout(CONNECT_TIMEOUT)//
			.setRedirectsEnabled(true)//
			.setRelativeRedirectsAllowed(true)//
			.setMaxRedirects(15)//
			.build();

	SocketConfig socketConfig = SocketConfig.custom()//
			.setSoKeepAlive(true)//
			.setSoReuseAddress(true)//
			.build();

	CookieSpecProvider cookieSpecProvider = new IgnoreSpecProvider();
	Lookup<CookieSpecProvider> cookieSpecRegistry = RegistryBuilder.<CookieSpecProvider>create()//
			.register(CookieSpecs.DEFAULT, cookieSpecProvider)//
			.register(CookieSpecs.STANDARD, cookieSpecProvider)//
			.register(CookieSpecs.STANDARD_STRICT, cookieSpecProvider)//
			.build();

	builder.setConnectionManager(connManager);
	builder.setDefaultSocketConfig(socketConfig);
	builder.setDefaultRequestConfig(requestConfig);
	builder.setDefaultCookieSpecRegistry(cookieSpecRegistry);

	return builder.addInterceptorLast((HttpRequest request, HttpContext context) -> {
		request.removeHeaders("Host");
		request.removeHeaders("Accept-Encoding");
		//request.removeHeaders("Connection");
		request.removeHeaders("User-Agent");
	}).build();
}
 
Example #6
Source File: LenientCookieSpec.java    From karate with MIT License 4 votes vote down vote up
public static Registry<CookieSpecProvider> registry() {
    CookieSpecProvider specProvider = (HttpContext hc) -> new LenientCookieSpec();
    return RegistryBuilder.<CookieSpecProvider>create()
            .register(KARATE, specProvider).build();
}
 
Example #7
Source File: Krb5HttpClientBuilder.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public SolrHttpClientBuilder getBuilder(SolrHttpClientBuilder builder) {
  if (System.getProperty(LOGIN_CONFIG_PROP) != null) {
    String configValue = System.getProperty(LOGIN_CONFIG_PROP);

    if (configValue != null) {
      log.info("Setting up SPNego auth with config: {}", configValue);
      final String useSubjectCredsProp = "javax.security.auth.useSubjectCredsOnly";
      String useSubjectCredsVal = System.getProperty(useSubjectCredsProp);

      // "javax.security.auth.useSubjectCredsOnly" should be false so that the underlying
      // 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.
        log.warn("System Property: {} set to: {} not false.  SPNego authentication may not be successful."
            , useSubjectCredsProp, useSubjectCredsVal);
      }

      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 {
    log.warn("{} is configured without specifying system property '{}'",
        getClass().getName(), LOGIN_CONFIG_PROP);
  }

  return builder;
}
 
Example #8
Source File: SdcSolrHttpClientBuilder.java    From datacollector with Apache License 2.0 4 votes vote down vote up
static SolrHttpClientBuilder create() {
  SolrHttpClientBuilder solrHttpClientBuilder = SolrHttpClientBuilder.create();

  final String useSubjectCredentialsProperty = USE_SUBJECT_CREDENTIALS_PROPERTY;
  String useSubjectCredentialsValue = System.getProperty(useSubjectCredentialsProperty);

  if (useSubjectCredentialsValue == null) {
    System.setProperty(useSubjectCredentialsProperty, FALSE);
  } else if (!useSubjectCredentialsValue.toLowerCase(Locale.ROOT).equals(FALSE)) {
    LOG.warn(String.format(
        "System Property: %s set to: %s not false. SPNego authentication may not be successful.",
        useSubjectCredentialsProperty,
        useSubjectCredentialsValue
    ));
  }

  solrHttpClientBuilder.setAuthSchemeRegistryProvider(() -> RegistryBuilder.<AuthSchemeProvider>create().register(AuthSchemes.SPNEGO,
      new SPNegoSchemeFactory(true)
  ).build());

  SolrPortAwareCookieSpecFactory cookieFactory = new SolrPortAwareCookieSpecFactory();
  solrHttpClientBuilder.setCookieSpecRegistryProvider(() -> RegistryBuilder.<CookieSpecProvider>create().register(SolrPortAwareCookieSpecFactory.POLICY_NAME,
      cookieFactory
  ).build());

  Credentials jassCredentials = new Credentials() {
    public String getPassword() {
      return null;
    }

    public Principal getUserPrincipal() {
      return null;
    }
  };

  CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
  credentialsProvider.setCredentials(AuthScope.ANY, jassCredentials);
  solrHttpClientBuilder.setDefaultCredentialsProvider(() -> credentialsProvider);

  return solrHttpClientBuilder;
}
 
Example #9
Source File: SolrHttpClientBuilder.java    From lucene-solr with Apache License 2.0 votes vote down vote up
Lookup<CookieSpecProvider> getCookieSpecRegistry(); 
Example #10
Source File: SolrHttpClientContextBuilder.java    From lucene-solr with Apache License 2.0 votes vote down vote up
public abstract Lookup<CookieSpecProvider> getCookieSpecRegistry();