org.apache.http.client.config.CookieSpecs Java Examples

The following examples show how to use org.apache.http.client.config.CookieSpecs. 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: HttpTransportClient.java    From vk-java-sdk with MIT License 8 votes vote down vote up
public HttpTransportClient(int retryAttemptsNetworkErrorCount, int retryAttemptsInvalidStatusCount) {
    this.retryAttemptsNetworkErrorCount = retryAttemptsNetworkErrorCount;
    this.retryAttemptsInvalidStatusCount = retryAttemptsInvalidStatusCount;

    CookieStore cookieStore = new BasicCookieStore();
    RequestConfig requestConfig = RequestConfig.custom()
            .setSocketTimeout(SOCKET_TIMEOUT_MS)
            .setConnectTimeout(CONNECTION_TIMEOUT_MS)
            .setConnectionRequestTimeout(CONNECTION_TIMEOUT_MS)
            .setCookieSpec(CookieSpecs.STANDARD)
            .build();

    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();

    connectionManager.setMaxTotal(MAX_SIMULTANEOUS_CONNECTIONS);
    connectionManager.setDefaultMaxPerRoute(MAX_SIMULTANEOUS_CONNECTIONS);

    httpClient = HttpClients.custom()
            .setConnectionManager(connectionManager)
            .setDefaultRequestConfig(requestConfig)
            .setDefaultCookieStore(cookieStore)
            .setUserAgent(USER_AGENT)
            .build();
}
 
Example #2
Source File: HttpPostParams.java    From SnowGraph with Apache License 2.0 6 votes vote down vote up
@Override
protected CloseableHttpResponse send(CloseableHttpClient httpClient, String base) throws Exception {
	List<NameValuePair> formparams = new ArrayList<NameValuePair>();
	for (String key : params.keySet()) {
		String value = params.get(key);
		formparams.add(new BasicNameValuePair(key, value));
	}
	HttpPost request = new HttpPost(base);

       RequestConfig localConfig = RequestConfig.custom()
               .setCookieSpec(CookieSpecs.STANDARD)
               .build();
       request.setConfig(localConfig); 
	request.setEntity(new UrlEncodedFormEntity(formparams, "UTF-8"));
	request.setHeader("Content-Type", "application/x-www-form-urlencoded");		//内容为post
	return httpClient.execute(request);
}
 
Example #3
Source File: DriverDownloader.java    From webdriverextensions-maven-plugin with Apache License 2.0 6 votes vote down vote up
private HttpClientBuilder prepareHttpClientBuilderWithTimeoutsAndProxySettings(Proxy proxySettings) throws MojoExecutionException {
    SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(FILE_DOWNLOAD_READ_TIMEOUT).build();
    RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(FILE_DOWNLOAD_CONNECT_TIMEOUT)
            .setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();
    HttpClientBuilder httpClientBuilder = HttpClients.custom();
    httpClientBuilder
            .setDefaultSocketConfig(socketConfig)
            .setDefaultRequestConfig(requestConfig)
            .disableContentCompression();
    HttpHost proxy = ProxyUtils.createProxyFromSettings(proxySettings);
    if (proxy != null) {
        httpClientBuilder.setProxy(proxy);
        CredentialsProvider proxyCredentials = ProxyUtils.createProxyCredentialsFromSettings(proxySettings);
        if (proxyCredentials != null) {
            httpClientBuilder.setDefaultCredentialsProvider(proxyCredentials);
        }
    }
    return httpClientBuilder;
}
 
Example #4
Source File: SaltService.java    From uyuni with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Default constructor
 */
