1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
38
39
40
41
42
43
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 }