diff --git a/src/main/java/io/jboot/support/swagger/Reader.java b/src/main/java/io/jboot/support/swagger/Reader.java index 1cf1ef4a0d602116ba5f562cd94ef2772dee6d17..a1702aebc7ceec2818c13c944f81bc46ae28d5e5 100644 --- a/src/main/java/io/jboot/support/swagger/Reader.java +++ b/src/main/java/io/jboot/support/swagger/Reader.java @@ -15,6 +15,7 @@ */ package io.jboot.support.swagger; +import com.jfinal.core.ActionKey; import com.jfinal.core.Controller; import io.jboot.web.controller.JbootControllerManager; import io.swagger.models.Operation; @@ -72,7 +73,11 @@ public class Reader { String methodPath = "index".equals(method.getName()) ? "" : "/" + method.getName(); String operationPath = JbootControllerManager.me().getPathByController((Class) context.getCls()) + methodPath; - + //如果有ActionKey注解的URL路径,则使用该路径而不是方法名 + ActionKey actionKeyAnnotation = ReflectionUtils.getAnnotation(method, ActionKey.class); + if(actionKeyAnnotation != null && !actionKeyAnnotation.value().isEmpty()){ + operationPath = actionKeyAnnotation.value(); + } String httpMethod = extension.getHttpMethod(context, method); if (operationPath == null || httpMethod == null) { diff --git a/src/main/java/io/jboot/utils/AntPathMatcher.java b/src/main/java/io/jboot/utils/AntPathMatcher.java new file mode 100644 index 0000000000000000000000000000000000000000..c777c050f1de2781d1ed40b66e7a0827020f7f36 --- /dev/null +++ b/src/main/java/io/jboot/utils/AntPathMatcher.java @@ -0,0 +1,846 @@ +package io.jboot.utils; + +import java.util.*; +import java.util.concurrent.ConcurrentHashMap; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + + +public class AntPathMatcher { + + /** + * Default path separator: "/". + */ + public static final String DEFAULT_PATH_SEPARATOR = "/"; + + private static final int CACHE_TURNOFF_THRESHOLD = 65536; + + private static final Pattern VARIABLE_PATTERN = Pattern.compile("\\{[^/]+?}"); + + private static final char[] WILDCARD_CHARS = {'*', '?', '{'}; + + + private String pathSeparator; + + private PathSeparatorPatternCache pathSeparatorPatternCache; + + private boolean caseSensitive = true; + + private boolean trimTokens = false; + + + private volatile Boolean cachePatterns; + + private final Map tokenizedPatternCache = new ConcurrentHashMap<>(256); + + final Map stringMatcherCache = new ConcurrentHashMap<>(256); + + + /** + * Create a new instance with the {@link #DEFAULT_PATH_SEPARATOR}. + */ + public AntPathMatcher() { + this.pathSeparator = DEFAULT_PATH_SEPARATOR; + this.pathSeparatorPatternCache = new PathSeparatorPatternCache(DEFAULT_PATH_SEPARATOR); + } + + /** + * A convenient, alternative constructor to use with a custom path separator. + * + * @param pathSeparator the path separator to use, must not be {@code null}. + */ + public AntPathMatcher(String pathSeparator) { + this.pathSeparator = pathSeparator; + this.pathSeparatorPatternCache = new PathSeparatorPatternCache(pathSeparator); + } + + + /** + * Set the path separator to use for pattern parsing. + *

Default is "/", as in Ant. + */ + public void setPathSeparator(String pathSeparator) { + this.pathSeparator = (pathSeparator != null ? pathSeparator : DEFAULT_PATH_SEPARATOR); + this.pathSeparatorPatternCache = new PathSeparatorPatternCache(this.pathSeparator); + } + + /** + * Specify whether to perform pattern matching in a case-sensitive fashion. + *

Default is {@code true}. Switch this to {@code false} for case-insensitive matching. * + */ + public void setCaseSensitive(boolean caseSensitive) { + this.caseSensitive = caseSensitive; + } + + /** + * Specify whether to trim tokenized paths and patterns. + *

Default is {@code false}. + */ + public void setTrimTokens(boolean trimTokens) { + this.trimTokens = trimTokens; + } + + /** + * 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(); + } + + + public boolean isPattern(String path) { + if (path == null) { + return false; + } + boolean uriVar = false; + for (int i = 0; i < path.length(); i++) { + char c = path.charAt(i); + if (c == '*' || c == '?') { + return true; + } + if (c == '{') { + uriVar = true; + continue; + } + if (c == '}' && uriVar) { + return true; + } + } + return false; + } + + + public boolean match(String pattern, String path) { + return doMatch(pattern, path, true, null); + } + + + public boolean matchStart(String pattern, String path) { + return doMatch(pattern, path, false, null); + } + + /** + * Actually match the given {@code path} against the given {@code pattern}. + * + * @param pattern the pattern to match against + * @param path the path 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.booleanValue()) { + 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.booleanValue()) { + this.tokenizedPatternCache.put(pattern, tokenized); + } + } + return tokenized; + } + + /** + * Tokenize the given path 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 StringUtils.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.booleanValue()) { + 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.booleanValue()) { + this.stringMatcherCache.put(pattern, matcher); + } + } + return matcher; + } + + /** + * Given a pattern and a full path, determine the pattern-mapped part.

For example:

+ *

Assumes that {@link #match} returns {@code true} for '{@code pattern}' and '{@code path}', but + * does not enforce this. + */ + + public String extractPathWithinPattern(String pattern, String path) { + String[] patternParts = StringUtils.tokenizeToStringArray(pattern, this.pathSeparator, this.trimTokens, true); + String[] pathParts = StringUtils.tokenizeToStringArray(path, this.pathSeparator, this.trimTokens, true); + StringBuilder builder = new StringBuilder(); + boolean pathStarted = false; + + for (int segment = 0; segment < patternParts.length; segment++) { + String patternPart = patternParts[segment]; + if (patternPart.indexOf('*') > -1 || patternPart.indexOf('?') > -1) { + for (; segment < pathParts.length; segment++) { + if (pathStarted || (segment == 0 && !pattern.startsWith(this.pathSeparator))) { + builder.append(this.pathSeparator); + } + builder.append(pathParts[segment]); + pathStarted = true; + } + } + } + + return builder.toString(); + } + + + 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; + } + + /** + * Given a full path, returns a {@link Comparator} suitable for sorting patterns in order of + * explicitness. + *

This {@code Comparator} will {@linkplain List#sort(Comparator) sort} + * a list so that more specific patterns (without URI templates or wild cards) come before + * generic patterns. So given a list with the following patterns, the returned comparator + * will sort this list so that the order will be as indicated. + *

    + *
  1. {@code /hotels/new}
  2. + *
  3. {@code /hotels/{hotel}}
  4. + *
  5. {@code /hotels/*}
  6. + *
+ *

The full path given as parameter is used to test for exact matches. So when the given path + * is {@code /hotels/2}, the pattern {@code /hotels/2} will be sorted before {@code /hotels/1}. + * + * @param path the full path to use for comparison + * @return a comparator capable of sorting patterns in order of explicitness + */ + + public Comparator getPatternComparator(String path) { + return new AntPatternComparator(path); + } + + + /** + * 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) { + this(pattern, true); + } + + 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; + } + + } + + + /** + * The default {@link Comparator} implementation returned by + * {@link #getPatternComparator(String)}. + *

In order, the most "generic" pattern is determined by the following: + *

+ */ + protected static class AntPatternComparator implements Comparator { + + private final String path; + + public AntPatternComparator(String path) { + this.path = path; + } + + /** + * Compare two patterns to determine which should match first, i.e. which + * is the most specific regarding the current path. + * + * @return a negative integer, zero, or a positive integer as pattern1 is + * more specific, equally specific, or less specific than pattern2. + */ + + public int compare(String pattern1, String pattern2) { + PatternInfo info1 = new PatternInfo(pattern1); + PatternInfo info2 = new PatternInfo(pattern2); + + if (info1.isLeastSpecific() && info2.isLeastSpecific()) { + return 0; + } else if (info1.isLeastSpecific()) { + return 1; + } else if (info2.isLeastSpecific()) { + return -1; + } + + boolean pattern1EqualsPath = pattern1.equals(this.path); + boolean pattern2EqualsPath = pattern2.equals(this.path); + if (pattern1EqualsPath && pattern2EqualsPath) { + return 0; + } else if (pattern1EqualsPath) { + return -1; + } else if (pattern2EqualsPath) { + return 1; + } + + if (info1.isPrefixPattern() && info2.isPrefixPattern()) { + return info2.getLength() - info1.getLength(); + } else if (info1.isPrefixPattern() && info2.getDoubleWildcards() == 0) { + return 1; + } else if (info2.isPrefixPattern() && info1.getDoubleWildcards() == 0) { + return -1; + } + + if (info1.getTotalCount() != info2.getTotalCount()) { + return info1.getTotalCount() - info2.getTotalCount(); + } + + if (info1.getLength() != info2.getLength()) { + return info2.getLength() - info1.getLength(); + } + + if (info1.getSingleWildcards() < info2.getSingleWildcards()) { + return -1; + } else if (info2.getSingleWildcards() < info1.getSingleWildcards()) { + return 1; + } + + if (info1.getUriVars() < info2.getUriVars()) { + return -1; + } else if (info2.getUriVars() < info1.getUriVars()) { + return 1; + } + + return 0; + } + + + /** + * Value class that holds information about the pattern, e.g. number of + * occurrences of "*", "**", and "{" pattern elements. + */ + private static class PatternInfo { + + + private final String pattern; + + private int uriVars; + + private int singleWildcards; + + private int doubleWildcards; + + private boolean catchAllPattern; + + private boolean prefixPattern; + + + private Integer length; + + public PatternInfo(String pattern) { + this.pattern = pattern; + if (this.pattern != null) { + initCounters(); + this.catchAllPattern = this.pattern.equals("/**"); + this.prefixPattern = !this.catchAllPattern && this.pattern.endsWith("/**"); + } + if (this.uriVars == 0) { + this.length = (this.pattern != null ? this.pattern.length() : 0); + } + } + + protected void initCounters() { + int pos = 0; + if (this.pattern != null) { + while (pos < this.pattern.length()) { + if (this.pattern.charAt(pos) == '{') { + this.uriVars++; + pos++; + } else if (this.pattern.charAt(pos) == '*') { + if (pos + 1 < this.pattern.length() && this.pattern.charAt(pos + 1) == '*') { + this.doubleWildcards++; + pos += 2; + } else if (pos > 0 && !this.pattern.substring(pos - 1).equals(".*")) { + this.singleWildcards++; + pos++; + } else { + pos++; + } + } else { + pos++; + } + } + } + } + + public int getUriVars() { + return this.uriVars; + } + + public int getSingleWildcards() { + return this.singleWildcards; + } + + public int getDoubleWildcards() { + return this.doubleWildcards; + } + + public boolean isLeastSpecific() { + return (this.pattern == null || this.catchAllPattern); + } + + public boolean isPrefixPattern() { + return this.prefixPattern; + } + + public int getTotalCount() { + return this.uriVars + this.singleWildcards + (2 * this.doubleWildcards); + } + + /** + * Returns the length of the given pattern, where template variables are considered to be 1 long. + */ + public int getLength() { + if (this.length == null) { + this.length = (this.pattern != null ? + VARIABLE_PATTERN.matcher(this.pattern).replaceAll("#").length() : 0); + } + return this.length; + } + } + } + + + /** + * A simple cache for patterns that depend on the configured path separator. + */ + private static class PathSeparatorPatternCache { + + private final String endsOnWildCard; + + private final String endsOnDoubleWildCard; + + public PathSeparatorPatternCache(String pathSeparator) { + this.endsOnWildCard = pathSeparator + "*"; + this.endsOnDoubleWildCard = pathSeparator + "**"; + } + + public String getEndsOnWildCard() { + return this.endsOnWildCard; + } + + public String getEndsOnDoubleWildCard() { + return this.endsOnDoubleWildCard; + } + } + +} + +abstract class StringUtils { + + private static final String[] EMPTY_STRING_ARRAY = {}; + + private static final String FOLDER_SEPARATOR = "/"; + + private static final String WINDOWS_FOLDER_SEPARATOR = "\\"; + + private static final String TOP_PATH = ".."; + + private static final String CURRENT_PATH = "."; + + private static final char EXTENSION_SEPARATOR = '.'; + + + public static String[] tokenizeToStringArray(String str, String delimiters) { + return tokenizeToStringArray(str, delimiters, true, true); + } + + public static String[] tokenizeToStringArray( + String str, String delimiters, boolean trimTokens, boolean ignoreEmptyTokens) { + + if (str == null) { + return EMPTY_STRING_ARRAY; + } + + 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 toStringArray(tokens); + } + + /** + * Copy the given {@link Collection} into a {@code String} array. + *

The {@code Collection} must contain {@code String} elements only. + * + * @param collection the {@code Collection} to copy + * (potentially {@code null} or empty) + * @return the resulting {@code String} array + */ + public static String[] toStringArray(Collection collection) { + return ((collection != null && collection.size() != 0) ? collection.toArray(EMPTY_STRING_ARRAY) : EMPTY_STRING_ARRAY); + } + +} \ No newline at end of file diff --git a/src/main/java/io/jboot/web/PathVariableActionMapping.java b/src/main/java/io/jboot/web/PathVariableActionMapping.java new file mode 100644 index 0000000000000000000000000000000000000000..f473afcfcdeb3e6128901af66bde6e33fc67087a --- /dev/null +++ b/src/main/java/io/jboot/web/PathVariableActionMapping.java @@ -0,0 +1,194 @@ +package io.jboot.web; + +import com.google.common.base.Joiner; +import com.jfinal.aop.Interceptor; +import com.jfinal.aop.InterceptorManager; +import com.jfinal.config.Routes; +import com.jfinal.core.Action; +import com.jfinal.core.ActionKey; +import com.jfinal.core.Controller; +import com.jfinal.core.NotAction; +import io.jboot.utils.AntPathMatcher; +import io.jboot.utils.ArrayUtil; + +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +public class PathVariableActionMapping extends JbootActionMapping { + private static final String PATH_VARIABLE_URL_PATTERN = ".*\\{[a-zA-Z0-9]+\\}.*"; + protected Map pathVariableUrlMapping = new ConcurrentHashMap<>(); + private static final AntPathMatcher antPathMatcher = new AntPathMatcher(); + + public PathVariableActionMapping(Routes routes) { + super(routes); + } + + @Override + protected void buildActionMapping() { + mapping.clear(); + Class dc; + InterceptorManager interMan = InterceptorManager.me(); + for (Routes routes : getRoutesList()) { + for (Routes.Route route : routes.getRouteItemList()) { + Class controllerClass = route.getControllerClass(); + Interceptor[] controllerInters = interMan.createControllerInterceptor(controllerClass); + + boolean declaredMethods = !routes.getMappingSuperClass() || controllerClass.getSuperclass() == Controller.class; + + Method[] methods = (declaredMethods ? controllerClass.getDeclaredMethods() : controllerClass.getMethods()); + for (Method method : methods) { + if (declaredMethods) { + if (!Modifier.isPublic(method.getModifiers())) + continue; + } else { + dc = method.getDeclaringClass(); + if (dc == Controller.class || dc == Object.class) + continue; + } + + if (method.getAnnotation(NotAction.class) != null) { + continue; + } + + Interceptor[] actionInters = interMan.buildControllerActionInterceptor(routes.getInterceptors(), controllerInters, controllerClass, method); + String controllerPath = route.getControllerPath(); + + String methodName = method.getName(); + ActionKey ak = method.getAnnotation(ActionKey.class); + String actionKey; + if (ak != null) { + actionKey = ak.value().trim(); + if ("".equals(actionKey)) { + throw new IllegalArgumentException(controllerClass.getName() + "." + methodName + "(): The argument of ActionKey can not be blank."); + } + if (actionKey.matches(PATH_VARIABLE_URL_PATTERN)) { + Action pathVariableAction = new Action(controllerPath, actionKey, controllerClass, method, methodName, actionInters, + route.getFinalViewPath(routes.getBaseViewPath())); + pathVariableUrlMapping.put(actionKey, pathVariableAction); + } + if (actionKey.startsWith(SLASH)) { + //actionKey = actionKey + } else if (actionKey.startsWith("./")) { + actionKey = controllerPath + actionKey.substring(1); + } else { + actionKey = SLASH + actionKey; + } +// if (!actionKey.startsWith(SLASH)) { +// actionKey = SLASH + actionKey; +// } + } else if (methodName.equals("index")) { + actionKey = controllerPath; + } else { + actionKey = controllerPath.equals(SLASH) ? SLASH + methodName : controllerPath + SLASH + methodName; + } + +// Action action = new Action(controllerPath, actionKey, controllerClass, method, methodName, actionInters, route.getFinalViewPath(routes.getBaseViewPath())); +// if (mapping.put(actionKey, action) != null) { +// throw new RuntimeException(buildMsg(actionKey, controllerClass, method)); +// } + + Action newAction = new Action(controllerPath, actionKey, controllerClass, method, methodName, actionInters, route.getFinalViewPath(routes.getBaseViewPath())); + Action existAction = mapping.get(actionKey); + if (existAction == null) { + mapping.put(actionKey, newAction); + } else { + + Type controllerType = controllerClass.getGenericSuperclass(); + Method existActionMethod = existAction.getMethod(); + + // 不是泛型 + if (!(controllerType instanceof ParameterizedType)) { + throw new RuntimeException(buildMsg(actionKey, method, existActionMethod)); + } + + if (method.getParameterCount() == 0 + || method.getParameterCount() != existActionMethod.getParameterCount() + || method.getDeclaringClass() != existActionMethod.getDeclaringClass()) { + throw new RuntimeException(buildMsg(actionKey, method, existActionMethod)); + } + + Type[] argumentTypes = ((ParameterizedType) controllerType).getActualTypeArguments(); + + Class[] paraTypes = method.getParameterTypes(); + Class[] existParaTypes = existActionMethod.getParameterTypes(); + + for (int i = 0; i < paraTypes.length; i++) { + Class newType = paraTypes[i]; + Class existType = existParaTypes[i]; + if (newType == existType) { + continue; + } + // newType 是父类 + else if (newType.isAssignableFrom(existType) && ArrayUtil.contains(argumentTypes, existType)) { + break; + } + // newType 是子类 + else if (existType.isAssignableFrom(newType) && ArrayUtil.contains(argumentTypes, newType)) { + mapping.put(actionKey, newAction); + break; + } else { + throw new RuntimeException(buildMsg(actionKey, method, existActionMethod)); + } + } + + } + } + } + } + routes.clear(); + + // support url = controllerPath + urlParas with "/" of controllerPath + Action action = mapping.get("/"); + if (action != null) { + mapping.put("", action); + } + } + + + + + /** + * Support four types of url + * 1: http://abc.com/controllerPath ---> 00 + * 2: http://abc.com/controllerPath/para ---> 01 + * 3: http://abc.com/controllerPath/method ---> 10 + * 4: http://abc.com/controllerPath/method/para ---> 11 + * 5: http://abc.com/foo/{id}/bar/{name} + * The controllerPath can also contains "/" + * Example: http://abc.com/uvw/xyz/method/para + */ + @Override + public Action getAction(String url, String[] urlPara) { + Action action = mapping.get(url); + if (action != null) { + return action; + } + for (String pattern : pathVariableUrlMapping.keySet()) { + //判断是否有匹配包含路径参数的URL映射 + if (antPathMatcher.match(pattern, url)) { + Action pathVariableUrlAction = pathVariableUrlMapping.get(pattern); + Map pathVariableValues = antPathMatcher.extractUriTemplateVariables(pattern, url); + urlPara[0] = null; + if (urlPara.length > 1) { + //urlPara[1]作为路径参数传入controller + urlPara[1] = Joiner.on("&").withKeyValueSeparator("=").join(pathVariableValues); + } + return pathVariableUrlAction; + } + } + // -------- + int i = url.lastIndexOf('/'); + if (i != -1) { + action = mapping.get(url.substring(0, i)); + if (action != null) { + urlPara[0] = url.substring(i + 1); + } + } + + return action; + } +} diff --git a/src/main/java/io/jboot/web/handler/PathVariableActionHandler.java b/src/main/java/io/jboot/web/handler/PathVariableActionHandler.java new file mode 100644 index 0000000000000000000000000000000000000000..a079ac0d9e47f56c02bfba6648044612250deaee --- /dev/null +++ b/src/main/java/io/jboot/web/handler/PathVariableActionHandler.java @@ -0,0 +1,182 @@ +/** + * Copyright (c) 2015-2021, Michael Yang 杨福海 (fuhai999@gmail.com). + *

+ * 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 + *

+ * http://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.jboot.web.handler; + +import com.google.common.base.Splitter; +import com.jfinal.aop.Invocation; +import com.jfinal.core.*; +import com.jfinal.log.Log; +import com.jfinal.render.Render; +import com.jfinal.render.RenderException; +import io.jboot.components.valid.ValidException; +import io.jboot.web.controller.JbootControllerContext; +import io.jboot.web.render.JbootRenderFactory; +import io.jboot.web.session.JbootServletRequestWrapper; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.util.Collections; +import java.util.Map; +import java.util.TreeMap; + +/** + * @author 没牙的小朋友 (mjl@nxu.edu.cn) + * @version V1.0 + */ +public class PathVariableActionHandler extends JbootActionHandler { + + private static final Log LOG = Log.getLog(PathVariableActionHandler.class); + + /** + * handle + * 1: Action action = actionMapping.getAction(target) + * 2: new Invocation(...).invoke() + * 3: render(...) + */ + @Override + public void handle(String target, HttpServletRequest request, HttpServletResponse response, boolean[] isHandled) { + if (target.lastIndexOf('.') != -1) { + return; + } + + isHandled[0] = true; + //urlPara数组增加第2个元素存储路径参数 + String[] urlPara = {null, null}; + Action action = getAction(target, urlPara, request); + + if (action == null) { + if (LOG.isWarnEnabled()) { + String qs = request.getQueryString(); + LOG.warn("404 Action Not Found: " + (qs == null ? target : target + "?" + qs)); + } + renderManager.getRenderFactory().getErrorRender(404).setContext(request, response).render(); + return; + } + + + Controller controller = null; + try { + controller = controllerFactory.getController(action.getControllerClass()); + //controller.init(request, response, urlPara[0]); + //存在封装的路径参数 + if(urlPara[1] != null){ + Map params = Splitter.on("&").withKeyValueSeparator("=").split(urlPara[1]); + PathVariableWrappedRequest wrappedRequest = new PathVariableWrappedRequest(request, response, params); + CPI._init_(controller, action, wrappedRequest, response, urlPara[0]); + }else { + CPI._init_(controller, action, request, response, urlPara[0]); + } + JbootControllerContext.hold(controller); + + //Invocation invocation = new Invocation(action, controller); + Invocation invocation = getInvocation(action, controller); + + if (JbootActionReporter.isReportEnable()) { + long time = System.currentTimeMillis(); + try { + doStartRender(target, request, response, isHandled, action, controller, invocation); + } finally { + JbootActionReporter.report(target, controller, action, invocation, time); + } + } else { + doStartRender(target, request, response, isHandled, action, controller, invocation); + } + + } catch (RenderException e) { + if (LOG.isErrorEnabled()) { + String qs = request.getQueryString(); + LOG.error(qs == null ? target : target + "?" + qs, e); + } + } catch (ActionException e) { + handleActionException(target, request, response, action, e); + } catch (ValidException e) { + handleValidException(target, request, response, action, e); + } catch (Exception e) { + handleException(target, request, response, action, e); + } finally { + JbootControllerContext.release(); + controllerFactory.recycle(controller); + } + } + + + private void doStartRender(String target + , HttpServletRequest request + , HttpServletResponse response + , boolean[] isHandled + , Action action + , Controller controller + , Invocation invocation) { + + invocation.invoke(); + + Render render = controller.getRender(); + if (render instanceof ForwardActionRender) { + String actionUrl = ((ForwardActionRender) render).getActionUrl(); + if (target.equals(actionUrl)) { + throw new RuntimeException("The forward action url is the same as before."); + } else { + handle(actionUrl, request, response, isHandled); + } + } else { + if (render == null && void.class != action.getMethod().getReturnType() && renderManager.getRenderFactory() instanceof JbootRenderFactory) { + JbootRenderFactory jbootRenderFactory = (JbootRenderFactory) renderManager.getRenderFactory(); + render = jbootRenderFactory.getReturnValueRender(action, invocation.getReturnValue()); + } + + if (render == null) { + render = renderManager.getRenderFactory().getDefaultRender(action.getViewPath() + action.getMethodName()); + } + + render.setContext(request, response, action.getViewPath()).render(); + } + } + + /** + * 请求包装类用于将路径变量的URL中的额外参数加入request中 + */ + private class PathVariableWrappedRequest extends JbootServletRequestWrapper{ + private final Map modifiableParameters; + private Map allParameters = null; + public PathVariableWrappedRequest(HttpServletRequest request, HttpServletResponse response, + Map params) { + super(request, response); + modifiableParameters = new TreeMap<>(); + params.keySet().forEach(k -> { + modifiableParameters.put(k, new String[]{params.get(k)}); + }); + } + + @Override + public Map getParameterMap() { + if (allParameters == null) { + allParameters = new TreeMap(); + allParameters.putAll(super.getParameterMap()); + allParameters.putAll(modifiableParameters); + } + return Collections.unmodifiableMap(allParameters); + } + + @Override + public String getParameter(final String name) { + String[] strings = getParameterMap().get(name); + if (strings != null) { + return strings[0]; + } + return super.getParameter(name); + } + } +}