001/* 002 * Copyright 2002-2012 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.util; 018 019import java.util.Comparator; 020import java.util.Map; 021 022/** 023 * Strategy interface for {@code String}-based path matching. 024 * 025 * <p>Used by {@link org.springframework.core.io.support.PathMatchingResourcePatternResolver}, 026 * {@link org.springframework.web.servlet.handler.AbstractUrlHandlerMapping}, 027 * {@link org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver}, 028 * and {@link org.springframework.web.servlet.mvc.WebContentInterceptor}. 029 * 030 * <p>The default implementation is {@link AntPathMatcher}, supporting the 031 * Ant-style pattern syntax. 032 * 033 * @author Juergen Hoeller 034 * @since 1.2 035 * @see AntPathMatcher 036 */ 037public interface PathMatcher { 038 039 /** 040 * Does the given {@code path} represent a pattern that can be matched 041 * by an implementation of this interface? 042 * <p>If the return value is {@code false}, then the {@link #match} 043 * method does not have to be used because direct equality comparisons 044 * on the static path Strings will lead to the same result. 045 * @param path the path String to check 046 * @return {@code true} if the given {@code path} represents a pattern 047 */ 048 boolean isPattern(String path); 049 050 /** 051 * Match the given {@code path} against the given {@code pattern}, 052 * according to this PathMatcher's matching strategy. 053 * @param pattern the pattern to match against 054 * @param path the path String to test 055 * @return {@code true} if the supplied {@code path} matched, 056 * {@code false} if it didn't 057 */ 058 boolean match(String pattern, String path); 059 060 /** 061 * Match the given {@code path} against the corresponding part of the given 062 * {@code pattern}, according to this PathMatcher's matching strategy. 063 * <p>Determines whether the pattern at least matches as far as the given base 064 * path goes, assuming that a full path may then match as well. 065 * @param pattern the pattern to match against 066 * @param path the path String to test 067 * @return {@code true} if the supplied {@code path} matched, 068 * {@code false} if it didn't 069 */ 070 boolean matchStart(String pattern, String path); 071 072 /** 073 * Given a pattern and a full path, determine the pattern-mapped part. 074 * <p>This method is supposed to find out which part of the path is matched 075 * dynamically through an actual pattern, that is, it strips off a statically 076 * defined leading path from the given full path, returning only the actually 077 * pattern-matched part of the path. 078 * <p>For example: For "myroot/*.html" as pattern and "myroot/myfile.html" 079 * as full path, this method should return "myfile.html". The detailed 080 * determination rules are specified to this PathMatcher's matching strategy. 081 * <p>A simple implementation may return the given full path as-is in case 082 * of an actual pattern, and the empty String in case of the pattern not 083 * containing any dynamic parts (i.e. the {@code pattern} parameter being 084 * a static path that wouldn't qualify as an actual {@link #isPattern pattern}). 085 * A sophisticated implementation will differentiate between the static parts 086 * and the dynamic parts of the given path pattern. 087 * @param pattern the path pattern 088 * @param path the full path to introspect 089 * @return the pattern-mapped part of the given {@code path} 090 * (never {@code null}) 091 */ 092 String extractPathWithinPattern(String pattern, String path); 093 094 /** 095 * Given a pattern and a full path, extract the URI template variables. URI template 096 * variables are expressed through curly brackets ('{' and '}'). 097 * <p>For example: For pattern "/hotels/{hotel}" and path "/hotels/1", this method will 098 * return a map containing "hotel"->"1". 099 * @param pattern the path pattern, possibly containing URI templates 100 * @param path the full path to extract template variables from 101 * @return a map, containing variable names as keys; variables values as values 102 */ 103 Map<String, String> extractUriTemplateVariables(String pattern, String path); 104 105 /** 106 * Given a full path, returns a {@link Comparator} suitable for sorting patterns 107 * in order of explicitness for that path. 108 * <p>The full algorithm used depends on the underlying implementation, but generally, 109 * the returned {@code Comparator} will 110 * {@linkplain java.util.Collections#sort(java.util.List, java.util.Comparator) sort} 111 * a list so that more specific patterns come before generic patterns. 112 * @param path the full path to use for comparison 113 * @return a comparator capable of sorting patterns in order of explicitness 114 */ 115 Comparator<String> getPatternComparator(String path); 116 117 /** 118 * Combines two patterns into a new pattern that is returned. 119 * <p>The full algorithm used for combining the two pattern depends on the underlying implementation. 120 * @param pattern1 the first pattern 121 * @param pattern2 the second pattern 122 * @return the combination of the two patterns 123 * @throws IllegalArgumentException when the two patterns cannot be combined 124 */ 125 String combine(String pattern1, String pattern2); 126 127}