8
8
import com .cloudinary .strategies .AbstractApiStrategy ;
9
9
import com .cloudinary .utils .ObjectUtils ;
10
10
import org .apache .hc .client5 .http .classic .methods .*;
11
+ import org .apache .hc .client5 .http .config .RequestConfig ;
11
12
import org .apache .hc .client5 .http .entity .UrlEncodedFormEntity ;
12
13
import org .apache .hc .client5 .http .impl .classic .CloseableHttpClient ;
13
14
import org .apache .hc .client5 .http .impl .classic .CloseableHttpResponse ;
15
+ import org .apache .hc .client5 .http .impl .classic .HttpClientBuilder ;
14
16
import org .apache .hc .client5 .http .impl .classic .HttpClients ;
17
+ import org .apache .hc .client5 .http .io .HttpClientConnectionManager ;
15
18
import org .apache .hc .core5 .http .HttpEntity ;
19
+ import org .apache .hc .core5 .http .HttpHost ;
16
20
import org .apache .hc .core5 .http .NameValuePair ;
17
21
import org .apache .hc .core5 .http .io .entity .EntityUtils ;
18
22
import org .apache .hc .core5 .http .io .entity .StringEntity ;
19
23
import org .apache .hc .core5 .net .URIBuilder ;
24
+ import org .apache .hc .core5 .util .Timeout ;
20
25
import org .cloudinary .json .JSONException ;
21
26
import org .cloudinary .json .JSONObject ;
22
27
@@ -36,15 +41,42 @@ public class ApiStrategy extends AbstractApiStrategy {
36
41
37
42
private CloseableHttpClient client ;
38
43
39
- @ Override
40
44
public void init (Api api ) {
41
45
super .init (api );
42
46
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 )
45
59
.build ();
46
60
}
47
61
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
+
48
80
@ SuppressWarnings ({"rawtypes" , "unchecked" })
49
81
public ApiResponse callApi (Api .HttpMethod method , String apiUrl , Map <String , ?> params , Map options , String autorizationHeader ) throws Exception {
50
82
HttpUriRequestBase request = prepareRequest (method , apiUrl , params , options );
0 commit comments