提交 3474afb1 编写于 作者: R Rossen Stoyanchev

Improve suffix pattern check

After this change dots inside URI variables in a request mapping
pattern are ignored and no longer considered an indication that
the pattern contains a suffix itself.

Issue: SPR-11532
上级 4d3ca431
......@@ -249,8 +249,7 @@ public final class PatternsRequestCondition extends AbstractRequestCondition<Pat
}
}
else {
boolean hasSuffix = pattern.indexOf('.') != -1;
if (!hasSuffix && this.pathMatcher.match(pattern + ".*", lookupPath)) {
if (!hasSuffix(pattern) && this.pathMatcher.match(pattern + ".*", lookupPath)) {
return pattern + ".*";
}
}
......@@ -266,6 +265,28 @@ public final class PatternsRequestCondition extends AbstractRequestCondition<Pat
return null;
}
private boolean hasSuffix(String pattern) {
boolean uriVarMode = false;
for (int i = pattern.length(); i > 0; i--) {
char c = pattern.charAt(i-1);
if (c == '}') {
uriVarMode = true;
}
else if (c == '{') {
uriVarMode = false;
}
else if (c == '/') {
return false;
}
else {
if (!uriVarMode && c == '.') {
return true;
}
}
}
return false;
}
/**
* Compare the two conditions based on the URL patterns they contain.
* Patterns are compared one at a time, from top to bottom via
......
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -115,6 +115,26 @@ public class PatternsRequestConditionTests {
assertEquals("/{foo}", match.getPatterns().iterator().next());
}
// SPR-11532
@Test
public void matchSuffixPatternWithUriVariables() {
testSuffixPattern("/employees/{areaOfResponsibility.owner.id}", "/employees/976685.json", false);
testSuffixPattern("/establishments/{establishmentId}", "/establishments/123456789.json", false);
testSuffixPattern("/a.b/c", "/a.b/c.json", false);
testSuffixPattern("/a/b.json", "/a/b.json", true);
testSuffixPattern("/a/{b}.{c}", "/a/b.c", true);
}
public void testSuffixPattern(String pattern, String url, boolean patternHasSuffix) {
MockHttpServletRequest request = new MockHttpServletRequest("GET", url);
PatternsRequestCondition condition = new PatternsRequestCondition(pattern);
PatternsRequestCondition match = condition.getMatchingCondition(request);
assertNotNull(match);
assertEquals((patternHasSuffix ? pattern : pattern + ".*"), match.getPatterns().iterator().next());
}
// SPR-8410
@Test
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册