public SaltService() {
    RequestConfig requestConfig = RequestConfig.custom()
            .setConnectTimeout(0)
            .setSocketTimeout(0)
            .setConnectionRequestTimeout(0)
            .setCookieSpec(CookieSpecs.STANDARD)
            .build();
    HttpAsyncClientBuilder httpClientBuilder = HttpAsyncClients.custom();
    httpClientBuilder.setDefaultRequestConfig(requestConfig);

    asyncHttpClient = httpClientBuilder
            .setMaxConnPerRoute(20)
            .setMaxConnTotal(20)
            .build();
    asyncHttpClient.start();

    SALT_CLIENT = new SaltClient(SALT_MASTER_URI, new HttpAsyncClientImpl(asyncHttpClient));
    saltSSHService = new SaltSSHService(SALT_CLIENT, SaltActionChainGeneratorService.INSTANCE);
    defaultBatch = Batch.custom().withBatchAsAmount(ConfigDefaults.get().getSaltBatchSize())
                    .withDelay(ConfigDefaults.get().getSaltBatchDelay())
                    .withPresencePingTimeout(ConfigDefaults.get().getSaltPresencePingTimeout())
                    .withPresencePingGatherJobTimeout(ConfigDefaults.get().getSaltPresencePingGatherJobTimeout())
                    .build();
}
 
Example #5
Source File: ClientUtil.java    From oxAuth with MIT License 6 votes vote down vote up
/**
 * Creates a special SSLContext using a custom TLS version and a set of ciphers enabled to process SSL connections.
 * @param tlsVersion TLS version, for example TLSv1.2
 * @param ciphers Set of ciphers used to create connections.
 */
public static CloseableHttpClient createHttpClient(String tlsVersion, String[] ciphers) {
    try {
        SSLContext sslContext = SSLContexts.createDefault();
        SSLConnectionSocketFactory sslConnectionFactory = new SSLConnectionSocketFactory(sslContext,
                new String[] { tlsVersion }, ciphers, NoopHostnameVerifier.INSTANCE);

        Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory> create()
                .register("https", sslConnectionFactory)
                .register("http", new PlainConnectionSocketFactory())
                .build();

        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);

        return HttpClients.custom()
                .setSSLContext(sslContext)
                .setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build())
                .setConnectionManager(cm)
                .build();
    } catch (Exception e) {
        log.error("Error creating HttpClient with a custom TLS version and custom ciphers", e);
        return null;
    }
}
 
Example #6
Source File: AbstractSSLHttpClient.java    From pay-spring-boot with GNU General Public License v3.0 6 votes vote down vote up
public AbstractSSLHttpClient(){
    /**
     * 请求配置
     */
    RequestConfig globalConfig = RequestConfig.
                                    custom().
                                    setCookieSpec(CookieSpecs.DEFAULT).
                                    setSocketTimeout(10000).
                                    setConnectTimeout(20000).
                                    setConnectionRequestTimeout(20000).
                                    build();
    /**
     * cookie容器
     */
    CookieStore cookieStore = new BasicCookieStore();
    
    /**
     * 核心请求对象
     */
    httpclient = HttpClients.
                    custom().
                    setDefaultRequestConfig(globalConfig).
                    setDefaultCookieStore(cookieStore).
                    setSSLSocketFactory(getSSLSocketFactory()).
                    build();
}
 
Example #7
Source File: HttpRequestUtil.java    From Alice-LiveMan with GNU Affero General Public License v3.0 6 votes vote down vote up
public static byte[] downloadUrl(URI url) throws IOException {
    HttpGet httpGet = new HttpGet(url);
    HttpClientContext context = HttpClientContext.create();
    RequestConfig.Builder builder = RequestConfig.custom();
    builder.setConnectTimeout(2000).setConnectionRequestTimeout(2000).setSocketTimeout(5000).setCookieSpec(CookieSpecs.IGNORE_COOKIES).setRedirectsEnabled(true);
    httpGet.setConfig(builder.build());
    httpGet.addHeader("Accept", "*/*");
    httpGet.addHeader("Accept-Encoding", "gzip, deflate");
    httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36");
    try (CloseableHttpResponse httpResponse = client.execute(httpGet, context)) {
        HttpEntity responseEntity = httpResponse.getEntity();
        if (httpResponse.getStatusLine().getStatusCode() != 200) {
            throw new IOException(httpResponse.getStatusLine().getStatusCode() + " " + httpResponse.getStatusLine().getReasonPhrase() + "\n Headers:" + Arrays.toString(httpResponse.getAllHeaders()) + "\n" + EntityUtils.toString(responseEntity));
        }
        return EntityUtils.toByteArray(responseEntity);
    } catch (IllegalStateException e) {
        initClient();
        throw e;
    }
}
 
