added @RequestHeader support

This commit is contained in:
Juergen Hoeller
2008-12-05 07:17:31 +00:00
parent b3866a974a
commit 0ed916495c
9 changed files with 281 additions and 80 deletions

View File

@@ -0,0 +1,61 @@
/*
* Copyright 2002-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.bind.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Annotation which indicates that a method parameter should be bound to a web request header.
* Supported for {@link RequestMapping} annotated handler methods in Servlet and Portlet environments.
*
* @author Juergen Hoeller
* @since 3.0
* @see RequestMapping
* @see org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
* @see org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter
*/
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestHeader {
/**
* The name of the request header to bind to.
*/
String value() default "";
/**
* Whether the header is required.
* <p>Default is <code>true</code>, leading to an exception thrown in case
* of the header missing in the request. Switch this to <code>false</code>
* if you prefer a <code>null</value> in case of the header missing.
* <p>Alternatively, provide a {@link #defaultValue() defaultValue},
* which implicitely sets this flag to <code>false</code>.
*/
boolean required() default true;
/**
* The default value to use as a fallback. Supplying a default value implicitely
* sets {@link #required()} to false.
*/
String defaultValue() default "";
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,33 +23,40 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Annotation which indicates that a method parameter should be bound to a web request parameter. Supported for {@link
* RequestMapping} annotated handler methods in Servlet and Portlet environments.
* Annotation which indicates that a method parameter should be bound to a web request parameter.
* Supported for {@link RequestMapping} annotated handler methods in Servlet and Portlet environments.
*
* @author Arjen Poutsma
* @author Juergen Hoeller
* @since 2.5
* @see RequestMapping
* @see org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
* @see org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter
* @since 2.5
*/
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestParam {
/** The request parameter to bind to. */
/**
* The name of the request parameter to bind to.
*/
String value() default "";
/**
* Whether the parameter is required. <p>Default is <code>true</code>, leading to an exception thrown in case of the
* parameter missing in the request. Switch this to <code>false</code> if you prefer a <code>null</value> in case of
* the parameter missing. <p>Alternatively, provide a {@link #defaultValue() defaultValue}, which implicitely sets this
* flag to <code>false</code>.
* Whether the parameter is required.
* <p>Default is <code>true</code>, leading to an exception thrown in case
* of the parameter missing in the request. Switch this to <code>false</code>
* if you prefer a <code>null</value> in case of the parameter missing.
* <p>Alternatively, provide a {@link #defaultValue() defaultValue},
* which implicitely sets this flag to <code>false</code>.
*/
boolean required() default true;
/** The default value to use as a fallback. Supplying a default value implicitely sets {@link #required()} to false. */
/**
* The default value to use as a fallback. Supplying a default value implicitely
* sets {@link #required()} to false.
*/
String defaultValue() default "";
}

View File

@@ -46,6 +46,7 @@ import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.DefaultSessionAttributeStore;
import org.springframework.web.bind.support.SessionAttributeStore;
@@ -156,36 +157,47 @@ public class HandlerMethodInvoker {
methodParam.initParameterNameDiscovery(this.parameterNameDiscoverer);
GenericTypeResolver.resolveParameterType(methodParam, handler.getClass());
String paramName = null;
boolean paramRequired = false;
String paramDefaultValue = null;
String headerName = null;
boolean required = false;
String defaultValue = null;
String pathVarName = null;
String attrName = null;
int found = 0;
Annotation[] paramAnns = methodParam.getParameterAnnotations();
for (Annotation paramAnn : paramAnns) {
if (RequestParam.class.isInstance(paramAnn)) {
RequestParam requestParam = (RequestParam) paramAnn;
paramName = requestParam.value();
paramRequired = requestParam.required();
paramDefaultValue = requestParam.defaultValue();
break;
required = requestParam.required();
defaultValue = requestParam.defaultValue();
found++;
}
else if (RequestHeader.class.isInstance(paramAnn)) {
RequestHeader requestHeader = (RequestHeader) paramAnn;
headerName = requestHeader.value();
required = requestHeader.required();
defaultValue = requestHeader.defaultValue();
found++;
}
else if (ModelAttribute.class.isInstance(paramAnn)) {
ModelAttribute attr = (ModelAttribute) paramAnn;
attrName = attr.value();
found++;
}
else if (PathVariable.class.isInstance(paramAnn)) {
PathVariable pathVar = (PathVariable) paramAnn;
pathVarName = pathVar.value();
found++;
}
}
if ((paramName != null && attrName != null) || (paramName != null && pathVarName != null) ||
(pathVarName != null && attrName != null)) {
throw new IllegalStateException("@RequestParam, @PathVariable and @ModelAttribute are exclusive " +
"choices - do not specify both on the same parameter: " + handlerMethod);
if (found > 1) {
throw new IllegalStateException("Handler parameter annotations are exclusive choices - " +
"do not specify more than one such annotation on the same parameter: " + handlerMethod);
}
if (paramName == null && attrName == null && pathVarName == null) {
if (found == 0) {
Object argValue = resolveCommonArgument(methodParam, webRequest);
if (argValue != WebArgumentResolver.UNRESOLVED) {
args[i] = argValue;
@@ -212,8 +224,10 @@ public class HandlerMethodInvoker {
}
if (paramName != null) {
args[i] = resolveRequestParam(paramName, paramRequired, paramDefaultValue, methodParam, webRequest,
handler);
args[i] = resolveRequestParam(paramName, required, defaultValue, methodParam, webRequest, handler);
}
else if (headerName != null) {
args[i] = resolveRequestHeader(headerName, required, defaultValue, methodParam, webRequest, handler);
}
else if (attrName != null) {
WebDataBinder binder = resolveModelAttribute(attrName, methodParam, implicitModel, webRequest, handler);
@@ -333,18 +347,13 @@ public class HandlerMethodInvoker {
return initBinderArgs;
}
private Object resolveRequestParam(String paramName, boolean paramRequired, String paramDefaultValue,
private Object resolveRequestParam(String paramName, boolean required, String defaultValue,
MethodParameter methodParam, NativeWebRequest webRequest, Object handlerForInitBinderCall)
throws Exception {
Class paramType = methodParam.getParameterType();
if (paramName.length() == 0) {
paramName = methodParam.getParameterName();
if (paramName == null) {
throw new IllegalStateException(
"No parameter specified for @RequestParam argument of type [" + paramType.getName() +
"], and no parameter name information found in class file either.");
}
paramName = getRequiredParameterName(methodParam);
}
Object paramValue = null;
if (webRequest.getNativeRequest() instanceof MultipartRequest) {
@@ -357,23 +366,64 @@ public class HandlerMethodInvoker {
}
}
if (paramValue == null) {
if (StringUtils.hasText(paramDefaultValue)) {
paramValue = paramDefaultValue;
if (StringUtils.hasText(defaultValue)) {
paramValue = defaultValue;
}
else if (paramRequired) {
else if (required) {
raiseMissingParameterException(paramName, paramType);
}
if (paramValue == null && paramType.isPrimitive()) {
throw new IllegalStateException("Optional " + paramType + " parameter '" + paramName +
"' is not present but cannot be translated into a null value due to being declared as a " +
"primitive type. Consider declaring it as object wrapper for the corresponding primitive type.");
}
checkValue(paramName, paramValue, paramType);
}
WebDataBinder binder = createBinder(webRequest, null, paramName);
initBinder(handlerForInitBinderCall, paramName, binder, webRequest);
return binder.convertIfNecessary(paramValue, paramType, methodParam);
}
private Object resolveRequestHeader(String headerName, boolean required, String defaultValue,
MethodParameter methodParam, NativeWebRequest webRequest, Object handlerForInitBinderCall)
throws Exception {
Class paramType = methodParam.getParameterType();
if (headerName.length() == 0) {
headerName = getRequiredParameterName(methodParam);
}
Object headerValue = null;
String[] headerValues = webRequest.getHeaderValues(headerName);
if (headerValues != null) {
headerValue = (headerValues.length == 1 ? headerValues[0] : headerValues);
}
if (headerValue == null) {
if (StringUtils.hasText(defaultValue)) {
headerValue = defaultValue;
}
else if (required) {
raiseMissingHeaderException(headerName, paramType);
}
checkValue(headerName, headerValue, paramType);
}
WebDataBinder binder = createBinder(webRequest, null, headerName);
initBinder(handlerForInitBinderCall, headerName, binder, webRequest);
return binder.convertIfNecessary(headerValue, paramType, methodParam);
}
private String getRequiredParameterName(MethodParameter methodParam) {
String name = methodParam.getParameterName();
if (name == null) {
throw new IllegalStateException("No parameter name specified for argument of type [" +
methodParam.getParameterType().getName() +
"], and no parameter name information found in class file either.");
}
return name;
}
private void checkValue(String name, Object value, Class paramType) {
if (value == null && paramType.isPrimitive()) {
throw new IllegalStateException("Optional " + paramType + " parameter '" + name +
"' is not present but cannot be translated into a null value due to being declared as a " +
"primitive type. Consider declaring it as object wrapper for the corresponding primitive type.");
}
}
private WebDataBinder resolveModelAttribute(String attrName, MethodParameter methodParam,
ExtendedModelMap implicitModel, NativeWebRequest webRequest, Object handler)
throws Exception {
@@ -471,6 +521,10 @@ public class HandlerMethodInvoker {
throw new IllegalStateException("Missing parameter '" + paramName + "' of type [" + paramType.getName() + "]");
}
protected void raiseMissingHeaderException(String headerName, Class paramType) throws Exception {
throw new IllegalStateException("Missing header '" + headerName + "' of type [" + paramType.getName() + "]");
}
protected void raiseSessionRequiredException(String message) throws Exception {
throw new IllegalStateException(message);
}