Package org.apache.http.impl.client

Examples of org.apache.http.impl.client.HttpClientBuilder


* @author jamesdbloom
*/
public abstract class AbstractClientProxyIntegrationTest {

    protected HttpClient createHttpClient() throws Exception {
        HttpClientBuilder httpClientBuilder = HttpClients
                .custom()
                .setSslcontext(SSLFactory.getInstance().sslContext())
                .setHostnameVerifier(new AllowAllHostnameVerifier());
        if (Boolean.parseBoolean(System.getProperty("defaultProxySet"))) {
            httpClientBuilder.setRoutePlanner(new SystemDefaultRoutePlanner(ProxySelector.getDefault())).build();
        } else if (Boolean.parseBoolean(System.getProperty("proxySet"))) {
            HttpHost httpHost = new HttpHost(System.getProperty("http.proxyHost"), Integer.parseInt(System.getProperty("http.proxyPort")));
            DefaultProxyRoutePlanner defaultProxyRoutePlanner = new DefaultProxyRoutePlanner(httpHost);
            httpClientBuilder.setRoutePlanner(defaultProxyRoutePlanner).build();
        } else {
            HttpHost httpHost = new HttpHost("localhost", getProxyPort());
            DefaultProxyRoutePlanner defaultProxyRoutePlanner = new DefaultProxyRoutePlanner(httpHost);
            httpClientBuilder.setRoutePlanner(defaultProxyRoutePlanner);
        }
        return httpClientBuilder.build();
    }
View Full Code Here


    @Override
    protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
        Map<String, Object> httpClientParameters = new HashMap<String, Object>(parameters);
        // http client can be configured from URI options
        HttpClientBuilder clientBuilder = HttpClientBuilder.create();
        // allow the builder pattern
        IntrospectionSupport.setProperties(clientBuilder, parameters, "httpClient.", true);
        // set the Request configure this way and allow the builder pattern
        RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
        IntrospectionSupport.setProperties(requestConfigBuilder, parameters, "httpClient.", true);
        clientBuilder.setDefaultRequestConfig(requestConfigBuilder.build());
       
        // validate that we could resolve all httpClient. parameters as this component is lenient
        validateParameters(uri, parameters, "httpClient.");
       
        // TODO cmueller: remove the "httpBindingRef" look up in Camel 3.0
View Full Code Here

    connManager.setMaxTotal(20);

    ConnectionConfig connectionConfig = ConnectionConfig.custom().setCharset(Consts.UTF_8).build();
    connManager.setDefaultConnectionConfig(connectionConfig);

    HttpClientBuilder clientBuilder = HttpClients.custom().setConnectionManager(connManager);

    Integer timeout = new Long(Utils.parseTimeValue(config, CFG_TIMEOUT, 5, TimeUnit.SECONDS)).intValue();

    if (timeout != null) {
      RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeout).setConnectTimeout(timeout).build();
      clientBuilder.setDefaultRequestConfig(requestConfig);
    }

    String remoteUsername = Utils.trimToNull(XContentMapValues.nodeStringValue(config.get(CFG_USERNAME), null));
    String remotePassword = XContentMapValues.nodeStringValue(config.get(CFG_PASSWORD), null);
    if (remoteUsername != null) {
      if (remotePassword == null && pwdLoader != null)
        remotePassword = pwdLoader.loadPassword(remoteUsername);
      if (remotePassword != null) {
        try {
          URL urlParsed = new URL(url);
          String host = urlParsed.getHost();
          CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
          credentialsProvider.setCredentials(new AuthScope(host, AuthScope.ANY_PORT), new UsernamePasswordCredentials(
              remoteUsername, remotePassword));
          clientBuilder.setDefaultCredentialsProvider(credentialsProvider);
          isAuthConfigured = true;
        } catch (MalformedURLException e) {
          // this should never happen due validation before
        }
      } else {
        logger.warn("Password not found so authentication is not used!");
        remoteUsername = null;
      }
    } else {
      remoteUsername = null;
    }
    httpclient = clientBuilder.build();
    return remoteUsername;
  }
View Full Code Here

    private ApacheHttpClientToMockServerResponseMapper apacheHttpClientToMockServerResponseMapper = new ApacheHttpClientToMockServerResponseMapper();
    private CloseableHttpClient httpClient;

    public ApacheHttpClient(boolean isSecure) {
        try {
            HttpClientBuilder httpClientBuilder = HttpClients.custom().disableCookieManagement();
            if (isSecure) {
                httpClientBuilder
                        .setSslcontext(
                                SSLContexts
                                        .custom()
                                        .loadTrustMaterial(SSLFactory.getInstance().buildKeyStore(), trustStrategy)
                                        .build()
                        )
                        .setHostnameVerifier(new AllowAllHostnameVerifier());
            }

            // TODO(jamesdbloom) the section below if commented out as it is still experimental
//            System.getProperty("http.keepAlive", "false");
//            httpClientBuilder
//                    .setKeepAliveStrategy(new ConnectionKeepAliveStrategy() {
//                        public long getKeepAliveDuration(org.apache.http.HttpResponse response, HttpContext context) {
//                            return 0;
//                        }
//                    })
//                    .setDefaultRequestConfig(
//                            RequestConfig
//                                    .custom()
//                                    .setConnectionRequestTimeout(new Long(maxTimeout()).intValue())
//                                    .setConnectTimeout(new Long(maxTimeout()).intValue())
//                                    .build()
//                    )
//                    .setDefaultSocketConfig(
//                            SocketConfig
//                                    .custom()
//                                    .setSoTimeout((int) TimeUnit.SECONDS.toMillis(10))
//                                    .setSoLinger((int) TimeUnit.SECONDS.toMillis(10))
//                                    .setSoKeepAlive(true)
//                                    .build()
//                    );
            this.httpClient = httpClientBuilder.build();
        } catch (Exception e) {
            throw new RuntimeException("Exception creating http client", e);
        }
    }
