Refine AntPathMatcher.combine when p1 contains '.'

Refine AntPathMatcher.combine rules to allow direct concatenation of
patterns when pattern1 does not contain '*.'. Prior to this commit
direct concatenation was allowed when pattern1 did not contain '.',
this prevented calls of the form:

	pathMatcher.combine("/1.0", "/foo/test")

from working as expected.

This commit also applies some general cleanup to the `combine` method.

Issue: SPR-10554
This commit is contained in:
Phillip Webb 2013-08-26 18:22:22 -07:00
parent 57dfc13f30
commit c1dafed886
2 changed files with 33 additions and 49 deletions

View File

@ -317,10 +317,10 @@ public class AntPathMatcher implements PathMatcher {
if (!StringUtils.hasText(pattern1) && !StringUtils.hasText(pattern2)) {
return "";
}
else if (!StringUtils.hasText(pattern1)) {
if (!StringUtils.hasText(pattern1)) {
return pattern2;
}
else if (!StringUtils.hasText(pattern2)) {
if (!StringUtils.hasText(pattern2)) {
return pattern1;
}
@ -330,55 +330,37 @@ public class AntPathMatcher implements PathMatcher {
// However /user + /user -> /usr/user ; /{foo} + /bar -> /{foo}/bar
return pattern2;
}
else if (pattern1.endsWith("/*")) {
if (pattern2.startsWith("/")) {
// /hotels/* + /booking -> /hotels/booking
return pattern1.substring(0, pattern1.length() - 1) + pattern2.substring(1);
}
else {
// /hotels/* + booking -> /hotels/booking
return pattern1.substring(0, pattern1.length() - 1) + pattern2;
}
}
else if (pattern1.endsWith("/**")) {
if (pattern2.startsWith("/")) {
// /hotels/** + /booking -> /hotels/**/booking
return pattern1 + pattern2;
}
else {
// /hotels/** + booking -> /hotels/**/booking
return pattern1 + "/" + pattern2;
}
}
else {
int dotPos1 = pattern1.indexOf('.');
if (dotPos1 == -1 || pattern1ContainsUriVar) {
// simply concatenate the two patterns
if (pattern1.endsWith("/") || pattern2.startsWith("/")) {
return pattern1 + pattern2;
}
else {
return pattern1 + "/" + pattern2;
}
}
String fileName1 = pattern1.substring(0, dotPos1);
String extension1 = pattern1.substring(dotPos1);
String fileName2;
String extension2;
int dotPos2 = pattern2.indexOf('.');
if (dotPos2 != -1) {
fileName2 = pattern2.substring(0, dotPos2);
extension2 = pattern2.substring(dotPos2);
}
else {
fileName2 = pattern2;
extension2 = "";
}
String fileName = fileName1.endsWith("*") ? fileName2 : fileName1;
String extension = extension1.startsWith("*") ? extension2 : extension1;
return fileName + extension;
// /hotels/* + /booking -> /hotels/booking
// /hotels/* + booking -> /hotels/booking
if (pattern1.endsWith("/*")) {
return slashConcat(pattern1.substring(0, pattern1.length() - 2), pattern2);
}
// /hotels/** + /booking -> /hotels/**/booking
// /hotels/** + booking -> /hotels/**/booking
if (pattern1.endsWith("/**")) {
return slashConcat(pattern1, pattern2);
}
int starDotPos1 = pattern1.indexOf("*.");
if (pattern1ContainsUriVar || starDotPos1 == -1) {
// simply concatenate the two patterns
return slashConcat(pattern1, pattern2);
}
String extension1 = pattern1.substring(starDotPos1 + 1);
int dotPos2 = pattern2.indexOf('.');
String fileName2 = (dotPos2 == -1 ? pattern2 : pattern2.substring(0, dotPos2));
String extension2 = (dotPos2 == -1 ? "" : pattern2.substring(dotPos2));
String extension = extension1.startsWith("*") ? extension2 : extension1;
return fileName2 + extension;
}
private String slashConcat(String path1, String path2) {
if (path1.endsWith("/") || path2.startsWith("/")) {
return path1 + path2;
}
return path1 + "/" + path2;
}
/**

View File

@ -399,6 +399,7 @@ public class AntPathMatcherTests {
assertEquals("/hotels/**/booking", pathMatcher.combine("/hotels/**", "/booking"));
assertEquals("/hotels/booking", pathMatcher.combine("/hotels", "/booking"));
assertEquals("/hotels/booking", pathMatcher.combine("/hotels", "booking"));
assertEquals("/hotels/booking", pathMatcher.combine("/hotels/", "booking"));
assertEquals("/hotels/{hotel}", pathMatcher.combine("/hotels/*", "{hotel}"));
assertEquals("/hotels/**/{hotel}", pathMatcher.combine("/hotels/**", "{hotel}"));
assertEquals("/hotels/{hotel}", pathMatcher.combine("/hotels", "{hotel}"));
@ -413,6 +414,7 @@ public class AntPathMatcherTests {
assertEquals("/{foo}/bar", pathMatcher.combine("/{foo}", "/bar")); // SPR-8858
assertEquals("/user/user", pathMatcher.combine("/user", "/user")); // SPR-7970
assertEquals("/{foo:.*[^0-9].*}/edit/", pathMatcher.combine("/{foo:.*[^0-9].*}", "/edit/")); // SPR-10062
assertEquals("/1.0/foo/test", pathMatcher.combine("/1.0", "/foo/test")); // SPR-10554
}
@Test