Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,21 @@
import com.cloudinary.utils.StringUtils;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.entity.mime.ByteArrayBody;
import org.apache.hc.client5.http.entity.mime.FileBody;
import org.apache.hc.client5.http.entity.mime.HttpMultipartMode;
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.io.HttpClientConnectionManager;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.util.Timeout;

import java.io.File;
import java.io.IOException;
Expand All @@ -35,11 +40,39 @@ public class UploaderStrategy extends AbstractUploaderStrategy {
public void init(Uploader uploader) {
super.init(uploader);

this.client = HttpClients.custom()
.setUserAgent(cloudinary().getUserAgent() + " ApacheHttpClient/" + APACHE_HTTP_CLIENT_VERSION)
HttpClientBuilder clientBuilder = HttpClients.custom();
clientBuilder.useSystemProperties().setUserAgent(cloudinary().getUserAgent() + " ApacheHttpClient/" + APACHE_HTTP_CLIENT_VERSION);

HttpClientConnectionManager connectionManager = (HttpClientConnectionManager) cloudinary().config.properties.get("connectionManager");
if (connectionManager != null) {
clientBuilder.setConnectionManager(connectionManager);
}

RequestConfig requestConfig = buildRequestConfig();

client = clientBuilder
.setDefaultRequestConfig(requestConfig)
.build();
}

public RequestConfig buildRequestConfig() {
RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();

if (cloudinary().config.proxyHost != null && cloudinary().config.proxyPort != 0) {
HttpHost proxy = new HttpHost(cloudinary().config.proxyHost, cloudinary().config.proxyPort);
requestConfigBuilder.setProxy(proxy);
}

int timeout = cloudinary().config.timeout;
if (timeout > 0) {
requestConfigBuilder.setResponseTimeout(Timeout.ofSeconds(timeout))
.setConnectionRequestTimeout(Timeout.ofSeconds(timeout))
.setConnectTimeout(Timeout.ofSeconds(timeout));
}

return requestConfigBuilder.build();
}

@SuppressWarnings({"rawtypes", "unchecked"})
@Override
public Map callApi(String action, Map<String, Object> params, Map options, Object file, ProgressCallback progressCallback) throws IOException {
Expand Down
27 changes: 25 additions & 2 deletions cloudinary-http5/src/test/java/com/cloudinary/test/ApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import org.junit.experimental.categories.Category;

import java.util.Map;
import java.util.UUID;

import static com.cloudinary.utils.ObjectUtils.asMap;


public class ApiTest extends AbstractApiTest {
Expand Down Expand Up @@ -48,7 +51,7 @@ public void testBuildRequestConfig_withoutProxy() {
@Category(TimeoutTest.class)
@Test(expected = Exception.class)
public void testConnectTimeoutParameter() throws Exception {
Map<String, Object> options = ObjectUtils.asMap(
Map<String, Object> options = asMap(
"max_results", 500,
"connect_timeout", 0.2);

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

Expand All @@ -76,4 +79,24 @@ public void testTimeoutParameter() throws Exception {
throw new Exception("Socket timeout");
}
}

@Category(TimeoutTest.class)
@Test(expected = Exception.class)
public void testUploaderTimeoutParameter() throws Exception {
Cloudinary cloudinary = new Cloudinary("cloudinary://test:test@test.com");
cloudinary.config.uploadPrefix = "https://10.255.255.1";
String publicId = UUID.randomUUID().toString();
// Set a very short request timeout to trigger a timeout exception
Map<String, Object> options = asMap(
"max_results", 500,
"timeout", Timeout.ofMilliseconds(10)); // Set the timeout to 1 second

try {
Map result = cloudinary.uploader().addContext(asMap("caption", "new caption"), new String[]{publicId, "no-such-id"}, options);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any other way of getting timeout without triggering errors on our BE?

} catch (Exception e) {
// Convert IOException to SocketTimeoutException if appropriate
throw new Exception("Socket timeout");
}
}

}