From 2d3fe96cd60b487dc5601e7e550fd53fe287dcd7 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Fri, 27 Feb 2015 19:10:41 +0100 Subject: [PATCH] Allow file locations for resource handling Prior to this change, location checks for serving resources would append `/` to the location path it didn't already have one. This commit makes sure not to append a `/` if the provided location is actually a file. Issue: SPR-12747 --- .../resource/ResourceHttpRequestHandler.java | 3 +++ .../ResourceHttpRequestHandlerTests.java | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java index e49cc2b66a7..274602b8f79 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java @@ -315,6 +315,9 @@ public class ResourceHttpRequestHandler extends WebContentGenerator implements H resourcePath = resource.getURL().getPath(); locationPath = location.getURL().getPath(); } + if(locationPath.equals(resourcePath)) { + return true; + } locationPath = (locationPath.endsWith("/") || !StringUtils.hasLength(locationPath) ? locationPath : locationPath + "/"); if (!resourcePath.startsWith(locationPath)) { diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java index 956238f7445..df27a76ebc8 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java @@ -280,6 +280,22 @@ public class ResourceHttpRequestHandlerTests { } + // SPR-12747 + @Test + public void getResourceWithResourceLocation() throws Exception { + List resourcePaths = new ArrayList(); + resourcePaths.add(new ClassPathResource("test/foo.css", getClass())); + this.handler.setLocations(resourcePaths); + MockHttpServletRequest request = new MockHttpServletRequest(); + request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "/foo.css"); + request.setMethod("GET"); + MockHttpServletResponse response = new MockHttpServletResponse(); + handler.handleRequest(request, response); + assertEquals("text/css", response.getContentType()); + assertEquals(17, response.getContentLength()); + } + + private static class TestServletContext extends MockServletContext { @Override