33import org .apache .hc .client5 .http .config .RequestConfig ;
44import org .apache .hc .client5 .http .impl .classic .CloseableHttpClient ;
55import org .apache .hc .client5 .http .impl .classic .HttpClientBuilder ;
6- import org .apache .hc .client5 .http .impl .classic .HttpClients ;
76import org .apache .hc .core5 .http .ClassicHttpRequest ;
87import org .apache .hc .core5 .http .io .entity .EntityUtils ;
98import org .apache .hc .core5 .http .io .support .ClassicRequestBuilder ;
@@ -32,21 +31,23 @@ public enum DEFAULTS {
3231
3332 private final Map <String ,Object > headers ;
3433 private CloseableHttpClient client ;
34+ // client configuration options like timeouts, forwarding ..
35+ private RequestConfig .Builder clientConfigBuilder ;
3536
3637 private boolean secureConnection = true ;
37- private int connectTimeoutSeconds = DEFAULTS .CONNECT_TIMEOUT_SECONDS .value ;
38- private int readTimeoutSeconds = DEFAULTS .READ_TIMEOUT_SECONDS .value ;
3938
4039 public HttpClient (Map <String ,Object > headers , int connectTimeoutSeconds , int readTimeoutSeconds ) {
4140 this .headers = headers ;
42- this .connectTimeoutSeconds = connectTimeoutSeconds ;
43- this .readTimeoutSeconds = readTimeoutSeconds ;
44- buildClientWithCustomConfig ();
41+ this .clientConfigBuilder = RequestConfig
42+ .custom ()
43+ .setConnectTimeout (Timeout .ofSeconds (connectTimeoutSeconds ))
44+ .setResponseTimeout (Timeout .ofSeconds (readTimeoutSeconds ));
45+
46+ this .client = buildClient ();
4547 }
4648
4749 public HttpClient (Map <String ,Object > headers ) {
48- this .headers = headers ;
49- buildClientWithCustomConfig ();
50+ this (headers , DEFAULTS .CONNECT_TIMEOUT_SECONDS .value , DEFAULTS .READ_TIMEOUT_SECONDS .value );
5051 }
5152
5253 /**
@@ -108,13 +109,13 @@ public ClientResponse execute(REQUEST_TYPES requestType, String url, String data
108109 // Setters and Getters
109110
110111 public void setConnectTimeoutSeconds (int connectTimeoutSeconds ) {
111- this . connectTimeoutSeconds = connectTimeoutSeconds ;
112- buildClientWithCustomConfig ();
112+ clientConfigBuilder . setConnectTimeout ( Timeout . ofSeconds ( connectTimeoutSeconds )) ;
113+ this . client = buildClient ();
113114 }
114115
115116 public void setReadTimeoutSeconds (int readTimeoutSeconds ) {
116- this . readTimeoutSeconds = readTimeoutSeconds ;
117- buildClientWithCustomConfig ();
117+ clientConfigBuilder . setResponseTimeout ( Timeout . ofSeconds ( readTimeoutSeconds )) ;
118+ this . client = buildClient ();
118119 }
119120
120121 public void setSecureConnection (boolean secureConnection ) {
@@ -141,22 +142,7 @@ public CloseableHttpClient getClient() {
141142 * @return initialized HTTP client
142143 */
143144 private CloseableHttpClient buildClient () {
144- return HttpClients .createDefault ();
145- }
146-
147- /**
148- * Build HTTP client used for requests with custom config settings
149- *
150- * @return initialized HTTP client
151- */
152- private void buildClientWithCustomConfig () {
153- RequestConfig requestConfig = RequestConfig
154- .custom ()
155- .setConnectTimeout (Timeout .ofSeconds (connectTimeoutSeconds ))
156- .setResponseTimeout (Timeout .ofSeconds (readTimeoutSeconds ))
157- .build ();
158-
159- this .client = HttpClientBuilder .create ().setDefaultRequestConfig (requestConfig ).build ();
145+ return HttpClientBuilder .create ().setDefaultRequestConfig (clientConfigBuilder .build ()).build ();
160146 }
161147
162148 /**
0 commit comments