Skip to content

Commit 3bb28f9

Browse files
committed
Added timeout parameter to admin api and Fixed test and configuration issues
* Added new timeout parameter to the api configuration * Renamed 'logo.png' to 'old_logo.png' * Created constants for the local and remote image names * (Automatically) Added Library reference in project.properties
1 parent 4c7b23b commit 3bb28f9

File tree

11 files changed

+181
-100
lines changed

11 files changed

+181
-100
lines changed

.gitignore

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,32 @@
1+
## Android default ignore
2+
# Built application files
3+
*.apk
4+
*.ap_
5+
6+
# Files for the Dalvik VM
7+
*.dex
8+
9+
# Java class files
110
*.class
211

3-
# Package Files #
4-
*.jar
5-
*.war
6-
*.ear
12+
# Generated files
13+
bin/
14+
gen/
15+
16+
# Gradle files
17+
.gradle/
18+
build/
19+
/*/build/
20+
21+
# Local configuration file (sdk path, etc)
22+
local.properties
23+
24+
# Proguard folder generated by Eclipse
25+
proguard/
26+
27+
# Log Files
28+
*.log
729

8-
.*
930
target/
1031
test-output/
1132
.settings
@@ -17,3 +38,4 @@ test-output/
1738
appengine-web.xml
1839

1940
cloudinary-android-test/src/main/AndroidManifest.xml
41+

cloudinary-android-test/project.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212

1313
# Project target.
1414
target=android-7
15+
android.library.reference.1=../cloudinary-android

cloudinary-android-test/src/main/assets/images/logo.png renamed to cloudinary-android-test/src/main/assets/images/old_logo.png

File renamed without changes.

cloudinary-android-test/src/main/java/com/cloudinary/test/CloudinaryTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,8 @@ public void testUnderlay() {
238238

239239
public void testFetchFormat() {
240240
// should support format for fetch urls
241-
String result = cloudinary.url().format("jpg").type("fetch").generate("http://cloudinary.com/images/logo.png");
242-
assertEquals("http://res.cloudinary.com/test123/image/fetch/f_jpg/http://cloudinary.com/images/logo.png", result);
241+
String result = cloudinary.url().format("jpg").type("fetch").generate("http://cloudinary.com/images/old_logo.png");
242+
assertEquals("http://res.cloudinary.com/test123/image/fetch/f_jpg/http://cloudinary.com/images/old_logo.png", result);
243243
}
244244

245245
public void testEffect() {

cloudinary-android-test/src/main/java/com/cloudinary/test/UploaderTest.java

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323

2424
public class UploaderTest extends InstrumentationTestCase {
2525

26-
27-
private Cloudinary cloudinary;
26+
27+
public static final String TEST_IMAGE = "images/old_logo.png";
28+
private Cloudinary cloudinary;
2829
private static boolean first = true;
2930

3031
public void setUp() throws Exception {
@@ -44,7 +45,7 @@ protected InputStream getAssetStream(String filename) throws IOException {
4445
public void testUpload() throws Exception {
4546
if (cloudinary.config.apiSecret == null)
4647
return;
47-
JSONObject result = new JSONObject(cloudinary.uploader().upload(getAssetStream("images/logo.png"), ObjectUtils.asMap("colors", true)));
48+
JSONObject result = new JSONObject(cloudinary.uploader().upload(getAssetStream(TEST_IMAGE), ObjectUtils.asMap("colors", true)));
4849
assertEquals(result.getLong("width"), 241L);
4950
assertEquals(result.getLong("height"), 51L);
5051
assertNotNull(result.get("colors"));
@@ -59,7 +60,7 @@ public void testUpload() throws Exception {
5960
public void testUnsignedUpload() throws Exception {
6061
if (cloudinary.config.apiSecret == null)
6162
return;
62-
JSONObject result = new JSONObject(cloudinary.uploader().unsignedUpload(getAssetStream("images/logo.png"), "sample_preset_dhfjhriu",
63+
JSONObject result = new JSONObject(cloudinary.uploader().unsignedUpload(getAssetStream(TEST_IMAGE), "sample_preset_dhfjhriu",
6364
ObjectUtils.emptyMap()));
6465
assertEquals(result.getLong("width"), 241L);
6566
assertEquals(result.getLong("height"), 51L);
@@ -74,7 +75,7 @@ public void testUnsignedUpload() throws Exception {
7475
public void testUploadUrl() throws Exception {
7576
if (cloudinary.config.apiSecret == null)
7677
return;
77-
JSONObject result = new JSONObject(cloudinary.uploader().upload("http://cloudinary.com/images/logo.png", ObjectUtils.emptyMap()));
78+
JSONObject result = new JSONObject(cloudinary.uploader().upload("http://cloudinary.com/images/old_logo.png", ObjectUtils.emptyMap()));
7879
assertEquals(result.getLong("width"), 241L);
7980
assertEquals(result.getLong("height"), 51L);
8081
Map<String, Object> to_sign = new HashMap<String, Object>();
@@ -113,7 +114,7 @@ public void testUploadExternalSignature() throws Exception {
113114
params.put("timestamp", Long.valueOf(System.currentTimeMillis() / 1000L).toString());
114115
params.put("signature", this.cloudinary.apiSignRequest(params, apiSecret));
115116
Cloudinary emptyCloudinary = new Cloudinary(config);
116-
JSONObject result = new JSONObject(emptyCloudinary.uploader().upload(getAssetStream("images/logo.png"), params));
117+
JSONObject result = new JSONObject(emptyCloudinary.uploader().upload(getAssetStream(TEST_IMAGE), params));
117118
assertEquals(result.getLong("width"), 241L);
118119
assertEquals(result.getLong("height"), 51L);
119120
Map<String, Object> to_sign = new HashMap<String, Object>();
@@ -126,7 +127,7 @@ public void testUploadExternalSignature() throws Exception {
126127
public void testRename() throws Exception {
127128
if (cloudinary.config.apiSecret == null)
128129
return;
129-
JSONObject result = new JSONObject(cloudinary.uploader().upload(getAssetStream("images/logo.png"), ObjectUtils.emptyMap()));
130+
JSONObject result = new JSONObject(cloudinary.uploader().upload(getAssetStream(TEST_IMAGE), ObjectUtils.emptyMap()));
130131

131132
cloudinary.uploader().rename(result.getString("public_id"), result.get("public_id") + "2", ObjectUtils.emptyMap());
132133

@@ -154,15 +155,15 @@ public void testExplicit() throws Exception {
154155
public void testEager() throws Exception {
155156
if (cloudinary.config.apiSecret == null)
156157
return;
157-
cloudinary.uploader().upload(getAssetStream("images/logo.png"),
158+
cloudinary.uploader().upload(getAssetStream(TEST_IMAGE),
158159
ObjectUtils.asMap("eager", Collections.singletonList(new Transformation().crop("scale").width(2.0))));
159160
}
160161

161162
public void testHeaders() throws Exception {
162163
if (cloudinary.config.apiSecret == null)
163164
return;
164-
cloudinary.uploader().upload(getAssetStream("images/logo.png"), ObjectUtils.asMap("headers", new String[] { "Link: 1" }));
165-
cloudinary.uploader().upload(getAssetStream("images/logo.png"), ObjectUtils.asMap("headers", ObjectUtils.asMap("Link", "1")));
165+
cloudinary.uploader().upload(getAssetStream(TEST_IMAGE), ObjectUtils.asMap("headers", new String[] { "Link: 1" }));
166+
cloudinary.uploader().upload(getAssetStream(TEST_IMAGE), ObjectUtils.asMap("headers", ObjectUtils.asMap("Link", "1")));
166167
}
167168

168169
public void testText() throws Exception {
@@ -176,8 +177,8 @@ public void testText() throws Exception {
176177
public void testSprite() throws Exception {
177178
if (cloudinary.config.apiSecret == null)
178179
return;
179-
cloudinary.uploader().upload(getAssetStream("images/logo.png"), ObjectUtils.asMap("tags", "sprite_test_tag", "public_id", "sprite_test_tag_1"));
180-
cloudinary.uploader().upload(getAssetStream("images/logo.png"), ObjectUtils.asMap("tags", "sprite_test_tag", "public_id", "sprite_test_tag_2"));
180+
cloudinary.uploader().upload(getAssetStream(TEST_IMAGE), ObjectUtils.asMap("tags", "sprite_test_tag", "public_id", "sprite_test_tag_1"));
181+
cloudinary.uploader().upload(getAssetStream(TEST_IMAGE), ObjectUtils.asMap("tags", "sprite_test_tag", "public_id", "sprite_test_tag_2"));
181182
JSONObject result = new JSONObject(cloudinary.uploader().generate_sprite("sprite_test_tag", ObjectUtils.emptyMap()));
182183
assertEquals(2, result.getJSONObject("image_infos").length());
183184
result = new JSONObject(cloudinary.uploader().generate_sprite("sprite_test_tag", ObjectUtils.asMap("transformation", "w_100")));
@@ -190,8 +191,8 @@ public void testSprite() throws Exception {
190191
public void testMulti() throws Exception {
191192
if (cloudinary.config.apiSecret == null)
192193
return;
193-
cloudinary.uploader().upload(getAssetStream("images/logo.png"), ObjectUtils.asMap("tags", "multi_test_tag", "public_id", "multi_test_tag_1"));
194-
cloudinary.uploader().upload(getAssetStream("images/logo.png"), ObjectUtils.asMap("tags", "multi_test_tag", "public_id", "multi_test_tag_2"));
194+
cloudinary.uploader().upload(getAssetStream(TEST_IMAGE), ObjectUtils.asMap("tags", "multi_test_tag", "public_id", "multi_test_tag_1"));
195+
cloudinary.uploader().upload(getAssetStream(TEST_IMAGE), ObjectUtils.asMap("tags", "multi_test_tag", "public_id", "multi_test_tag_2"));
195196
JSONObject result = new JSONObject(cloudinary.uploader().multi("multi_test_tag", ObjectUtils.emptyMap()));
196197
assertTrue((result.getString("url")).endsWith(".gif"));
197198
result = new JSONObject(cloudinary.uploader().multi("multi_test_tag", ObjectUtils.asMap("transformation", "w_100")));
@@ -205,9 +206,9 @@ public void testUniqueFilename() throws Exception {
205206
if (cloudinary.config.apiSecret == null)
206207
return;
207208

208-
File f = new File(getInstrumentation().getContext().getCacheDir() + "/logo.png");
209+
File f = new File(getInstrumentation().getContext().getCacheDir() + "/old_logo.png");
209210

210-
InputStream is = getAssetStream("images/logo.png");
211+
InputStream is = getAssetStream(TEST_IMAGE);
211212
int size = is.available();
212213
byte[] buffer = new byte[size];
213214
is.read(buffer);
@@ -232,7 +233,7 @@ public void testFaceCoordinates() throws Exception {
232233
Rectangle rect2 = new Rectangle(120, 30, 229, 270);
233234
coordinates.addRect(rect1);
234235
coordinates.addRect(rect2);
235-
JSONObject result = new JSONObject(cloudinary.uploader().upload(getAssetStream("images/logo.png"), ObjectUtils.asMap("face_coordinates", coordinates, "faces", true)));
236+
JSONObject result = new JSONObject(cloudinary.uploader().upload(getAssetStream(TEST_IMAGE), ObjectUtils.asMap("face_coordinates", coordinates, "faces", true)));
236237
JSONArray resultFaces = result.getJSONArray("faces");
237238
assertEquals(2, resultFaces.length());
238239

@@ -258,14 +259,14 @@ public void testContext() throws Exception {
258259
return;
259260
@SuppressWarnings("rawtypes")
260261
Map context = ObjectUtils.asMap("caption", "some caption", "alt", "alternative");
261-
cloudinary.uploader().upload(getAssetStream("images/logo.png"), ObjectUtils.asMap("context", context));
262+
cloudinary.uploader().upload(getAssetStream(TEST_IMAGE), ObjectUtils.asMap("context", context));
262263
}
263264

264265
public void testModerationRequest() throws Exception {
265266
// should support requesting manual moderation
266267
if (cloudinary.config.apiSecret == null)
267268
return;
268-
JSONObject result = new JSONObject(cloudinary.uploader().upload(getAssetStream("images/logo.png"), ObjectUtils.asMap("moderation", "manual")));
269+
JSONObject result = new JSONObject(cloudinary.uploader().upload(getAssetStream(TEST_IMAGE), ObjectUtils.asMap("moderation", "manual")));
269270
assertEquals("manual", result.getJSONArray("moderation").getJSONObject(0).getString("kind"));
270271
assertEquals("pending", result.getJSONArray("moderation").getJSONObject(0).getString("status"));
271272
}
@@ -286,7 +287,7 @@ public void testCategorizationRequest() {
286287
if (cloudinary.config.apiSecret == null)
287288
return;
288289
try {
289-
cloudinary.uploader().upload(getAssetStream("images/logo.png"), ObjectUtils.asMap("categorization", "illegal"));
290+
cloudinary.uploader().upload(getAssetStream(TEST_IMAGE), ObjectUtils.asMap("categorization", "illegal"));
290291
} catch (Exception e) {
291292
assertTrue(e.getMessage().matches(".*illegal is not a valid.*"));
292293
}
@@ -297,7 +298,7 @@ public void testDetectionRequest() {
297298
if (cloudinary.config.apiSecret == null)
298299
return;
299300
try {
300-
cloudinary.uploader().upload(getAssetStream("images/logo.png"), ObjectUtils.asMap("detection", "illegal"));
301+
cloudinary.uploader().upload(getAssetStream(TEST_IMAGE), ObjectUtils.asMap("detection", "illegal"));
301302
} catch (Exception e) {
302303
assertTrue(e.getMessage().matches(".*illegal is not a valid.*"));
303304
}
@@ -309,7 +310,7 @@ public void testAutoTaggingRequest() {
309310
return;
310311

311312
try {
312-
cloudinary.uploader().upload(getAssetStream("images/logo.png"), ObjectUtils.asMap("auto_tagging", 0.5f));
313+
cloudinary.uploader().upload(getAssetStream(TEST_IMAGE), ObjectUtils.asMap("auto_tagging", 0.5f));
313314
} catch (Exception e) {
314315
for (int i = 0; i < e.getStackTrace().length; i++) {
315316
StackTraceElement x = e.getStackTrace()[i];

cloudinary-core/src/main/java/com/cloudinary/Configuration.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,16 @@ public class Configuration {
3636
public Map<String, Object> properties = new HashMap<String, Object>();
3737
public Boolean secureCdnSubdomain;
3838
public boolean useRootPath;
39+
public int timeout;
40+
3941
public Configuration(){
4042
}
41-
42-
private Configuration(String cloudName, String apiKey, String apiSecret, String secureDistribution, String cname, String uploadPrefix, boolean secure, boolean privateCdn, boolean cdnSubdomain, boolean shorten, String callback,String proxyHost,int proxyPort,Boolean secureCdnSubdomain,boolean useRootPath) {
43+
44+
private Configuration(String cloudName, String apiKey, String apiSecret, String secureDistribution, String cname, String uploadPrefix, boolean secure, boolean privateCdn, boolean cdnSubdomain, boolean shorten, String callback, String proxyHost, int proxyPort, Boolean secureCdnSubdomain, boolean useRootPath) {
45+
this(cloudName, apiKey, apiSecret, secureDistribution, cname, uploadPrefix, secure, privateCdn, cdnSubdomain, shorten, callback, proxyHost, proxyPort, secureCdnSubdomain, useRootPath, 0);
46+
}
47+
48+
private Configuration(String cloudName, String apiKey, String apiSecret, String secureDistribution, String cname, String uploadPrefix, boolean secure, boolean privateCdn, boolean cdnSubdomain, boolean shorten, String callback, String proxyHost, int proxyPort, Boolean secureCdnSubdomain, boolean useRootPath, int timeout) {
4349
this.cloudName = cloudName;
4450
this.apiKey = apiKey;
4551
this.apiSecret = apiSecret;
@@ -55,6 +61,7 @@ private Configuration(String cloudName, String apiKey, String apiSecret, String
5561
this.proxyPort = proxyPort;
5662
this.secureCdnSubdomain = secureCdnSubdomain;
5763
this.useRootPath = useRootPath;
64+
this.timeout = 0;
5865
}
5966

6067

@@ -80,6 +87,7 @@ public void update(Map config) {
8087
this.proxyPort = ObjectUtils.asInteger(config.get("proxy_port"),0);
8188
this.secureCdnSubdomain = ObjectUtils.asBoolean(config.get("secure_cdn_subdomain"), null);
8289
this.useRootPath = ObjectUtils.asBoolean(config.get("use_root_path"), false);
90+
this.timeout = ObjectUtils.asInteger(config.get("timeout"), 0);
8391
}
8492

8593

@@ -100,6 +108,7 @@ public Configuration(Configuration other) {
100108
this.proxyPort = other.proxyPort;
101109
this.secureCdnSubdomain = other.secureCdnSubdomain;
102110
this.useRootPath = other.useRootPath;
111+
this.timeout = other.timeout;
103112
}
104113

105114

@@ -188,7 +197,18 @@ public static class Builder {
188197
private int proxyPort;
189198
private Boolean secureCdnSubdomain;
190199
private boolean useRootPath;
191-
200+
private int timeout;
201+
202+
/**
203+
* Set the HTTP connection timeout.
204+
* @param timeout time in milliseconds, or 0 to use the default platform value
205+
* @return builder for chaining
206+
*/
207+
public Builder setTimeout(int timeout) {
208+
this.timeout = timeout;
209+
return this;
210+
}
211+
192212

