View Javadoc
1   /*
2    * Copyright 2002-2012 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.Map;
21  
22  import org.springframework.core.MethodParameter;
23  import org.springframework.util.CollectionUtils;
24  import org.springframework.util.LinkedMultiValueMap;
25  import org.springframework.util.MultiValueMap;
26  import org.springframework.util.StringUtils;
27  import org.springframework.web.bind.annotation.MatrixVariable;
28  import org.springframework.web.bind.annotation.ValueConstants;
29  import org.springframework.web.bind.support.WebDataBinderFactory;
30  import org.springframework.web.context.request.NativeWebRequest;
31  import org.springframework.web.context.request.RequestAttributes;
32  import org.springframework.web.method.support.HandlerMethodArgumentResolver;
33  import org.springframework.web.method.support.ModelAndViewContainer;
34  import org.springframework.web.servlet.HandlerMapping;
35  
36  /**
37   * Resolves method arguments of type Map annotated with
38   * {@link MatrixVariable @MatrixVariable} where the annotation the does not
39   * specify a name. If a name specified then the argument will by resolved by the
40   * {@link MatrixVariableMethodArgumentResolver} instead.
41   *
42   * @author Rossen Stoyanchev
43   * @since 3.2
44   */
45  public class MatrixVariableMapMethodArgumentResolver implements HandlerMethodArgumentResolver {
46  
47  	@Override
48  	public boolean supportsParameter(MethodParameter parameter) {
49  		MatrixVariable paramAnnot = parameter.getParameterAnnotation(MatrixVariable.class);
50  		if (paramAnnot != null) {
51  			if (Map.class.isAssignableFrom(parameter.getParameterType())) {
52  				return !StringUtils.hasText(paramAnnot.value());
53  			}
54  		}
55  		return false;
56  	}
57  
58  	@Override
59  	public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
60  			NativeWebRequest request, WebDataBinderFactory binderFactory) throws Exception {
61  
62  		@SuppressWarnings("unchecked")
63  		Map<String, MultiValueMap<String, String>> matrixVariables =
64  				(Map<String, MultiValueMap<String, String>>) request.getAttribute(
65  						HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);
66  
67  		if (CollectionUtils.isEmpty(matrixVariables)) {
68  			return Collections.emptyMap();
69  		}
70  
71  		String pathVariable = parameter.getParameterAnnotation(MatrixVariable.class).pathVar();
72  
73  		if (!pathVariable.equals(ValueConstants.DEFAULT_NONE)) {
74  			MultiValueMap<String, String> map = matrixVariables.get(pathVariable);
75  			return (map != null) ? map : Collections.emptyMap();
76  		}
77  
78  		MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
79  		for (MultiValueMap<String, String> vars : matrixVariables.values()) {
80  			for (String name : vars.keySet()) {
81  				for (String value : vars.get(name)) {
82  					map.add(name, value);
83  				}
84  			}
85  		}
86  
87  		return map;
88  	}
89  
90  }