RequestMappingHandlerMapping.java 13.0 KB
Newer Older
1
/*
S
Sebastien Deleuze 已提交
2
 * Copyright 2002-2015 the original author or authors.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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 org.springframework.web.servlet.mvc.method.annotation;

19
import java.lang.reflect.AnnotatedElement;
20
import java.lang.reflect.Method;
21
import java.util.List;
22

23
import org.springframework.context.EmbeddedValueResolverAware;
24
import org.springframework.core.annotation.AnnotatedElementUtils;
25 26
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Controller;
27
import org.springframework.util.Assert;
S
Sebastien Deleuze 已提交
28
import org.springframework.util.CollectionUtils;
29
import org.springframework.util.StringValueResolver;
30
import org.springframework.web.accept.ContentNegotiationManager;
S
Sebastien Deleuze 已提交
31
import org.springframework.web.bind.annotation.CrossOrigin;
32
import org.springframework.web.bind.annotation.RequestMapping;
S
Sebastien Deleuze 已提交
33 34 35
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.method.HandlerMethod;
36 37
import org.springframework.web.servlet.mvc.condition.AbstractRequestCondition;
import org.springframework.web.servlet.mvc.condition.CompositeRequestCondition;
S
Sebastien Deleuze 已提交
38
import org.springframework.web.servlet.mvc.condition.NameValueExpression;
39
import org.springframework.web.servlet.mvc.condition.RequestCondition;
40 41
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
42 43

/**
S
Stevo Slavic 已提交
44 45
 * Creates {@link RequestMappingInfo} instances from type and method-level
 * {@link RequestMapping @RequestMapping} annotations in
46
 * {@link Controller @Controller} classes.
47
 *
48 49
 * @author Arjen Poutsma
 * @author Rossen Stoyanchev
50
 * @author Sam Brannen
51
 * @since 3.1
52
 */
