Java Code Examples for org.apache.http.impl.client.HttpClients#custom()
The following examples show how to use
org.apache.http.impl.client.HttpClients#custom() .
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: KeySetRetriever.java From deprecated-security-advanced-modules with Apache License 2.0 | 7 votes |
private CloseableHttpClient createHttpClient(HttpCacheStorage httpCacheStorage) { HttpClientBuilder builder; if (httpCacheStorage != null) { builder = CachingHttpClients.custom().setCacheConfig(cacheConfig).setHttpCacheStorage(httpCacheStorage); } else { builder = HttpClients.custom(); } builder.useSystemProperties(); if (sslConfig != null) { builder.setSSLSocketFactory(sslConfig.toSSLConnectionSocketFactory()); } return builder.build(); }
Example 2
Source File: ProcessConnection.java From jasperreports with GNU Lesser General Public License v3.0 | 6 votes |
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 3
Source File: WxPayServiceApacheHttpImpl.java From weixin-java-tools with Apache License 2.0 | 6 votes |
private HttpClientBuilder createHttpClientBuilder(boolean useKey) throws WxPayException { HttpClientBuilder httpClientBuilder = HttpClients.custom(); if (useKey) { this.initSSLContext(httpClientBuilder); } if (StringUtils.isNotBlank(this.getConfig().getHttpProxyHost()) && this.getConfig().getHttpProxyPort() > 0) { // 使用代理服务器 需要用户认证的代理服务器 CredentialsProvider provider = new BasicCredentialsProvider(); provider.setCredentials( new AuthScope(this.getConfig().getHttpProxyHost(), this.getConfig().getHttpProxyPort()), new UsernamePasswordCredentials(this.getConfig().getHttpProxyUsername(), this.getConfig().getHttpProxyPassword())); httpClientBuilder.setDefaultCredentialsProvider(provider); } return httpClientBuilder; }
Example 4
Source File: AbstractCheckThread.java From sitemonitoring-production with BSD 3-Clause "New" or "Revised" License | 6 votes |
protected CloseableHttpClient buildHttpClient() { HttpClientBuilder httpClientBuilder = HttpClients.custom(); CredentialsProvider credsProvider = new BasicCredentialsProvider(); try { Credentials credentials = check.getCredentials(); if (credentials != null) { basicAuthentication(httpClientBuilder, credsProvider, credentials); } } catch (Exception ex) { throw new RuntimeException("Could not use credentials"); } // set proxy if (check.getHttpProxyUsername() != null && !check.getHttpProxyPassword().isEmpty()) { credsProvider.setCredentials(new AuthScope(check.getHttpProxyServer(), check.getHttpProxyPort()), new UsernamePasswordCredentials(check.getHttpProxyUsername(), check.getHttpProxyPassword())); httpClientBuilder.setDefaultCredentialsProvider(credsProvider); } return httpClientBuilder.build(); }
Example 5
Source File: HTTPFileClient.java From desktopclient-java with GNU General Public License v3.0 | 5 votes |
private static CloseableHttpClient httpClientOrNull(PrivateKey privateKey, X509Certificate certificate, boolean validateCertificate) { HttpClientBuilder clientBuilder = HttpClients.custom(); try { SSLContext sslContext = TrustUtils.getCustomSSLContext(privateKey, certificate, validateCertificate); clientBuilder.setSSLContext(sslContext); } catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException | KeyManagementException | UnrecoverableKeyException ex) { LOGGER.log(Level.WARNING, "unable to set SSL context", ex); return null; } RequestConfig requestConfig = RequestConfig.custom() // handle redirects :) TODO ? .setRedirectsEnabled(true) // HttpClient bug caused by Lighttpd .setExpectContinueEnabled(false) .setConnectTimeout(10 * 1000) .setSocketTimeout(10 * 1000) .build(); clientBuilder.setDefaultRequestConfig(requestConfig); // create connection manager //ClientConnectionManager connMgr = new SingleClientConnManager(params, registry); return clientBuilder.build(); }
Example 6
Source File: VaultConfig.java From cloudbreak with Apache License 2.0 | 5 votes |
private ClientHttpRequestFactory usingHttpComponents(ClientOptions options, SslConfiguration sslConfiguration) throws GeneralSecurityException, IOException { HttpClientBuilder httpClientBuilder = HttpClients.custom(); httpClientBuilder.setRoutePlanner(new SystemDefaultRoutePlanner( DefaultSchemePortResolver.INSTANCE, ProxySelector.getDefault())); if (isNoneEmpty(httpsProxyUser, httpsProxyPassword)) { UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(httpsProxyUser, httpsProxyPassword); CredentialsProvider provider = new BasicCredentialsProvider(); provider.setCredentials(AuthScope.ANY, credentials); httpClientBuilder.setDefaultCredentialsProvider(provider); } if (hasSslConfiguration(sslConfiguration)) { SSLContext sslContext = getSSLContext(sslConfiguration, getTrustManagers(sslConfiguration)); SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory( sslContext); httpClientBuilder.setSSLSocketFactory(sslSocketFactory); httpClientBuilder.setSSLContext(sslContext); } RequestConfig requestConfig = RequestConfig .custom() .setConnectTimeout(Math.toIntExact(options.getConnectionTimeout().toMillis())) .setSocketTimeout(Math.toIntExact(options.getReadTimeout().toMillis())) .setAuthenticationEnabled(true) .build(); httpClientBuilder.setDefaultRequestConfig(requestConfig); httpClientBuilder.setRedirectStrategy(new LaxRedirectStrategy()); return new HttpComponentsClientHttpRequestFactory(httpClientBuilder.build()); }
Example 7
Source File: HttpManager.java From PicCrawler with Apache License 2.0 | 5 votes |
/** * 获取Http客户端连接对象 * @param timeOut 超时时间 * @param proxy 代理 * @param cookie Cookie * @return Http客户端连接对象 */ private CloseableHttpClient createHttpClient(int timeOut,HttpHost proxy,BasicClientCookie cookie) { // 创建Http请求配置参数 RequestConfig.Builder builder = RequestConfig.custom() // 获取连接超时时间 .setConnectionRequestTimeout(timeOut) // 请求超时时间 .setConnectTimeout(timeOut) // 响应超时时间 .setSocketTimeout(timeOut) .setCookieSpec(CookieSpecs.STANDARD); if (proxy!=null) { builder.setProxy(proxy); } RequestConfig requestConfig = builder.build(); // 创建httpClient HttpClientBuilder httpClientBuilder = HttpClients.custom(); httpClientBuilder // 把请求相关的超时信息设置到连接客户端 .setDefaultRequestConfig(requestConfig) // 把请求重试设置到连接客户端 .setRetryHandler(new RetryHandler()) // 配置连接池管理对象 .setConnectionManager(connManager); if (cookie!=null) { CookieStore cookieStore = new BasicCookieStore(); cookieStore.addCookie(cookie); httpClientBuilder.setDefaultCookieStore(cookieStore); } return httpClientBuilder.build(); }
Example 8
Source File: NexusITSupport.java From nexus-public with Eclipse Public License 1.0 | 5 votes |
protected HttpClientBuilder clientBuilder(final URL nexusUrl, final boolean useCredentials) throws Exception { HttpClientBuilder builder = HttpClients.custom(); builder.setDefaultRequestConfig(requestConfig()); if (useCredentials) { doUseCredentials(nexusUrl, builder); } builder.setSSLSocketFactory(sslSocketFactory()); return builder; }
Example 9
Source File: ApacheSyncClientExecutor.java From BootNettyRpc with Apache License 2.0 | 5 votes |
public void initialize(NettyRpcProperties properties, boolean https) throws Exception { CommonProperties cp = properties.getCommonProperties(); RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(Integer.parseInt(cp.getHttpConnectTimeout())) .setConnectionRequestTimeout(Integer.parseInt(cp.getHttpConnectRequestTimeout())) .setSocketTimeout(Integer.parseInt(cp.getHttpSocketTimeout())) .build(); HttpClientBuilder clientBuilder = HttpClients.custom(); clientBuilder.setDefaultRequestConfig(requestConfig); if (https) { SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() { @Override public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { return true; } }).build(); HostnameVerifier hostnameVerifier = new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession session) { return true; } }; SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier); clientBuilder.setSSLSocketFactory(sslConnectionSocketFactory); } httpSyncClient = clientBuilder.build(); LOG.info("Create apache sync client with {} successfully", https ? "https mode" : "http mode"); }
Example 10
Source File: HttpClientHelper.java From herd with Apache License 2.0 | 5 votes |
/** * Creates a new HTTP client. * * @param trustSelfSignedCertificate specifies whether to trust a self-signed certificate * @param disableHostnameVerification specifies whether to turn off hostname verification * * @return the HTTP client * @throws KeyStoreException if a key store exception occurs * @throws NoSuchAlgorithmException if a no such algorithm exception occurs * @throws KeyManagementException if key management exception */ public CloseableHttpClient createHttpClient(Boolean trustSelfSignedCertificate, Boolean disableHostnameVerification) throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException { // Create an HTTP client builder. HttpClientBuilder httpClientBuilder = HttpClients.custom(); // Create an SSL context builder. SSLContextBuilder sslContextBuilder = SSLContexts.custom(); // If specified, setup a trust strategy that allows all certificates. if (BooleanUtils.isTrue(trustSelfSignedCertificate)) { sslContextBuilder.loadTrustMaterial(null, new TrustSelfSignedStrategy()); } // If specified, turn hostname verification off. HostnameVerifier hostnameVerifier = BooleanUtils.isTrue(disableHostnameVerification) ? SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER : SSLConnectionSocketFactory.STRICT_HOSTNAME_VERIFIER; // Create and assign an SSL connection socket factory. SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContextBuilder.build(), hostnameVerifier); httpClientBuilder.setSSLSocketFactory(sslConnectionSocketFactory); // Build and return an HTTP client. return httpClientBuilder.build(); }
Example 11
Source File: Main.java From git-lfs-migrate with MIT License | 5 votes |
@NotNull private static Client createClient(@NotNull AuthProvider auth, @NotNull CmdArgs cmd) throws GeneralSecurityException { final HttpClientBuilder httpBuilder = HttpClients.custom(); httpBuilder.setUserAgent("git-lfs-migrate"); if (cmd.noCheckCertificate) { httpBuilder.setSSLHostnameVerifier((hostname, session) -> true); httpBuilder.setSSLContext(SSLContexts.custom() .loadTrustMaterial((chain, authType) -> true) .build()); } return new Client(auth, httpBuilder.build()); }
Example 12
Source File: HttpHelper.java From sputnik with Apache License 2.0 | 5 votes |
@NotNull public CloseableHttpClient buildClient(@NotNull HttpHost httpHost, @NotNull ConnectorDetails connectorDetails) { HttpClientBuilder httpClientBuilder = HttpClients.custom(); httpClientBuilder.setDefaultCredentialsProvider(buildBasicCredentialsProvider(httpHost, connectorDetails.getUsername(), connectorDetails.getPassword())); if (connectorDetails.isHttps()) { httpClientBuilder.setSSLSocketFactory(buildSSLSocketFactory(connectorDetails)); } return httpClientBuilder.build(); }
Example 13
Source File: ProxyTest.java From verano-http with MIT License | 5 votes |
@Test public void appendsProxyToClient() { final HttpClientBuilder builder = HttpClients.custom(); MatcherAssert.assertThat( new Proxy("localhost", 8080).apply(builder), new IsEqual<>(builder) ); MatcherAssert.assertThat( builder.build(), new IsNot<>(new IsNull<>()) ); }
Example 14
Source File: VrApacheClient.java From verano-http with MIT License | 5 votes |
/** * Build http client from provided context information. * @param contexts Apache contexts * @return Client Http client */ private static CloseableHttpClient buildHttpClient( final Iterable<ApacheContext> contexts ) { HttpClientBuilder builder = HttpClients.custom(); for (final ApacheContext context : contexts) { builder = context.apply(builder); } return builder.build(); }
Example 15
Source File: NexusITSupport.java From nexus-public with Eclipse Public License 1.0 | 5 votes |
protected HttpClientBuilder clientBuilder(final URL nexusUrl, final boolean useCredentials) throws Exception { HttpClientBuilder builder = HttpClients.custom(); builder.setDefaultRequestConfig(requestConfig()); if (useCredentials) { doUseCredentials(nexusUrl, builder); } builder.setSSLSocketFactory(sslSocketFactory()); return builder; }
Example 16
Source File: HTTPStrictTransportSecurityIT.java From qonduit with Apache License 2.0 | 5 votes |
@Test public void testHttpRequestGet() throws Exception { RequestConfig.Builder req = RequestConfig.custom(); req.setConnectTimeout(5000); req.setConnectionRequestTimeout(5000); req.setRedirectsEnabled(false); req.setSocketTimeout(5000); req.setExpectContinueEnabled(false); HttpGet get = new HttpGet("http://127.0.0.1:54322/login"); get.setConfig(req.build()); PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setDefaultMaxPerRoute(5); HttpClientBuilder builder = HttpClients.custom(); builder.disableAutomaticRetries(); builder.disableRedirectHandling(); builder.setConnectionTimeToLive(5, TimeUnit.SECONDS); builder.setKeepAliveStrategy(DefaultConnectionKeepAliveStrategy.INSTANCE); builder.setConnectionManager(cm); CloseableHttpClient client = builder.build(); String s = client.execute(get, new ResponseHandler<String>() { @Override public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException { assertEquals(301, response.getStatusLine().getStatusCode()); return "success"; } }); assertEquals("success", s); }
Example 17
Source File: ApacheSyncClientExecutor.java From distributed-limit with Apache License 2.0 | 5 votes |
public void initialize(boolean https) throws Exception { RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(Integer.parseInt(HTTPCLIENT_CONNCT_TIMEOUT_DEFAULT)) .setConnectionRequestTimeout(Integer.parseInt(HTTPCLIENT_CONNCT_REQUEST_TIMEOUT_DEFAULT)) .setSocketTimeout(Integer.parseInt(HTTPCLIENT_SOCKET_TIMEOUT_DEFAULT)) .build(); HttpClientBuilder clientBuilder = HttpClients.custom(); clientBuilder.setDefaultRequestConfig(requestConfig); if (https) { SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() { @Override public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { return true; } }).build(); HostnameVerifier hostnameVerifier = new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession session) { return true; } }; SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier); clientBuilder.setSSLSocketFactory(sslConnectionSocketFactory); } httpSyncClient = clientBuilder.build(); LOG.info("Create apache sync client with {} successfully", https ? "https mode" : "http mode"); }
Example 18
Source File: HttpClientConfigurableHttpConnectionFactory.java From spring-cloud-config with Apache License 2.0 | 4 votes |
private HttpClientBuilder lookupHttpClientBuilder(final URL url) { Map<String, HttpClientBuilder> builderMap = this.httpClientBuildersByUri .entrySet().stream().filter(entry -> { String key = entry.getKey(); String spec = getUrlWithPlaceholders(url, key); if (spec.equals(key)) { return true; } int index = spec.lastIndexOf("/"); while (index != -1) { spec = spec.substring(0, index); if (spec.equals(key)) { return true; } index = spec.lastIndexOf("/"); } return false; }).collect(toMap(Map.Entry::getKey, Map.Entry::getValue)); if (builderMap.isEmpty()) { this.log.warn(String.format("No custom http config found for URL: %s", url)); return HttpClients.custom(); } if (builderMap.size() > 1) { /* * Try to determine if there is an exact match URL or not. So if there is a * placeholder in the URL, filter it out. We should be left with only URLs * which have no placeholders. That is the one we want to use in the case * there are multiple matches. */ List<String> keys = builderMap.keySet().stream() .filter(key -> !PLACEHOLDER_PATTERN.matcher(key).find()) .collect(Collectors.toList()); if (keys.size() == 1) { return builderMap.get(keys.get(0)); } this.log.error(String.format( "More than one git repo URL template matched URL:" + " %s, proxy and skipSslValidation config won't be applied. Matched templates: %s", url, builderMap.keySet().stream().collect(Collectors.joining(", ")))); return HttpClients.custom(); } return new ArrayList<>(builderMap.values()).get(0); }
Example 19
Source File: HttpClient.java From ats-framework with Apache License 2.0 | 4 votes |
protected void initialzeInternalClient() { if (!needsInternalClientInialization) { // internal client is already initialized return; } // release any resources if this client was already used close(); // rebuild the client HttpClientBuilder httpClientBuilder = HttpClients.custom(); // Add this interceptor to get the values of all HTTP headers in the request. // Some of them are provided by the user while others are generated by Apache HTTP Components. httpClientBuilder.addInterceptorLast(new HttpRequestInterceptor() { @Override public void process( HttpRequest request, HttpContext context ) throws HttpException, IOException { Header[] requestHeaders = request.getAllHeaders(); actualRequestHeaders = new ArrayList<HttpHeader>(); for (Header header : requestHeaders) { addHeaderToList(actualRequestHeaders, header.getName(), header.getValue()); } if (debugLevel != HttpDebugLevel.NONE) { logHTTPRequest(requestHeaders, request); } } }); // connect and read timeouts httpClientBuilder.setDefaultRequestConfig(RequestConfig.custom() .setConnectTimeout(connectTimeoutSeconds * 1000) .setSocketTimeout(readTimeoutSeconds * 1000) .build()); // socket buffer size if (this.socketBufferSize > 0) { httpClientBuilder.setDefaultSocketConfig(SocketConfig.custom() .setRcvBufSize(this.socketBufferSize) .setSndBufSize(this.socketBufferSize) .build()); } // SSL if (isOverSsl) { setupSSL(httpClientBuilder); } // setup authentication if (!StringUtils.isNullOrEmpty(username)) { setupAuthentication(httpClientBuilder); } // set proxy if (AtsSystemProperties.SYSTEM_HTTP_PROXY_HOST != null && AtsSystemProperties.SYSTEM_HTTP_PROXY_PORT != null) { HttpHost proxy = new HttpHost(AtsSystemProperties.SYSTEM_HTTP_PROXY_HOST, Integer.parseInt(AtsSystemProperties.SYSTEM_HTTP_PROXY_PORT)); DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy); httpClientBuilder.setRoutePlanner(routePlanner); } // now build the client after we have already set everything needed on the client builder httpClient = httpClientBuilder.build(); // do not come here again until not needed needsInternalClientInialization = false; }
Example 20
Source File: DefaultHttpClientFactory.java From knox with Apache License 2.0 | 4 votes |
@Override public HttpClient createHttpClient(FilterConfig filterConfig) { final String serviceRole = filterConfig.getInitParameter(PARAMETER_SERVICE_ROLE); HttpClientBuilder builder; GatewayConfig gatewayConfig = (GatewayConfig) filterConfig.getServletContext().getAttribute(GatewayConfig.GATEWAY_CONFIG_ATTRIBUTE); GatewayServices services = (GatewayServices) filterConfig.getServletContext() .getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE); if (gatewayConfig != null && gatewayConfig.isMetricsEnabled()) { MetricsService metricsService = services.getService(ServiceType.METRICS_SERVICE); builder = metricsService.getInstrumented(HttpClientBuilder.class); } else { builder = HttpClients.custom(); } // Conditionally set a custom SSLContext SSLContext sslContext = createSSLContext(services, filterConfig, serviceRole); if(sslContext != null) { builder.setSSLSocketFactory(new SSLConnectionSocketFactory(sslContext)); } if (Boolean.parseBoolean(System.getProperty(GatewayConfig.HADOOP_KERBEROS_SECURED))) { CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UseJaasCredentials()); Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create() .register(AuthSchemes.SPNEGO, new KnoxSpnegoAuthSchemeFactory(true)) .build(); builder.setDefaultAuthSchemeRegistry(authSchemeRegistry) .setDefaultCookieStore(new HadoopAuthCookieStore(gatewayConfig)) .setDefaultCredentialsProvider(credentialsProvider); } else { builder.setDefaultCookieStore(new NoCookieStore()); } builder.setKeepAliveStrategy( DefaultConnectionKeepAliveStrategy.INSTANCE ); builder.setConnectionReuseStrategy( DefaultConnectionReuseStrategy.INSTANCE ); builder.setRedirectStrategy( new NeverRedirectStrategy() ); builder.setRetryHandler( new NeverRetryHandler() ); int maxConnections = getMaxConnections( filterConfig ); builder.setMaxConnTotal( maxConnections ); builder.setMaxConnPerRoute( maxConnections ); builder.setDefaultRequestConfig(getRequestConfig(filterConfig, serviceRole)); // See KNOX-1530 for details builder.disableContentCompression(); return builder.build(); }