RequestMappingHandlerMapping.java 15.1 KB
Newer Older
1
/*
2
 * Copyright 2002-2018 the original author or authors.
3 4 5 6 7
 *
 * 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
 *
S
Spring Operator 已提交
8
 *      https://www.apache.org/licenses/LICENSE-2.0
9 10 11 12 13 14 15 16 17 18
 *
 * 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 org.springframework.web.servlet.mvc.method.annotation;

19
import java.lang.reflect.AnnotatedElement;
20
import java.lang.reflect.Method;
21 22
import java.util.Collections;
import java.util.LinkedHashMap;
23
import java.util.List;
24
import java.util.Map;
25
import java.util.Set;
26
import java.util.function.Predicate;
27

28
import javax.servlet.http.HttpServletRequest;
29

30
import org.springframework.context.EmbeddedValueResolverAware;
31
import org.springframework.core.annotation.AnnotatedElementUtils;
32
import org.springframework.lang.Nullable;
33
import org.springframework.stereotype.Controller;
34
import org.springframework.util.Assert;
S
Sebastien Deleuze 已提交
35
import org.springframework.util.CollectionUtils;
36
import org.springframework.util.StringValueResolver;
37
import org.springframework.web.accept.ContentNegotiationManager;
S
Sebastien Deleuze 已提交
38
import org.springframework.web.bind.annotation.CrossOrigin;
39
import org.springframework.web.bind.annotation.RequestMapping;
S
Sebastien Deleuze 已提交
40 41 42
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.method.HandlerMethod;
43 44
import org.springframework.web.servlet.handler.MatchableHandlerMapping;
import org.springframework.web.servlet.handler.RequestMatchResult;
45 46
import org.springframework.web.servlet.mvc.condition.AbstractRequestCondition;
import org.springframework.web.servlet.mvc.condition.CompositeRequestCondition;
47
import org.springframework.web.servlet.mvc.condition.RequestCondition;
48 49
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
50 51

/**
S
Stevo Slavic 已提交
52 53
 * Creates {@link RequestMappingInfo} instances from type and method-level
 * {@link RequestMapping @RequestMapping} annotations in
54
 * {@link Controller @Controller} classes.
55
 *
56 57
 * @author Arjen Poutsma
 * @author Rossen Stoyanchev
58
 * @author Sam Brannen
59
 * @since 3.1
60
 */
