下面的代码摘自tomcat源码,用来匹配<filter-mapping>中的<url-pattern>,相信比文字更容易理解:
java 代码
java 代码
- /**
- * Return <code>true</code> if the context-relative request path
- * matches the requirements of the specified filter mapping;
- * otherwise, return <code>null</code>.
- *
- * @param filterMap Filter mapping being checked
- * @param requestPath Context-relative request path of this request
- */
- private boolean matchFiltersURL(FilterMap filterMap, String requestPath) {
- if (requestPath == null)
- return (false);
- // Match on context relative request path
- String testPath = filterMap.getURLPattern();
- if (testPath == null)
- return (false);
- // Case 1 - Exact Match
- if (testPath.equals(requestPath))
- return (true);
- // Case 2 - Path Match ("/.../*")
- if (testPath.equals("/*"))
- return (true);
- if (testPath.endsWith("/*")) {
- if (testPath.regionMatches(0, requestPath, 0,
- testPath.length() - 2)) {
- if (requestPath.length() == (testPath.length() - 2)) {
- return (true);
- } else if ('/' == requestPath.charAt(testPath.length() - 2)) {
- return (true);
- }
- }
- return (false);
- }
- // Case 3 - Extension Match
- if (testPath.startsWith("*.")) {
- int slash = requestPath.lastIndexOf('/');
- int period = requestPath.lastIndexOf('.');
- if ((slash >= 0) && (period > slash)
- && (period != requestPath.length() - 1)
- && ((requestPath.length() - period)
- == (testPath.length() - 1))) {
- return (testPath.regionMatches(2, requestPath, period + 1,
- testPath.length() - 2));
- }
- }
- // Case 4 - "Default" Match
- return (false); // NOTE - Not relevant for selecting filters
- }