All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.signalfx.metrics.connection.HttpClientConnectionManagerFactory Maven / Gradle / Ivy

The newest version!
package com.signalfx.metrics.connection;

import java.io.IOException;

import javax.net.ssl.SSLSocket;

import com.signalfx.shaded.apache.http.config.RegistryBuilder;
import com.signalfx.shaded.apache.http.config.SocketConfig;
import com.signalfx.shaded.apache.http.conn.HttpClientConnectionManager;
import com.signalfx.shaded.apache.http.conn.socket.ConnectionSocketFactory;
import com.signalfx.shaded.apache.http.conn.socket.PlainConnectionSocketFactory;
import com.signalfx.shaded.apache.http.conn.ssl.SSLConnectionSocketFactory;
import com.signalfx.shaded.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import com.signalfx.shaded.apache.http.ssl.SSLContexts;

public class HttpClientConnectionManagerFactory {

  private HttpClientConnectionManagerFactory() {
    // prevent instantiation
  }

  public static HttpClientConnectionManager withTimeoutMs(int timeoutMs) {
    PoolingHttpClientConnectionManager httpClientConnectionManager = new PoolingHttpClientConnectionManager(
        RegistryBuilder.create()
            .register("http", PlainConnectionSocketFactory.getSocketFactory())
            .register("https", new SSLConnectionSocketFactoryWithTimeout(timeoutMs))
            .build());

    httpClientConnectionManager.setDefaultSocketConfig(
        SocketConfig.custom().setSoTimeout(timeoutMs).build());

    return httpClientConnectionManager;
  }

  /**
   * Uses STRICT_HOSTNAME_VERIFIER and sets a socket timeout before attempting the SSL handshake
   */
  private static class SSLConnectionSocketFactoryWithTimeout extends SSLConnectionSocketFactory {
    private final int timeoutMs;

    public SSLConnectionSocketFactoryWithTimeout(int timeoutMs) {
      super(SSLContexts.createDefault(), SSLConnectionSocketFactory.STRICT_HOSTNAME_VERIFIER);
      this.timeoutMs = timeoutMs;
    }

    @Override
    protected void prepareSocket(SSLSocket socket) throws IOException {
      socket.setSoTimeout(timeoutMs);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy