From 05ff83f95a7a7460688131cf489b7e5487af0273 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 28 Oct 2016 15:13:37 +0200 Subject: [PATCH] ResourceHttpRequestHandler initializes PathExtensionContentNegotiationStrategy in afterPropertiesSet Issue: SPR-14851 (cherry picked from commit b7d3a96) --- .../annotation/ResourceHandlerRegistry.java | 3 +- .../resource/ResourceHttpRequestHandler.java | 40 +++++++++---------- .../ResourceHttpRequestHandlerTests.java | 6 --- 3 files changed, 19 insertions(+), 30 deletions(-) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistry.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistry.java index e4b804045c..2bdb987f19 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistry.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistry.java @@ -145,9 +145,8 @@ public class ResourceHandlerRegistry { handler.setContentNegotiationManager(this.contentNegotiationManager); try { handler.afterPropertiesSet(); - handler.afterSingletonsInstantiated(); } - catch (Exception ex) { + catch (Throwable ex) { throw new BeanInitializationException("Failed to init ResourceHttpRequestHandler", ex); } urlMap.put(pathPattern, handler); 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 d73ae890b5..e95778bd2a 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 @@ -22,7 +22,6 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; @@ -32,7 +31,6 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.InitializingBean; -import org.springframework.beans.factory.SmartInitializingSingleton; import org.springframework.core.io.Resource; import org.springframework.core.io.support.ResourceRegion; import org.springframework.http.HttpHeaders; @@ -92,7 +90,7 @@ import org.springframework.web.servlet.support.WebContentGenerator; * @since 3.0.4 */ public class ResourceHttpRequestHandler extends WebContentGenerator - implements HttpRequestHandler, InitializingBean, SmartInitializingSingleton, CorsConfigurationSource { + implements HttpRequestHandler, InitializingBean, CorsConfigurationSource { // Servlet 3.1 setContentLengthLong(long) available? private static final boolean contentLengthLongAvailable = @@ -113,9 +111,7 @@ public class ResourceHttpRequestHandler extends WebContentGenerator private ContentNegotiationManager contentNegotiationManager; - private PathExtensionContentNegotiationStrategy pathExtensionStrategy; - - private ServletContext servletContext; + private PathExtensionContentNegotiationStrategy contentNegotiationStrategy; private CorsConfiguration corsConfiguration; @@ -190,7 +186,7 @@ public class ResourceHttpRequestHandler extends WebContentGenerator } /** - * Return the list of configured resource converters. + * Return the configured resource converter. * @since 4.3 */ public ResourceHttpMessageConverter getResourceHttpMessageConverter() { @@ -207,7 +203,7 @@ public class ResourceHttpRequestHandler extends WebContentGenerator } /** - * Return the list of configured resource region converters. + * Return the configured resource region converter. * @since 4.3 */ public ResourceRegionHttpMessageConverter getResourceRegionHttpMessageConverter() { @@ -249,11 +245,6 @@ public class ResourceHttpRequestHandler extends WebContentGenerator return this.corsConfiguration; } - @Override - protected void initServletContext(ServletContext servletContext) { - this.servletContext = servletContext; - } - @Override public void afterPropertiesSet() throws Exception { @@ -261,16 +252,20 @@ public class ResourceHttpRequestHandler extends WebContentGenerator logger.warn("Locations list is empty. No resources will be served unless a " + "custom ResourceResolver is configured as an alternative to PathResourceResolver."); } + if (this.resourceResolvers.isEmpty()) { this.resourceResolvers.add(new PathResourceResolver()); } initAllowedLocations(); + if (this.resourceHttpMessageConverter == null) { this.resourceHttpMessageConverter = new ResourceHttpMessageConverter(); } if (this.resourceRegionHttpMessageConverter == null) { this.resourceRegionHttpMessageConverter = new ResourceRegionHttpMessageConverter(); } + + this.contentNegotiationStrategy = initContentNegotiationStrategy(); } /** @@ -293,12 +288,13 @@ public class ResourceHttpRequestHandler extends WebContentGenerator } } - @Override - public void afterSingletonsInstantiated() { - this.pathExtensionStrategy = initPathExtensionStrategy(); - } - - protected PathExtensionContentNegotiationStrategy initPathExtensionStrategy() { + /** + * Initialize the content negotiation strategy depending on the {@code ContentNegotiationManager} + * setup and the availability of a {@code ServletContext}. + * @see ServletPathExtensionContentNegotiationStrategy + * @see PathExtensionContentNegotiationStrategy + */ + protected PathExtensionContentNegotiationStrategy initContentNegotiationStrategy() { Map mediaTypes = null; if (getContentNegotiationManager() != null) { PathExtensionContentNegotiationStrategy strategy = @@ -307,9 +303,9 @@ public class ResourceHttpRequestHandler extends WebContentGenerator mediaTypes = new HashMap(strategy.getMediaTypes()); } } - return (getServletContext() != null) ? + return (getServletContext() != null ? new ServletPathExtensionContentNegotiationStrategy(getServletContext(), mediaTypes) : - new PathExtensionContentNegotiationStrategy(mediaTypes); + new PathExtensionContentNegotiationStrategy(mediaTypes)); } @@ -528,7 +524,7 @@ public class ResourceHttpRequestHandler extends WebContentGenerator if (mediaType != null) { return mediaType; } - return this.pathExtensionStrategy.getMediaTypeForResource(resource); + return this.contentNegotiationStrategy.getMediaTypeForResource(resource); } /** 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 1a5b74f55b..e835aff8c1 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 @@ -81,7 +81,6 @@ public class ResourceHttpRequestHandlerTests { this.handler.setCacheSeconds(3600); this.handler.setServletContext(new TestServletContext()); this.handler.afterPropertiesSet(); - this.handler.afterSingletonsInstantiated(); this.request = new MockHttpServletRequest("GET", ""); this.response = new MockHttpServletResponse(); @@ -148,7 +147,6 @@ public class ResourceHttpRequestHandlerTests { .addFixedVersionStrategy("versionString", "/**"); this.handler.setResourceResolvers(Arrays.asList(versionResolver, new PathResourceResolver())); this.handler.afterPropertiesSet(); - this.handler.afterSingletonsInstantiated(); this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "versionString/foo.css"); this.handler.handleRequest(this.request, this.response); @@ -255,7 +253,6 @@ public class ResourceHttpRequestHandlerTests { handler.setLocations(paths); handler.setContentNegotiationManager(manager); handler.afterPropertiesSet(); - handler.afterSingletonsInstantiated(); this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css"); handler.handleRequest(this.request, this.response); @@ -277,7 +274,6 @@ public class ResourceHttpRequestHandlerTests { handler.setLocations(paths); handler.setContentNegotiationManager(manager); handler.afterPropertiesSet(); - handler.afterSingletonsInstantiated(); this.request.addHeader("Accept", "application/json,text/plain,*/*"); this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.html"); @@ -306,7 +302,6 @@ public class ResourceHttpRequestHandlerTests { handler.setServletContext(servletContext); handler.setLocations(paths); handler.afterPropertiesSet(); - handler.afterSingletonsInstantiated(); this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css"); handler.handleRequest(this.request, this.response); @@ -421,7 +416,6 @@ public class ResourceHttpRequestHandlerTests { handler.setServletContext(new MockServletContext()); handler.setLocations(Arrays.asList(location1, location2)); handler.afterPropertiesSet(); - handler.afterSingletonsInstantiated(); Resource[] locations = pathResolver.getAllowedLocations(); assertEquals(1, locations.length);