Example #8
Source File: HttpEventPublisher.java    From DataflowTemplates with Apache License 2.0 6 votes vote down vote up
/**
 * Utility method to create a {@link CloseableHttpClient} to make http POSTs against Splunk's
 * HEC.
 *
 * @param maxConnections max number of parallel connections.
 * @param disableCertificateValidation should disable certificate validation.
 */
private CloseableHttpClient getHttpClient(
    int maxConnections, boolean disableCertificateValidation)
    throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {

  HttpClientBuilder builder = ApacheHttpTransport.newDefaultHttpClientBuilder();

  if (genericUrl().getScheme().equalsIgnoreCase(HTTPS_PROTOCOL_PREFIX)) {
    LOG.info("SSL connection requested");

    HostnameVerifier hostnameVerifier =
        disableCertificateValidation
            ? NoopHostnameVerifier.INSTANCE
            : new DefaultHostnameVerifier();

    SSLContextBuilder sslContextBuilder = SSLContextBuilder.create();
    if (disableCertificateValidation) {
      LOG.info("Certificate validation is disabled");
      sslContextBuilder.loadTrustMaterial((TrustStrategy) (chain, authType) -> true);
    }

    SSLConnectionSocketFactory connectionSocketFactory =
        new SSLConnectionSocketFactory(sslContextBuilder.build(), hostnameVerifier);
    builder.setSSLSocketFactory(connectionSocketFactory);
  }

  builder.setMaxConnTotal(maxConnections);
  builder.setDefaultRequestConfig(
      RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build());

  return builder.build();
}
 
Example #9
Source File: HttpsAdminServerTest.java    From blynk-server with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testGetUserFromAdminPageNoAccessWithFakeCookie2() throws Exception {
    login(admin.email, admin.pass);

    SSLContext sslcontext = TestUtil.initUnsecuredSSLContext();
    // Allow TLSv1 protocol only
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new MyHostVerifier());
    CloseableHttpClient httpclient2 = HttpClients.custom()
            .setSSLSocketFactory(sslsf)
            .setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build())
            .build();


    String testUser = "[email protected]";
    String appName = "Blynk";
    HttpGet request = new HttpGet(httpsAdminServerUrl + "/users/" + testUser + "-" + appName);
    request.setHeader("Cookie", "session=123");

    try (CloseableHttpResponse response = httpclient2.execute(request)) {
        assertEquals(404, response.getStatusLine().getStatusCode());
    }
}
 
Example #10
Source File: BaseTest.java    From oxAuth with MIT License 6 votes vote down vote up
public static CloseableHttpClient createHttpClientTrustAll()
		throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
	SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(new TrustStrategy() {
		@Override
		public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
			return true;
		}
	}).build();
	SSLConnectionSocketFactory sslContextFactory = new SSLConnectionSocketFactory(sslContext);
	CloseableHttpClient httpclient = HttpClients.custom()
			.setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build())
			.setSSLSocketFactory(sslContextFactory)
			.setRedirectStrategy(new LaxRedirectStrategy()).build();

	return httpclient;
}
 
