88import com .cloudinary .utils .StringUtils ;
99import org .apache .hc .client5 .http .classic .methods .HttpPost ;
1010import org .apache .hc .client5 .http .classic .methods .HttpUriRequestBase ;
11+ import org .apache .hc .client5 .http .config .RequestConfig ;
1112import org .apache .hc .client5 .http .entity .mime .ByteArrayBody ;
1213import org .apache .hc .client5 .http .entity .mime .FileBody ;
1314import org .apache .hc .client5 .http .entity .mime .HttpMultipartMode ;
1415import org .apache .hc .client5 .http .entity .mime .MultipartEntityBuilder ;
1516import org .apache .hc .client5 .http .impl .classic .CloseableHttpClient ;
1617import org .apache .hc .client5 .http .impl .classic .CloseableHttpResponse ;
18+ import org .apache .hc .client5 .http .impl .classic .HttpClientBuilder ;
1719import org .apache .hc .client5 .http .impl .classic .HttpClients ;
20+ import org .apache .hc .client5 .http .io .HttpClientConnectionManager ;
1821import org .apache .hc .core5 .http .ContentType ;
22+ import org .apache .hc .core5 .http .HttpHost ;
1923import org .apache .hc .core5 .http .ParseException ;
2024import org .apache .hc .core5 .http .io .entity .EntityUtils ;
25+ import org .apache .hc .core5 .util .Timeout ;
2126
2227import java .io .File ;
2328import java .io .IOException ;
@@ -35,11 +40,39 @@ public class UploaderStrategy extends AbstractUploaderStrategy {
3540 public void init (Uploader uploader ) {
3641 super .init (uploader );
3742
38- this .client = HttpClients .custom ()
39- .setUserAgent (cloudinary ().getUserAgent () + " ApacheHttpClient/" + APACHE_HTTP_CLIENT_VERSION )
43+ HttpClientBuilder clientBuilder = HttpClients .custom ();
44+ clientBuilder .useSystemProperties ().setUserAgent (cloudinary ().getUserAgent () + " ApacheHttpClient/" + APACHE_HTTP_CLIENT_VERSION );
45+
46+ HttpClientConnectionManager connectionManager = (HttpClientConnectionManager ) cloudinary ().config .properties .get ("connectionManager" );
47+ if (connectionManager != null ) {
48+ clientBuilder .setConnectionManager (connectionManager );
49+ }
50+
51+ RequestConfig requestConfig = buildRequestConfig ();
52+
53+ client = clientBuilder
54+ .setDefaultRequestConfig (requestConfig )
4055 .build ();
4156 }
4257
58+ public RequestConfig buildRequestConfig () {
59+ RequestConfig .Builder requestConfigBuilder = RequestConfig .custom ();
60+
61+ if (cloudinary ().config .proxyHost != null && cloudinary ().config .proxyPort != 0 ) {
62+ HttpHost proxy = new HttpHost (cloudinary ().config .proxyHost , cloudinary ().config .proxyPort );
63+ requestConfigBuilder .setProxy (proxy );
64+ }
65+
66+ int timeout = cloudinary ().config .timeout ;
67+ if (timeout > 0 ) {
68+ requestConfigBuilder .setResponseTimeout (Timeout .ofSeconds (timeout ))
69+ .setConnectionRequestTimeout (Timeout .ofSeconds (timeout ))
70+ .setConnectTimeout (Timeout .ofSeconds (timeout ));
71+ }
72+
73+ return requestConfigBuilder .build ();
74+ }
75+
4376 @ SuppressWarnings ({"rawtypes" , "unchecked" })
4477 @ Override
4578 public Map callApi (String action , Map <String , Object > params , Map options , Object file , ProgressCallback progressCallback ) throws IOException {
0 commit comments