53 54
public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMapping
		implements EmbeddedValueResolverAware {
55

56 57
	private boolean useSuffixPatternMatch = true;

58 59
	private boolean useRegisteredSuffixPatternMatch = false;

60
	private boolean useTrailingSlashMatch = true;
61

62 63
	private ContentNegotiationManager contentNegotiationManager = new ContentNegotiationManager();

64 65
	private StringValueResolver embeddedValueResolver;

66 67
	private RequestMappingInfo.BuilderConfiguration config = new RequestMappingInfo.BuilderConfiguration();

68

69
	/**
70
	 * Whether to use suffix pattern match (".*") when matching patterns to
71
	 * requests. If enabled a method mapped to "/users" also matches to "/users.*".
S
Stevo Slavic 已提交
72
	 * <p>The default value is {@code true}.
73
	 * <p>Also see {@link #setUseRegisteredSuffixPatternMatch(boolean)} for
S
Sam Brannen 已提交
74
	 * more fine-grained control over specific suffixes to allow.
75 76 77 78
	 */
	public void setUseSuffixPatternMatch(boolean useSuffixPatternMatch) {
		this.useSuffixPatternMatch = useSuffixPatternMatch;
	}
79

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
	/**
	 * Whether to use suffix pattern match for registered file extensions only
	 * when matching patterns to requests.
	 * <p>If enabled, a controller method mapped to "/users" also matches to
	 * "/users.json" assuming ".json" is a file extension registered with the
	 * provided {@link #setContentNegotiationManager(ContentNegotiationManager)
	 * contentNegotiationManager}. This can be useful for allowing only specific
	 * URL extensions to be used as well as in cases where a "." in the URL path
	 * can lead to ambiguous interpretation of path variable content, (e.g. given
	 * "/users/{user}" and incoming URLs such as "/users/john.j.joe" and
	 * "/users/john.j.joe.json").
	 * <p>If enabled, this flag also enables
	 * {@link #setUseSuffixPatternMatch(boolean) useSuffixPatternMatch}. The
	 * default value is {@code false}.
	 */
95 96
	public void setUseRegisteredSuffixPatternMatch(boolean useRegisteredSuffixPatternMatch) {
		this.useRegisteredSuffixPatternMatch = useRegisteredSuffixPatternMatch;
J
Juergen Hoeller 已提交
97
		this.useSuffixPatternMatch = (useRegisteredSuffixPatternMatch || this.useSuffixPatternMatch);
98 99
	}

100 101 102 103 104 105 106 107
	/**
	 * 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;
	}
108

109 110 111 112 113
	/**
	 * 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 已提交
114
		Assert.notNull(contentNegotiationManager, "ContentNegotiationManager must not be null");
115 116 117
		this.contentNegotiationManager = contentNegotiationManager;
	}

J
Juergen Hoeller 已提交
118 119 120 121 122 123 124 125
	@Override
	public void setEmbeddedValueResolver(StringValueResolver resolver) {
		this.embeddedValueResolver  = resolver;
	}

	@Override
	public void afterPropertiesSet() {

126 127 128 129 130 131 132
		this.config = new RequestMappingInfo.BuilderConfiguration();
		this.config.setPathHelper(getUrlPathHelper());
		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 已提交
133

134 135
		super.afterPropertiesSet();
	}
J
Juergen Hoeller 已提交
136

137
	/**
138
	 * Whether to use suffix pattern matching.
139
	 */
140 141
	public boolean useSuffixPatternMatch() {
		return this.useSuffixPatternMatch;
142
	}
143 144 145 146 147

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

151
	/**
J
Juergen Hoeller 已提交
152
	 * Whether to match to URLs irrespective of the presence of a trailing slash.
153 154 155 156
	 */
	public boolean useTrailingSlashMatch() {
		return this.useTrailingSlashMatch;
	}
157

158 159 160 161
	/**
	 * Return the configured {@link ContentNegotiationManager}.
	 */
	public ContentNegotiationManager getContentNegotiationManager() {
162 163 164 165
		return this.contentNegotiationManager;
	}

	/**
166
	 * Return the file extensions to use for suffix pattern matching.
167
	 */
168
	public List<String> getFileExtensions() {
169
		return this.config.getFileExtensions();
170 171
	}

172

173
	/**
S
Stevo Slavic 已提交
174
	 * {@inheritDoc}
175
	 * Expects a handler to have a type-level @{@link Controller} annotation.
176 177
	 */
	@Override
178
	protected boolean isHandler(Class<?> beanType) {
179 180
		return ((AnnotationUtils.findAnnotation(beanType, Controller.class) != null) ||
				(AnnotationUtils.findAnnotation(beanType, RequestMapping.class) != null));
181 182 183
	}

	/**
184 185 186 187 188 189
	 * 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)
190 191
	 */
	@Override
192
	protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
193 194 195 196 197
		RequestMappingInfo info = createRequestMappingInfo(method);
		if (info != null) {
			RequestMappingInfo typeInfo = createRequestMappingInfo(handlerType);
			if (typeInfo != null) {
				info = typeInfo.combine(info);
198
			}
199
		}
200 201 202 203
		return info;
	}

	/**
204
	 * Provide a custom type-level request condition.
S
Stevo Slavic 已提交
205
	 * The custom {@link RequestCondition} can be of any type so long as the
206
	 * same condition type is returned from all calls to this method in order
S
Stevo Slavic 已提交
207
	 * to ensure custom request conditions can be combined and compared.
208 209 210 211
	 * <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
212 213
	 * @return the condition, or {@code null}
	 */
214
	protected RequestCondition<?> getCustomTypeCondition(Class<?> handlerType) {
215 216
		return null;
	}
217

218
	/**
219
	 * Provide a custom method-level request condition.
S
Stevo Slavic 已提交
220
	 * The custom {@link RequestCondition} can be of any type so long as the
221
	 * same condition type is returned from all calls to this method in order
S
Stevo Slavic 已提交
222
	 * to ensure custom request conditions can be combined and compared.
223 224 225 226
	 * <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
227 228
	 * @return the condition, or {@code null}
	 */
229
	protected RequestCondition<?> getCustomMethodCondition(Method method) {
230
		return null;
231 232
	}

233
	/**
234 235 236 237 238 239
	 * 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)
240
	 */
241
	private RequestMappingInfo createRequestMappingInfo(AnnotatedElement annotatedElement) {
242 243 244 245
		RequestMapping requestMapping = AnnotatedElementUtils.findAnnotation(annotatedElement, RequestMapping.class);
		RequestCondition<?> customCondition = ((annotatedElement instanceof Class<?>) ? getCustomTypeCondition((Class<?>) annotatedElement)
				: getCustomMethodCondition((Method) annotatedElement));
		return ((requestMapping != null) ? createRequestMappingInfo(requestMapping, customCondition) : null);
246 247 248
	}

	/**
249 250 251 252
	 * 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.
253
	 */
