Skip to content

Commit

Permalink
Encode curly brackets in proxy client
Browse files Browse the repository at this point in the history
Signed-off-by: jansupol <[email protected]>
  • Loading branch information
jansupol authored and senivam committed Aug 16, 2023
1 parent cb031c7 commit c5ad99a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -77,13 +78,13 @@ void addParameter(final Object value, final Map<Class<?>, Annotation> anns)
newTarget = newTarget.resolveTemplate(((PathParam) ann).value(), value);
} else if ((ann = anns.get((QueryParam.class))) != null) {
if (value instanceof Collection) {
newTarget = newTarget.queryParam(((QueryParam) ann).value(), convert((Collection<?>) value));
newTarget = newTarget.queryParam(((QueryParam) ann).value(), convert((Collection<?>) value, true));
} else {
newTarget = newTarget.queryParam(((QueryParam) ann).value(), value);
newTarget = newTarget.queryParam(((QueryParam) ann).value(), encodeTemplate(value));
}
} else if ((ann = anns.get((HeaderParam.class))) != null) {
if (value instanceof Collection) {
headers.addAll(((HeaderParam) ann).value(), convert((Collection<?>) value));
headers.addAll(((HeaderParam) ann).value(), convert((Collection<?>) value, false));
} else {
headers.addAll(((HeaderParam) ann).value(), value);
}
Expand Down Expand Up @@ -117,9 +118,9 @@ void addParameter(final Object value, final Map<Class<?>, Annotation> anns)
}
} else if ((ann = anns.get((MatrixParam.class))) != null) {
if (value instanceof Collection) {
newTarget = newTarget.matrixParam(((MatrixParam) ann).value(), convert((Collection<?>) value));
newTarget = newTarget.matrixParam(((MatrixParam) ann).value(), convert((Collection<?>) value, true));
} else {
newTarget = newTarget.matrixParam(((MatrixParam) ann).value(), value);
newTarget = newTarget.matrixParam(((MatrixParam) ann).value(), encodeTemplate(value));
}
} else if ((ann = anns.get((FormParam.class))) != null) {
if (value instanceof Collection) {
Expand Down Expand Up @@ -187,8 +188,23 @@ private List<Field> getAllFields(List<Field> fields, Class<?> type) {
return fields;
}

private Object[] convert(final Collection<?> value) {
return value.toArray();
private Object[] convert(Collection<?> value, boolean encode) {
Object[] array = new Object[value.size()];
int index = 0;
for (Iterator<?> it = value.iterator(); it.hasNext();) {
Object o = it.next();
array[index++] = o == null ? o : (encode ? encodeTemplate(o) : o.toString());
}
return array;
}

/**
* The Query and Matrix arguments are never templates
* @param notNull an Object that is not null
* @return encoded curly brackets within the string representation of the {@code notNull}
*/
private String encodeTemplate(Object notNull) {
return notNull.toString().replace("{", "%7B").replace("}", "%7D");
}

public static boolean hasAnyParamAnnotation(final Map<Class<?>, Annotation> anns) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.glassfish.jersey.test.TestProperties;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -337,4 +338,27 @@ public void testHashCode() throws Exception {
public void testEquals() {
assertFalse(resource.equals(resource2), "The two resource instances should not be considered equals as they are unique");
}

@Test
void testParamWithCurly() {
String param = "faulty {";

String result = resource.getByName(param);
Assertions.assertEquals(param, result);

result = resource.getByNameCookie(param);
Assertions.assertEquals(param, result);

result = resource.getByNameHeader(param);
Assertions.assertEquals(param, result);

result = resource.getByNameMatrix(param);
Assertions.assertEquals(param, result);

result = resource.postByNameFormParam(param);
Assertions.assertEquals(param, result);

result = resource.getId(param);
Assertions.assertEquals(param, result);
}
}

0 comments on commit c5ad99a

Please sign in to comment.