001/* 002 * Copyright 2002-2020 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.web.servlet.config.annotation; 018 019import java.util.LinkedHashMap; 020import java.util.Map; 021import java.util.function.Predicate; 022 023import org.springframework.lang.Nullable; 024import org.springframework.util.PathMatcher; 025import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; 026import org.springframework.web.util.UrlPathHelper; 027 028/** 029 * Helps with configuring HandlerMappings path matching options such as trailing 030 * slash match, suffix registration, path matcher and path helper. 031 * 032 * <p>Configured path matcher and path helper instances are shared for: 033 * <ul> 034 * <li>RequestMappings</li> 035 * <li>ViewControllerMappings</li> 036 * <li>ResourcesMappings</li> 037 * </ul> 038 * 039 * @author Brian Clozel 040 * @since 4.0.3 041 * @see RequestMappingHandlerMapping 042 * @see org.springframework.web.servlet.handler.SimpleUrlHandlerMapping 043 */ 044public class PathMatchConfigurer { 045 046 @Nullable 047 private Boolean suffixPatternMatch; 048 049 @Nullable 050 private Boolean registeredSuffixPatternMatch; 051 052 @Nullable 053 private Boolean trailingSlashMatch; 054 055 @Nullable 056 private UrlPathHelper urlPathHelper; 057 058 @Nullable 059 private PathMatcher pathMatcher; 060 061 @Nullable 062 private Map<String, Predicate<Class<?>>> pathPrefixes; 063 064 065 /** 066 * Whether to use suffix pattern match (".*") when matching patterns to 067 * requests. If enabled a method mapped to "/users" also matches to "/users.*". 068 * <p>By default this is set to {@code true}. 069 * @see #registeredSuffixPatternMatch 070 * @deprecated as of 5.2.4. See class-level note in 071 * {@link RequestMappingHandlerMapping} on the deprecation of path extension 072 * config options. As there is no replacement for this method, for the time 073 * being it's necessary to set it to {@code false}. In 5.3 when {@code false} 074 * becomes the default, use of this property will no longer be necessary. 075 */ 076 @Deprecated 077 public PathMatchConfigurer setUseSuffixPatternMatch(Boolean suffixPatternMatch) { 078 this.suffixPatternMatch = suffixPatternMatch; 079 return this; 080 } 081 082 /** 083 * Whether suffix pattern matching should work only against path extensions 084 * explicitly registered when you 085 * {@link WebMvcConfigurer#configureContentNegotiation configure content 086 * negotiation}. This is generally recommended to reduce ambiguity and to 087 * avoid issues such as when a "." appears in the path for other reasons. 088 * <p>By default this is set to "false". 089 * @see WebMvcConfigurer#configureContentNegotiation 090 * @deprecated as of 5.2.4. See class-level note in 091 * {@link RequestMappingHandlerMapping} on the deprecation of path extension 092 * config options. 093 */ 094 @Deprecated 095 public PathMatchConfigurer setUseRegisteredSuffixPatternMatch(Boolean registeredSuffixPatternMatch) { 096 this.registeredSuffixPatternMatch = registeredSuffixPatternMatch; 097 return this; 098 } 099 100 /** 101 * Whether to match to URLs irrespective of the presence of a trailing slash. 102 * If enabled a method mapped to "/users" also matches to "/users/". 103 * <p>The default value is {@code true}. 104 */ 105 public PathMatchConfigurer setUseTrailingSlashMatch(Boolean trailingSlashMatch) { 106 this.trailingSlashMatch = trailingSlashMatch; 107 return this; 108 } 109 110 /** 111 * Set the UrlPathHelper to use for resolution of lookup paths. 112 * <p>Use this to override the default UrlPathHelper with a custom subclass, 113 * or to share common UrlPathHelper settings across multiple HandlerMappings 114 * and MethodNameResolvers. 115 */ 116 public PathMatchConfigurer setUrlPathHelper(UrlPathHelper urlPathHelper) { 117 this.urlPathHelper = urlPathHelper; 118 return this; 119 } 120 121 /** 122 * Set the PathMatcher implementation to use for matching URL paths 123 * against registered URL patterns. Default is AntPathMatcher. 124 * @see org.springframework.util.AntPathMatcher 125 */ 126 public PathMatchConfigurer setPathMatcher(PathMatcher pathMatcher) { 127 this.pathMatcher = pathMatcher; 128 return this; 129 } 130 131 /** 132 * Configure a path prefix to apply to matching controller methods. 133 * <p>Prefixes are used to enrich the mappings of every {@code @RequestMapping} 134 * method whose controller type is matched by the corresponding 135 * {@code Predicate}. The prefix for the first matching predicate is used. 136 * <p>Consider using {@link org.springframework.web.method.HandlerTypePredicate 137 * HandlerTypePredicate} to group controllers. 138 * @param prefix the prefix to apply 139 * @param predicate a predicate for matching controller types 140 * @since 5.1 141 */ 142 public PathMatchConfigurer addPathPrefix(String prefix, Predicate<Class<?>> predicate) { 143 if (this.pathPrefixes == null) { 144 this.pathPrefixes = new LinkedHashMap<>(); 145 } 146 this.pathPrefixes.put(prefix, predicate); 147 return this; 148 } 149 150 151 /** 152 * Whether to use registered suffixes for pattern matching. 153 * @deprecated as of 5.2.4. See class-level note in 154 * {@link RequestMappingHandlerMapping} on the deprecation of path extension 155 * config options. 156 */ 157 @Nullable 158 @Deprecated 159 public Boolean isUseSuffixPatternMatch() { 160 return this.suffixPatternMatch; 161 } 162 163 /** 164 * Whether to use registered suffixes for pattern matching. 165 * @deprecated as of 5.2.4. See class-level note in 166 * {@link RequestMappingHandlerMapping} on the deprecation of path extension 167 * config options. 168 */ 169 @Nullable 170 @Deprecated 171 public Boolean isUseRegisteredSuffixPatternMatch() { 172 return this.registeredSuffixPatternMatch; 173 } 174 175 @Nullable 176 public Boolean isUseTrailingSlashMatch() { 177 return this.trailingSlashMatch; 178 } 179 180 @Nullable 181 public UrlPathHelper getUrlPathHelper() { 182 return this.urlPathHelper; 183 } 184 185 @Nullable 186 public PathMatcher getPathMatcher() { 187 return this.pathMatcher; 188 } 189 190 @Nullable 191 protected Map<String, Predicate<Class<?>>> getPathPrefixes() { 192 return this.pathPrefixes; 193 } 194}