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.beans.PropertyEditor;
20 import java.util.HashMap;
21 import java.util.Map;
22
23 import org.springframework.core.MethodParameter;
24 import org.springframework.core.convert.ConversionService;
25 import org.springframework.core.convert.TypeDescriptor;
26 import org.springframework.core.convert.converter.Converter;
27 import org.springframework.util.StringUtils;
28 import org.springframework.web.bind.ServletRequestBindingException;
29 import org.springframework.web.bind.WebDataBinder;
30 import org.springframework.web.bind.annotation.PathVariable;
31 import org.springframework.web.bind.annotation.ValueConstants;
32 import org.springframework.web.context.request.NativeWebRequest;
33 import org.springframework.web.context.request.RequestAttributes;
34 import org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver;
35 import org.springframework.web.method.annotation.RequestParamMapMethodArgumentResolver;
36 import org.springframework.web.method.support.ModelAndViewContainer;
37 import org.springframework.web.method.support.UriComponentsContributor;
38 import org.springframework.web.servlet.HandlerMapping;
39 import org.springframework.web.servlet.View;
40 import org.springframework.web.util.UriComponentsBuilder;
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 public class PathVariableMethodArgumentResolver extends AbstractNamedValueMethodArgumentResolver
67 implements UriComponentsContributor {
68
69 private static final TypeDescriptor STRING_TYPE_DESCRIPTOR = TypeDescriptor.valueOf(String.class);
70
71
72 public PathVariableMethodArgumentResolver() {
73 }
74
75
76 @Override
77 public boolean supportsParameter(MethodParameter parameter) {
78 if (!parameter.hasParameterAnnotation(PathVariable.class)) {
79 return false;
80 }
81 if (Map.class.isAssignableFrom(parameter.getParameterType())) {
82 String paramName = parameter.getParameterAnnotation(PathVariable.class).value();
83 return StringUtils.hasText(paramName);
84 }
85 return true;
86 }
87
88 @Override
89 protected NamedValueInfo createNamedValueInfo(MethodParameter parameter) {
90 PathVariable annotation = parameter.getParameterAnnotation(PathVariable.class);
91 return new PathVariableNamedValueInfo(annotation);
92 }
93
94 @Override
95 @SuppressWarnings("unchecked")
96 protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest request) throws Exception {
97 Map<String, String> uriTemplateVars =
98 (Map<String, String>) request.getAttribute(
99 HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);
100 return (uriTemplateVars != null) ? uriTemplateVars.get(name) : null;
101 }
102
103 @Override
104 protected void handleMissingValue(String name, MethodParameter parameter) throws ServletRequestBindingException {
105 throw new ServletRequestBindingException("Missing URI template variable '" + name +
106 "' for method parameter of type " + parameter.getParameterType().getSimpleName());
107 }
108
109 @Override
110 @SuppressWarnings("unchecked")
111 protected void handleResolvedValue(Object arg, String name, MethodParameter parameter,
112 ModelAndViewContainer mavContainer, NativeWebRequest request) {
113
114 String key = View.PATH_VARIABLES;
115 int scope = RequestAttributes.SCOPE_REQUEST;
116 Map<String, Object> pathVars = (Map<String, Object>) request.getAttribute(key, scope);
117 if (pathVars == null) {
118 pathVars = new HashMap<String, Object>();
119 request.setAttribute(key, pathVars, scope);
120 }
121 pathVars.put(name, arg);
122 }
123
124 @Override
125 public void contributeMethodArgument(MethodParameter parameter, Object value,
126 UriComponentsBuilder builder, Map<String, Object> uriVariables, ConversionService conversionService) {
127
128 if (Map.class.isAssignableFrom(parameter.getParameterType())) {
129 return;
130 }
131
132 PathVariable ann = parameter.getParameterAnnotation(PathVariable.class);
133 String name = (ann == null || StringUtils.isEmpty(ann.value()) ? parameter.getParameterName() : ann.value());
134 value = formatUriValue(conversionService, new TypeDescriptor(parameter), value);
135 uriVariables.put(name, value);
136 }
137
138 protected String formatUriValue(ConversionService cs, TypeDescriptor sourceType, Object value) {
139 if (value == null) {
140 return null;
141 }
142 else if (value instanceof String) {
143 return (String) value;
144 }
145 else if (cs != null) {
146 return (String) cs.convert(value, sourceType, STRING_TYPE_DESCRIPTOR);
147 }
148 else {
149 return value.toString();
150 }
151 }
152
153
154 private static class PathVariableNamedValueInfo extends NamedValueInfo {
155
156 public PathVariableNamedValueInfo(PathVariable annotation) {
157 super(annotation.value(), true, ValueConstants.DEFAULT_NONE);
158 }
159 }
160
161 }