|
@@ -13,17 +13,25 @@ import org.springframework.util.StringUtils;
|
|
* Matcher which compares a pre-defined ant-style pattern against the URL
|
|
* Matcher which compares a pre-defined ant-style pattern against the URL
|
|
* ({@code servletPath + pathInfo}) of an {@code HttpServletRequest}.
|
|
* ({@code servletPath + pathInfo}) of an {@code HttpServletRequest}.
|
|
* The query string of the URL is ignored and matching is case-insensitive.
|
|
* The query string of the URL is ignored and matching is case-insensitive.
|
|
|
|
+ * <p>
|
|
|
|
+ * Using a pattern value of {@code /**} or {@code **} is treated as a universal
|
|
|
|
+ * match, which will match any request. Patterns which end with {@code /**} (and have no other wildcards)
|
|
|
|
+ * are optimized by using a substring match — a pattern of {@code /aaa/**} will match {@code /aaa},
|
|
|
|
+ * {@code /aaa/} and any sub-directories, such as {@code /aaa/bbb/ccc}.
|
|
|
|
+ * <p>
|
|
|
|
+ * For all other cases, Spring's {@link AntPathMatcher} is used to perform the match. See the Spring documentation
|
|
|
|
+ * for this class for comprehensive information on the syntax used.
|
|
*
|
|
*
|
|
* @author Luke Taylor
|
|
* @author Luke Taylor
|
|
* @since 3.1
|
|
* @since 3.1
|
|
*
|
|
*
|
|
- * @see AntPathMatcher
|
|
|
|
|
|
+ * @see org.springframework.util.AntPathMatcher
|
|
*/
|
|
*/
|
|
public final class AntPathRequestMatcher implements RequestMatcher {
|
|
public final class AntPathRequestMatcher implements RequestMatcher {
|
|
- private final static Log logger = LogFactory.getLog(AntPathRequestMatcher.class);
|
|
|
|
-
|
|
|
|
- private static final AntPathMatcher antMatcher = new AntPathMatcher();
|
|
|
|
|
|
+ private static final Log logger = LogFactory.getLog(AntPathRequestMatcher.class);
|
|
|
|
+ private static final String MATCH_ALL = "/**";
|
|
|
|
|
|
|
|
+ private final Matcher matcher;
|
|
private final String pattern;
|
|
private final String pattern;
|
|
private final HttpMethod httpMethod;
|
|
private final HttpMethod httpMethod;
|
|
|
|
|
|
@@ -45,7 +53,23 @@ public final class AntPathRequestMatcher implements RequestMatcher {
|
|
*/
|
|
*/
|
|
public AntPathRequestMatcher(String pattern, String httpMethod) {
|
|
public AntPathRequestMatcher(String pattern, String httpMethod) {
|
|
Assert.hasText(pattern, "Pattern cannot be null or empty");
|
|
Assert.hasText(pattern, "Pattern cannot be null or empty");
|
|
- this.pattern = pattern.toLowerCase();
|
|
|
|
|
|
+
|
|
|
|
+ if (pattern.equals(MATCH_ALL) || pattern.equals("**")) {
|
|
|
|
+ pattern = MATCH_ALL;
|
|
|
|
+ matcher = null;
|
|
|
|
+ } else {
|
|
|
|
+ pattern = pattern.toLowerCase();
|
|
|
|
+
|
|
|
|
+ // If the pattern ends with {@code /**} and has no other wildcards, then optimize to a sub-path match
|
|
|
|
+ if (pattern.endsWith(MATCH_ALL) && pattern.indexOf('?') == -1 &&
|
|
|
|
+ pattern.indexOf("*") == pattern.length() - 2) {
|
|
|
|
+ matcher = new SubpathMatcher(pattern.substring(0, pattern.length() - 3));
|
|
|
|
+ } else {
|
|
|
|
+ matcher = new SpringAntMatcher(pattern);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ this.pattern = pattern;
|
|
this.httpMethod = StringUtils.hasText(httpMethod) ? HttpMethod.valueOf(httpMethod) : null;
|
|
this.httpMethod = StringUtils.hasText(httpMethod) ? HttpMethod.valueOf(httpMethod) : null;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -57,23 +81,41 @@ public final class AntPathRequestMatcher implements RequestMatcher {
|
|
*/
|
|
*/
|
|
public boolean matches(HttpServletRequest request) {
|
|
public boolean matches(HttpServletRequest request) {
|
|
if (httpMethod != null && httpMethod != HttpMethod.valueOf(request.getMethod())) {
|
|
if (httpMethod != null && httpMethod != HttpMethod.valueOf(request.getMethod())) {
|
|
|
|
+ if (logger.isDebugEnabled()) {
|
|
|
|
+ logger.debug("Request '" + request.getMethod() + " " + getRequestPath(request) + "'"
|
|
|
|
+ + " doesn't match '" + httpMethod + " " + pattern);
|
|
|
|
+ }
|
|
|
|
+
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
- String url = request.getServletPath();
|
|
|
|
|
|
+ if (pattern.equals(MATCH_ALL)) {
|
|
|
|
+ if (logger.isDebugEnabled()) {
|
|
|
|
+ logger.debug("Request '" + getRequestPath(request) + "' matched by universal pattern '/**'");
|
|
|
|
+ }
|
|
|
|
|
|
- if (request.getPathInfo() != null) {
|
|
|
|
- url += request.getPathInfo();
|
|
|
|
|
|
+ return true;
|
|
}
|
|
}
|
|
|
|
|
|
- url = url.toLowerCase();
|
|
|
|
|
|
+ String url = getRequestPath(request);
|
|
|
|
|
|
if (logger.isDebugEnabled()) {
|
|
if (logger.isDebugEnabled()) {
|
|
logger.debug("Checking match of request : '" + url + "'; against '" + pattern + "'");
|
|
logger.debug("Checking match of request : '" + url + "'; against '" + pattern + "'");
|
|
}
|
|
}
|
|
|
|
|
|
- // TODO: Optimise, since the pattern is fixed.
|
|
|
|
- return antMatcher.match(pattern, url);
|
|
|
|
|
|
+ return matcher.matches(url);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private String getRequestPath(HttpServletRequest request) {
|
|
|
|
+ String url = request.getServletPath();
|
|
|
|
+
|
|
|
|
+ if (request.getPathInfo() != null) {
|
|
|
|
+ url += request.getPathInfo();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ url = url.toLowerCase();
|
|
|
|
+
|
|
|
|
+ return url;
|
|
}
|
|
}
|
|
|
|
|
|
public String getPattern() {
|
|
public String getPattern() {
|
|
@@ -96,11 +138,47 @@ public final class AntPathRequestMatcher implements RequestMatcher {
|
|
sb.append("Ant [pattern='").append(pattern).append("'");
|
|
sb.append("Ant [pattern='").append(pattern).append("'");
|
|
|
|
|
|
if (httpMethod != null) {
|
|
if (httpMethod != null) {
|
|
- sb.append(", " + httpMethod);
|
|
|
|
|
|
+ sb.append(", ").append(httpMethod);
|
|
}
|
|
}
|
|
|
|
|
|
sb.append("]");
|
|
sb.append("]");
|
|
|
|
|
|
return sb.toString();
|
|
return sb.toString();
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ private static interface Matcher {
|
|
|
|
+ boolean matches(String path);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static class SpringAntMatcher implements Matcher {
|
|
|
|
+ private static final AntPathMatcher antMatcher = new AntPathMatcher();
|
|
|
|
+
|
|
|
|
+ private final String pattern;
|
|
|
|
+
|
|
|
|
+ private SpringAntMatcher(String pattern) {
|
|
|
|
+ this.pattern = pattern;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public boolean matches(String path) {
|
|
|
|
+ return antMatcher.match(pattern, path);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Optimized matcher for trailing wildcards
|
|
|
|
+ */
|
|
|
|
+ private static class SubpathMatcher implements Matcher {
|
|
|
|
+ private final String subpath;
|
|
|
|
+ private final int length;
|
|
|
|
+
|
|
|
|
+ private SubpathMatcher(String subpath) {
|
|
|
|
+ assert !subpath.contains("*");
|
|
|
|
+ this.subpath = subpath;
|
|
|
|
+ this.length = subpath.length();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public boolean matches(String path) {
|
|
|
|
+ return path.startsWith(subpath) && (path.length() == length || path.charAt(length) == '/');
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|