Add decoding matrix variable values

Issue: SPR-10140
This commit is contained in:
Rossen Stoyanchev
2013-01-08 09:21:17 -05:00
parent 2391277115
commit 87109b348c
3 changed files with 66 additions and 19 deletions

View File

@@ -100,7 +100,7 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe
request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, decodedUriVariables);
if (isMatrixVariableContentAvailable()) {
request.setAttribute(HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE, extractMatrixVariables(uriVariables));
request.setAttribute(HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE, extractMatrixVariables(request, uriVariables));
}
if (!info.getProducesCondition().getProducibleMediaTypes().isEmpty()) {
@@ -113,7 +113,9 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe
return !getUrlPathHelper().shouldRemoveSemicolonContent();
}
private Map<String, MultiValueMap<String, String>> extractMatrixVariables(Map<String, String> uriVariables) {
private Map<String, MultiValueMap<String, String>> extractMatrixVariables(
HttpServletRequest request, Map<String, String> uriVariables) {
Map<String, MultiValueMap<String, String>> result = new LinkedHashMap<String, MultiValueMap<String, String>>();
for (Entry<String, String> uriVar : uriVariables.entrySet()) {
String uriVarValue = uriVar.getValue();
@@ -134,7 +136,8 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe
uriVariables.put(uriVar.getKey(), uriVarValue.substring(0, semicolonIndex));
}
result.put(uriVar.getKey(), WebUtils.parseMatrixVariables(matrixVariables));
MultiValueMap<String, String> vars = WebUtils.parseMatrixVariables(matrixVariables);
result.put(uriVar.getKey(), getUrlPathHelper().decodeMatrixVariables(request, vars));
}
return result;
}

View File

@@ -310,48 +310,63 @@ public class RequestMappingInfoHandlerMappingTests {
MultiValueMap<String, String> matrixVariables;
Map<String, String> uriVariables;
String lookupPath = "/cars;colors=red,blue,green;year=2012";
// Pattern "/{cars}" : matrix variables stripped from "cars" variable
request = new MockHttpServletRequest();
testHandleMatch(request, "/{cars}", lookupPath);
testHandleMatch(request, "/{cars}", "/cars;colors=red,blue,green;year=2012");
matrixVariables = getMatrixVariables(request, "cars");
uriVariables = getUriTemplateVariables(request);
assertNotNull(matrixVariables);
assertEquals(Arrays.asList("red", "blue", "green"), matrixVariables.get("colors"));
assertEquals("2012", matrixVariables.getFirst("year"));
uriVariables = getUriTemplateVariables(request);
assertEquals("cars", uriVariables.get("cars"));
// Pattern "/{cars:[^;]+}{params}" : "cars" and "params" variables unchanged
request = new MockHttpServletRequest();
testHandleMatch(request, "/{cars:[^;]+}{params}", lookupPath);
testHandleMatch(request, "/{cars:[^;]+}{params}", "/cars;colors=red,blue,green;year=2012");
matrixVariables = getMatrixVariables(request, "params");
uriVariables = getUriTemplateVariables(request);
assertNotNull(matrixVariables);
assertEquals(Arrays.asList("red", "blue", "green"), matrixVariables.get("colors"));
assertEquals("2012", matrixVariables.getFirst("year"));
uriVariables = getUriTemplateVariables(request);
assertEquals("cars", uriVariables.get("cars"));
assertEquals(";colors=red,blue,green;year=2012", uriVariables.get("params"));
// matrix variables not present : "params" variable is empty
request = new MockHttpServletRequest();
testHandleMatch(request, "/{cars:[^;]+}{params}", "/cars");
matrixVariables = getMatrixVariables(request, "params");
assertNull(matrixVariables);
uriVariables = getUriTemplateVariables(request);
assertNull(matrixVariables);
assertEquals("cars", uriVariables.get("cars"));
assertEquals("", uriVariables.get("params"));
}
@Test
public void matrixVariablesDecoding() {
MockHttpServletRequest request;
UrlPathHelper urlPathHelper = new UrlPathHelper();
urlPathHelper.setUrlDecode(false);
urlPathHelper.setRemoveSemicolonContent(false);
this.handlerMapping.setUrlPathHelper(urlPathHelper );
request = new MockHttpServletRequest();
testHandleMatch(request, "/path{filter}", "/path;mvar=a%2fb");
MultiValueMap<String, String> matrixVariables = getMatrixVariables(request, "filter");
Map<String, String> uriVariables = getUriTemplateVariables(request);
assertNotNull(matrixVariables);
assertEquals(Arrays.asList("a/b"), matrixVariables.get("mvar"));
assertEquals(";mvar=a/b", uriVariables.get("filter"));
}
private void testHandleMatch(MockHttpServletRequest request, String pattern, String lookupPath) {
PatternsRequestCondition patterns = new PatternsRequestCondition(pattern);
RequestMappingInfo info = new RequestMappingInfo(patterns, null, null, null, null, null, null);