View Javadoc
1   /*
2    * Copyright 2002-2014 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.web.servlet.mvc.method.annotation;
18  
19  import java.util.Collections;
20  import java.util.LinkedHashMap;
21  import java.util.Map;
22  
23  import org.springframework.core.MethodParameter;
24  import org.springframework.util.CollectionUtils;
25  import org.springframework.util.StringUtils;
26  import org.springframework.web.bind.annotation.PathVariable;
27  import org.springframework.web.bind.support.WebDataBinderFactory;
28  import org.springframework.web.context.request.NativeWebRequest;
29  import org.springframework.web.context.request.RequestAttributes;
30  import org.springframework.web.method.support.HandlerMethodArgumentResolver;
31  import org.springframework.web.method.support.ModelAndViewContainer;
32  import org.springframework.web.servlet.HandlerMapping;
33  
34  /**
35   * Resolves {@link Map} method arguments annotated with an @{@link PathVariable}
36   * where the annotation does not specify a path variable name. The created
37   * {@link Map} contains all URI template name/value pairs.
38   *
39   * @author Rossen Stoyanchev
40   * @since 3.2
41   * @see PathVariableMethodArgumentResolver
42   */
43  public class PathVariableMapMethodArgumentResolver implements HandlerMethodArgumentResolver {
44  
45  	@Override
46  	public boolean supportsParameter(MethodParameter parameter) {
47  		PathVariable ann = parameter.getParameterAnnotation(PathVariable.class);
48  		return (ann != null && (Map.class.isAssignableFrom(parameter.getParameterType()))
49  				&& !StringUtils.hasText(ann.value()));
50  	}
51  
52  	/**
53  	 * Return a Map with all URI template variables or an empty map.
54  	 */
55  	@Override
56  	public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
57  			NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
58  
59  		@SuppressWarnings("unchecked")
60  		Map<String, String> uriTemplateVars =
61  				(Map<String, String>) webRequest.getAttribute(
62  						HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);
63  
64  		if (!CollectionUtils.isEmpty(uriTemplateVars)) {
65  			return new LinkedHashMap<String, String>(uriTemplateVars);
66  		}
67  		else {
68  			return Collections.emptyMap();
69  		}
70  	}
71  
72  }