@RequestMapping type-level param constraints taken into account consistently
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
* Copyright 2002-2009 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.
|
||||
@@ -24,9 +24,9 @@ package org.springframework.web.bind;
|
||||
*/
|
||||
public class MissingServletRequestParameterException extends ServletRequestBindingException {
|
||||
|
||||
private String parameterName;
|
||||
private final String parameterName;
|
||||
|
||||
private String parameterType;
|
||||
private final String parameterType;
|
||||
|
||||
|
||||
/**
|
||||
@@ -49,14 +49,14 @@ public class MissingServletRequestParameterException extends ServletRequestBindi
|
||||
/**
|
||||
* Return the name of the offending parameter.
|
||||
*/
|
||||
public String getParameterName() {
|
||||
public final String getParameterName() {
|
||||
return this.parameterName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the expected type of the offending parameter.
|
||||
*/
|
||||
public String getParameterType() {
|
||||
public final String getParameterType() {
|
||||
return this.parameterType;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Copyright 2002-2009 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;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* {@link ServletRequestBindingException} subclass that indicates an unsatisfied
|
||||
* parameter condition, as typically expressed using an <code>@RequestMapping</code>
|
||||
* annotation at the <code>@Controller</code> type level.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 3.0
|
||||
* @see org.springframework.web.bind.annotation.RequestMapping#params()
|
||||
*/
|
||||
public class UnsatisfiedServletRequestParameterException extends ServletRequestBindingException {
|
||||
|
||||
private final String[] paramConditions;
|
||||
|
||||
private final Map<String, String[]> actualParams;
|
||||
|
||||
|
||||
/**
|
||||
* Create a new UnsatisfiedServletRequestParameterException.
|
||||
* @param paramConditions the parameter conditions that have been violated
|
||||
* @param actualParams the actual parameter Map associated with the ServletRequest
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public UnsatisfiedServletRequestParameterException(String[] paramConditions, Map actualParams) {
|
||||
super("");
|
||||
this.paramConditions = paramConditions;
|
||||
this.actualParams = (Map<String, String[]>) actualParams;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return "Parameter conditions \"" + StringUtils.arrayToDelimitedString(this.paramConditions, ", ") +
|
||||
"\" not met for actual request parameters: " + requestParameterMapToString(this.actualParams);
|
||||
}
|
||||
|
||||
private static String requestParameterMapToString(Map<String, String[]> actualParams) {
|
||||
StringBuilder result = new StringBuilder();
|
||||
for (Iterator<Map.Entry<String, String[]>> it = actualParams.entrySet().iterator(); it.hasNext();) {
|
||||
Map.Entry<String, String[]> entry = it.next();
|
||||
result.append(entry.getKey()).append('=').append(ObjectUtils.nullSafeToString(entry.getValue()));
|
||||
if (it.hasNext()) {
|
||||
result.append(", ");
|
||||
}
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the parameter conditions that have been violated.
|
||||
* @see org.springframework.web.bind.annotation.RequestMapping#params()
|
||||
*/
|
||||
public final String[] getParamConditions() {
|
||||
return this.paramConditions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the actual parameter Map associated with the ServletRequest.
|
||||
* @see javax.servlet.ServletRequest#getParameterMap()
|
||||
*/
|
||||
public final Map<String, String[]> getActualParams() {
|
||||
return this.actualParams;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
* Copyright 2002-2009 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.
|
||||
@@ -21,7 +21,6 @@ import java.util.HashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
@@ -30,6 +29,7 @@ import org.springframework.stereotype.Controller;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
||||
import org.springframework.web.bind.UnsatisfiedServletRequestParameterException;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMapping;
|
||||
@@ -172,9 +172,11 @@ public class DefaultAnnotationHandlerMapping extends AbstractDetectingUrlHandler
|
||||
*/
|
||||
@Override
|
||||
protected void validateHandler(Object handler, HttpServletRequest request) throws Exception {
|
||||
RequestMapping mapping = this.cachedMappings.get(handler.getClass());
|
||||
Class handlerClass = (handler instanceof String ?
|
||||
getApplicationContext().getType((String) handler) : handler.getClass());
|
||||
RequestMapping mapping = this.cachedMappings.get(handlerClass);
|
||||
if (mapping == null) {
|
||||
mapping = AnnotationUtils.findAnnotation(handler.getClass(), RequestMapping.class);
|
||||
mapping = AnnotationUtils.findAnnotation(handlerClass, RequestMapping.class);
|
||||
}
|
||||
if (mapping != null) {
|
||||
validateMapping(mapping, request);
|
||||
@@ -200,12 +202,8 @@ public class DefaultAnnotationHandlerMapping extends AbstractDetectingUrlHandler
|
||||
|
||||
String[] mappedParams = mapping.params();
|
||||
if (!ServletAnnotationMappingUtils.checkParameters(mappedParams, request)) {
|
||||
throw new ServletException("Parameter conditions {" +
|
||||
StringUtils.arrayToDelimitedString(mappedParams, ", ") +
|
||||
"} not met for request parameters: " + request.getParameterMap());
|
||||
throw new UnsatisfiedServletRequestParameterException(mappedParams, request.getParameterMap());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user