61
public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMapping
62
		implements MatchableHandlerMapping, EmbeddedValueResolverAware {
63

64 65
	private boolean useSuffixPatternMatch = true;

66 67
	private boolean useRegisteredSuffixPatternMatch = false;

68
	private boolean useTrailingSlashMatch = true;
69

70
	private Map<String, Predicate<Class<?>>> pathPrefixes = new LinkedHashMap<>();
71

72 73
	private ContentNegotiationManager contentNegotiationManager = new ContentNegotiationManager();

74
	@Nullable
75 76
	private StringValueResolver embeddedValueResolver;

77 78
	private RequestMappingInfo.BuilderConfiguration config = new RequestMappingInfo.BuilderConfiguration();

79

80
	/**
81
	 * Whether to use suffix pattern match (".*") when matching patterns to
82
	 * requests. If enabled a method mapped to "/users" also matches to "/users.*".
S
Stevo Slavic 已提交
83
	 * <p>The default value is {@code true}.
84
	 * <p>Also see {@link #setUseRegisteredSuffixPatternMatch(boolean)} for
S
Sam Brannen 已提交
85
	 * more fine-grained control over specific suffixes to allow.
86 87 88 89
	 */
	public void setUseSuffixPatternMatch(boolean useSuffixPatternMatch) {
		this.useSuffixPatternMatch = useSuffixPatternMatch;
	}
90

91
	/**
R
Polish  
Rossen Stoyanchev 已提交
92 93 94 95 96
	 * Whether suffix pattern matching should work only against path extensions
	 * explicitly registered with the {@link ContentNegotiationManager}. This
	 * is generally recommended to reduce ambiguity and to avoid issues such as
	 * when a "." appears in the path for other reasons.
	 * <p>By default this is set to "false".
97
	 */
98 99
	public void setUseRegisteredSuffixPatternMatch(boolean useRegisteredSuffixPatternMatch) {
		this.useRegisteredSuffixPatternMatch = useRegisteredSuffixPatternMatch;
J
Juergen Hoeller 已提交
100
		this.useSuffixPatternMatch = (useRegisteredSuffixPatternMatch || this.useSuffixPatternMatch);
101 102
	}

103 104 105 106 107 108 109 110
	/**
	 * Whether to match to URLs irrespective of the presence of a trailing slash.
	 * If enabled a method mapped to "/users" also matches to "/users/".
	 * <p>The default value is {@code true}.
	 */
	public void setUseTrailingSlashMatch(boolean useTrailingSlashMatch) {
		this.useTrailingSlashMatch = useTrailingSlashMatch;
	}
111

112 113 114 115
	/**
	 * Configure path prefixes to apply to controller methods.
	 * <p>Prefixes are used to enrich the mappings of every {@code @RequestMapping}
	 * method whose controller type is matched by the corresponding
116 117 118
	 * {@code Predicate}. The prefix for the first matching predicate is used.
	 * <p>Consider using {@link org.springframework.web.method.HandlerTypePredicate
	 * HandlerTypePredicate} to group controllers.
119 120 121
	 * @param prefixes a map with path prefixes as key
	 * @since 5.1
	 */
122
	public void setPathPrefixes(Map<String, Predicate<Class<?>>> prefixes) {
123
		this.pathPrefixes = Collections.unmodifiableMap(new LinkedHashMap<>(prefixes));
124 125
	}

J
Juergen Hoeller 已提交
126 127 128 129 130 131 132 133
	/**
	 * The configured path prefixes as a read-only, possibly empty map.
	 * @since 5.1
	 */
	public Map<String, Predicate<Class<?>>> getPathPrefixes() {
		return this.pathPrefixes;
	}

134 135 136 137 138
	/**
	 * Set the {@link ContentNegotiationManager} to use to determine requested media types.
	 * If not set, the default constructor is used.
	 */
	public void setContentNegotiationManager(ContentNegotiationManager contentNegotiationManager) {
J
Juergen Hoeller 已提交
139
		Assert.notNull(contentNegotiationManager, "ContentNegotiationManager must not be null");
140 141 142
		this.contentNegotiationManager = contentNegotiationManager;
	}

J
Juergen Hoeller 已提交
143 144 145 146 147 148 149
	/**
	 * Return the configured {@link ContentNegotiationManager}.
	 */
	public ContentNegotiationManager getContentNegotiationManager() {
		return this.contentNegotiationManager;
	}

J
Juergen Hoeller 已提交
150 151
	@Override
	public void setEmbeddedValueResolver(StringValueResolver resolver) {
152
		this.embeddedValueResolver = resolver;
J
Juergen Hoeller 已提交
153 154 155 156
	}

	@Override
	public void afterPropertiesSet() {
157
		this.config = new RequestMappingInfo.BuilderConfiguration();
158
		this.config.setUrlPathHelper(getUrlPathHelper());
159 160 161 162 163
		this.config.setPathMatcher(getPathMatcher());
		this.config.setSuffixPatternMatch(this.useSuffixPatternMatch);
		this.config.setTrailingSlashMatch(this.useTrailingSlashMatch);
		this.config.setRegisteredSuffixPatternMatch(this.useRegisteredSuffixPatternMatch);
		this.config.setContentNegotiationManager(getContentNegotiationManager());
J
Juergen Hoeller 已提交
164

165 166
		super.afterPropertiesSet();
	}
J
Juergen Hoeller 已提交
167

J
Juergen Hoeller 已提交
168

169
	/**
170
	 * Whether to use suffix pattern matching.
171
	 */
172 173
	public boolean useSuffixPatternMatch() {
		return this.useSuffixPatternMatch;
174
	}
175 176 177 178 179

	/**
	 * Whether to use registered suffixes for pattern matching.
	 */
	public boolean useRegisteredSuffixPatternMatch() {
J
Juergen Hoeller 已提交
180
		return this.useRegisteredSuffixPatternMatch;
181 182
	}

183
	/**
J
Juergen Hoeller 已提交
184
	 * Whether to match to URLs irrespective of the presence of a trailing slash.
185 186 187 188
	 */
	public boolean useTrailingSlashMatch() {
		return this.useTrailingSlashMatch;
	}
189

190
	/**
191
	 * Return the file extensions to use for suffix pattern matching.
192
	 */
193
	@Nullable
194
	public List<String> getFileExtensions() {
195
		return this.config.getFileExtensions();
196 197
	}

198

199
	/**
S
Stevo Slavic 已提交
200
	 * {@inheritDoc}
201 202
	 * <p>Expects a handler to have either a type-level @{@link Controller}
	 * annotation or a type-level @{@link RequestMapping} annotation.
203 204
	 */
	@Override
205
	protected boolean isHandler(Class<?> beanType) {
206 207
		return (AnnotatedElementUtils.hasAnnotation(beanType, Controller.class) ||
				AnnotatedElementUtils.hasAnnotation(beanType, RequestMapping.class));
208 209 210
	}

	/**
211 212 213 214 215 216
	 * Uses method and type-level @{@link RequestMapping} annotations to create
	 * the RequestMappingInfo.
	 * @return the created RequestMappingInfo, or {@code null} if the method
	 * does not have a {@code @RequestMapping} annotation.
	 * @see #getCustomMethodCondition(Method)
	 * @see #getCustomTypeCondition(Class)
217 218
	 */
	@Override
219
	@Nullable
220
	protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
221 222 223 224 225
		RequestMappingInfo info = createRequestMappingInfo(method);
		if (info != null) {
			RequestMappingInfo typeInfo = createRequestMappingInfo(handlerType);
			if (typeInfo != null) {
				info = typeInfo.combine(info);
226
			}
227 228
			String prefix = getPathPrefix(handlerType);
			if (prefix != null) {
229
				info = RequestMappingInfo.paths(prefix).options(this.config).build().combine(info);
230
			}
231
		}
232 233 234
		return info;
	}

