resolve handler bean name early; validate header conditions as well

master
Juergen Hoeller 15 years ago
parent aec2bc097e
commit 39a97a61af
  1. 22
      org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/handler/AbstractUrlHandlerMapping.java
  2. 14
      org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/annotation/DefaultAnnotationHandlerMapping.java

@ -170,6 +170,11 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping {
rawHandler = getDefaultHandler(); rawHandler = getDefaultHandler();
} }
if (rawHandler != null) { if (rawHandler != null) {
// Bean name or resolved handler?
if (rawHandler instanceof String) {
String handlerName = (String) rawHandler;
rawHandler = getApplicationContext().getBean(handlerName);
}
validateHandler(rawHandler, request); validateHandler(rawHandler, request);
handler = buildPathExposingHandler(rawHandler, lookupPath, null); handler = buildPathExposingHandler(rawHandler, lookupPath, null);
} }
@ -200,6 +205,11 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping {
// Direct match? // Direct match?
Object handler = this.handlerMap.get(urlPath); Object handler = this.handlerMap.get(urlPath);
if (handler != null) { if (handler != null) {
// Bean name or resolved handler?
if (handler instanceof String) {
String handlerName = (String) handler;
handler = getApplicationContext().getBean(handlerName);
}
validateHandler(handler, request); validateHandler(handler, request);
return buildPathExposingHandler(handler, urlPath, null); return buildPathExposingHandler(handler, urlPath, null);
} }
@ -213,6 +223,11 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping {
} }
if (bestPathMatch != null) { if (bestPathMatch != null) {
handler = this.handlerMap.get(bestPathMatch); handler = this.handlerMap.get(bestPathMatch);
// Bean name or resolved handler?
if (handler instanceof String) {
String handlerName = (String) handler;
handler = getApplicationContext().getBean(handlerName);
}
validateHandler(handler, request); validateHandler(handler, request);
String pathWithinMapping = getPathMatcher().extractPathWithinPattern(bestPathMatch, urlPath); String pathWithinMapping = getPathMatcher().extractPathWithinPattern(bestPathMatch, urlPath);
Map<String, String> uriTemplateVariables = Map<String, String> uriTemplateVariables =
@ -248,11 +263,6 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping {
protected Object buildPathExposingHandler( protected Object buildPathExposingHandler(
Object rawHandler, String pathWithinMapping, Map<String, String> uriTemplateVariables) { Object rawHandler, String pathWithinMapping, Map<String, String> uriTemplateVariables) {
// Bean name or resolved handler?
if (rawHandler instanceof String) {
String handlerName = (String) rawHandler;
rawHandler = getApplicationContext().getBean(handlerName);
}
HandlerExecutionChain chain = new HandlerExecutionChain(rawHandler); HandlerExecutionChain chain = new HandlerExecutionChain(rawHandler);
chain.addInterceptor(new PathExposingHandlerInterceptor(pathWithinMapping)); chain.addInterceptor(new PathExposingHandlerInterceptor(pathWithinMapping));
if (!CollectionUtils.isEmpty(uriTemplateVariables)) { if (!CollectionUtils.isEmpty(uriTemplateVariables)) {
@ -353,7 +363,7 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping {
* as value. * as value.
* @see #getDefaultHandler() * @see #getDefaultHandler()
*/ */
public final Map getHandlerMap() { public final Map<String, Object> getHandlerMap() {
return Collections.unmodifiableMap(this.handlerMap); return Collections.unmodifiableMap(this.handlerMap);
} }

@ -30,6 +30,7 @@ import org.springframework.stereotype.Controller;
import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.HttpRequestMethodNotSupportedException; import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.UnsatisfiedServletRequestParameterException; import org.springframework.web.bind.UnsatisfiedServletRequestParameterException;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
@ -186,11 +187,9 @@ public class DefaultAnnotationHandlerMapping extends AbstractDetectingUrlHandler
*/ */
@Override @Override
protected void validateHandler(Object handler, HttpServletRequest request) throws Exception { protected void validateHandler(Object handler, HttpServletRequest request) throws Exception {
Class handlerClass = (handler instanceof String ? RequestMapping mapping = this.cachedMappings.get(handler.getClass());
getApplicationContext().getType((String) handler) : handler.getClass());
RequestMapping mapping = this.cachedMappings.get(handlerClass);
if (mapping == null) { if (mapping == null) {
mapping = AnnotationUtils.findAnnotation(handlerClass, RequestMapping.class); mapping = AnnotationUtils.findAnnotation(handler.getClass(), RequestMapping.class);
} }
if (mapping != null) { if (mapping != null) {
validateMapping(mapping, request); validateMapping(mapping, request);
@ -218,6 +217,13 @@ public class DefaultAnnotationHandlerMapping extends AbstractDetectingUrlHandler
if (!ServletAnnotationMappingUtils.checkParameters(mappedParams, request)) { if (!ServletAnnotationMappingUtils.checkParameters(mappedParams, request)) {
throw new UnsatisfiedServletRequestParameterException(mappedParams, request.getParameterMap()); throw new UnsatisfiedServletRequestParameterException(mappedParams, request.getParameterMap());
} }
String[] mappedHeaders = mapping.headers();
if (!ServletAnnotationMappingUtils.checkHeaders(mappedHeaders, request)) {
throw new ServletRequestBindingException("Header conditions \"" +
StringUtils.arrayToDelimitedString(mappedHeaders, ", ") +
"\" not met for actual request");
}
} }
} }

Loading…
Cancel
Save