88import com .cloudinary .strategies .AbstractApiStrategy ;
99import com .cloudinary .utils .ObjectUtils ;
1010import org .apache .hc .client5 .http .classic .methods .*;
11+ import org .apache .hc .client5 .http .config .RequestConfig ;
1112import org .apache .hc .client5 .http .entity .UrlEncodedFormEntity ;
1213import org .apache .hc .client5 .http .impl .classic .CloseableHttpClient ;
1314import org .apache .hc .client5 .http .impl .classic .CloseableHttpResponse ;
15+ import org .apache .hc .client5 .http .impl .classic .HttpClientBuilder ;
1416import org .apache .hc .client5 .http .impl .classic .HttpClients ;
17+ import org .apache .hc .client5 .http .io .HttpClientConnectionManager ;
1518import org .apache .hc .core5 .http .HttpEntity ;
19+ import org .apache .hc .core5 .http .HttpHost ;
1620import org .apache .hc .core5 .http .NameValuePair ;
1721import org .apache .hc .core5 .http .io .entity .EntityUtils ;
1822import org .apache .hc .core5 .http .io .entity .StringEntity ;
1923import org .apache .hc .core5 .net .URIBuilder ;
24+ import org .apache .hc .core5 .util .Timeout ;
2025import org .cloudinary .json .JSONException ;
2126import org .cloudinary .json .JSONObject ;
2227
@@ -36,15 +41,42 @@ public class ApiStrategy extends AbstractApiStrategy {
3641
3742 private CloseableHttpClient client ;
3843
39- @ Override
4044 public void init (Api api ) {
4145 super .init (api );
4246
43- this .client = HttpClients .custom ()
44- .setUserAgent (this .api .cloudinary .getUserAgent () + " ApacheHttpClient/" + APACHE_HTTP_CLIENT_VERSION )
47+ HttpClientBuilder clientBuilder = HttpClients .custom ();
48+ clientBuilder .useSystemProperties ().setUserAgent (this .api .cloudinary .getUserAgent () + " ApacheHttpClient/" + APACHE_HTTP_CLIENT_VERSION );
49+
50+ HttpClientConnectionManager connectionManager = (HttpClientConnectionManager ) api .cloudinary .config .properties .get ("connectionManager" );
51+ if (connectionManager != null ) {
52+ clientBuilder .setConnectionManager (connectionManager );
53+ }
54+
55+ RequestConfig requestConfig = buildRequestConfig ();
56+
57+ client = clientBuilder
58+ .setDefaultRequestConfig (requestConfig )
4559 .build ();
4660 }
4761
62+ public RequestConfig buildRequestConfig () {
63+ RequestConfig .Builder requestConfigBuilder = RequestConfig .custom ();
64+
65+ if (api .cloudinary .config .proxyHost != null && api .cloudinary .config .proxyPort != 0 ) {
66+ HttpHost proxy = new HttpHost (api .cloudinary .config .proxyHost , api .cloudinary .config .proxyPort );
67+ requestConfigBuilder .setProxy (proxy );
68+ }
69+
70+ int timeout = this .api .cloudinary .config .timeout ;
71+ if (timeout > 0 ) {
72+ requestConfigBuilder .setResponseTimeout (Timeout .ofSeconds (timeout ))
73+ .setConnectionRequestTimeout (Timeout .ofSeconds (timeout ))
74+ .setConnectTimeout (Timeout .ofSeconds (timeout ));
75+ }
76+
77+ return requestConfigBuilder .build ();
78+ }
79+
4880 @ SuppressWarnings ({"rawtypes" , "unchecked" })
4981 public ApiResponse callApi (Api .HttpMethod method , String apiUrl , Map <String , ?> params , Map options , String autorizationHeader ) throws Exception {
5082 HttpUriRequestBase request = prepareRequest (method , apiUrl , params , options );
0 commit comments