Example #11
Source File: HttpPostParams.java    From GoogleTranslation with Apache License 2.0 6 votes vote down vote up
@Override
protected CloseableHttpResponse send(CloseableHttpClient httpClient, String base) throws Exception {
    List<NameValuePair> formParams = new ArrayList<>();
    for (String key : params.keySet()) {
        String value = params.get(key);
        formParams.add(new BasicNameValuePair(key, value));
    }
    HttpPost request = new HttpPost(base);

    RequestConfig localConfig = RequestConfig.custom()
            .setCookieSpec(CookieSpecs.STANDARD)
            .build();
    request.setConfig(localConfig);
    request.setEntity(new UrlEncodedFormEntity(formParams, "UTF-8"));
    request.setHeader("Content-Type", "application/x-www-form-urlencoded");        //内容为post
    return httpClient.execute(request);
}
 
Example #12
Source File: ODataProductSynchronizer.java    From DataHubSystem with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public CloseableHttpAsyncClient generateClient ()
{
   CredentialsProvider credsProvider = new BasicCredentialsProvider();
   credsProvider.setCredentials(new AuthScope (AuthScope.ANY),
           new UsernamePasswordCredentials(serviceUser, servicePass));
   RequestConfig rqconf = RequestConfig.custom()
         .setCookieSpec(CookieSpecs.DEFAULT)
         .setSocketTimeout(Timeouts.SOCKET_TIMEOUT)
         .setConnectTimeout(Timeouts.CONNECTION_TIMEOUT)
         .setConnectionRequestTimeout(Timeouts.CONNECTION_REQUEST_TIMEOUT)
         .build();
   CloseableHttpAsyncClient res = HttpAsyncClients.custom ()
         .setDefaultCredentialsProvider (credsProvider)
         .setDefaultRequestConfig(rqconf)
         .build ();
   res.start ();
   return res;
}
 
Example #13
Source File: OTATest.java    From blynk-server with GNU General Public License v3.0 6 votes vote down vote up
@Before
public void init() throws Exception {
    httpServer = new HardwareAndHttpAPIServer(holder).start();
    httpsServer = new MobileAndHttpsServer(holder).start();
    httpsAdminServerUrl = String.format("https://localhost:%s/admin", properties.getHttpsPort());

    String pass = "admin";
    User user = new User();
    user.isSuperAdmin = true;
    user.email = "[email protected]";
    user.pass = SHA256Util.makeHash(pass, user.email);
    holder.userDao.add(user);

    auth = (user.email + ":" + pass).getBytes();

    // Allow TLSv1 protocol only
    SSLContext sslcontext = initUnsecuredSSLContext();
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new MyHostVerifier());
    this.httpclient = HttpClients.custom()
            .setSSLSocketFactory(sslsf)
            .setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build())
            .build();
    clientPair = initAppAndHardPair(properties);
}
 
Example #14
Source File: HttpClientHelper.java    From cosmic with Apache License 2.0 6 votes vote down vote up
public static CloseableHttpClient createHttpClient(final int maxRedirects) throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
    s_logger.info("Creating new HTTP connection pool and client");
    final Registry<ConnectionSocketFactory> socketFactoryRegistry = createSocketFactoryConfigration();
    final BasicCookieStore cookieStore = new BasicCookieStore();
    final PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
    connManager.setDefaultMaxPerRoute(MAX_ALLOCATED_CONNECTIONS_PER_ROUTE);
    connManager.setMaxTotal(MAX_ALLOCATED_CONNECTIONS);
    final RequestConfig requestConfig = RequestConfig.custom()
                                                     .setCookieSpec(CookieSpecs.DEFAULT)
                                                     .setMaxRedirects(maxRedirects)
                                                     .setSocketTimeout(DEFAULT_SOCKET_TIMEOUT)
                                                     .setConnectionRequestTimeout(DEFAULT_CONNECTION_REQUEST_TIMEOUT)
                                                     .setConnectTimeout(DEFAULT_CONNECT_TIMEOUT)
                                                     .build();
    return HttpClientBuilder.create()
                            .setConnectionManager(connManager)
                            .setRedirectStrategy(new LaxRedirectStrategy())
                            .setDefaultRequestConfig(requestConfig)
                            .setDefaultCookieStore(cookieStore)
                            .setRetryHandler(new StandardHttpRequestRetryHandler())
                            .build();
}
 