254
	protected RequestMappingInfo createRequestMappingInfo(RequestMapping requestMapping,
255 256
			RequestCondition<?> customCondition) {

257 258 259 260 261 262 263
		return RequestMappingInfo.paths(resolveEmbeddedValuesInPatterns(requestMapping.path()))
				.methods(requestMapping.method())
				.params(requestMapping.params())
				.headers(requestMapping.headers())
				.consumes(requestMapping.consumes())
				.produces(requestMapping.produces())
				.mappingName(requestMapping.name())
264 265 266
				.customCondition(customCondition)
				.options(this.config)
				.build();
R
Rossen Stoyanchev 已提交
267
	}
268

269 270 271 272 273 274 275 276 277 278
	/**
	 * 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 已提交
279
			for (int i = 0; i < patterns.length; i++) {
280 281 282 283 284 285
				resolvedPatterns[i] = this.embeddedValueResolver.resolveStringValue(patterns[i]);
			}
			return resolvedPatterns;
		}
	}

S
Sebastien Deleuze 已提交
286 287 288
	@Override
	protected CorsConfiguration initCorsConfiguration(Object handler, Method method, RequestMappingInfo mappingInfo) {
		HandlerMethod handlerMethod = createHandlerMethod(handler, method);
289 290
		CrossOrigin typeAnnotation = AnnotationUtils.findAnnotation(handlerMethod.getBeanType(), CrossOrigin.class);
		CrossOrigin methodAnnotation = AnnotationUtils.findAnnotation(method, CrossOrigin.class);
S
Sebastien Deleuze 已提交
291

292 293 294
		if (typeAnnotation == null && methodAnnotation == null) {
			return null;
		}
S
Sebastien Deleuze 已提交
295

296
		CorsConfiguration config = new CorsConfiguration();
297 298
		updateCorsConfig(config, typeAnnotation);
		updateCorsConfig(config, methodAnnotation);
S
Sebastien Deleuze 已提交
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314

		if (CollectionUtils.isEmpty(config.getAllowedMethods())) {
			for (RequestMethod allowedMethod : mappingInfo.getMethodsCondition().getMethods()) {
				config.addAllowedMethod(allowedMethod.name());
			}
		}
		if (CollectionUtils.isEmpty(config.getAllowedHeaders())) {
			for (NameValueExpression<String> headerExpression : mappingInfo.getHeadersCondition().getExpressions()) {
				if (!headerExpression.isNegated()) {
					config.addAllowedHeader(headerExpression.getName());
				}
			}
		}
		return config;
	}

315
	private void updateCorsConfig(CorsConfiguration config, CrossOrigin annotation) {
S
Sebastien Deleuze 已提交
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
		if (annotation == null) {
			return;
		}
		for (String origin : annotation.origin()) {
			config.addAllowedOrigin(origin);
		}
		for (RequestMethod method : annotation.method()) {
			config.addAllowedMethod(method.name());
		}
		for (String header : annotation.allowedHeaders()) {
			config.addAllowedHeader(header);
		}
		for (String header : annotation.exposedHeaders()) {
			config.addExposedHeader(header);
		}
331 332 333

		String allowCredentials = annotation.allowCredentials();
		if ("true".equalsIgnoreCase(allowCredentials)) {
S
Sebastien Deleuze 已提交
334 335
			config.setAllowCredentials(true);
		}
336
		else if ("false".equalsIgnoreCase(allowCredentials)) {
S
Sebastien Deleuze 已提交
337 338
			config.setAllowCredentials(false);
		}
339 340 341
		else if (!allowCredentials.isEmpty()) {
			throw new IllegalStateException("@CrossOrigin's allowCredentials value must be \"true\", \"false\", "
					+ "or an empty string (\"\"); current value is [" + allowCredentials + "].");
S
Sebastien Deleuze 已提交
342
		}
343 344

		if ((annotation.maxAge() >= 0) && (config.getMaxAge() == null)) {
S
Sebastien Deleuze 已提交
345 346 347 348
			config.setMaxAge(annotation.maxAge());
		}
	}

349
}