Browse Source

Add getMatchingPatterns method to Patterns condition

Issue: SPR-11484
pull/473/head
Rossen Stoyanchev 11 years ago
parent
commit
801237aec1
  1. 23
      spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/PatternsRequestCondition.java

23
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/PatternsRequestCondition.java

@ -201,11 +201,30 @@ public final class PatternsRequestCondition extends AbstractRequestCondition<Pat
*/ */
@Override @Override
public PatternsRequestCondition getMatchingCondition(HttpServletRequest request) { public PatternsRequestCondition getMatchingCondition(HttpServletRequest request) {
if (this.patterns.isEmpty()) { if (this.patterns.isEmpty()) {
return this; return this;
} }
String lookupPath = this.pathHelper.getLookupPathForRequest(request); String lookupPath = this.pathHelper.getLookupPathForRequest(request);
List<String> matches = getMatchingPatterns(lookupPath);
return matches.isEmpty() ? null :
new PatternsRequestCondition(matches, this.pathHelper, this.pathMatcher, this.useSuffixPatternMatch,
this.useTrailingSlashMatch, this.fileExtensions);
}
/**
* Find the patterns matching the given lookup path. Invoking this method should
* yield results equivalent to those of calling
* {@link #getMatchingCondition(javax.servlet.http.HttpServletRequest)}.
* This method is provided as an alternative to be used if no request is available
* (e.g. introspection, tooling, etc).
*
* @param lookupPath the lookup path to match to existing patterns
* @return a collection of matching patterns sorted with the closest match at the top
*/
public List<String> getMatchingPatterns(String lookupPath) {
List<String> matches = new ArrayList<String>(); List<String> matches = new ArrayList<String>();
for (String pattern : this.patterns) { for (String pattern : this.patterns) {
String match = getMatchingPattern(pattern, lookupPath); String match = getMatchingPattern(pattern, lookupPath);
@ -214,9 +233,7 @@ public final class PatternsRequestCondition extends AbstractRequestCondition<Pat
} }
} }
Collections.sort(matches, this.pathMatcher.getPatternComparator(lookupPath)); Collections.sort(matches, this.pathMatcher.getPatternComparator(lookupPath));
return matches.isEmpty() ? null : return matches;
new PatternsRequestCondition(matches, this.pathHelper, this.pathMatcher, this.useSuffixPatternMatch,
this.useTrailingSlashMatch, this.fileExtensions);
} }
private String getMatchingPattern(String pattern, String lookupPath) { private String getMatchingPattern(String pattern, String lookupPath) {

Loading…
Cancel
Save