Skip to content

Commit c646ad4

Browse files
authored
Fix Uploader strategy
1 parent 61a5c90 commit c646ad4

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

cloudinary-http5/src/main/java/com/cloudinary/http5/UploaderStrategy.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,21 @@
88
import com.cloudinary.utils.StringUtils;
99
import org.apache.hc.client5.http.classic.methods.HttpPost;
1010
import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
11+
import org.apache.hc.client5.http.config.RequestConfig;
1112
import org.apache.hc.client5.http.entity.mime.ByteArrayBody;
1213
import org.apache.hc.client5.http.entity.mime.FileBody;
1314
import org.apache.hc.client5.http.entity.mime.HttpMultipartMode;
1415
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
1516
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
1617
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
18+
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
1719
import org.apache.hc.client5.http.impl.classic.HttpClients;
20+
import org.apache.hc.client5.http.io.HttpClientConnectionManager;
1821
import org.apache.hc.core5.http.ContentType;
22+
import org.apache.hc.core5.http.HttpHost;
1923
import org.apache.hc.core5.http.ParseException;
2024
import org.apache.hc.core5.http.io.entity.EntityUtils;
25+
import org.apache.hc.core5.util.Timeout;
2126

2227
import java.io.File;
2328
import 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 {

cloudinary-http5/src/test/java/com/cloudinary/test/ApiTest.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
import org.junit.experimental.categories.Category;
1212

1313
import java.util.Map;
14+
import java.util.UUID;
15+
16+
import static com.cloudinary.utils.ObjectUtils.asMap;
1417

1518

1619
public class ApiTest extends AbstractApiTest {
@@ -48,7 +51,7 @@ public void testBuildRequestConfig_withoutProxy() {
4851
@Category(TimeoutTest.class)
4952
@Test(expected = Exception.class)
5053
public void testConnectTimeoutParameter() throws Exception {
51-
Map<String, Object> options = ObjectUtils.asMap(
54+
Map<String, Object> options = asMap(
5255
"max_results", 500,
5356
"connect_timeout", 0.2);
5457

@@ -65,7 +68,7 @@ public void testConnectTimeoutParameter() throws Exception {
6568
@Test(expected = Exception.class)
6669
public void testTimeoutParameter() throws Exception {
6770
// Set a very short request timeout to trigger a timeout exception
68-
Map<String, Object> options = ObjectUtils.asMap(
71+
Map<String, Object> options = asMap(
6972
"max_results", 500,
7073
"timeout", Timeout.ofMilliseconds(1000)); // Set the timeout to 1 second
7174

@@ -76,4 +79,24 @@ public void testTimeoutParameter() throws Exception {
7679
throw new Exception("Socket timeout");
7780
}
7881
}
82+
83+
@Category(TimeoutTest.class)
84+
@Test(expected = Exception.class)
85+
public void testUploaderTimeoutParameter() throws Exception {
86+
Cloudinary cloudinary = new Cloudinary("cloudinary://test:test@test.com");
87+
cloudinary.config.uploadPrefix = "https://10.255.255.1";
88+
String publicId = UUID.randomUUID().toString();
89+
// Set a very short request timeout to trigger a timeout exception
90+
Map<String, Object> options = asMap(
91+
"max_results", 500,
92+
"timeout", Timeout.ofMilliseconds(10)); // Set the timeout to 1 second
93+
94+
try {
95+
Map result = cloudinary.uploader().addContext(asMap("caption", "new caption"), new String[]{publicId, "no-such-id"}, options);
96+
} catch (Exception e) {
97+
// Convert IOException to SocketTimeoutException if appropriate
98+
throw new Exception("Socket timeout");
99+
}
100+
}
101+
79102
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy