SPR-6301 - Support @RequestHeader on HttpHeaders parameters
This commit is contained in:
@@ -77,12 +77,16 @@ import java.lang.annotation.Target;
|
||||
* <li>{@link RequestParam @RequestParam} annotated parameters for access to
|
||||
* specific Servlet/Portlet request parameters. Parameter values will be
|
||||
* converted to the declared method argument type. Additionally,
|
||||
* {@code RequestParam @RequestParam} can be used on a {@link java.util.Map Map} or
|
||||
* {@link org.springframework.util.MultiValueMap MultiValueMap} to gain access
|
||||
* to all request parameters.
|
||||
* {@code @RequestParam} can be used on a {@link java.util.Map Map<String, String>} or
|
||||
* {@link org.springframework.util.MultiValueMap MultiValueMap<String, String>}
|
||||
* method parameter to gain access to all request parameters.
|
||||
* <li>{@link RequestHeader @RequestHeader} annotated parameters for access to
|
||||
* specific Servlet/Portlet request HTTP headers. Parameter values will be
|
||||
* converted to the declared method argument type.
|
||||
* converted to the declared method argument type. Additionally,
|
||||
* {@code @RequestHeader} can be used on a {@link java.util.Map Map<String, String>},
|
||||
* {@link org.springframework.util.MultiValueMap MultiValueMap<String, String>}, or
|
||||
* {@link org.springframework.http.HttpHeaders HttpHeaders} method parameter to
|
||||
* gain access to all request headers.
|
||||
* <li>{@link RequestBody @RequestBody} annotated parameters for access to
|
||||
* the Servlet request HTTP contents. Parameter values will be
|
||||
* converted to the declared method argument type using
|
||||
|
||||
@@ -27,6 +27,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
@@ -41,6 +42,7 @@ import org.springframework.core.ParameterNameDiscoverer;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.http.HttpInputMessage;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.ui.ExtendedModelMap;
|
||||
import org.springframework.ui.Model;
|
||||
@@ -457,11 +459,15 @@ public class HandlerMethodInvoker {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Object resolveRequestHeader(String headerName, boolean required, String defaultValue,
|
||||
MethodParameter methodParam, NativeWebRequest webRequest, Object handlerForInitBinderCall)
|
||||
throws Exception {
|
||||
|
||||
Class<?> paramType = methodParam.getParameterType();
|
||||
if (Map.class.isAssignableFrom(paramType)) {
|
||||
return resolveRequestHeaderMap((Class<? extends Map>) paramType, webRequest);
|
||||
}
|
||||
if (headerName.length() == 0) {
|
||||
headerName = getRequiredParameterName(methodParam);
|
||||
}
|
||||
@@ -484,6 +490,34 @@ public class HandlerMethodInvoker {
|
||||
return binder.convertIfNecessary(headerValue, paramType, methodParam);
|
||||
}
|
||||
|
||||
private Map resolveRequestHeaderMap(Class<? extends Map> mapType, NativeWebRequest webRequest) {
|
||||
if (MultiValueMap.class.isAssignableFrom(mapType)) {
|
||||
MultiValueMap<String, String> result;
|
||||
if (HttpHeaders.class.isAssignableFrom(mapType)) {
|
||||
result = new HttpHeaders();
|
||||
} else {
|
||||
result = new LinkedMultiValueMap<String, String>();
|
||||
}
|
||||
for (Iterator<String> iterator = webRequest.getHeaderNames(); iterator.hasNext();) {
|
||||
String headerName = iterator.next();
|
||||
for (String headerValue : webRequest.getHeaderValues(headerName)) {
|
||||
result.add(headerName, headerValue);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
else {
|
||||
Map<String, String> result = new LinkedHashMap<String, String>();
|
||||
for (Iterator<String> iterator = webRequest.getHeaderNames(); iterator.hasNext();) {
|
||||
String headerName = iterator.next();
|
||||
String headerValue = webRequest.getHeader(headerName);
|
||||
result.put(headerName, headerValue);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Resolves the given {@link RequestBody @RequestBody} annotation.
|
||||
*/
|
||||
|
||||
@@ -72,6 +72,10 @@ public class FacesWebRequest extends FacesRequestAttributes implements NativeWeb
|
||||
return getExternalContext().getRequestHeaderValuesMap().get(headerName);
|
||||
}
|
||||
|
||||
public Iterator<String> getHeaderNames() {
|
||||
return getExternalContext().getRequestHeaderMap().keySet().iterator();
|
||||
}
|
||||
|
||||
public String getParameter(String paramName) {
|
||||
return getExternalContext().getRequestParameterMap().get(paramName);
|
||||
}
|
||||
|
||||
@@ -102,6 +102,11 @@ public class ServletWebRequest extends ServletRequestAttributes implements Nativ
|
||||
return (!ObjectUtils.isEmpty(headerValues) ? headerValues : null);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Iterator<String> getHeaderNames() {
|
||||
return CollectionUtils.toIterator(getRequest().getHeaderNames());
|
||||
}
|
||||
|
||||
public String getParameter(String paramName) {
|
||||
return getRequest().getParameter(paramName);
|
||||
}
|
||||
|
||||
@@ -47,6 +47,13 @@ public interface WebRequest extends RequestAttributes {
|
||||
*/
|
||||
String[] getHeaderValues(String headerName);
|
||||
|
||||
/**
|
||||
* Return a Iterator over request header names.
|
||||
* @see javax.servlet.http.HttpServletRequest#getHeaderNames()
|
||||
* @since 3.0
|
||||
*/
|
||||
Iterator<String> getHeaderNames();
|
||||
|
||||
/**
|
||||
* Return the request parameter of the given name, or <code>null</code> if none.
|
||||
* <p>Retrieves the first parameter value in case of a multi-value parameter.
|
||||
|
||||
Reference in New Issue
Block a user