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.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   * Resolves method arguments annotated with an @{@link PathVariable}.
44   *
45   * <p>An @{@link PathVariable} is a named value that gets resolved from a URI
46   * template variable. It is always required and does not have a default value
47   * to fall back on. See the base class
48   * {@link org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver}
49   * for more information on how named values are processed.
50   *
51   * <p>If the method parameter type is {@link Map}, the name specified in the
52   * annotation is used to resolve the URI variable String value. The value is
53   * then converted to a {@link Map} via type conversion assuming a suitable
54   * {@link Converter} or {@link PropertyEditor} has been registered.
55   * Or if the annotation does not specify name the
56   * {@link RequestParamMapMethodArgumentResolver} is used instead to provide
57   * access to all URI variables in a map.
58   *
59   * <p>A {@link WebDataBinder} is invoked to apply type conversion to resolved
60   * path variable values that don't yet match the method parameter type.
61   *
62   * @author Rossen Stoyanchev
63   * @author Arjen Poutsma
64   * @since 3.1
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 }