View Full Code Here

        } catch (Exception e) {
            LOGGER.error("can't set TLS Support. Error is: {}", e, e);
        }

        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create()
                .setMaxConnPerRoute(metadata.getMaxConnectionsPerAddress())
                .setMaxConnTotal(metadata.getMaxConnectionsTotal())
                .setDefaultRequestConfig(requestConfig)
                .setKeepAliveStrategy(new InfraConnectionKeepAliveStrategy(metadata.getIdleTimeout()))
                .setSslcontext(sslContext);

        if (!followRedirects) {
            httpClientBuilder.disableRedirectHandling();
        }

        httpClient = httpClientBuilder.build();

        httpAsyncClient = HttpAsyncClients.custom()
                .setDefaultRequestConfig(requestConfig)
                .setMaxConnPerRoute(metadata.getMaxConnectionsPerAddress())
                .setMaxConnTotal(metadata.getMaxConnectionsTotal())
View Full Code Here

  protected CloseableHttpClient createHttpClient(GoogleAnalyticsConfig config) {
    PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
    connManager.setDefaultMaxPerRoute(getDefaultMaxPerRoute(config));

    HttpClientBuilder builder = HttpClients.custom().setConnectionManager(connManager);

    if (isNotEmpty(config.getUserAgent())) {
      builder.setUserAgent(config.getUserAgent());
    }

    if (isNotEmpty(config.getProxyHost())) {
      builder.setProxy(new HttpHost(config.getProxyHost(), config.getProxyPort()));

      if (isNotEmpty(config.getProxyUserName())) {
        BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(new AuthScope(config.getProxyHost(), config.getProxyPort()),
            new UsernamePasswordCredentials(config.getProxyUserName(), config.getProxyPassword()));
        builder.setDefaultCredentialsProvider(credentialsProvider);
      }
    }

    return builder.build();
  }
View Full Code Here

  protected CloseableHttpClient createHttpClient(GoogleAnalyticsConfig config) {
    PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
    connManager.setDefaultMaxPerRoute(getDefaultMaxPerRoute(config));

    HttpClientBuilder builder = HttpClients.custom().setConnectionManager(connManager);

    if (isNotEmpty(config.getUserAgent())) {
      builder.setUserAgent(config.getUserAgent());
    }

    if (isNotEmpty(config.getProxyHost())) {
      builder.setProxy(new HttpHost(config.getProxyHost(), config.getProxyPort()));

      if (isNotEmpty(config.getProxyUserName())) {
        BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(new AuthScope(config.getProxyHost(), config.getProxyPort()),
            new UsernamePasswordCredentials(config.getProxyUserName(), config.getProxyPassword()));
        builder.setDefaultCredentialsProvider(credentialsProvider);
      }
    }

    return builder.build();
  }
View Full Code Here

                .setAsynchronousWorkersCore(1)
                .setRevalidationQueueSize(100)
                .setSharedCache(true)
                .build();

        final HttpClientBuilder clientBuilder = CachingHttpClientBuilder.create().setCacheConfig(cacheConfig);
        clientBuilder.setMaxConnTotal(1);
        clientBuilder.setMaxConnPerRoute(1);

        final RequestConfig config = RequestConfig.custom()
                .setSocketTimeout(10000)
                .setConnectTimeout(10000)
                .setConnectionRequestTimeout(1000)
                .build();

        clientBuilder.setDefaultRequestConfig(config);


        client = clientBuilder.build();
    }
View Full Code Here

    @Override
    protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
        Map<String, Object> httpClientParameters = new HashMap<String, Object>(parameters);
        // http client can be configured from URI options
        HttpClientBuilder clientBuilder = HttpClientBuilder.create();
        // allow the builder pattern
        IntrospectionSupport.setProperties(clientBuilder, parameters, "httpClient.", true);
        // set the Request configure this way and allow the builder pattern
        RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
        IntrospectionSupport.setProperties(requestConfigBuilder, parameters, "httpClient.", true);
        clientBuilder.setDefaultRequestConfig(requestConfigBuilder.build());
       
        // validate that we could resolve all httpClient. parameters as this component is lenient
        validateParameters(uri, parameters, "httpClient.");
       
        // TODO cmueller: remove the "httpBindingRef" look up in Camel 3.0
View Full Code Here

    @Override
    protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
        Map<String, Object> httpClientParameters = new HashMap<String, Object>(parameters);
        // http client can be configured from URI options
        HttpClientBuilder clientBuilder = HttpClientBuilder.create();
        // allow the builder pattern
        IntrospectionSupport.setProperties(clientBuilder, parameters, "httpClient.", true);
        // set the Request configure this way and allow the builder pattern
        RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
        IntrospectionSupport.setProperties(requestConfigBuilder, parameters, "httpClient.", true);
        clientBuilder.setDefaultRequestConfig(requestConfigBuilder.build());
       
        // validate that we could resolve all httpClient. parameters as this component is lenient
        validateParameters(uri, parameters, "httpClient.");
       
        // TODO cmueller: remove the "httpBindingRef" look up in Camel 3.0
View Full Code Here

TOP

Related Classes of org.apache.http.impl.client.HttpClientBuilder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.