SPR-6378 - RC2: Issue with RequestMethod.GET differs from M2

This commit is contained in:
Arjen Poutsma 2009-11-25 11:12:03 +00:00
parent 5d2d2bcf39
commit 89975c8b79
5 changed files with 191 additions and 32 deletions

View File

@ -64,6 +64,16 @@ public interface HandlerMapping {
*/ */
String PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE = HandlerMapping.class.getName() + ".pathWithinHandlerMapping"; String PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE = HandlerMapping.class.getName() + ".pathWithinHandlerMapping";
/**
* Name of the {@link HttpServletRequest} attribute that contains the
* best matching pattern within the handler mapping.
* <p>Note: This attribute is not required to be supported by all
* HandlerMapping implementations. URL-based HandlerMappings will
* typically support it, but handlers should not necessarily expect
* this request attribute to be present in all scenarios.
*/
String BEST_MATCHING_PATTERN_ATTRIBUTE = HandlerMapping.class.getName() + ".bestMatchingPattern";
/** /**
* Name of the {@link HttpServletRequest} attribute that contains the URI * Name of the {@link HttpServletRequest} attribute that contains the URI
* templates map, mapping variable names to values. * templates map, mapping variable names to values.
@ -74,7 +84,6 @@ public interface HandlerMapping {
*/ */
String URI_TEMPLATE_VARIABLES_ATTRIBUTE = HandlerMapping.class.getName() + ".uriTemplateVariables"; String URI_TEMPLATE_VARIABLES_ATTRIBUTE = HandlerMapping.class.getName() + ".uriTemplateVariables";
/** /**
* Return a handler and any interceptors for this request. The choice may be made * Return a handler and any interceptors for this request. The choice may be made
* on request URL, session state, or any factor the implementing class chooses. * on request URL, session state, or any factor the implementing class chooses.

View File

@ -178,7 +178,7 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping {
rawHandler = getApplicationContext().getBean(handlerName); rawHandler = getApplicationContext().getBean(handlerName);
} }
validateHandler(rawHandler, request); validateHandler(rawHandler, request);
handler = buildPathExposingHandler(rawHandler, lookupPath, null); handler = buildPathExposingHandler(rawHandler, lookupPath, lookupPath, null);
} }
} }
if (handler != null && logger.isDebugEnabled()) { if (handler != null && logger.isDebugEnabled()) {
@ -213,35 +213,35 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping {
handler = getApplicationContext().getBean(handlerName); handler = getApplicationContext().getBean(handlerName);
} }
validateHandler(handler, request); validateHandler(handler, request);
return buildPathExposingHandler(handler, urlPath, null); return buildPathExposingHandler(handler, urlPath, urlPath, null);
} }
// Pattern match? // Pattern match?
List<String> matchingPaths = new ArrayList<String>(); List<String> matchingPatterns = new ArrayList<String>();
for (String registeredPath : this.handlerMap.keySet()) { for (String registeredPattern : this.handlerMap.keySet()) {
if (getPathMatcher().match(registeredPath, urlPath)) { if (getPathMatcher().match(registeredPattern, urlPath)) {
matchingPaths.add(registeredPath); matchingPatterns.add(registeredPattern);
} }
} }
String bestPathMatch = null; String bestPatternMatch = null;
if (!matchingPaths.isEmpty()) { if (!matchingPatterns.isEmpty()) {
Collections.sort(matchingPaths, getPathMatcher().getPatternComparator(urlPath)); Collections.sort(matchingPatterns, getPathMatcher().getPatternComparator(urlPath));
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("Matching path for request [" + urlPath + "] are " + matchingPaths); logger.debug("Matching patterns for request [" + urlPath + "] are " + matchingPatterns);
} }
bestPathMatch = matchingPaths.get(0); bestPatternMatch = matchingPatterns.get(0);
} }
if (bestPathMatch != null) { if (bestPatternMatch != null) {
handler = this.handlerMap.get(bestPathMatch); handler = this.handlerMap.get(bestPatternMatch);
// Bean name or resolved handler? // Bean name or resolved handler?
if (handler instanceof String) { if (handler instanceof String) {
String handlerName = (String) handler; String handlerName = (String) handler;
handler = getApplicationContext().getBean(handlerName); handler = getApplicationContext().getBean(handlerName);
} }
validateHandler(handler, request); validateHandler(handler, request);
String pathWithinMapping = getPathMatcher().extractPathWithinPattern(bestPathMatch, urlPath); String pathWithinMapping = getPathMatcher().extractPathWithinPattern(bestPatternMatch, urlPath);
Map<String, String> uriTemplateVariables = Map<String, String> uriTemplateVariables =
getPathMatcher().extractUriTemplateVariables(bestPathMatch, urlPath); getPathMatcher().extractUriTemplateVariables(bestPatternMatch, urlPath);
return buildPathExposingHandler(handler, pathWithinMapping, uriTemplateVariables); return buildPathExposingHandler(handler, bestPatternMatch, pathWithinMapping, uriTemplateVariables);
} }
// No handler found... // No handler found...
return null; return null;
@ -269,11 +269,13 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping {
* @param uriTemplateVariables the URI template variables, can be <code>null</code> if no variables found * @param uriTemplateVariables the URI template variables, can be <code>null</code> if no variables found
* @return the final handler object * @return the final handler object
*/ */
protected Object buildPathExposingHandler( protected Object buildPathExposingHandler(Object rawHandler,
Object rawHandler, String pathWithinMapping, Map<String, String> uriTemplateVariables) { String bestMatchingPattern,
String pathWithinMapping,
Map<String, String> uriTemplateVariables) {
HandlerExecutionChain chain = new HandlerExecutionChain(rawHandler); HandlerExecutionChain chain = new HandlerExecutionChain(rawHandler);
chain.addInterceptor(new PathExposingHandlerInterceptor(pathWithinMapping)); chain.addInterceptor(new PathExposingHandlerInterceptor(bestMatchingPattern, pathWithinMapping));
if (!CollectionUtils.isEmpty(uriTemplateVariables)) { if (!CollectionUtils.isEmpty(uriTemplateVariables)) {
chain.addInterceptor(new UriTemplateVariablesHandlerInterceptor(uriTemplateVariables)); chain.addInterceptor(new UriTemplateVariablesHandlerInterceptor(uriTemplateVariables));
} }
@ -286,7 +288,8 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping {
* @param request the request to expose the path to * @param request the request to expose the path to
* @see #PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE * @see #PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE
*/ */
protected void exposePathWithinMapping(String pathWithinMapping, HttpServletRequest request) { protected void exposePathWithinMapping(String bestMatchingPattern, String pathWithinMapping, HttpServletRequest request) {
request.setAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE, bestMatchingPattern);
request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, pathWithinMapping); request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, pathWithinMapping);
} }
@ -384,15 +387,18 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping {
*/ */
private class PathExposingHandlerInterceptor extends HandlerInterceptorAdapter { private class PathExposingHandlerInterceptor extends HandlerInterceptorAdapter {
private final String bestMatchingPattern;
private final String pathWithinMapping; private final String pathWithinMapping;
private PathExposingHandlerInterceptor(String pathWithinMapping) { private PathExposingHandlerInterceptor(String bestMatchingPattern, String pathWithinMapping) {
this.bestMatchingPattern = bestMatchingPattern;
this.pathWithinMapping = pathWithinMapping; this.pathWithinMapping = pathWithinMapping;
} }
@Override @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
exposePathWithinMapping(this.pathWithinMapping, request); exposePathWithinMapping(this.bestMatchingPattern, this.pathWithinMapping, request);
return true; return true;
} }
} }