235 236 237 238 239 240 241 242 243 244 245 246 247 248
	@Nullable
	String getPathPrefix(Class<?> handlerType) {
		for (Map.Entry<String, Predicate<Class<?>>> entry : this.pathPrefixes.entrySet()) {
			if (entry.getValue().test(handlerType)) {
				String prefix = entry.getKey();
				if (this.embeddedValueResolver != null) {
					prefix = this.embeddedValueResolver.resolveStringValue(prefix);
				}
				return prefix;
			}
		}
		return null;
	}

R
Polish  
Rossen Stoyanchev 已提交
249 250 251 252 253 254 255
	/**
	 * Delegates to {@link #createRequestMappingInfo(RequestMapping, RequestCondition)},
	 * supplying the appropriate custom {@link RequestCondition} depending on whether
	 * the supplied {@code annotatedElement} is a class or method.
	 * @see #getCustomTypeCondition(Class)
	 * @see #getCustomMethodCondition(Method)
	 */
256
	@Nullable
R
Polish  
Rossen Stoyanchev 已提交
257
	private RequestMappingInfo createRequestMappingInfo(AnnotatedElement element) {
258
		RequestMapping requestMapping = AnnotatedElementUtils.findMergedAnnotation(element, RequestMapping.class);
259
		RequestCondition<?> condition = (element instanceof Class ?
J
Juergen Hoeller 已提交
260
				getCustomTypeCondition((Class<?>) element) : getCustomMethodCondition((Method) element));
R
Polish  
Rossen Stoyanchev 已提交
261 262 263
		return (requestMapping != null ? createRequestMappingInfo(requestMapping, condition) : null);
	}

