All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.pustike.web.utils.AntPathMatcher Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2021 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.github.pustike.web.utils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * PathMatcher implementation for Ant-style path patterns.
 *
 * 

Part of this mapping code has been kindly borrowed from Apache Ant. * *

The mapping matches URLs using the following rules:
*

    *
  • {@code ?} matches one character
  • *
  • {@code *} matches zero or more characters
  • *
  • {@code **} matches zero or more directories in a path
  • *
  • {@code {spring:[a-z]+}} matches the regexp {@code [a-z]+} as a path variable named "spring"
  • *
* *

Examples

*
    *
  • {@code com/t?st.jsp} — matches {@code com/test.jsp} but also * {@code com/tast.jsp} or {@code com/txst.jsp}
  • *
  • {@code com/*.jsp} — matches all {@code .jsp} files in the {@code com} directory
  • *
  • com/**/test.jsp — matches all {@code test.jsp} * files underneath the {@code com} path
  • *
  • org/springframework/**/*.jsp — matches all * {@code .jsp} files underneath the {@code org/springframework} path
  • *
  • org/**/servlet/bla.jsp — matches {@code org/springframework/servlet/bla.jsp} but also * {@code org/springframework/testing/servlet/bla.jsp} and {@code org/servlet/bla.jsp}
  • *
  • {@code com/{filename:\\w+}.jsp} will match {@code com/test.jsp} and assign the value {@code test} * to the {@code filename} variable
  • *
* *

Note: a pattern and a path must both be absolute or must both be relative in order for * the two to match. Therefore it is recommended that users of this implementation to sanitize patterns * in order to prefix them with "/" as it makes sense in the context in which they're used. * *

* Source: This code has been borrowed from * * Spring Framework's AntPathMatcher.java. *

*/ public class AntPathMatcher { private static final String DEFAULT_PATH_SEPARATOR = "/"; private static final int CACHE_TURNOFF_THRESHOLD = 65536; private static final char[] WILDCARD_CHARS = {'*', '?', '{'}; private final String pathSeparator; private final boolean caseSensitive = true; private final boolean trimTokens = false; private volatile Boolean cachePatterns; private final Map tokenizedPatternCache; private final Map stringMatcherCache; /** * Create a new instance with the {@link #DEFAULT_PATH_SEPARATOR}. */ public AntPathMatcher() { this.pathSeparator = DEFAULT_PATH_SEPARATOR; this.tokenizedPatternCache = new ConcurrentHashMap<>(256); this.stringMatcherCache = new ConcurrentHashMap<>(256); } /** * Specify whether to cache parsed pattern metadata for patterns passed * into this matcher's {@link #match} method. A value of {@code true} * activates an unlimited pattern cache; a value of {@code false} turns * the pattern cache off completely. *

Default is for the cache to be on, but with the variant to automatically * turn it off when encountering too many patterns to cache at runtime * (the threshold is 65536), assuming that arbitrary permutations of patterns * are coming in, with little chance for encountering a recurring pattern. * @see #getStringMatcher(String) */ public void setCachePatterns(boolean cachePatterns) { this.cachePatterns = cachePatterns; } private void deactivatePatternCache() { this.cachePatterns = false; this.tokenizedPatternCache.clear(); this.stringMatcherCache.clear(); } /** * Match the given {@code path} against the given {@code pattern}, according to this PathMatcher's matching * strategy. * @param pattern the pattern to match against * @param path the path String to test * @return {@code true} if the supplied {@code path} matched, {@code false} if it didn't */ public boolean match(String pattern, String path) { return doMatch(pattern, path, true,null); } /** * Actually match the given {@code path} against the given {@code pattern}. * @param pattern the pattern to match against * @param path the path String to test * @param fullMatch whether a full pattern match is required (else a pattern match * as far as the given base path goes is sufficient) * @return {@code true} if the supplied {@code path} matched, {@code false} if it didn't */ protected boolean doMatch(String pattern, String path, boolean fullMatch, Map uriTemplateVariables) { if (path == null || path.startsWith(this.pathSeparator) != pattern.startsWith(this.pathSeparator)) { return false; } String[] pattDirs = tokenizePattern(pattern); if (fullMatch && this.caseSensitive && !isPotentialMatch(path, pattDirs)) { return false; } String[] pathDirs = tokenizePath(path); int pattIdxStart = 0; int pattIdxEnd = pattDirs.length - 1; int pathIdxStart = 0; int pathIdxEnd = pathDirs.length - 1; // Match all elements up to the first ** while (pattIdxStart <= pattIdxEnd && pathIdxStart <= pathIdxEnd) { String pattDir = pattDirs[pattIdxStart]; if ("**".equals(pattDir)) { break; } if (!matchStrings(pattDir, pathDirs[pathIdxStart], uriTemplateVariables)) { return false; } pattIdxStart++; pathIdxStart++; } if (pathIdxStart > pathIdxEnd) { // Path is exhausted, only match if rest of pattern is * or **'s if (pattIdxStart > pattIdxEnd) { return (pattern.endsWith(this.pathSeparator) == path.endsWith(this.pathSeparator)); } if (!fullMatch) { return true; } if (pattIdxStart == pattIdxEnd && pattDirs[pattIdxStart].equals("*") && path.endsWith(this.pathSeparator)) { return true; } for (int i = pattIdxStart; i <= pattIdxEnd; i++) { if (!pattDirs[i].equals("**")) { return false; } } return true; } else if (pattIdxStart > pattIdxEnd) { // String not exhausted, but pattern is. Failure. return false; } else if (!fullMatch && "**".equals(pattDirs[pattIdxStart])) { // Path start definitely matches due to "**" part in pattern. return true; } // up to last '**' while (pattIdxStart <= pattIdxEnd && pathIdxStart <= pathIdxEnd) { String pattDir = pattDirs[pattIdxEnd]; if (pattDir.equals("**")) { break; } if (!matchStrings(pattDir, pathDirs[pathIdxEnd], uriTemplateVariables)) { return false; } pattIdxEnd--; pathIdxEnd--; } if (pathIdxStart > pathIdxEnd) { // String is exhausted for (int i = pattIdxStart; i <= pattIdxEnd; i++) { if (!pattDirs[i].equals("**")) { return false; } } return true; } while (pattIdxStart != pattIdxEnd && pathIdxStart <= pathIdxEnd) { int patIdxTmp = -1; for (int i = pattIdxStart + 1; i <= pattIdxEnd; i++) { if (pattDirs[i].equals("**")) { patIdxTmp = i; break; } } if (patIdxTmp == pattIdxStart + 1) { // '**/**' situation, so skip one pattIdxStart++; continue; } // Find the pattern between padIdxStart & padIdxTmp in str between // strIdxStart & strIdxEnd int patLength = (patIdxTmp - pattIdxStart - 1); int strLength = (pathIdxEnd - pathIdxStart + 1); int foundIdx = -1; strLoop: for (int i = 0; i <= strLength - patLength; i++) { for (int j = 0; j < patLength; j++) { String subPat = pattDirs[pattIdxStart + j + 1]; String subStr = pathDirs[pathIdxStart + i + j]; if (!matchStrings(subPat, subStr, uriTemplateVariables)) { continue strLoop; } } foundIdx = pathIdxStart + i; break; } if (foundIdx == -1) { return false; } pattIdxStart = patIdxTmp; pathIdxStart = foundIdx + patLength; } for (int i = pattIdxStart; i <= pattIdxEnd; i++) { if (!pattDirs[i].equals("**")) { return false; } } return true; } private boolean isPotentialMatch(String path, String[] pattDirs) { if (!this.trimTokens) { int pos = 0; for (String pattDir : pattDirs) { int skipped = skipSeparator(path, pos, this.pathSeparator); pos += skipped; skipped = skipSegment(path, pos, pattDir); if (skipped < pattDir.length()) { return (skipped > 0 || (pattDir.length() > 0 && isWildcardChar(pattDir.charAt(0)))); } pos += skipped; } } return true; } private int skipSegment(String path, int pos, String prefix) { int skipped = 0; for (int i = 0; i < prefix.length(); i++) { char c = prefix.charAt(i); if (isWildcardChar(c)) { return skipped; } int currPos = pos + skipped; if (currPos >= path.length()) { return 0; } if (c == path.charAt(currPos)) { skipped++; } } return skipped; } private int skipSeparator(String path, int pos, String separator) { int skipped = 0; while (path.startsWith(separator, pos + skipped)) { skipped += separator.length(); } return skipped; } private boolean isWildcardChar(char c) { for (char candidate : WILDCARD_CHARS) { if (c == candidate) { return true; } } return false; } /** * Tokenize the given path pattern into parts, based on this matcher's settings. *

Performs caching based on {@link #setCachePatterns}, delegating to * {@link #tokenizePath(String)} for the actual tokenization algorithm. * @param pattern the pattern to tokenize * @return the tokenized pattern parts */ protected String[] tokenizePattern(String pattern) { String[] tokenized = null; Boolean cachePatterns = this.cachePatterns; if (cachePatterns == null || cachePatterns) { tokenized = this.tokenizedPatternCache.get(pattern); } if (tokenized == null) { tokenized = tokenizePath(pattern); if (cachePatterns == null && this.tokenizedPatternCache.size() >= CACHE_TURNOFF_THRESHOLD) { // Try to adapt to the runtime situation that we're encountering: // There are obviously too many different patterns coming in here... // So let's turn off the cache since the patterns are unlikely to be reoccurring. deactivatePatternCache(); return tokenized; } if (cachePatterns == null || cachePatterns) { this.tokenizedPatternCache.put(pattern, tokenized); } } return tokenized; } /** * Tokenize the given path String into parts, based on this matcher's settings. * @param path the path to tokenize * @return the tokenized path parts */ protected String[] tokenizePath(String path) { return tokenizeToStringArray(path, this.pathSeparator, this.trimTokens, true); } /** * Test whether or not a string matches against a pattern. * @param pattern the pattern to match against (never {@code null}) * @param str the String which must be matched against the pattern (never {@code null}) * @return {@code true} if the string matches against the pattern, or {@code false} otherwise */ private boolean matchStrings(String pattern, String str, Map uriTemplateVariables) { return getStringMatcher(pattern).matchStrings(str, uriTemplateVariables); } /** * Build or retrieve an {@link AntPathStringMatcher} for the given pattern. *

The default implementation checks this AntPathMatcher's internal cache * (see {@link #setCachePatterns}), creating a new AntPathStringMatcher instance * if no cached copy is found. *

When encountering too many patterns to cache at runtime (the threshold is 65536), * it turns the default cache off, assuming that arbitrary permutations of patterns * are coming in, with little chance for encountering a recurring pattern. *

This method may be overridden to implement a custom cache strategy. * @param pattern the pattern to match against (never {@code null}) * @return a corresponding AntPathStringMatcher (never {@code null}) * @see #setCachePatterns */ protected AntPathStringMatcher getStringMatcher(String pattern) { AntPathStringMatcher matcher = null; Boolean cachePatterns = this.cachePatterns; if (cachePatterns == null || cachePatterns) { matcher = this.stringMatcherCache.get(pattern); } if (matcher == null) { matcher = new AntPathStringMatcher(pattern, this.caseSensitive); if (cachePatterns == null && this.stringMatcherCache.size() >= CACHE_TURNOFF_THRESHOLD) { // Try to adapt to the runtime situation that we're encountering: // There are obviously too many different patterns coming in here... // So let's turn off the cache since the patterns are unlikely to be reoccurring. deactivatePatternCache(); return matcher; } if (cachePatterns == null || cachePatterns) { this.stringMatcherCache.put(pattern, matcher); } } return matcher; } /** * Given a pattern and a full path, extract the URI template variables. URI template * variables are expressed through curly brackets ('{' and '}'). * *

For example: For pattern "/hotels/{hotel}" and path "/hotels/1", this method will * return a map containing "hotel"->"1". * @param pattern the path pattern, possibly containing URI templates * @param path the full path to extract template variables from * @return a map, containing variable names as keys; variables values as values */ public Map extractUriTemplateVariables(String pattern, String path) { Map variables = new LinkedHashMap<>(); boolean result = doMatch(pattern, path, true, variables); if (!result) { throw new IllegalStateException("Pattern \"" + pattern + "\" is not a match for \"" + path + "\""); } return variables; } /** * Tests whether or not a string matches against a pattern via a {@link Pattern}. *

The pattern may contain special characters: '*' means zero or more characters; '?' means one and * only one character; '{' and '}' indicate a URI template pattern. For example /users/{user}. */ protected static class AntPathStringMatcher { private static final Pattern GLOB_PATTERN = Pattern.compile("\\?|\\*|\\{((?:\\{[^/]+?}|[^/{}]|\\\\[{}])+?)}"); private static final String DEFAULT_VARIABLE_PATTERN = "((?s).*)"; private final String rawPattern; private final boolean caseSensitive; private final boolean exactMatch; private final Pattern pattern; private final List variableNames = new ArrayList<>(); public AntPathStringMatcher(String pattern, boolean caseSensitive) { this.rawPattern = pattern; this.caseSensitive = caseSensitive; StringBuilder patternBuilder = new StringBuilder(); Matcher matcher = GLOB_PATTERN.matcher(pattern); int end = 0; while (matcher.find()) { patternBuilder.append(quote(pattern, end, matcher.start())); String match = matcher.group(); if ("?".equals(match)) { patternBuilder.append('.'); } else if ("*".equals(match)) { patternBuilder.append(".*"); } else if (match.startsWith("{") && match.endsWith("}")) { int colonIdx = match.indexOf(':'); if (colonIdx == -1) { patternBuilder.append(DEFAULT_VARIABLE_PATTERN); this.variableNames.add(matcher.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); this.variableNames.add(variableName); } } end = matcher.end(); } // No glob pattern was found, this is an exact String match if (end == 0) { this.exactMatch = true; this.pattern = null; } else { this.exactMatch = false; patternBuilder.append(quote(pattern, end, pattern.length())); this.pattern = (this.caseSensitive ? Pattern.compile(patternBuilder.toString()) : Pattern.compile(patternBuilder.toString(), Pattern.CASE_INSENSITIVE)); } } private String quote(String s, int start, int end) { if (start == end) { return ""; } return Pattern.quote(s.substring(start, end)); } /** * Main entry point. * @return {@code true} if the string matches against the pattern, or {@code false} otherwise. */ public boolean matchStrings(String str, Map uriTemplateVariables) { if (this.exactMatch) { return this.caseSensitive ? this.rawPattern.equals(str) : this.rawPattern.equalsIgnoreCase(str); } else if (this.pattern != null) { Matcher matcher = this.pattern.matcher(str); if (matcher.matches()) { if (uriTemplateVariables != null) { if (this.variableNames.size() != matcher.groupCount()) { throw new IllegalArgumentException("The number of capturing groups in the pattern segment " + this.pattern + " does not match the number of URI template variables it defines, " + "which can occur if capturing groups are used in a URI template regex. " + "Use non-capturing groups instead."); } for (int i = 1; i <= matcher.groupCount(); i++) { String name = this.variableNames.get(i - 1); String value = matcher.group(i); uriTemplateVariables.put(name, value); } } return true; } } return false; } } /** * Tokenize the given {@code String} into a {@code String} array via a {@link StringTokenizer}. * *

The given {@code delimiters} string can consist of any number of delimiter characters. * Each of those characters can be used to separate tokens. * @param str the {@code String} to tokenize * @param delimiters the delimiter characters, assembled as a {@code String} * (each of the characters is individually considered as a delimiter) * @param trimTokens trim the tokens via {@link String#trim()} * @param ignoreEmptyTokens omit empty tokens from the result array * (only applies to tokens that are empty after trimming; StringTokenizer * will not consider subsequent delimiters as token in the first place). * @return an array of the tokens ({@code null} if the input {@code String} was {@code null}) * @see java.util.StringTokenizer * @see String#trim() */ public static String[] tokenizeToStringArray(String str, String delimiters, boolean trimTokens, boolean ignoreEmptyTokens) { if (str == null) { return null; } StringTokenizer st = new StringTokenizer(str, delimiters); List tokens = new ArrayList<>(); while (st.hasMoreTokens()) { String token = st.nextToken(); if (trimTokens) { token = token.trim(); } if (!ignoreEmptyTokens || token.length() > 0) { tokens.add(token); } } return tokens.toArray(new String[0]); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy