protected void setProxyHost(HttpClientBuilder httpClientBuilder) {
    if ((_proxyHostName == null) || _proxyHostName.equals("")) {
      return;
    }

    HttpHost httpHost = new HttpHost(_proxyHostName, _proxyHostPort);

    HttpRoutePlanner httpRoutePlanner = new DefaultProxyRoutePlanner(httpHost);

    httpClientBuilder.setRoutePlanner(httpRoutePlanner);
  }
Example #2
0
  @Override
  public void configure(final Config conf) {

    super.configure(conf);

    this.maxContent = ConfUtils.getInt(conf, "http.content.limit", -1);

    String userAgent =
        getAgentString(
            ConfUtils.getString(conf, "http.agent.name"),
            ConfUtils.getString(conf, "http.agent.version"),
            ConfUtils.getString(conf, "http.agent.description"),
            ConfUtils.getString(conf, "http.agent.url"),
            ConfUtils.getString(conf, "http.agent.email"));

    builder =
        HttpClients.custom()
            .setUserAgent(userAgent)
            .setConnectionManager(CONNECTION_MANAGER)
            .setConnectionManagerShared(true)
            .disableRedirectHandling()
            .disableAutomaticRetries();

    String proxyHost = ConfUtils.getString(conf, "http.proxy.host", null);
    int proxyPort = ConfUtils.getInt(conf, "http.proxy.port", 8080);

    boolean useProxy = proxyHost != null && proxyHost.length() > 0;

    // use a proxy?
    if (useProxy) {
      HttpHost proxy = new HttpHost(proxyHost, proxyPort);
      DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
      builder.setRoutePlanner(routePlanner);
    }

    int timeout = ConfUtils.getInt(conf, "http.timeout", 10000);

    Builder requestConfigBuilder = RequestConfig.custom();
    requestConfigBuilder.setSocketTimeout(timeout);
    requestConfigBuilder.setConnectTimeout(timeout);
    requestConfigBuilder.setConnectionRequestTimeout(timeout);
    requestConfig = requestConfigBuilder.build();
  }
  public Session(
      URL url, String login, String password, boolean trustSelfSigned, int maxConnections) {

    _executorService = Executors.newFixedThreadPool(maxConnections);

    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();

    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();

    credentialsProvider.setCredentials(
        new AuthScope(url.getHost(), url.getPort()),
        new UsernamePasswordCredentials(login, password));

    httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);

    httpClientBuilder.setMaxConnPerRoute(maxConnections);
    httpClientBuilder.setMaxConnTotal(maxConnections);
    httpClientBuilder.setRoutePlanner(_getHttpRoutePlanner());

    if (trustSelfSigned) {
      try {
        SSLContextBuilder sslContextBuilder = new SSLContextBuilder();

        sslContextBuilder.loadTrustMaterial(null, new TrustSelfSignedStrategy());

        SSLConnectionSocketFactory sslConnectionSocketFactory =
            new SSLConnectionSocketFactory(
                sslContextBuilder.build(), SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        httpClientBuilder.setSSLSocketFactory(sslConnectionSocketFactory);
      } catch (Exception e) {
        _logger.error(e.getMessage(), e);
      }
    }

    _httpClient = httpClientBuilder.build();

    _httpHost = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
  }