Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encode curly brackets in proxy client #5387

Merged
merged 1 commit into from
Aug 16, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
}
}