From 57d327d1ff3a339d4e1a0d82e81f0028f712d9e2 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Wed, 20 Apr 2011 08:43:57 +0000 Subject: [PATCH] SPR-8255 --- .../mvc/method/annotation/RequestKey.java | 13 +++++++------ .../ServletInvocableHandlerMethod.java | 2 +- .../method/annotation/RequestKeyTests.java | 8 ++++++-- ...questMappingHandlerMethodMappingTests.java | 19 +++++++++++++++++++ 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestKey.java b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestKey.java index 7e45971829..16158fe8d2 100644 --- a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestKey.java +++ b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestKey.java @@ -26,6 +26,7 @@ import java.util.Set; import javax.servlet.http.HttpServletRequest; import org.springframework.util.PathMatcher; +import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.mvc.method.condition.RequestCondition; @@ -92,7 +93,7 @@ public final class RequestKey { } Set result = new LinkedHashSet(patterns.size()); for (String pattern : patterns) { - if (!pattern.startsWith("/")) { + if (StringUtils.hasLength(pattern) && !pattern.startsWith("/")) { pattern = "/" + pattern; } result.add(pattern); @@ -169,9 +170,9 @@ public final class RequestKey { *
    *
  • URL patterns: *
      - *
    • If both keys have path patterns combine them according to the rules of the given {@link PathMatcher}. - *
    • If either key contains path patterns but not both use only what is available. - *
    • If neither key contains path patterns use "/". + *
    • If both have patterns combine them according to the rules of the given {@link PathMatcher} + *
    • If either contains patterns, but not both, use the available pattern + *
    • If neither contains patterns use "" *
    *
  • HTTP methods are combined as union of all HTTP methods listed in both keys. *
  • Request parameter are combined into a logical AND. @@ -198,8 +199,8 @@ public final class RequestKey { Set result = new LinkedHashSet(); if (!typePatterns.isEmpty() && !methodPatterns.isEmpty()) { for (String pattern1 : typePatterns) { - for (String p2 : methodPatterns) { - result.add(pathMatcher.combine(pattern1, p2)); + for (String pattern2 : methodPatterns) { + result.add(pathMatcher.combine(pattern1, pattern2)); } } } diff --git a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletInvocableHandlerMethod.java b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletInvocableHandlerMethod.java index fc920b9c4c..2145491651 100644 --- a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletInvocableHandlerMethod.java +++ b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletInvocableHandlerMethod.java @@ -123,7 +123,7 @@ public class ServletInvocableHandlerMethod extends InvocableHandlerMethod { webRequest.getResponse().sendError(this.responseStatus.value(), this.responseReason); } else { - webRequest.getResponse().sendError(this.responseStatus.value()); + webRequest.getResponse().setStatus(this.responseStatus.value()); } // to be picked up by the RedirectView diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestKeyTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestKeyTests.java index 18f39a3e09..fbe16149d2 100644 --- a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestKeyTests.java +++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestKeyTests.java @@ -77,9 +77,13 @@ public class RequestKeyTests { key1 = createKeyFromPatterns(); key2 = createKeyFromPatterns(); - key3 = createKeyFromPatterns("/"); + key3 = createKeyFromPatterns(""); + assertEquals(key3.getPatterns(), key1.combine(key2, pathMatcher).getPatterns()); + + key1 = createKeyFromPatterns("/t1"); + key2 = createKeyFromPatterns(""); + key3 = createKeyFromPatterns("/t1"); assertEquals(key3.getPatterns(), key1.combine(key2, pathMatcher).getPatterns()); - } @Test diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMethodMappingTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMethodMappingTests.java index 2fcbd3f455..dc24f57000 100644 --- a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMethodMappingTests.java +++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMethodMappingTests.java @@ -60,12 +60,15 @@ public class RequestMappingHandlerMethodMappingTests { private HandlerMethod barMethod; + private HandlerMethod emptyMethod; + @Before public void setUp() throws Exception { handler = new Handler(); fooMethod = new HandlerMethod(handler, "foo"); fooParamMethod = new HandlerMethod(handler, "fooParam"); barMethod = new HandlerMethod(handler, "bar"); + emptyMethod = new HandlerMethod(handler, "empty"); StaticApplicationContext context = new StaticApplicationContext(); context.registerSingleton("handler", handler.getClass()); @@ -88,6 +91,17 @@ public class RequestMappingHandlerMethodMappingTests { assertEquals(barMethod.getMethod(), hm.getMethod()); } + @Test + public void emptyPathMatch() throws Exception { + MockHttpServletRequest request = new MockHttpServletRequest("GET", ""); + HandlerMethod hm = (HandlerMethod) mapping.getHandler(request).getHandler(); + assertEquals(emptyMethod.getMethod(), hm.getMethod()); + + request = new MockHttpServletRequest("GET", "/"); + hm = (HandlerMethod) mapping.getHandler(request).getHandler(); + assertEquals(emptyMethod.getMethod(), hm.getMethod()); + } + // TODO: SPR-8247 @Ignore @Test @@ -144,6 +158,7 @@ public class RequestMappingHandlerMethodMappingTests { @SuppressWarnings("unused") @Controller + @RequestMapping private static class Handler { @RequestMapping(value = "/foo", method = RequestMethod.GET) @@ -157,6 +172,10 @@ public class RequestMappingHandlerMethodMappingTests { @RequestMapping(value = "/ba*", method = { RequestMethod.GET, RequestMethod.HEAD }) public void bar() { } + + @RequestMapping(value = "") + public void empty() { + } } } \ No newline at end of file