Example #15
Source File: HttpDataService.java    From jasperreports with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected CloseableHttpClient createHttpClient(Map<String, Object> parameters)
{
	HttpClientBuilder clientBuilder = HttpClients.custom();
	
	// single connection
	BasicHttpClientConnectionManager connManager = new BasicHttpClientConnectionManager();
	clientBuilder.setConnectionManager(connManager);
	
	// ignore cookies for now
	RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();
	clientBuilder.setDefaultRequestConfig(requestConfig);
	
	setAuthentication(parameters, clientBuilder);
	
	CloseableHttpClient client = clientBuilder.build();
	return client;
}
 
Example #16
Source File: ProcessConnection.java    From jasperreports with GNU Lesser General Public License v3.0 6 votes vote down vote up
public ProcessConnection(ProcessDirector director, PhantomJSProcess process)
{
	this.process = process;
	
	HttpClientBuilder clientBuilder = HttpClients.custom();
	
	// single connection
	BasicHttpClientConnectionManager connManager = new BasicHttpClientConnectionManager();
	clientBuilder.setConnectionManager(connManager);
	
	RequestConfig requestConfig = RequestConfig.custom()
			// ignore cookies for now
			.setCookieSpec(CookieSpecs.IGNORE_COOKIES)
			.setSocketTimeout(director.getRequestTimeout()).build();
	clientBuilder.setDefaultRequestConfig(requestConfig);
	
	this.httpClient = clientBuilder.build();
}
 
Example #17
Source File: UserInfoParser.java    From clouddisk with MIT License 6 votes vote down vote up
@Override
public HttpGet initRequest(final UserInfoParameter userInfoParameter) {
	try {
		final URIBuilder uriBuilder = new URIBuilder(CONST.URI_PATH);
		uriBuilder.addParameter(CONST.SRC_KEY, CONST.SRC_VAL);
		uriBuilder.addParameter(CONST.FROM_KEY, CONST.FROM_VAL);
		uriBuilder.addParameter(CONST.CHARSET_KEY, CONST.CHARSET_VAL);
		uriBuilder.addParameter(CONST.METHOD_KEY, CONST.METHOD_VAL);
		uriBuilder.addParameter(CONST.REQUESTSCEMA_KEY, CONST.REQUESTSCEMA_VAL);
		uriBuilder.addParameter(CONST.O_KEY, CONST.O_VAL);
		uriBuilder.addParameter(CONST.SHOW_NAME_FLAG_NAME, CONST.SHOW_NAME_FLAG_VALUE);
		uriBuilder.addParameter(CONST.HEAD_TYPE_NAME, CONST.HEAD_TYPE_VAL);
		uriBuilder.addParameter("-", TimeUtil.getTimeLenth(13));
		final HttpGet request = new HttpGet(uriBuilder.build());
		request.setConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.NETSCAPE).build());
		return request;
	} catch (URISyntaxException e) {
		LOGGER.error("Error",e);
	}
	return null;
}
 
Example #18
Source File: UserCheckLoginParser.java    From clouddisk with MIT License 6 votes vote down vote up
@Override
public HttpPost initRequest(final UserCheckLoginParameter userCheckLoginParameter) {
	setBinaryFilename(LoginConst.USER_INFO_PATH_NAME);
	Login login = readObjForDisk(Login.class);
	if(null==login){
		login = new Login();
	}
	final HttpPost request = new HttpPost(CONST.URI_PATH);
	final List<NameValuePair> data = new ArrayList<>();
	data.add(new BasicNameValuePair(CONST.QID_NAME, login.getQid()));
	data.add(new BasicNameValuePair(CONST.METHOD_KEY, CONST.METHOD_VAL));
	data.add(new BasicNameValuePair(CONST.AJAX_KEY, CONST.AJAX_VAL));
	data.add(new BasicNameValuePair("t", TimeUtil.getTimeLenth(13)));
	request.setConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.NETSCAPE).build());
	request.setEntity(new UrlEncodedFormEntity(data, Consts.UTF_8));
	return request;
}
 
Example #19
Source File: TemplateContext.java    From cryptotrader with GNU Affero General Public License v3.0 6 votes vote down vote up
protected TemplateContext(String id) {

        this.id = id;

        this.executor = Executors.newCachedThreadPool();

        this.state = new AtomicReference<>(StateType.ACTIVE);

        this.client = HttpClients.custom()
                .evictExpiredConnections()
                .evictIdleConnections(CLIENT_TIMEOUT.toMillis(), MILLISECONDS)
                .setConnectionTimeToLive(CLIENT_TIMEOUT.toMillis(), MILLISECONDS)
                .setMaxConnPerRoute(Byte.MAX_VALUE)
                .setMaxConnTotal(Byte.MAX_VALUE)
                .setDefaultRequestConfig(
                        RequestConfig.custom()
                                .setCookieSpec(CookieSpecs.STANDARD)
                                .setSocketTimeout((int) CLIENT_TIMEOUT.toMillis())
                                .setConnectTimeout((int) CLIENT_TIMEOUT.toMillis())
                                .setConnectionRequestTimeout((int) CLIENT_TIMEOUT.toMillis())
                                .build()
                )
                .build();


    }
 
Example #20
Source File: DefaultsCustomizer.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void customize(final HttpClientPlan plan) {
  checkNotNull(plan);

  plan.setUserAgentBase(userAgentGenerator.generate());

  plan.getClient().setKeepAliveStrategy(new NexusConnectionKeepAliveStrategy(keepAliveDuration.toMillis()));
  plan.getClient().setRetryHandler(new StandardHttpRequestRetryHandler(2, false));

  plan.getConnection().setBufferSize(bufferSize.toBytesI());

  plan.getRequest().setConnectionRequestTimeout(connectionRequestTimeout.toMillisI());
  plan.getRequest().setCookieSpec(CookieSpecs.IGNORE_COOKIES);
  plan.getRequest().setExpectContinueEnabled(false);

  int requestTimeoutMillis = requestTimeout.toMillisI();
  plan.getSocket().setSoTimeout(requestTimeoutMillis);
  plan.getRequest().setConnectTimeout(requestTimeoutMillis);
  plan.getRequest().setSocketTimeout(requestTimeoutMillis);
}
 
Example #21
Source File: ODataClient.java    From DataHubSystem with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public CloseableHttpAsyncClient generateClient ()
{
   CredentialsProvider credsProvider = new BasicCredentialsProvider();
   credsProvider.setCredentials(new AuthScope (AuthScope.ANY),
            new UsernamePasswordCredentials(username, password));
   RequestConfig rqconf = RequestConfig.custom()
         .setCookieSpec(CookieSpecs.DEFAULT)
         .setSocketTimeout(Timeouts.SOCKET_TIMEOUT)
         .setConnectTimeout(Timeouts.CONNECTION_TIMEOUT)
         .setConnectionRequestTimeout(Timeouts.CONNECTION_REQUEST_TIMEOUT)
         .build();
   CloseableHttpAsyncClient res = HttpAsyncClients.custom ()
         .setDefaultCredentialsProvider (credsProvider)
         .setDefaultRequestConfig(rqconf)
         .build ();
   res.start ();
   return res;
}
 
Example #22
Source File: HttpEventPublisher.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a {@link CloseableHttpClient} to make HTTP POSTs against Splunk's HEC.
 *
 * @param maxConnections max number of parallel connections
 * @param disableCertificateValidation should disable certificate validation
 */
