SPR-5812 - Custom regex matching for @PathVariable
This commit is contained in:
parent
0096930a72
commit
ed98bf0668
|
|
@ -23,11 +23,11 @@ import java.util.regex.Matcher;
|
|||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Package-protected helper class for {@link AntPathMatcher}.
|
||||
* Tests whether or not a string matches against a pattern using a regular expression.
|
||||
* Package-protected helper class for {@link AntPathMatcher}. Tests whether or not a string matches against a pattern
|
||||
* using a regular expression.
|
||||
*
|
||||
* <p>The pattern may contain special characters: '*' means zero or more characters;
|
||||
* '?' means one and only one character; '{' and '}' indicate a URI template pattern.
|
||||
* <p>The pattern may contain special characters: '*' means zero or more characters; '?' means one and only one
|
||||
* character; '{' and '}' indicate a URI template pattern.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 3.0
|
||||
|
|
@ -36,6 +36,8 @@ class AntPatchStringMatcher {
|
|||
|
||||
private static final Pattern GLOB_PATTERN = Pattern.compile("\\?|\\*|\\{([^/]+?)\\}");
|
||||
|
||||
private static final String DEFAULT_VARIABLE_PATTERN = "(.*)";
|
||||
|
||||
private final Pattern pattern;
|
||||
|
||||
private String str;
|
||||
|
|
@ -65,9 +67,20 @@ class AntPatchStringMatcher {
|
|||
patternBuilder.append(".*");
|
||||
}
|
||||
else if (match.startsWith("{") && match.endsWith("}")) {
|
||||
patternBuilder.append("(.*)");
|
||||
int colonIdx = match.indexOf(':');
|
||||
if (colonIdx == -1) {
|
||||
patternBuilder.append(DEFAULT_VARIABLE_PATTERN);
|
||||
variableNames.add(m.group(1));
|
||||
}
|
||||
else {
|
||||
String variablePattern = match.substring(colonIdx + 1, match.length() - 1);
|
||||
patternBuilder.append('(');
|
||||
patternBuilder.append(variablePattern);
|
||||
patternBuilder.append(')');
|
||||
String variableName = match.substring(1, colonIdx);
|
||||
variableNames.add(variableName);
|
||||
}
|
||||
}
|
||||
end = m.end();
|
||||
}
|
||||
patternBuilder.append(quote(pattern, end, pattern.length()));
|
||||
|
|
|
|||
|
|
@ -334,6 +334,20 @@ public class AntPathMatcherTests {
|
|||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void extractUriTemplateVariablesCustomRegex() {
|
||||
Map<String, String> result = pathMatcher
|
||||
.extractUriTemplateVariables("{symbolicName:[\\w\\.]+}-{version:[\\w\\.]+}.jar",
|
||||
"com.example-1.0.0.jar");
|
||||
assertEquals("com.example", result.get("symbolicName"));
|
||||
assertEquals("1.0.0", result.get("version"));
|
||||
|
||||
result = pathMatcher.extractUriTemplateVariables("{symbolicName:[\\w\\.]+}-sources-{version:[\\w\\.]+}.jar",
|
||||
"com.example-sources-1.0.0.jar");
|
||||
assertEquals("com.example", result.get("symbolicName"));
|
||||
assertEquals("1.0.0", result.get("version"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void combine() {
|
||||
assertEquals("", pathMatcher.combine(null, null));
|
||||
|
|
|
|||
Loading…
Reference in New Issue