private CloseableHttpClient buildClient ( boolean trustSelfSigned ) { try { // if required, define custom SSL context allowing self-signed certs SSLContext sslContext = ! trustSelfSigned ? SSLContexts . createSystemDefault ( ) : SSLContexts . custom ( ) . loadTrustMaterial ( null , new TrustSelfSignedStrategy ( ) ) . build ( ) ; // set timeouts for the HTTP client int globalTimeout = readFromProperty ( "bdTimeout" , 100000 ) ; int connectTimeout = readFromProperty ( "bdConnectTimeout" , globalTimeout ) ; int connectionRequestTimeout = readFromProperty ( "bdConnectionRequestTimeout" , globalTimeout ) ; int socketTimeout = readFromProperty ( "bdSocketTimeout" , globalTimeout ) ; RequestConfig requestConfig = RequestConfig . copy ( RequestConfig . DEFAULT ) . setConnectTimeout ( connectTimeout ) . setSocketTimeout ( socketTimeout ) . setConnectionRequestTimeout ( connectionRequestTimeout ) . build ( ) ; // configure caching CacheConfig cacheConfig = CacheConfig . copy ( CacheConfig . DEFAULT ) . setSharedCache ( false ) . setMaxCacheEntries ( 1000 ) . setMaxObjectSize ( 2 * 1024 * 1024 ) . build ( ) ; // configure connection pooling PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager ( RegistryBuilder . < ConnectionSocketFactory > create ( ) . register ( "http" , PlainConnectionSocketFactory . getSocketFactory ( ) ) . register ( "https" , new SSLConnectionSocketFactory ( sslContext ) ) . build ( ) ) ; int connectionLimit = readFromProperty ( "bdMaxConnections" , 40 ) ; // there's only one server to connect to, so max per route matters connManager . setMaxTotal ( connectionLimit ) ; connManager . setDefaultMaxPerRoute ( connectionLimit ) ; // create the HTTP client return CachingHttpClientBuilder . create ( ) . setCacheConfig ( cacheConfig ) . setDefaultRequestConfig ( requestConfig ) . setConnectionManager ( connManager ) . build ( ) ; } catch ( GeneralSecurityException e ) { throw new InternalConfigurationException ( "Failed to set up SSL context" , e ) ; } }
Builds the HTTP client to connect to the server .