private CloseableHttpClient getHttpClient(
    int maxConnections, boolean disableCertificateValidation)
    throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {

  HttpClientBuilder builder = ApacheHttpTransport.newDefaultHttpClientBuilder();

  if (genericUrl().getScheme().equalsIgnoreCase(HTTPS_PROTOCOL_PREFIX)) {
    LOG.info("SSL connection requested");

    HostnameVerifier hostnameVerifier =
        disableCertificateValidation
            ? NoopHostnameVerifier.INSTANCE
            : new DefaultHostnameVerifier();

    SSLContextBuilder sslContextBuilder = SSLContextBuilder.create();
    if (disableCertificateValidation) {
      LOG.info("Certificate validation is disabled");
      sslContextBuilder.loadTrustMaterial((TrustStrategy) (chain, authType) -> true);
    }

    SSLConnectionSocketFactory connectionSocketFactory =
        new SSLConnectionSocketFactory(sslContextBuilder.build(), hostnameVerifier);
    builder.setSSLSocketFactory(connectionSocketFactory);
  }

  builder.setMaxConnTotal(maxConnections);
  builder.setDefaultRequestConfig(
      RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build());

  return builder.build();
}
 
Example #23
Source File: GitHubAuthFilter.java    From para with Apache License 2.0 5 votes vote down vote up
/**
 * Default constructor.
 * @param defaultFilterProcessesUrl the url of the filter
 */
public GitHubAuthFilter(final String defaultFilterProcessesUrl) {
	super(defaultFilterProcessesUrl);
	this.jreader = ParaObjectUtils.getJsonReader(Map.class);
	int timeout = 30 * 1000;
	this.httpclient = HttpClientBuilder.create().
			setConnectionReuseStrategy(new NoConnectionReuseStrategy()).
			setDefaultRequestConfig(RequestConfig.custom().
					setConnectTimeout(timeout).
					setConnectionRequestTimeout(timeout).
					setCookieSpec(CookieSpecs.STANDARD).
					setSocketTimeout(timeout).
					build()).
			build();
}
 
Example #24
Source File: GenericOAuth2Filter.java    From para with Apache License 2.0 5 votes vote down vote up
/**
 * Default constructor.
 * @param defaultFilterProcessesUrl the url of the filter
 */
public GenericOAuth2Filter(final String defaultFilterProcessesUrl) {
	super(defaultFilterProcessesUrl);
	this.jreader = ParaObjectUtils.getJsonReader(Map.class);
	int timeout = 30 * 1000;
	this.httpclient = HttpClientBuilder.create().
			setConnectionReuseStrategy(new NoConnectionReuseStrategy()).
			setDefaultRequestConfig(RequestConfig.custom().
					setConnectTimeout(timeout).
					setConnectionRequestTimeout(timeout).
					setCookieSpec(CookieSpecs.STANDARD).
					setSocketTimeout(timeout).
					build()).
			build();
}
 
Example #25
Source File: LinkedInAuthFilter.java    From para with Apache License 2.0 5 votes vote down vote up
/**
 * Default constructor.
 * @param defaultFilterProcessesUrl the url of the filter
 */
public LinkedInAuthFilter(final String defaultFilterProcessesUrl) {
	super(defaultFilterProcessesUrl);
	this.jreader = ParaObjectUtils.getJsonReader(Map.class);
	int timeout = 30 * 1000;
	this.httpclient = HttpClientBuilder.create().
			setConnectionReuseStrategy(new NoConnectionReuseStrategy()).
			setDefaultRequestConfig(RequestConfig.custom().
					setConnectTimeout(timeout).
					setConnectionRequestTimeout(timeout).
					setCookieSpec(CookieSpecs.STANDARD).
					setSocketTimeout(timeout).
					build()).
			build();
}
 
Example #26
Source File: DefaultApacheHttpClientFactoryTests.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void createClient() throws Exception {
	final RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(100)
			.setConnectTimeout(200).setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();
	CloseableHttpClient httpClient = new DefaultApacheHttpClientFactory(
			HttpClientBuilder.create()).createBuilder()
					.setConnectionManager(mock(HttpClientConnectionManager.class))
					.setDefaultRequestConfig(requestConfig).build();
	BDDAssertions.then(httpClient).isInstanceOf(Configurable.class);
	RequestConfig config = ((Configurable) httpClient).getConfig();
	then(config.getSocketTimeout()).isEqualTo(100);
	then(config.getConnectTimeout()).isEqualTo(200);
	then(config.getCookieSpec()).isEqualTo(CookieSpecs.IGNORE_COOKIES);
}
 
Example #27
Source File: PostmanRequestRunner.java    From postman-runner with Apache License 2.0 5 votes vote down vote up
protected CloseableHttpClient createHttpClient() {
	try {
		SSLContext sslContext = SSLContexts.custom().useProtocol("TLSv1.2").build();
		RequestConfig config = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(5000)
				.setConnectionRequestTimeout(60000).setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();

		CloseableHttpClient httpClient = HttpClientBuilder.create().setSSLContext(sslContext)
				.setDefaultRequestConfig(config).build();
		return httpClient;
	} catch (KeyManagementException | NoSuchAlgorithmException e) {
		logger.error("Failed to create http client using TLSv1.2");
		throw new RuntimeException("Failed to create http client using TLSv1.2.", e);
	}
}
 
Example #28
Source File: GoogleAuthFilter.java    From para with Apache License 2.0 5 votes vote down vote up
/**
 * Default constructor.
 * @param defaultFilterProcessesUrl the url of the filter
 */
public GoogleAuthFilter(final String defaultFilterProcessesUrl) {
	super(defaultFilterProcessesUrl);
	this.jreader = ParaObjectUtils.getJsonReader(Map.class);
	int timeout = 30 * 1000;
	this.httpclient = HttpClientBuilder.create().
			setConnectionReuseStrategy(new NoConnectionReuseStrategy()).
			setDefaultRequestConfig(RequestConfig.custom().
					setConnectTimeout(timeout).
					setConnectionRequestTimeout(timeout).
					setCookieSpec(CookieSpecs.STANDARD).
					setSocketTimeout(timeout).
					build()).
			build();
}
 
Example #29
Source File: SlackAuthFilter.java    From para with Apache License 2.0 5 votes vote down vote up
/**
 * Default constructor.
 * @param defaultFilterProcessesUrl the url of the filter
 */
public SlackAuthFilter(final String defaultFilterProcessesUrl) {
	super(defaultFilterProcessesUrl);
	this.jreader = ParaObjectUtils.getJsonReader(Map.class);
	int timeout = 30 * 1000;
	this.httpclient = HttpClientBuilder.create().
			setConnectionReuseStrategy(new NoConnectionReuseStrategy()).
			setDefaultRequestConfig(RequestConfig.custom().
					setConnectTimeout(timeout).
					setConnectionRequestTimeout(timeout).
					setCookieSpec(CookieSpecs.STANDARD).
					setSocketTimeout(timeout).
					build()).
			build();
}
 
Example #30
Source File: TwitterAuthFilter.java    From para with Apache License 2.0 5 votes vote down vote up
/**
 * Default constructor.
 * @param defaultFilterProcessesUrl the url of the filter
 */
public TwitterAuthFilter(final String defaultFilterProcessesUrl) {
	super(defaultFilterProcessesUrl);
	this.jreader = ParaObjectUtils.getJsonReader(Map.class);
	int timeout = 30 * 1000;
	this.httpclient = HttpClientBuilder.create().
			setConnectionReuseStrategy(new NoConnectionReuseStrategy()).
			setDefaultRequestConfig(RequestConfig.custom().
					setConnectTimeout(timeout).
					setConnectionRequestTimeout(timeout).
					setCookieSpec(CookieSpecs.STANDARD).
					setSocketTimeout(timeout).
					build()).
			build();
}