Skip to content

Commit c5ad99a

Browse files
jansupolsenivam
authored andcommitted
Encode curly brackets in proxy client
Signed-off-by: jansupol <jan.supol@oracle.com>
1 parent cb031c7 commit c5ad99a

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

ext/proxy-client/src/main/java/org/glassfish/jersey/client/proxy/RequestParameters.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.Arrays;
2727
import java.util.Collection;
2828
import java.util.HashMap;
29+
import java.util.Iterator;
2930
import java.util.LinkedList;
3031
import java.util.List;
3132
import java.util.Map;
@@ -77,13 +78,13 @@ void addParameter(final Object value, final Map<Class<?>, Annotation> anns)
7778
newTarget = newTarget.resolveTemplate(((PathParam) ann).value(), value);
7879
} else if ((ann = anns.get((QueryParam.class))) != null) {
7980
if (value instanceof Collection) {
80-
newTarget = newTarget.queryParam(((QueryParam) ann).value(), convert((Collection<?>) value));
81+
newTarget = newTarget.queryParam(((QueryParam) ann).value(), convert((Collection<?>) value, true));
8182
} else {
82-
newTarget = newTarget.queryParam(((QueryParam) ann).value(), value);
83+
newTarget = newTarget.queryParam(((QueryParam) ann).value(), encodeTemplate(value));
8384
}
8485
} else if ((ann = anns.get((HeaderParam.class))) != null) {
8586
if (value instanceof Collection) {
86-
headers.addAll(((HeaderParam) ann).value(), convert((Collection<?>) value));
87+
headers.addAll(((HeaderParam) ann).value(), convert((Collection<?>) value, false));
8788
} else {
8889
headers.addAll(((HeaderParam) ann).value(), value);
8990
}
@@ -117,9 +118,9 @@ void addParameter(final Object value, final Map<Class<?>, Annotation> anns)
117118
}
118119
} else if ((ann = anns.get((MatrixParam.class))) != null) {
119120
if (value instanceof Collection) {
120-
newTarget = newTarget.matrixParam(((MatrixParam) ann).value(), convert((Collection<?>) value));
121+
newTarget = newTarget.matrixParam(((MatrixParam) ann).value(), convert((Collection<?>) value, true));
121122
} else {
122-
newTarget = newTarget.matrixParam(((MatrixParam) ann).value(), value);
123+
newTarget = newTarget.matrixParam(((MatrixParam) ann).value(), encodeTemplate(value));
123124
}
124125
} else if ((ann = anns.get((FormParam.class))) != null) {
125126
if (value instanceof Collection) {
@@ -187,8 +188,23 @@ private List<Field> getAllFields(List<Field> fields, Class<?> type) {
187188
return fields;
188189
}
189190

190-
private Object[] convert(final Collection<?> value) {
191-
return value.toArray();
191+
private Object[] convert(Collection<?> value, boolean encode) {
192+
Object[] array = new Object[value.size()];
193+
int index = 0;
194+
for (Iterator<?> it = value.iterator(); it.hasNext();) {
195+
Object o = it.next();
196+
array[index++] = o == null ? o : (encode ? encodeTemplate(o) : o.toString());
197+
}
198+
return array;
199+
}
200+
201+
/**
202+
* The Query and Matrix arguments are never templates
203+
* @param notNull an Object that is not null
204+
* @return encoded curly brackets within the string representation of the {@code notNull}
205+
*/
206+
private String encodeTemplate(Object notNull) {
207+
return notNull.toString().replace("{", "%7B").replace("}", "%7D");
192208
}
193209

194210
public static boolean hasAnyParamAnnotation(final Map<Class<?>, Annotation> anns) {

ext/proxy-client/src/test/java/org/glassfish/jersey/client/proxy/WebResourceFactoryTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.glassfish.jersey.server.ResourceConfig;
4040
import org.glassfish.jersey.test.JerseyTest;
4141
import org.glassfish.jersey.test.TestProperties;
42+
import org.junit.jupiter.api.Assertions;
4243
import org.junit.jupiter.api.BeforeEach;
4344
import org.junit.jupiter.api.Disabled;
4445
import org.junit.jupiter.api.Test;
@@ -337,4 +338,27 @@ public void testHashCode() throws Exception {
337338
public void testEquals() {
338339
assertFalse(resource.equals(resource2), "The two resource instances should not be considered equals as they are unique");
339340
}
341+
342+
@Test
343+
void testParamWithCurly() {
344+
String param = "faulty {";
345+
346+
String result = resource.getByName(param);
347+
Assertions.assertEquals(param, result);
348+
349+
result = resource.getByNameCookie(param);
350+
Assertions.assertEquals(param, result);
351+
352+
result = resource.getByNameHeader(param);
353+
Assertions.assertEquals(param, result);
354+
355+
result = resource.getByNameMatrix(param);
356+
Assertions.assertEquals(param, result);
357+
358+
result = resource.postByNameFormParam(param);
359+
Assertions.assertEquals(param, result);
360+
361+
result = resource.getId(param);
362+
Assertions.assertEquals(param, result);
363+
}
340364
}

0 commit comments

Comments
 (0)