Browse Source

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
pull/774/head
Brian Clozel 11 years ago
parent
commit
2d3fe96cd6
  1. 3
      spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java
  2. 16
      spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java

3
spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java

@ -315,6 +315,9 @@ public class ResourceHttpRequestHandler extends WebContentGenerator implements H @@ -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)) {

16
spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java

@ -280,6 +280,22 @@ public class ResourceHttpRequestHandlerTests { @@ -280,6 +280,22 @@ public class ResourceHttpRequestHandlerTests {
}
// SPR-12747
@Test
public void getResourceWithResourceLocation() throws Exception {
List<Resource> resourcePaths = new ArrayList<Resource>();
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

Loading…
Cancel
Save