Skip to content

Commit

Permalink
AntPathRequestMatcher implements RequestVariableExtractor
Browse files Browse the repository at this point in the history
Issue gh-3964
  • Loading branch information
Rob Winch committed Jul 6, 2016
1 parent e4c13e3 commit 9d50944
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public Object lookupVariable(String name) {
if (this.variables == null) {
this.variables = extractVariables(request);
}
name = postProcessVariableName(name);
return this.variables.get(name);
}

Expand All @@ -63,6 +62,4 @@ public Object lookupVariable(String name) {

abstract Map<String, String> extractVariables(HttpServletRequest request);

abstract String postProcessVariableName(String variableName);

}
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,6 @@ private static LinkedHashMap<RequestMatcher, Collection<ConfigAttribute>> proces

private static AbstractVariableEvaluationContextPostProcessor createPostProcessor(
Object request) {
if (request instanceof AntPathRequestMatcher) {
return new AntPathMatcherEvaluationContextPostProcessor(
(AntPathRequestMatcher) request);
}
if (request instanceof RequestVariablesExtractor) {
return new RequestVariablesExtractorEvaluationContextPostProcessor(
(RequestVariablesExtractor) request);
Expand All @@ -117,11 +113,6 @@ public AntPathMatcherEvaluationContextPostProcessor(
Map<String, String> extractVariables(HttpServletRequest request) {
return this.matcher.extractUriTemplateVariables(request);
}

@Override
String postProcessVariableName(String variableName) {
return this.matcher.postProcessVariableName(variableName);
}
}

static class RequestVariablesExtractorEvaluationContextPostProcessor
Expand All @@ -137,11 +128,6 @@ public RequestVariablesExtractorEvaluationContextPostProcessor(
Map<String, String> extractVariables(HttpServletRequest request) {
return this.matcher.extractUriTemplateVariables(request);
}

@Override
String postProcessVariableName(String variableName) {
return variableName;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
*
* @see org.springframework.util.AntPathMatcher
*/
public final class AntPathRequestMatcher implements RequestMatcher {
public final class AntPathRequestMatcher
implements RequestMatcher, RequestVariablesExtractor {
private static final Log logger = LogFactory.getLog(AntPathRequestMatcher.class);
private static final String MATCH_ALL = "/**";

Expand Down Expand Up @@ -102,21 +103,17 @@ public AntPathRequestMatcher(String pattern, String httpMethod,
this.matcher = null;
}
else {
if (!caseSensitive) {
pattern = pattern.toLowerCase();
}

// If the pattern ends with {@code /**} and has no other wildcards or path
// variables, then optimize to a sub-path match
if (pattern.endsWith(MATCH_ALL)
&& (pattern.indexOf('?') == -1 && pattern.indexOf('{') == -1
&& pattern.indexOf('}') == -1)
&& pattern.indexOf("*") == pattern.length() - 2) {
this.matcher = new SubpathMatcher(
pattern.substring(0, pattern.length() - 3));
pattern.substring(0, pattern.length() - 3), caseSensitive);
}
else {
this.matcher = new SpringAntMatcher(pattern);
this.matcher = new SpringAntMatcher(pattern, caseSensitive);
}
}

Expand Down Expand Up @@ -164,6 +161,7 @@ public boolean matches(HttpServletRequest request) {
return this.matcher.matches(url);
}

@Override
public Map<String, String> extractUriTemplateVariables(HttpServletRequest request) {
if (this.matcher == null || !matches(request)) {
return Collections.emptyMap();
Expand All @@ -172,21 +170,13 @@ public Map<String, String> extractUriTemplateVariables(HttpServletRequest reques
return this.matcher.extractUriTemplateVariables(url);
}

public String postProcessVariableName(String variableName) {
return this.caseSensitive ? variableName : variableName.toLowerCase();
}

private String getRequestPath(HttpServletRequest request) {
String url = request.getServletPath();

if (request.getPathInfo() != null) {
url += request.getPathInfo();
}

if (!this.caseSensitive) {
url = url.toLowerCase();
}

return url;
}

Expand Down Expand Up @@ -253,27 +243,29 @@ private static interface Matcher {
}

private static class SpringAntMatcher implements Matcher {
private static final AntPathMatcher antMatcher = createMatcher();
private final AntPathMatcher antMatcher;

private final String pattern;

private SpringAntMatcher(String pattern) {
private SpringAntMatcher(String pattern, boolean caseSensitive) {
this.pattern = pattern;
this.antMatcher = createMatcher(caseSensitive);
}

@Override
public boolean matches(String path) {
return antMatcher.match(this.pattern, path);
return this.antMatcher.match(this.pattern, path);
}

@Override
public Map<String, String> extractUriTemplateVariables(String path) {
return antMatcher.extractUriTemplateVariables(this.pattern, path);
return this.antMatcher.extractUriTemplateVariables(this.pattern, path);
}

private static AntPathMatcher createMatcher() {
private static AntPathMatcher createMatcher(boolean caseSensitive) {
AntPathMatcher matcher = new AntPathMatcher();
matcher.setTrimTokens(false);
matcher.setCaseSensitive(caseSensitive);
return matcher;
}
}
Expand All @@ -284,15 +276,20 @@ private static AntPathMatcher createMatcher() {
private static class SubpathMatcher implements Matcher {
private final String subpath;
private final int length;
private final boolean caseSensitive;

private SubpathMatcher(String subpath) {
private SubpathMatcher(String subpath, boolean caseSensitive) {
assert!subpath.contains("*");
this.subpath = subpath;
this.subpath = caseSensitive ? subpath : subpath.toLowerCase();
this.length = subpath.length();
this.caseSensitive = caseSensitive;
}

@Override
public boolean matches(String path) {
if (!this.caseSensitive) {
path = path.toLowerCase();
}
return path.startsWith(this.subpath)
&& (path.length() == this.length || path.charAt(this.length) == '/');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,6 @@ public void extractVariables() {
assertThat(this.context.lookupVariable(KEY)).isEqualTo(VALUE);
}

@Test
public void postProcessVariableName() {
this.context = this.processor.postProcess(this.context, this.invocation);

assertThat(this.context.lookupVariable("nothing")).isEqualTo(VALUE);
}

@Test
public void extractVariablesOnlyUsedOnce() {
this.context = this.processor.postProcess(this.context, this.invocation);
Expand All @@ -89,10 +82,5 @@ static class VariableEvaluationContextPostProcessor
protected Map<String, String> extractVariables(HttpServletRequest request) {
return this.results;
}

@Override
String postProcessVariableName(String variableName) {
return KEY;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -210,21 +210,6 @@ public void matchesWithInvalidMethod() {
assertThat(matcher.matches(request)).isFalse();
}

@Test
public void postProcessVariableNameCaseInsensitive() {
AntPathRequestMatcher matcher = new AntPathRequestMatcher("/**", null, false);
String variableName = "userName";
assertThat(matcher.postProcessVariableName(variableName))
.isEqualTo(variableName.toLowerCase());
}

@Test
public void postProcessVariableNameCaseSensitive() {
AntPathRequestMatcher matcher = new AntPathRequestMatcher("/**", null, true);
String variableName = "userName";
assertThat(matcher.postProcessVariableName(variableName)).isEqualTo(variableName);
}

private HttpServletRequest createRequestWithNullMethod(String path) {
when(this.request.getQueryString()).thenReturn("doesntMatter");
when(this.request.getServletPath()).thenReturn(path);
Expand Down

0 comments on commit 9d50944

Please sign in to comment.