Search in sources :

Example 1 with Builder

use of org.apache.http.client.config.RequestConfig.Builder in project cloudbreak by hortonworks.

the class ConsulUtils method createHttpClient.

private static HttpClient createHttpClient(String clientCert, String clientKey, String serverCert) throws Exception {
    SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom().loadTrustMaterial(KeyStoreUtil.createTrustStore(serverCert), null).loadKeyMaterial(KeyStoreUtil.createKeyStore(clientCert, clientKey), "consul".toCharArray()).build();
    try (PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(setupSchemeRegistry(sslContext))) {
        connectionManager.setMaxTotal(MAX_CONNECTION);
        connectionManager.setDefaultMaxPerRoute(MAX_ROUTE);
        Builder requestBuilder = RequestConfig.custom().setConnectTimeout(DEFAULT_TIMEOUT_MS).setConnectionRequestTimeout(DEFAULT_TIMEOUT_MS).setSocketTimeout(DEFAULT_TIMEOUT_MS);
        return HttpClientBuilder.create().setConnectionManager(connectionManager).setDefaultRequestConfig(requestBuilder.build()).build();
    }
}
Also used : RegistryBuilder(org.apache.http.config.RegistryBuilder) Builder(org.apache.http.client.config.RequestConfig.Builder) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) SSLContext(javax.net.ssl.SSLContext) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Example 2 with Builder

use of org.apache.http.client.config.RequestConfig.Builder in project webtools.sourceediting by eclipse.

the class HttpClientProvider method download.

private static File download(File file, URL url) {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    CloseableHttpResponse response = null;
    OutputStream out = null;
    file.getParentFile().mkdirs();
    try {
        HttpHost target = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
        Builder builder = RequestConfig.custom();
        HttpHost proxy = getProxy(target);
        if (proxy != null) {
            builder = builder.setProxy(proxy);
        }
        RequestConfig config = builder.build();
        HttpGet request = new HttpGet(url.toURI());
        request.setConfig(config);
        response = httpclient.execute(target, request);
        InputStream in = response.getEntity().getContent();
        out = new BufferedOutputStream(new FileOutputStream(file));
        copy(in, out);
        return file;
    } catch (Exception e) {
        logWarning(e);
        ;
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
            }
        }
        if (response != null) {
            try {
                response.close();
            } catch (IOException e) {
            }
        }
        try {
            httpclient.close();
        } catch (IOException e) {
        }
    }
    return null;
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestConfig(org.apache.http.client.config.RequestConfig) HttpHost(org.apache.http.HttpHost) InputStream(java.io.InputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Builder(org.apache.http.client.config.RequestConfig.Builder) HttpGet(org.apache.http.client.methods.HttpGet) FileOutputStream(java.io.FileOutputStream) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException)

Example 3 with Builder

use of org.apache.http.client.config.RequestConfig.Builder in project spring-security-oauth by spring-projects.

the class ServerRunning method createRestTemplate.

public RestOperations createRestTemplate() {
    RestTemplate client = new RestTemplate();
    client.setRequestFactory(new HttpComponentsClientHttpRequestFactory() {

        @Override
        protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {
            HttpClientContext context = HttpClientContext.create();
            Builder builder = RequestConfig.custom().setCookieSpec(CookieSpecs.IGNORE_COOKIES).setAuthenticationEnabled(false).setRedirectsEnabled(false);
            context.setRequestConfig(builder.build());
            return context;
        }
    });
    client.setErrorHandler(new DefaultResponseErrorHandler() {

        @Override
        public boolean hasError(ClientHttpResponse response) throws IOException {
            return false;
        }
    });
    return client;
}
Also used : DefaultResponseErrorHandler(org.springframework.web.client.DefaultResponseErrorHandler) Builder(org.apache.http.client.config.RequestConfig.Builder) RestTemplate(org.springframework.web.client.RestTemplate) HttpContext(org.apache.http.protocol.HttpContext) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) HttpComponentsClientHttpRequestFactory(org.springframework.http.client.HttpComponentsClientHttpRequestFactory) IOException(java.io.IOException) URI(java.net.URI) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) HttpMethod(org.springframework.http.HttpMethod)

Example 4 with Builder

