Polishing

This commit is contained in:
Juergen Hoeller
2017-11-14 12:31:01 +01:00
parent d52d9fd268
commit 640c8ff693
14 changed files with 85 additions and 99 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@@ -90,8 +90,7 @@ public class ResourceHandlerRegistry {
/**
* A variant of
* {@link #ResourceHandlerRegistry(ApplicationContext, ServletContext, ContentNegotiationManager)}
* that also accepts the {@link UrlPathHelper} used for mapping requests
* to static resources.
* that also accepts the {@link UrlPathHelper} used for mapping requests to static resources.
* @since 4.3.13
*/
public ResourceHandlerRegistry(ApplicationContext applicationContext, ServletContext servletContext,
@@ -106,13 +105,12 @@ public class ResourceHandlerRegistry {
/**
* Add a resource handler for serving static resources based on the specified URL path
* patterns. The handler will be invoked for every incoming request that matches to
* one of the specified path patterns.
* <p>Patterns like {@code "/static/**"} or {@code "/css/{filename:\\w+\\.css}"}
* are allowed. See {@link org.springframework.util.AntPathMatcher} for more details on the
* syntax.
* @return A {@link ResourceHandlerRegistration} to use to further configure the
* Add a resource handler for serving static resources based on the specified URL path patterns.
* The handler will be invoked for every incoming request that matches to one of the specified
* path patterns.
* <p>Patterns like {@code "/static/**"} or {@code "/css/{filename:\\w+\\.css}"} are allowed.
* See {@link org.springframework.util.AntPathMatcher} for more details on the syntax.
* @return a {@link ResourceHandlerRegistration} to use to further configure the
* registered resource handler
*/
public ResourceHandlerRegistration addResourceHandler(String... pathPatterns) {

View File

@@ -92,7 +92,6 @@ public class PathResourceResolver extends AbstractResourceResolver {
* <p><strong>Note:</strong> the charset is used only if the
* {@link #setUrlPathHelper urlPathHelper} property is also configured and
* its {@code urlDecode} property is set to true.
* @param locationCharsets charsets by location
* @since 4.3.13
*/
public void setLocationCharsets(Map<Resource, Charset> locationCharsets) {
@@ -105,14 +104,13 @@ public class PathResourceResolver extends AbstractResourceResolver {
* @since 4.3.13
*/
public Map<Resource, Charset> getLocationCharsets() {
return Collections.unmodifiableMap(locationCharsets);
return Collections.unmodifiableMap(this.locationCharsets);
}
/**
* Provide a reference to the {@link UrlPathHelper} used to map requests to
* static resources. This helps to derive information about the lookup path
* such as whether it is decoded or not.
* @param urlPathHelper a reference to the path helper
* @since 4.3.13
*/
public void setUrlPathHelper(UrlPathHelper urlPathHelper) {
@@ -127,6 +125,7 @@ public class PathResourceResolver extends AbstractResourceResolver {
return this.urlPathHelper;
}
@Override
protected Resource resolveResourceInternal(HttpServletRequest request, String requestPath,
List<? extends Resource> locations, ResourceResolverChain chain) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@@ -152,10 +152,9 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
* {@link org.springframework.core.io.UrlResource URL resources} such as a
* file or an HTTP URL location and is used in {@link PathResourceResolver}
* to correctly encode paths relative to the location.
* <p><strong>Note:</strong> the charset is used only if the
* <p><strong>Note:</strong> The charset is used only if the
* {@link #setUrlPathHelper urlPathHelper} property is also configured and
* its {@code urlDecode} property is set to true.
* @param locationCharsets charsets by location
* its {@code urlDecode} property is set to {@code true}.
* @since 4.3.13
*/
public void setLocationCharsets(Map<Resource,Charset> locationCharsets) {
@@ -168,7 +167,7 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
* @since 4.3.13
*/
public Map<Resource, Charset> getLocationCharsets() {
return Collections.unmodifiableMap(locationCharsets);
return Collections.unmodifiableMap(this.locationCharsets);
}
/**
@@ -281,7 +280,6 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
* Provide a reference to the {@link UrlPathHelper} used to map requests to
* static resources. This helps to derive information about the lookup path
* such as whether it is decoded or not.
* @param urlPathHelper a reference to the path helper
* @since 4.3.13
*/
public void setUrlPathHelper(UrlPathHelper urlPathHelper) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@@ -48,7 +48,7 @@ import org.springframework.web.servlet.HandlerMapping;
import static org.junit.Assert.*;
/**
* Unit tests for ResourceHttpRequestHandler.
* Unit tests for {@link ResourceHttpRequestHandler}.
*
* @author Keith Donald
* @author Jeremy Grelle
@@ -67,7 +67,7 @@ public class ResourceHttpRequestHandlerTests {
@Before
public void setUp() throws Exception {
public void setup() throws Exception {
dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
@@ -86,6 +86,7 @@ public class ResourceHttpRequestHandlerTests {
this.response = new MockHttpServletResponse();
}
@Test
public void getResource() throws Exception {
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css");
@@ -240,7 +241,7 @@ public class ResourceHttpRequestHandlerTests {
assertEquals("function foo() { console.log(\"hello world\"); }", this.response.getContentAsString());
}
@Test // SPR-13658
@Test // SPR-13658
public void getResourceWithRegisteredMediaType() throws Exception {
ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean();
factory.addMediaType("css", new MediaType("foo", "bar"));
@@ -261,7 +262,7 @@ public class ResourceHttpRequestHandlerTests {
assertEquals("h1 { color:red; }", this.response.getContentAsString());
}
@Test // SPR-14577
@Test // SPR-14577
public void getMediaTypeWithFavorPathExtensionOff() throws Exception {
ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean();
factory.setFavorPathExtension(false);
@@ -282,18 +283,16 @@ public class ResourceHttpRequestHandlerTests {
assertEquals("text/html", this.response.getContentType());
}
@Test // SPR-14368
@Test // SPR-14368
public void getResourceWithMediaTypeResolvedThroughServletContext() throws Exception {
MockServletContext servletContext = new MockServletContext() {
@Override
public String getMimeType(String filePath) {
return "foo/bar";
}
@Override
public String getVirtualServerName() {
return null;
return "";
}
};
@@ -606,8 +605,7 @@ public class ResourceHttpRequestHandlerTests {
assertEquals("t.", ranges[11]);
}
// SPR-14005
@Test
@Test // SPR-14005
public void doOverwriteExistingCacheControlHeaders() throws Exception {
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css");
this.response.setHeader("Cache-Control", "no-store");
@@ -618,10 +616,6 @@ public class ResourceHttpRequestHandlerTests {
}
private long dateHeaderAsLong(String responseHeaderName) throws Exception {
return dateFormat.parse(this.response.getHeader(responseHeaderName)).getTime();
}
private long resourceLastModified(String resourceName) throws IOException {
return new ClassPathResource(resourceName, getClass()).getFile().lastModified();
}
@@ -631,6 +625,10 @@ public class ResourceHttpRequestHandlerTests {
return dateFormat.format(lastModified);
}
private long dateHeaderAsLong(String responseHeaderName) throws Exception {
return dateFormat.parse(this.response.getHeader(responseHeaderName)).getTime();
}
private static class TestServletContext extends MockServletContext {