Search in sources :

Example 26 with HttpClientBuilder

use of org.apache.http.impl.client.HttpClientBuilder in project YCSB by brianfrankcooper.

the class RestClient method setupClient.

private void setupClient() {
    RequestConfig.Builder requestBuilder = RequestConfig.custom();
    requestBuilder = requestBuilder.setConnectTimeout(conTimeout);
    requestBuilder = requestBuilder.setConnectionRequestTimeout(readTimeout);
    requestBuilder = requestBuilder.setSocketTimeout(readTimeout);
    HttpClientBuilder clientBuilder = HttpClientBuilder.create().setDefaultRequestConfig(requestBuilder.build());
    this.client = clientBuilder.setConnectionManagerShared(true).build();
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder)

Example 27 with HttpClientBuilder

use of org.apache.http.impl.client.HttpClientBuilder in project gocd by gocd.

the class GoAgentServerHttpClientBuilder method build.

public CloseableHttpClient build() throws Exception {
    HttpClientBuilder builder = HttpClients.custom();
    builder.setDefaultSocketConfig(SocketConfig.custom().setTcpNoDelay(true).setSoKeepAlive(true).build()).setKeepAliveStrategy(DefaultConnectionKeepAliveStrategy.INSTANCE);
    HostnameVerifier hostnameVerifier = sslVerificationMode.verifier();
    TrustStrategy trustStrategy = sslVerificationMode.trustStrategy();
    KeyStore trustStore = agentTruststore();
    SSLContextBuilder sslContextBuilder = SSLContextBuilder.create().useProtocol(systemEnvironment.get(SystemEnvironment.GO_SSL_TRANSPORT_PROTOCOL_TO_BE_USED_BY_AGENT));
    if (trustStore != null || trustStrategy != null) {
        sslContextBuilder.loadTrustMaterial(trustStore, trustStrategy);
    }
    sslContextBuilder.loadKeyMaterial(agentKeystore(), keystorePassword().toCharArray());
    SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContextBuilder.build(), hostnameVerifier);
    builder.setSSLSocketFactory(sslConnectionSocketFactory);
    return builder.build();
}
Also used : TrustStrategy(org.apache.http.conn.ssl.TrustStrategy) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) HostnameVerifier(javax.net.ssl.HostnameVerifier)

Example 28 with HttpClientBuilder

use of org.apache.http.impl.client.HttpClientBuilder in project gradle by gradle.

the class HttpClientHelper method getClient.

private synchronized CloseableHttpClient getClient() {
    if (client == null) {
        HttpClientBuilder builder = HttpClientBuilder.create();
        builder.setRedirectStrategy(new AlwaysRedirectRedirectStrategy());
        new HttpClientConfigurer(settings).configure(builder);
        this.client = builder.build();
    }
    return client;
}
Also used : HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder)

Example 29 with HttpClientBuilder

use of org.apache.http.impl.client.HttpClientBuilder in project openhab1-addons by openhab.

the class Util method getConnection.

public static Sardine getConnection(CalDavConfig config) {
    if (config.isDisableCertificateVerification()) {
        if (config.getUrl().startsWith(HTTP_URL_PREFIX)) {
            log.error("do not use '{}' if no ssl is used", CalDavLoaderImpl.PROP_DISABLE_CERTIFICATE_VERIFICATION);
        }
        log.trace("connecting to caldav '{}' with disabled certificate verification (url={}, username={}, password={})", config.getKey(), config.getUrl(), config.getUsername(), config.getPassword());
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create().setHostnameVerifier(new AllowAllHostnameVerifier());
        try {
            httpClientBuilder.setSslcontext(new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {

                @Override
                public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
                    return true;
                }
            }).build());
        } catch (KeyManagementException e) {
            log.error("error verifying certificate", e);
        } catch (NoSuchAlgorithmException e) {
            log.error("error verifying certificate", e);
        } catch (KeyStoreException e) {
            log.error("error verifying certificate", e);
        }
        if (StringUtils.isEmpty(config.getUsername()) && StringUtils.isEmpty(config.getPassword())) {
            log.trace("connecting without credentials for '{}'", config.getKey());
            return new SardineImpl(httpClientBuilder);
        } else {
            return new SardineImpl(httpClientBuilder, config.getUsername(), config.getPassword());
        }
    } else {
        log.trace("connecting to caldav '{}' (url={}, username={}, password={})", config.getKey(), config.getUrl(), config.getUsername(), config.getPassword());
        if (StringUtils.isEmpty(config.getUsername()) && StringUtils.isEmpty(config.getPassword())) {
            log.trace("connecting without credentials for '{}'", config.getKey());
            return new SardineImpl();
        } else {
            return new SardineImpl(config.getUsername(), config.getPassword());
        }
    }
}
Also used : TrustStrategy(org.apache.http.conn.ssl.TrustStrategy) AllowAllHostnameVerifier(org.apache.http.conn.ssl.AllowAllHostnameVerifier) CertificateException(java.security.cert.CertificateException) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) X509Certificate(java.security.cert.X509Certificate) KeyManagementException(java.security.KeyManagementException) SSLContextBuilder(org.apache.http.conn.ssl.SSLContextBuilder) SardineImpl(com.github.sardine.impl.SardineImpl)

Example 30 with HttpClientBuilder

use of org.apache.http.impl.client.HttpClientBuilder in project camel by apache.

the class HttpComponent method createHttpClientBuilder.

protected HttpClientBuilder createHttpClientBuilder(final String uri, final Map<String, Object> parameters, final Map<String, Object> httpClientOptions) throws Exception {
    // http client can be configured from URI options
    HttpClientBuilder clientBuilder = HttpClientBuilder.create();
    // allow the builder pattern
    httpClientOptions.putAll(IntrospectionSupport.extractProperties(parameters, "httpClient."));
    IntrospectionSupport.setProperties(clientBuilder, httpClientOptions);
    // set the Request configure this way and allow the builder pattern
    RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
    IntrospectionSupport.setProperties(requestConfigBuilder, httpClientOptions);
    clientBuilder.setDefaultRequestConfig(requestConfigBuilder.build());
    // validate that we could resolve all httpClient. parameters as this component is lenient
    validateParameters(uri, httpClientOptions, null);
    return clientBuilder;
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder)

Aggregations

HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)42 RequestConfig (org.apache.http.client.config.RequestConfig)17 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)12 HttpHost (org.apache.http.HttpHost)10 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)10 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)10 SSLContext (javax.net.ssl.SSLContext)9 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)9 CredentialsProvider (org.apache.http.client.CredentialsProvider)9 ConnectionSocketFactory (org.apache.http.conn.socket.ConnectionSocketFactory)9 HttpClient (org.apache.http.client.HttpClient)8 IOException (java.io.IOException)7 HttpResponse (org.apache.http.HttpResponse)7 PlainConnectionSocketFactory (org.apache.http.conn.socket.PlainConnectionSocketFactory)7 AuthScope (org.apache.http.auth.AuthScope)6 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)6 HttpGet (org.apache.http.client.methods.HttpGet)5 HttpPost (org.apache.http.client.methods.HttpPost)5 URI (java.net.URI)4 HttpEntity (org.apache.http.HttpEntity)4