RequestMappingHandlerMapping.java 9.2 KB
Newer Older
1
/*
S
Stevo Slavic 已提交
2
 * Copyright 2002-2012 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 24

import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Controller;
25
import org.springframework.util.Assert;
26
import org.springframework.web.accept.ContentNegotiationManager;
27
import org.springframework.web.bind.annotation.RequestMapping;
28 29
import org.springframework.web.servlet.mvc.condition.AbstractRequestCondition;
import org.springframework.web.servlet.mvc.condition.CompositeRequestCondition;
30 31 32 33 34
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;
35
import org.springframework.web.servlet.mvc.condition.RequestCondition;
36
import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition;
37 38
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
39 40

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

51 52
	private boolean useSuffixPatternMatch = true;

53 54
	private boolean useRegisteredSuffixPatternMatch = false;

55
	private boolean useTrailingSlashMatch = true;
56

57 58
	private ContentNegotiationManager contentNegotiationManager = new ContentNegotiationManager();

59
	private final List<String> fileExtensions = new ArrayList<String>();
60

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

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
	/**
	 * 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;
	}

94 95 96 97 98 99 100 101
	/**
	 * 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;
	}
102

103 104 105 106 107
	/**
	 * Set the {@link ContentNegotiationManager} to use to determine requested media types.
	 * If not set, the default constructor is used.
	 */
	public void setContentNegotiationManager(ContentNegotiationManager contentNegotiationManager) {
108
		Assert.notNull(contentNegotiationManager);
109 110 111
		this.contentNegotiationManager = contentNegotiationManager;
	}

112
	/**
113
	 * Whether to use suffix pattern matching.
114
	 */
115 116
	public boolean useSuffixPatternMatch() {
		return this.useSuffixPatternMatch;
117
	}
118 119 120 121 122 123 124 125

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

126 127 128 129 130 131
	/**
	 * Whether to match to URLs irrespective of the presence of a trailing  slash.
	 */
	public boolean useTrailingSlashMatch() {
		return this.useTrailingSlashMatch;
	}
132

133 134 135 136
	/**
	 * Return the configured {@link ContentNegotiationManager}.
	 */
	public ContentNegotiationManager getContentNegotiationManager() {
137 138 139 140
		return this.contentNegotiationManager;
	}

	/**
141
	 * Return the file extensions to use for suffix pattern matching.
142
	 */
143 144 145 146 147 148 149 150 151 152
	public List<String> getFileExtensions() {
		return fileExtensions;
	}

	@Override
	public void afterPropertiesSet() {
		super.afterPropertiesSet();
		if (this.useRegisteredSuffixPatternMatch) {
			this.fileExtensions.addAll(contentNegotiationManager.getAllFileExtensions());
		}
153 154
	}

155
	/**
S
Stevo Slavic 已提交
156
	 * {@inheritDoc}
157
	 * Expects a handler to have a type-level @{@link Controller} annotation.
158 159
	 */
	@Override
160
	protected boolean isHandler(Class<?> beanType) {
161 162
		return ((AnnotationUtils.findAnnotation(beanType, Controller.class) != null) ||
				(AnnotationUtils.findAnnotation(beanType, RequestMapping.class) != null));
163 164 165
	}

	/**
166 167
	 * Uses method and type-level @{@link RequestMapping} annotations to create
	 * the RequestMappingInfo.
S
Stevo Slavic 已提交
168
	 *
169 170
	 * @return the created RequestMappingInfo, or {@code null} if the method
	 * does not have a {@code @RequestMapping} annotation.
S
Stevo Slavic 已提交
171
	 *
172 173
	 * @see #getCustomMethodCondition(Method)
	 * @see #getCustomTypeCondition(Class)
174 175
	 */
	@Override
176
	protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
177 178 179 180 181 182 183 184 185
		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);
186
			}
187
		}
188 189 190 191
		return info;
	}

	/**
192
	 * Provide a custom type-level request condition.
S
Stevo Slavic 已提交
193
	 * The custom {@link RequestCondition} can be of any type so long as the
194
	 * same condition type is returned from all calls to this method in order
S
Stevo Slavic 已提交
195
	 * to ensure custom request conditions can be combined and compared.
196 197 198 199 200 201
	 *
	 * <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
202 203
	 * @return the condition, or {@code null}
	 */
204
	protected RequestCondition<?> getCustomTypeCondition(Class<?> handlerType) {
205 206
		return null;
	}
207

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

225
	/**
226
	 * Created a RequestMappingInfo from a RequestMapping annotation.
227
	 */
228
	private RequestMappingInfo createRequestMappingInfo(RequestMapping annotation, RequestCondition<?> customCondition) {
229
		return new RequestMappingInfo(
230
				new PatternsRequestCondition(annotation.value(), getUrlPathHelper(), getPathMatcher(),
231
						this.useSuffixPatternMatch, this.useTrailingSlashMatch, this.fileExtensions),
232 233 234 235
				new RequestMethodsRequestCondition(annotation.method()),
				new ParamsRequestCondition(annotation.params()),
				new HeadersRequestCondition(annotation.headers()),
				new ConsumesRequestCondition(annotation.consumes(), annotation.headers()),
236
				new ProducesRequestCondition(annotation.produces(), annotation.headers(), getContentNegotiationManager()),
237
				customCondition);
R
Rossen Stoyanchev 已提交
238
	}
239

240
}