RequestMappingHandlerMapping.java 10.1 KB
Newer Older
1
/*
2
 * Copyright 2002-2013 the original author or authors.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * 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;

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

23
import org.springframework.context.EmbeddedValueResolverAware;
24 25
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Controller;
26
import org.springframework.util.Assert;
27
import org.springframework.util.StringValueResolver;
28
import org.springframework.web.accept.ContentNegotiationManager;
29
import org.springframework.web.bind.annotation.RequestMapping;
30 31
import org.springframework.web.servlet.mvc.condition.AbstractRequestCondition;
import org.springframework.web.servlet.mvc.condition.CompositeRequestCondition;
32 33 34 35 36
import org.springframework.web.servlet.mvc.condition.ConsumesRequestCondition;
import org.springframework.web.servlet.mvc.condition.HeadersRequestCondition;
import org.springframework.web.servlet.mvc.condition.ParamsRequestCondition;
import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
import org.springframework.web.servlet.mvc.condition.ProducesRequestCondition;
37
import org.springframework.web.servlet.mvc.condition.RequestCondition;
38
import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition;
39 40
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
41 42

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

54 55
	private boolean useSuffixPatternMatch = true;

56 57
	private boolean useRegisteredSuffixPatternMatch = false;

58
	private boolean useTrailingSlashMatch = true;
59

60 61
	private ContentNegotiationManager contentNegotiationManager = new ContentNegotiationManager();

62
	private final List<String> fileExtensions = new ArrayList<String>();
63

64 65
	private StringValueResolver embeddedValueResolver;

66

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

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
	/**
	 * 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}.
	 */
	public void setUseRegisteredSuffixPatternMatch(boolean useRegsiteredSuffixPatternMatch) {
		this.useRegisteredSuffixPatternMatch = useRegsiteredSuffixPatternMatch;
		this.useSuffixPatternMatch = useRegsiteredSuffixPatternMatch ? true : this.useSuffixPatternMatch;
	}

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
	@Override
	public void setEmbeddedValueResolver(StringValueResolver resolver) {
		this.embeddedValueResolver  = resolver;
	}

114 115 116 117 118
	/**
	 * Set the {@link ContentNegotiationManager} to use to determine requested media types.
	 * If not set, the default constructor is used.
	 */
	public void setContentNegotiationManager(ContentNegotiationManager contentNegotiationManager) {
119
		Assert.notNull(contentNegotiationManager);
120 121 122
		this.contentNegotiationManager = contentNegotiationManager;
	}

123
	/**
124
	 * Whether to use suffix pattern matching.
125
	 */
126 127
	public boolean useSuffixPatternMatch() {
		return this.useSuffixPatternMatch;
128
	}
129 130 131 132 133 134 135 136

	/**
	 * Whether to use registered suffixes for pattern matching.
	 */
	public boolean useRegisteredSuffixPatternMatch() {
		return useRegisteredSuffixPatternMatch;
	}

137 138 139 140 141 142
	/**
	 * Whether to match to URLs irrespective of the presence of a trailing  slash.
	 */
	public boolean useTrailingSlashMatch() {
		return this.useTrailingSlashMatch;
	}
143

144 145 146 147
	/**
	 * Return the configured {@link ContentNegotiationManager}.
	 */
	public ContentNegotiationManager getContentNegotiationManager() {
148 149 150 151
		return this.contentNegotiationManager;
	}

	/**
152
	 * Return the file extensions to use for suffix pattern matching.
153
	 */
154
	public List<String> getFileExtensions() {
155
		return this.fileExtensions;
156 157 158 159 160 161 162
	}

	@Override
	public void afterPropertiesSet() {
		if (this.useRegisteredSuffixPatternMatch) {
			this.fileExtensions.addAll(contentNegotiationManager.getAllFileExtensions());
		}
163
		super.afterPropertiesSet();
164 165
	}

166
	/**
S
Stevo Slavic 已提交
167
	 * {@inheritDoc}
168
	 * Expects a handler to have a type-level @{@link Controller} annotation.
169 170
	 */
	@Override
171
	protected boolean isHandler(Class<?> beanType) {
172 173
		return ((AnnotationUtils.findAnnotation(beanType, Controller.class) != null) ||
				(AnnotationUtils.findAnnotation(beanType, RequestMapping.class) != null));
174 175 176
	}

	/**
177 178
	 * Uses method and type-level @{@link RequestMapping} annotations to create
	 * the RequestMappingInfo.
S
Stevo Slavic 已提交
179
	 *
180 181
	 * @return the created RequestMappingInfo, or {@code null} if the method
	 * does not have a {@code @RequestMapping} annotation.
S
Stevo Slavic 已提交
182
	 *
183 184
	 * @see #getCustomMethodCondition(Method)
	 * @see #getCustomTypeCondition(Class)
185 186
	 */
	@Override
187
	protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
188 189 190 191 192 193 194 195 196
		RequestMappingInfo info = null;
		RequestMapping methodAnnotation = AnnotationUtils.findAnnotation(method, RequestMapping.class);
		if (methodAnnotation != null) {
			RequestCondition<?> methodCondition = getCustomMethodCondition(method);
			info = createRequestMappingInfo(methodAnnotation, methodCondition);
			RequestMapping typeAnnotation = AnnotationUtils.findAnnotation(handlerType, RequestMapping.class);
			if (typeAnnotation != null) {
				RequestCondition<?> typeCondition = getCustomTypeCondition(handlerType);
				info = createRequestMappingInfo(typeAnnotation, typeCondition).combine(info);
197
			}
198
		}
199 200 201 202
		return info;
	}

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

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

236
	/**
237
	 * Created a RequestMappingInfo from a RequestMapping annotation.
238
	 */
239 240 241
	protected RequestMappingInfo createRequestMappingInfo(RequestMapping annotation,
			RequestCondition<?> customCondition) {

242
		String[] patterns = resolveEmbeddedValuesInPatterns(annotation.value());
243
		return new RequestMappingInfo(
244
				new PatternsRequestCondition(patterns, getUrlPathHelper(), getPathMatcher(),
245
						this.useSuffixPatternMatch, this.useTrailingSlashMatch, this.fileExtensions),
246 247 248 249
				new RequestMethodsRequestCondition(annotation.method()),
				new ParamsRequestCondition(annotation.params()),
				new HeadersRequestCondition(annotation.headers()),
				new ConsumesRequestCondition(annotation.consumes(), annotation.headers()),
250
				new ProducesRequestCondition(annotation.produces(), annotation.headers(), getContentNegotiationManager()),
251
				customCondition);
R
Rossen Stoyanchev 已提交
252
	}
253

254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
	/**
	 * 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];
			for (int i=0; i < patterns.length; i++) {
				resolvedPatterns[i] = this.embeddedValueResolver.resolveStringValue(patterns[i]);
			}
			return resolvedPatterns;
		}
	}

271
}