Skip to content

Commit 3505fcd

Browse files
author
Amir Tocker
committed
Escape \ and = in context
1 parent 5d2c01b commit 3505fcd

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public Map explicit(String publicId, Map options) throws IOException {
188188
params.put("custom_coordinates", Coordinates.parseCoordinates(options.get("custom_coordinates")).toString());
189189
}
190190
if (options.get("context") != null) {
191-
params.put("context", ObjectUtils.encodeMap(options.get("context")));
191+
params.put("context", Util.encodeContext(options.get("context")));
192192
}
193193
if (options.get("responsive_breakpoints") != null) {
194194
params.put("responsive_breakpoints", JSONObject.wrap(options.get("responsive_breakpoints")));

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
package com.cloudinary;
22

3-
import java.util.ArrayList;
4-
import java.util.HashMap;
5-
import java.util.Iterator;
6-
import java.util.List;
7-
import java.util.Map;
3+
import java.util.*;
84

95
import com.cloudinary.utils.ObjectUtils;
106
import com.cloudinary.utils.StringUtils;
@@ -100,7 +96,7 @@ public static final void processWriteParameters(Map<String, Object> options, Map
10096
if (options.get("custom_coordinates") != null)
10197
params.put("custom_coordinates", Coordinates.parseCoordinates(options.get("custom_coordinates")).toString());
10298
if (options.get("context") != null)
103-
params.put("context", ObjectUtils.encodeMap(options.get("context")));
99+
params.put("context", encodeContext(options.get("context")));
104100
putObject("ocr", options, params);
105101
putObject("raw_convert", options, params);
106102
putObject("categorization", options, params);
@@ -111,6 +107,22 @@ public static final void processWriteParameters(Map<String, Object> options, Map
111107
params.put("auto_tagging", ObjectUtils.asFloat(options.get("auto_tagging")));
112108
}
113109

110+
protected static String encodeContext(Object context) {
111+
if (context != null && context instanceof Map) {
112+
Map<String, String> mapArg = (Map<String, String>) context;
113+
HashSet out = new HashSet();
114+
for (Map.Entry<String, String> entry : mapArg.entrySet()) {
115+
final String value = entry.getValue().replaceAll("([=\\|])","\\\\$1");
116+
out.add(entry.getKey() + "=" + value);
117+
}
118+
return StringUtils.join(out.toArray(), "|");
119+
} else if (context == null) {
120+
return null;
121+
} else {
122+
return context.toString();
123+
}
124+
}
125+
114126
@SuppressWarnings("unchecked")
115127
protected static final String buildCustomHeaders(Object headers) {
116128
if (headers == null) {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.cloudinary;
2+
3+
import com.cloudinary.utils.ObjectUtils;
4+
import org.junit.Test;
5+
6+
import java.util.Map;
7+
8+
import static org.junit.Assert.*;
9+
10+
/**
11+
* Created by amir on 17/11/2016.
12+
*/
13+
public class UtilTest {
14+
@Test
15+
public void encodeContext() throws Exception {
16+
Map context = ObjectUtils.asMap("caption", "different = caption", "alt2", "alt|alternative");
17+
String result = Util.encodeContext(context);
18+
assertEquals("caption=different \\= caption|alt2=alt\\|alternative", result);
19+
}
20+
21+
}

cloudinary-test-common/src/main/java/com/cloudinary/test/AbstractUploaderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ public void testContext() throws Exception {
338338
Map result = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("context", context, "tags", SDK_TEST_TAG));
339339
Map info = cloudinary.api().resource((String) result.get("public_id"), ObjectUtils.asMap("context", true));
340340
assertEquals(ObjectUtils.asMap("custom", context), info.get("context"));
341-
Map differentContext = ObjectUtils.asMap("caption", "different caption", "alt2", "alternative alternative");
341+
Map differentContext = ObjectUtils.asMap("caption", "different = caption", "alt2", "alt|alternative alternative");
342342
cloudinary.uploader().explicit((String) result.get("public_id"), ObjectUtils.asMap("type", "upload", "context", differentContext));
343343
info = cloudinary.api().resource((String) result.get("public_id"), ObjectUtils.asMap("context", true));
344344
assertEquals(ObjectUtils.asMap("custom", differentContext), info.get("context"));

0 commit comments

Comments
 (0)