264
	/**
265
	 * Provide a custom type-level request condition.
S
Stevo Slavic 已提交
266
	 * The custom {@link RequestCondition} can be of any type so long as the
267
	 * same condition type is returned from all calls to this method in order
S
Stevo Slavic 已提交
268
	 * to ensure custom request conditions can be combined and compared.
269 270 271 272
	 * <p>Consider extending {@link AbstractRequestCondition} for custom
	 * condition types and using {@link CompositeRequestCondition} to provide
	 * multiple custom conditions.
	 * @param handlerType the handler type for which to create the condition
273 274
	 * @return the condition, or {@code null}
	 */
275
	@Nullable
276
	protected RequestCondition<?> getCustomTypeCondition(Class<?> handlerType) {
277 278
		return null;
	}
279

280
	/**
281
	 * Provide a custom method-level request condition.
S
Stevo Slavic 已提交
282
	 * The custom {@link RequestCondition} can be of any type so long as the
283
	 * same condition type is returned from all calls to this method in order
S
Stevo Slavic 已提交
284
	 * to ensure custom request conditions can be combined and compared.
285 286 287 288
	 * <p>Consider extending {@link AbstractRequestCondition} for custom
	 * condition types and using {@link CompositeRequestCondition} to provide
	 * multiple custom conditions.
	 * @param method the handler method for which to create the condition
289 290
	 * @return the condition, or {@code null}
	 */
291
	@Nullable
292
	protected RequestCondition<?> getCustomMethodCondition(Method method) {
293
		return null;
294 295
	}

296
	/**
297 298 299 300
	 * Create a {@link RequestMappingInfo} from the supplied
	 * {@link RequestMapping @RequestMapping} annotation, which is either
	 * a directly declared annotation, a meta-annotation, or the synthesized
	 * result of merging annotation attributes within an annotation hierarchy.
301
	 */
J
Juergen Hoeller 已提交
302
	protected RequestMappingInfo createRequestMappingInfo(
303
			RequestMapping requestMapping, @Nullable RequestCondition<?> customCondition) {
304

305
		RequestMappingInfo.Builder builder = RequestMappingInfo
R
Polish  
Rossen Stoyanchev 已提交
306
				.paths(resolveEmbeddedValuesInPatterns(requestMapping.path()))
307 308 309 310 311
				.methods(requestMapping.method())
				.params(requestMapping.params())
				.headers(requestMapping.headers())
				.consumes(requestMapping.consumes())
				.produces(requestMapping.produces())
312 313 314 315 316
				.mappingName(requestMapping.name());
		if (customCondition != null) {
			builder.customCondition(customCondition);
		}
		return builder.options(this.config).build();
R
Rossen Stoyanchev 已提交
317
	}
318

319 320 321 322 323 324 325 326 327 328
	/**
	 * Resolve placeholder values in the given array of patterns.
	 * @return a new array with updated patterns
	 */
	protected String[] resolveEmbeddedValuesInPatterns(String[] patterns) {
		if (this.embeddedValueResolver == null) {
			return patterns;
		}
		else {
			String[] resolvedPatterns = new String[patterns.length];
J
Juergen Hoeller 已提交
329
			for (int i = 0; i < patterns.length; i++) {
330 331 332 333 334 335
				resolvedPatterns[i] = this.embeddedValueResolver.resolveStringValue(patterns[i]);
			}
			return resolvedPatterns;
		}
	}

336 337 338 339 340 341 342 343 344 345 346 347
	@Override
	public RequestMatchResult match(HttpServletRequest request, String pattern) {
		RequestMappingInfo info = RequestMappingInfo.paths(pattern).options(this.config).build();
		RequestMappingInfo matchingInfo = info.getMatchingCondition(request);
		if (matchingInfo == null) {
			return null;
		}
		Set<String> patterns = matchingInfo.getPatternsCondition().getPatterns();
		String lookupPath = getUrlPathHelper().getLookupPathForRequest(request);
		return new RequestMatchResult(patterns.iterator().next(), lookupPath, getPathMatcher());
	}