193213
/**
194214
* Creates a {@link Configuration} with the arguments supplied to this builder
@@ -313,6 +333,7 @@ public Builder from(Configuration other) {
313333
this.proxyPort = other.proxyPort;
314334
this.secureCdnSubdomain = other.secureCdnSubdomain;
315335
this.useRootPath = other.useRootPath;
336+
this.timeout = other.timeout;
316337
return this;
317338
}
318339

cloudinary-http42/src/main/java/com/cloudinary/http42/ApiStrategy.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import org.apache.http.client.utils.URIBuilder;
1616
import org.apache.http.conn.ClientConnectionManager;
1717
import org.apache.http.impl.client.DefaultHttpClient;
18+
import org.apache.http.params.HttpConnectionParams;
19+
import org.apache.http.params.HttpParams;
1820
import org.cloudinary.json.JSONException;
1921
import org.cloudinary.json.JSONObject;
2022

@@ -43,11 +45,13 @@ public ApiResponse callApi(HttpMethod method, Iterable<String> uri, Map<String,
4345
String apiKey = ObjectUtils.asString(options.get("api_key"), this.api.cloudinary.config.apiKey);
4446
if (apiKey == null)
4547
throw new IllegalArgumentException("Must supply api_key");
46-
String apiSecret = ObjectUtils.asString(options.get("api_secret"), this.api.cloudinary.config.apiSecret);
47-
if (apiSecret == null)
48-
throw new IllegalArgumentException("Must supply api_secret");
48+
String apiSecret = ObjectUtils.asString(options.get("api_secret"), this.api.cloudinary.config.apiSecret);
49+
if (apiSecret == null)
50+
throw new IllegalArgumentException("Must supply api_secret");
4951

50-
String apiUrl = StringUtils.join(Arrays.asList(prefix, "v1_1", cloudName), "/");
52+
int timeout = ObjectUtils.asInteger(options.get("timeout"), this.api.cloudinary.config.timeout);
53+
54+
String apiUrl = StringUtils.join(Arrays.asList(prefix, "v1_1", cloudName), "/");
5155
for (String component : uri) {
5256
apiUrl = apiUrl + "/" + component;
5357
}
@@ -63,7 +67,13 @@ public ApiResponse callApi(HttpMethod method, Iterable<String> uri, Map<String,
6367
}
6468
ClientConnectionManager connectionManager = (ClientConnectionManager) this.api.cloudinary.config.properties.get("connectionManager");
6569

66-
DefaultHttpClient client = new DefaultHttpClient(connectionManager);
70+
DefaultHttpClient client = new DefaultHttpClient(connectionManager);
71+
if (timeout > 0) {
72+
HttpParams httpParams = client.getParams();
73+
HttpConnectionParams.setConnectionTimeout(httpParams, timeout );
74+
HttpConnectionParams.setSoTimeout(httpParams, timeout );
75+
}
76+
6777
URI apiUri = apiUrlBuilder.build();
6878
HttpUriRequest request = null;
6979
switch (method) {

0 commit comments

Comments
 (0)