use of org.apache.http.client.config.RequestConfig.Builder in project lucene-solr by apache.

the class HttpClientUtil method setupBuilder.

private static HttpClientBuilder setupBuilder(HttpClientBuilder builder, SolrParams config) {
    Builder requestConfigBuilder = RequestConfig.custom().setRedirectsEnabled(config.getBool(HttpClientUtil.PROP_FOLLOW_REDIRECTS, false)).setDecompressionEnabled(false).setConnectTimeout(config.getInt(HttpClientUtil.PROP_CONNECTION_TIMEOUT, DEFAULT_CONNECT_TIMEOUT)).setSocketTimeout(config.getInt(HttpClientUtil.PROP_SO_TIMEOUT, DEFAULT_SO_TIMEOUT));
    String cpolicy = cookiePolicy;
    if (cpolicy != null) {
        requestConfigBuilder.setCookieSpec(cpolicy);
    }
    RequestConfig requestConfig = requestConfigBuilder.build();
    HttpClientBuilder retBuilder = builder.setDefaultRequestConfig(requestConfig);
    if (config.getBool(HttpClientUtil.PROP_USE_RETRY, true)) {
        retBuilder = retBuilder.setRetryHandler(new SolrHttpRequestRetryHandler(3));
    } else {
        retBuilder = retBuilder.setRetryHandler(NO_RETRY);
    }
    final String basicAuthUser = config.get(HttpClientUtil.PROP_BASIC_AUTH_USER);
    final String basicAuthPass = config.get(HttpClientUtil.PROP_BASIC_AUTH_PASS);
    if (basicAuthUser != null && basicAuthPass != null) {
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(basicAuthUser, basicAuthPass));
        retBuilder.setDefaultCredentialsProvider(credsProvider);
    }
    if (config.getBool(HttpClientUtil.PROP_ALLOW_COMPRESSION, false)) {
        retBuilder.addInterceptorFirst(new UseCompressionRequestInterceptor());
        retBuilder.addInterceptorFirst(new UseCompressionResponseInterceptor());
    } else {
        retBuilder.disableContentCompression();
    }
    return retBuilder;
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) RegistryBuilder(org.apache.http.config.RegistryBuilder) Builder(org.apache.http.client.config.RequestConfig.Builder) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 5 with Builder

use of org.apache.http.client.config.RequestConfig.Builder in project lucene-solr by apache.

the class HttpClientUtil method createDefaultRequestConfigBuilder.

public static Builder createDefaultRequestConfigBuilder() {
    String cpolicy = cookiePolicy;
    Builder builder = RequestConfig.custom();
    builder.setSocketTimeout(DEFAULT_SO_TIMEOUT).setConnectTimeout(DEFAULT_CONNECT_TIMEOUT).setRedirectsEnabled(false).setDecompressionEnabled(// we do our own compression / decompression
    false);
    if (cpolicy != null) {
        builder.setCookieSpec(cpolicy);
    }
    return builder;
}
Also used : RegistryBuilder(org.apache.http.config.RegistryBuilder) Builder(org.apache.http.client.config.RequestConfig.Builder) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder)

Aggregations

Builder (org.apache.http.client.config.RequestConfig.Builder)11 IOException (java.io.IOException)5 RequestConfig (org.apache.http.client.config.RequestConfig)5 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)4 URISyntaxException (java.net.URISyntaxException)3 HttpHost (org.apache.http.HttpHost)3 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)3 RegistryBuilder (org.apache.http.config.RegistryBuilder)3 SSLContext (javax.net.ssl.SSLContext)2 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)2 CredentialsProvider (org.apache.http.client.CredentialsProvider)2 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)2 HttpContext (org.apache.http.protocol.HttpContext)2 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 JoynrChannelMissingException (io.joynr.exceptions.JoynrChannelMissingException)1 JoynrCommunicationException (io.joynr.exceptions.JoynrCommunicationException)1 JoynrDelayMessageException (io.joynr.exceptions.JoynrDelayMessageException)1 JoynrMessageNotSentException (io.joynr.exceptions.JoynrMessageNotSentException)1 JoynrShutdownException (io.joynr.exceptions.JoynrShutdownException)1 JoynrMessagingError (io.joynr.messaging.datatypes.JoynrMessagingError)1