2020import io .split .engine .segments .RefreshableSegmentFetcher ;
2121import io .split .engine .segments .SegmentChangeFetcher ;
2222import io .split .integrations .IntegrationsConfig ;
23- import org .apache .http .auth .AuthScope ;
24- import org .apache .http .auth .Credentials ;
25- import org .apache .http .auth .UsernamePasswordCredentials ;
26- import org .apache .http .client .CredentialsProvider ;
27- import org .apache .http .client .config .CookieSpecs ;
28- import org .apache .http .client .config .RequestConfig ;
29- import org .apache .http .config .Registry ;
30- import org .apache .http .config .RegistryBuilder ;
31- import org .apache .http .conn .socket .ConnectionSocketFactory ;
32- import org .apache .http .conn .socket .PlainConnectionSocketFactory ;
33- import org .apache .http .conn .ssl .SSLConnectionSocketFactory ;
34- import org .apache .http .conn .ssl .SSLContexts ;
35- import org .apache .http .impl .client .BasicCredentialsProvider ;
36- import org .apache .http .impl .client .CloseableHttpClient ;
37- import org .apache .http .impl .client .HttpClientBuilder ;
38- import org .apache .http .impl .client .HttpClients ;
39- import org .apache .http .impl .conn .DefaultProxyRoutePlanner ;
40- import org .apache .http .impl .conn .PoolingHttpClientConnectionManager ;
23+ import org .apache .hc .client5 .http .auth .AuthScope ;
24+ import org .apache .hc .client5 .http .auth .Credentials ;
25+ import org .apache .hc .client5 .http .auth .CredentialsProvider ;
26+ import org .apache .hc .client5 .http .auth .UsernamePasswordCredentials ;
27+ import org .apache .hc .client5 .http .config .RequestConfig ;
28+ import org .apache .hc .client5 .http .cookie .StandardCookieSpec ;
29+ import org .apache .hc .client5 .http .impl .auth .BasicCredentialsProvider ;
30+ import org .apache .hc .client5 .http .impl .classic .CloseableHttpClient ;
31+ import org .apache .hc .client5 .http .impl .classic .HttpClientBuilder ;
32+ import org .apache .hc .client5 .http .impl .classic .HttpClients ;
33+ import org .apache .hc .client5 .http .impl .io .PoolingHttpClientConnectionManager ;
34+ import org .apache .hc .client5 .http .impl .io .PoolingHttpClientConnectionManagerBuilder ;
35+ import org .apache .hc .client5 .http .impl .routing .DefaultProxyRoutePlanner ;
36+ import org .apache .hc .client5 .http .socket .ConnectionSocketFactory ;
37+ import org .apache .hc .client5 .http .socket .PlainConnectionSocketFactory ;
38+ import org .apache .hc .client5 .http .ssl .SSLConnectionSocketFactory ;
39+ import org .apache .hc .client5 .http .ssl .SSLConnectionSocketFactoryBuilder ;
40+ import org .apache .hc .core5 .http .config .Registry ;
41+ import org .apache .hc .core5 .http .config .RegistryBuilder ;
42+ import org .apache .hc .core5 .http .io .SocketConfig ;
43+ import org .apache .hc .core5 .http .ssl .TLS ;
44+ import org .apache .hc .core5 .ssl .SSLContexts ;
45+ import org .apache .hc .core5 .util .Timeout ;
4146import org .slf4j .Logger ;
4247import org .slf4j .LoggerFactory ;
4348
@@ -65,46 +70,55 @@ public class SplitFactoryImpl implements SplitFactory {
6570 private final String _apiToken ;
6671 private boolean isTerminated = false ;
6772
68- public SplitFactoryImpl (String apiToken , SplitClientConfig config ) throws URISyntaxException {
69- _apiToken = apiToken ;
70- SSLContext sslContext = null ;
71- try {
72- sslContext = SSLContexts .custom ()
73- .useTLS ()
74- .build ();
75- } catch (NoSuchAlgorithmException | KeyManagementException e ) {
76- throw new RuntimeException ("Unable to create support for secure connection." );
77- }
73+ private static CloseableHttpClient buildHttpClient (String apiToken , SplitClientConfig config ) {
7874
79- SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory (
80- sslContext ,
81- new String []{"TLSv1.1" , "TLSv1.2" },
82- null ,
83- SSLConnectionSocketFactory .getDefaultHostnameVerifier ());
84-
85- Registry <ConnectionSocketFactory > registry = RegistryBuilder .<ConnectionSocketFactory >create ()
86- .register ("http" , PlainConnectionSocketFactory .getSocketFactory ())
87- .register ("https" , sslsf )
75+ SSLConnectionSocketFactory sslSocketFactory = SSLConnectionSocketFactoryBuilder .create ()
76+ .setSslContext (SSLContexts .createSystemDefault ())
77+ .setTlsVersions (TLS .V_1_1 , TLS .V_1_2 )
8878 .build ();
8979
9080 RequestConfig requestConfig = RequestConfig .custom ()
91- .setConnectTimeout (config .connectionTimeout ())
92- .setSocketTimeout (config .readTimeout ())
93- .setCookieSpec (CookieSpecs .STANDARD )
81+ .setConnectTimeout (Timeout .ofMilliseconds (config .connectionTimeout ()))
82+ .setCookieSpec (StandardCookieSpec .STRICT )
9483 .build ();
9584
96- PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager (registry );
85+ PoolingHttpClientConnectionManager cm = PoolingHttpClientConnectionManagerBuilder .create ()
86+ .setSSLSocketFactory (sslSocketFactory )
87+ .setDefaultSocketConfig (SocketConfig .custom ()
88+ .setSoTimeout (Timeout .ofMilliseconds (config .readTimeout ()))
89+ .build ())
90+ .build ();
9791 cm .setMaxTotal (20 );
9892 cm .setDefaultMaxPerRoute (20 );
9993
10094 HttpClientBuilder httpClientbuilder = HttpClients .custom ()
10195 .setConnectionManager (cm )
10296 .setDefaultRequestConfig (requestConfig )
103- .setSSLSocketFactory (sslsf )
104- .addInterceptorLast (AddSplitHeadersFilter .instance (apiToken , config .ipAddressEnabled ()))
105- .addInterceptorLast (new GzipEncoderRequestInterceptor ())
106- .addInterceptorLast (new GzipDecoderResponseInterceptor ());
97+ .addRequestInterceptorLast (AddSplitHeadersFilter .instance (apiToken , config .ipAddressEnabled ()))
98+ .addRequestInterceptorLast (new GzipEncoderRequestInterceptor ())
99+ .addResponseInterceptorLast ((new GzipDecoderResponseInterceptor ()));
100+
101+ // Set up proxy is it exists
102+ if (config .proxy () != null ) {
103+ _log .info ("Initializing Split SDK with proxy settings" );
104+ DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner (config .proxy ());
105+ httpClientbuilder .setRoutePlanner (routePlanner );
106+
107+ if (config .proxyUsername () != null && config .proxyPassword () != null ) {
108+ _log .debug ("Proxy setup using credentials" );
109+ BasicCredentialsProvider credsProvider = new BasicCredentialsProvider ();
110+ AuthScope siteScope = new AuthScope (config .proxy ().getHostName (), config .proxy ().getPort ());
111+ Credentials siteCreds = new UsernamePasswordCredentials (config .proxyUsername (), config .proxyPassword ().toCharArray ());
112+ credsProvider .setCredentials (siteScope , siteCreds );
113+ httpClientbuilder .setDefaultCredentialsProvider (credsProvider );
114+ }
115+ }
116+
117+ return httpClientbuilder .build ();
118+ }
107119
120+ public SplitFactoryImpl (String apiToken , SplitClientConfig config ) throws URISyntaxException {
121+ _apiToken = apiToken ;
108122
109123 if (USED_API_TOKENS .contains (apiToken )) {
110124 String message = String .format ("factory instantiation: You already have %s with this API Key. " +
@@ -126,24 +140,9 @@ public SplitFactoryImpl(String apiToken, SplitClientConfig config) throws URISyn
126140 "if no ready config has been set when building factory" );
127141
128142 }
129- // Set up proxy is it exists
130- if (config .proxy () != null ) {
131- _log .info ("Initializing Split SDK with proxy settings" );
132- DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner (config .proxy ());
133- httpClientbuilder .setRoutePlanner (routePlanner );
134143
135- if (config .proxyUsername () != null && config .proxyPassword () != null ) {
136- _log .debug ("Proxy setup using credentials" );
137- CredentialsProvider credsProvider = new BasicCredentialsProvider ();
138- AuthScope siteScope = new AuthScope (config .proxy ().getHostName (), config .proxy ().getPort ());
139- Credentials siteCreds = new UsernamePasswordCredentials (config .proxyUsername (), config .proxyPassword ());
140- credsProvider .setCredentials (siteScope , siteCreds );
141-
142- httpClientbuilder .setDefaultCredentialsProvider (credsProvider );
143- }
144- }
145144
146- final CloseableHttpClient httpclient = httpClientbuilder . build ( );
145+ final CloseableHttpClient httpclient = buildHttpClient ( apiToken , config );
147146
148147 URI rootTarget = URI .create (config .endpoint ());
149148 URI eventsRootTarget = URI .create (config .eventsEndpoint ());
0 commit comments