View File

@ -432,7 +432,7 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator implemen
if (mappingInfo.paths.length > 0) { if (mappingInfo.paths.length > 0) {
List<String> matchedPaths = new ArrayList<String>(mappingInfo.paths.length); List<String> matchedPaths = new ArrayList<String>(mappingInfo.paths.length);
for (String methodLevelPattern : mappingInfo.paths) { for (String methodLevelPattern : mappingInfo.paths) {
String matchedPattern = getMatchedPattern(methodLevelPattern, lookupPath); String matchedPattern = getMatchedPattern(methodLevelPattern, lookupPath, request);
if (matchedPattern != null) { if (matchedPattern != null) {
if (mappingInfo.matches(request)) { if (mappingInfo.matches(request)) {
match = true; match = true;
@ -518,12 +518,23 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator implemen
} }
} }
private String getMatchedPattern(String methodLevelPattern, String lookupPath) { /**
if ((!hasTypeLevelMapping() || ObjectUtils.isEmpty(getTypeLevelMapping().value())) && * Determines the matched pattern for the given methodLevelPattern and path.
isPathMatchInternal(methodLevelPattern, lookupPath)) { *
return methodLevelPattern; * <p>Uses the following algorithm:
} * <ol>
if (hasTypeLevelMapping()) { * <li>If there is a type-level mapping with path information, it is
* {@linkplain PathMatcher#combine(String, String) combined} with the method-level pattern.
* <li>If there is a {@linkplain HandlerMapping#BEST_MATCHING_PATTERN_ATTRIBUTE best matching pattern} in the
* request, it is combined with the method-level pattern.
* <li>Otherwise,
* @param methodLevelPattern
* @param lookupPath
* @param request
* @return
*/
private String getMatchedPattern(String methodLevelPattern, String lookupPath, HttpServletRequest request) {
if (hasTypeLevelMapping() && (!ObjectUtils.isEmpty(getTypeLevelMapping().value()))) {
String[] typeLevelPatterns = getTypeLevelMapping().value(); String[] typeLevelPatterns = getTypeLevelMapping().value();
for (String typeLevelPattern : typeLevelPatterns) { for (String typeLevelPattern : typeLevelPatterns) {
if (!typeLevelPattern.startsWith("/")) { if (!typeLevelPattern.startsWith("/")) {
@ -534,7 +545,17 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator implemen
return combinedPattern; return combinedPattern;
} }
} }
return null;
}
String bestMatchingPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
if (StringUtils.hasText(bestMatchingPattern)) {
String combinedPattern = pathMatcher.combine(bestMatchingPattern, methodLevelPattern);
if (!combinedPattern.equals(bestMatchingPattern) && (isPathMatchInternal(combinedPattern, lookupPath))) {
return combinedPattern;
}
}
if (isPathMatchInternal(methodLevelPattern, lookupPath)) {
return methodLevelPattern;
} }
return null; return null;
} }

View File

@ -0,0 +1,52 @@
/*
* 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.servlet.mvc.annotation;
import java.io.IOException;
import java.io.Writer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
/**
* Used for testing the combination of ControllerClassNameHandlerMapping/SimpleUrlHandlerMapping with @RequestParam in
* {@link ServletAnnotationControllerTests}. Implemented as a top-level class (rather than an inner class) to make the
* ControllerClassNameHandlerMapping work.
*
* @author Arjen Poutsma
*/
@Controller
public class BookController {
@RequestMapping("list")
public void list(Writer writer) throws IOException {
writer.write("list");
}
@RequestMapping("show")
public void show(@RequestParam(required = true) Long id, Writer writer) throws IOException {
writer.write("show-id=" + id);
}
@RequestMapping(method = RequestMethod.POST)
public void create(Writer writer) throws IOException {
writer.write("create");
}
}

View File

@ -55,6 +55,7 @@ import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.DerivedTestBean; import org.springframework.beans.DerivedTestBean;
import org.springframework.beans.ITestBean; import org.springframework.beans.ITestBean;
import org.springframework.beans.TestBean; import org.springframework.beans.TestBean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
@ -107,6 +108,7 @@ import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View; import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
import org.springframework.web.servlet.mvc.AbstractController; import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver; import org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver;
import org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping; import org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping;
@ -1159,6 +1161,75 @@ public class ServletAnnotationControllerTests {
assertEquals("Content-Type=[text/html],Custom-Header=[value21,value22]", response.getContentAsString()); assertEquals("Content-Type=[text/html],Custom-Header=[value21,value22]", response.getContentAsString());
} }
@Test
public void controllerClassNameNoTypeLevelAnn() throws Exception {
servlet = new DispatcherServlet() {
@Override
protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent)
throws BeansException {
GenericWebApplicationContext wac = new GenericWebApplicationContext();
wac.registerBeanDefinition("controller", new RootBeanDefinition(BookController.class));
RootBeanDefinition mapping = new RootBeanDefinition(ControllerClassNameHandlerMapping.class);
mapping.getPropertyValues().add("excludedPackages", null);
wac.registerBeanDefinition("handlerMapping", mapping);
wac.refresh();
return wac;
}
};
servlet.init(new MockServletConfig());
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/book/list");
MockHttpServletResponse response = new MockHttpServletResponse();
servlet.service(request, response);
assertEquals("list", response.getContentAsString());
request = new MockHttpServletRequest("GET", "/book/show");
request.addParameter("id", "12");
response = new MockHttpServletResponse();
servlet.service(request, response);
assertEquals("show-id=12", response.getContentAsString());
request = new MockHttpServletRequest("POST", "/book");
response = new MockHttpServletResponse();
servlet.service(request, response);
assertEquals("create", response.getContentAsString());
}
@Test
public void simpleUrlHandlerMapping() throws Exception {
servlet = new DispatcherServlet() {
@Override
protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent)
throws BeansException {
GenericWebApplicationContext wac = new GenericWebApplicationContext();
wac.registerBeanDefinition("controller", new RootBeanDefinition(BookController.class));
RootBeanDefinition hmDef = new RootBeanDefinition(SimpleUrlHandlerMapping.class);
hmDef.getPropertyValues().add("mappings", "/book/*=controller\n/book=controller");
wac.registerBeanDefinition("handlerMapping", hmDef);
wac.refresh();
return wac;
}
};
servlet.init(new MockServletConfig());
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/book/list");
MockHttpServletResponse response = new MockHttpServletResponse();
servlet.service(request, response);
assertEquals("list", response.getContentAsString());
request = new MockHttpServletRequest("GET", "/book/show");
request.addParameter("id", "12");
response = new MockHttpServletResponse();
servlet.service(request, response);
assertEquals("show-id=12", response.getContentAsString());
request = new MockHttpServletRequest("POST", "/book");
response = new MockHttpServletResponse();
servlet.service(request, response);
assertEquals("create", response.getContentAsString());
}
/* /*
* Controllers * Controllers