S
Sebastien Deleuze 已提交
348 349 350
	@Override
	protected CorsConfiguration initCorsConfiguration(Object handler, Method method, RequestMappingInfo mappingInfo) {
		HandlerMethod handlerMethod = createHandlerMethod(handler, method);
R
Rossen Stoyanchev 已提交
351 352
		Class<?> beanType = handlerMethod.getBeanType();
		CrossOrigin typeAnnotation = AnnotatedElementUtils.findMergedAnnotation(beanType, CrossOrigin.class);
353
		CrossOrigin methodAnnotation = AnnotatedElementUtils.findMergedAnnotation(method, CrossOrigin.class);
S
Sebastien Deleuze 已提交
354

355 356 357
		if (typeAnnotation == null && methodAnnotation == null) {
			return null;
		}
S
Sebastien Deleuze 已提交
358

359
		CorsConfiguration config = new CorsConfiguration();
360 361
		updateCorsConfig(config, typeAnnotation);
		updateCorsConfig(config, methodAnnotation);
S
Sebastien Deleuze 已提交
362 363 364 365 366 367

		if (CollectionUtils.isEmpty(config.getAllowedMethods())) {
			for (RequestMethod allowedMethod : mappingInfo.getMethodsCondition().getMethods()) {
				config.addAllowedMethod(allowedMethod.name());
			}
		}
R
Polish  
Rossen Stoyanchev 已提交
368
		return config.applyPermitDefaultValues();
S
Sebastien Deleuze 已提交
369 370
	}

371
	private void updateCorsConfig(CorsConfiguration config, @Nullable CrossOrigin annotation) {
S
Sebastien Deleuze 已提交
372 373 374
		if (annotation == null) {
			return;
		}
S
Sam Brannen 已提交
375
		for (String origin : annotation.origins()) {
376
			config.addAllowedOrigin(resolveCorsAnnotationValue(origin));
S
Sebastien Deleuze 已提交
377
		}
S
Sam Brannen 已提交
378
		for (RequestMethod method : annotation.methods()) {
S
Sebastien Deleuze 已提交
379 380 381
			config.addAllowedMethod(method.name());
		}
		for (String header : annotation.allowedHeaders()) {
382
			config.addAllowedHeader(resolveCorsAnnotationValue(header));
S
Sebastien Deleuze 已提交
383 384
		}
		for (String header : annotation.exposedHeaders()) {
385
			config.addExposedHeader(resolveCorsAnnotationValue(header));
S
Sebastien Deleuze 已提交
386
		}
387

388
		String allowCredentials = resolveCorsAnnotationValue(annotation.allowCredentials());
389
		if ("true".equalsIgnoreCase(allowCredentials)) {
S
Sebastien Deleuze 已提交
390 391
			config.setAllowCredentials(true);
		}
392
		else if ("false".equalsIgnoreCase(allowCredentials)) {
S
Sebastien Deleuze 已提交
393 394
			config.setAllowCredentials(false);
		}
395
		else if (!allowCredentials.isEmpty()) {
396 397
			throw new IllegalStateException("@CrossOrigin's allowCredentials value must be \"true\", \"false\", " +
					"or an empty string (\"\"): current value is [" + allowCredentials + "]");
S
Sebastien Deleuze 已提交
398
		}
399

J
Juergen Hoeller 已提交
400
		if (annotation.maxAge() >= 0 && config.getMaxAge() == null) {
S
Sebastien Deleuze 已提交
401 402 403 404
			config.setMaxAge(annotation.maxAge());
		}
	}

405
	private String resolveCorsAnnotationValue(String value) {
406 407 408 409 410 411 412
		if (this.embeddedValueResolver != null) {
			String resolved = this.embeddedValueResolver.resolveStringValue(value);
			return (resolved != null ? resolved : "");
		}
		else {
			return value;
		}
413 414
	}

415
}