ResourceHttpRequestHandler initializes PathExtensionContentNegotiationStrategy in afterPropertiesSet

Issue: SPR-14851
This commit is contained in:
Juergen Hoeller 2016-10-28 15:13:37 +02:00
parent dbaafdd04e
commit b7d3a969a4
3 changed files with 17 additions and 20 deletions

View File

@ -145,9 +145,8 @@ public class ResourceHandlerRegistry {
handler.setContentNegotiationManager(this.contentNegotiationManager); handler.setContentNegotiationManager(this.contentNegotiationManager);
try { try {
handler.afterPropertiesSet(); handler.afterPropertiesSet();
handler.afterSingletonsInstantiated();
} }
catch (Exception ex) { catch (Throwable ex) {
throw new BeanInitializationException("Failed to init ResourceHttpRequestHandler", ex); throw new BeanInitializationException("Failed to init ResourceHttpRequestHandler", ex);
} }
urlMap.put(pathPattern, handler); urlMap.put(pathPattern, handler);

View File

@ -31,7 +31,6 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.core.io.support.ResourceRegion; import org.springframework.core.io.support.ResourceRegion;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
@ -91,7 +90,7 @@ import org.springframework.web.servlet.support.WebContentGenerator;
* @since 3.0.4 * @since 3.0.4
*/ */
public class ResourceHttpRequestHandler extends WebContentGenerator public class ResourceHttpRequestHandler extends WebContentGenerator
implements HttpRequestHandler, InitializingBean, SmartInitializingSingleton, CorsConfigurationSource { implements HttpRequestHandler, InitializingBean, CorsConfigurationSource {
// Servlet 3.1 setContentLengthLong(long) available? // Servlet 3.1 setContentLengthLong(long) available?
private static final boolean contentLengthLongAvailable = private static final boolean contentLengthLongAvailable =
@ -112,7 +111,7 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
private ContentNegotiationManager contentNegotiationManager; private ContentNegotiationManager contentNegotiationManager;
private PathExtensionContentNegotiationStrategy pathExtensionStrategy; private PathExtensionContentNegotiationStrategy contentNegotiationStrategy;
private CorsConfiguration corsConfiguration; private CorsConfiguration corsConfiguration;
@ -253,16 +252,20 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
logger.warn("Locations list is empty. No resources will be served unless a " + logger.warn("Locations list is empty. No resources will be served unless a " +
"custom ResourceResolver is configured as an alternative to PathResourceResolver."); "custom ResourceResolver is configured as an alternative to PathResourceResolver.");
} }
if (this.resourceResolvers.isEmpty()) { if (this.resourceResolvers.isEmpty()) {
this.resourceResolvers.add(new PathResourceResolver()); this.resourceResolvers.add(new PathResourceResolver());
} }
initAllowedLocations(); initAllowedLocations();
if (this.resourceHttpMessageConverter == null) { if (this.resourceHttpMessageConverter == null) {
this.resourceHttpMessageConverter = new ResourceHttpMessageConverter(); this.resourceHttpMessageConverter = new ResourceHttpMessageConverter();
} }
if (this.resourceRegionHttpMessageConverter == null) { if (this.resourceRegionHttpMessageConverter == null) {
this.resourceRegionHttpMessageConverter = new ResourceRegionHttpMessageConverter(); this.resourceRegionHttpMessageConverter = new ResourceRegionHttpMessageConverter();
} }
this.contentNegotiationStrategy = initContentNegotiationStrategy();
} }
/** /**
@ -285,11 +288,12 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
} }
} }
@Override /**
public void afterSingletonsInstantiated() { * Initialize the content negotiation strategy depending on the {@code ContentNegotiationManager}
this.pathExtensionStrategy = initContentNegotiationStrategy(); * setup and the availability of a {@code ServletContext}.
} * @see ServletPathExtensionContentNegotiationStrategy
* @see PathExtensionContentNegotiationStrategy
*/
protected PathExtensionContentNegotiationStrategy initContentNegotiationStrategy() { protected PathExtensionContentNegotiationStrategy initContentNegotiationStrategy() {
Map<String, MediaType> mediaTypes = null; Map<String, MediaType> mediaTypes = null;
if (getContentNegotiationManager() != null) { if (getContentNegotiationManager() != null) {
@ -299,9 +303,9 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
mediaTypes = new HashMap<>(strategy.getMediaTypes()); mediaTypes = new HashMap<>(strategy.getMediaTypes());
} }
} }
return (getServletContext() != null) ? return (getServletContext() != null ?
new ServletPathExtensionContentNegotiationStrategy(getServletContext(), mediaTypes) : new ServletPathExtensionContentNegotiationStrategy(getServletContext(), mediaTypes) :
new PathExtensionContentNegotiationStrategy(mediaTypes); new PathExtensionContentNegotiationStrategy(mediaTypes));
} }
@ -514,7 +518,7 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
* @return the corresponding media type, or {@code null} if none found * @return the corresponding media type, or {@code null} if none found
*/ */
protected MediaType getMediaType(HttpServletRequest request, Resource resource) { protected MediaType getMediaType(HttpServletRequest request, Resource resource) {
return this.pathExtensionStrategy.getMediaTypeForResource(resource); return this.contentNegotiationStrategy.getMediaTypeForResource(resource);
} }
/** /**

View File

@ -81,7 +81,6 @@ public class ResourceHttpRequestHandlerTests {
this.handler.setCacheSeconds(3600); this.handler.setCacheSeconds(3600);
this.handler.setServletContext(new TestServletContext()); this.handler.setServletContext(new TestServletContext());
this.handler.afterPropertiesSet(); this.handler.afterPropertiesSet();
this.handler.afterSingletonsInstantiated();
this.request = new MockHttpServletRequest("GET", ""); this.request = new MockHttpServletRequest("GET", "");
this.response = new MockHttpServletResponse(); this.response = new MockHttpServletResponse();
@ -148,7 +147,6 @@ public class ResourceHttpRequestHandlerTests {
.addFixedVersionStrategy("versionString", "/**"); .addFixedVersionStrategy("versionString", "/**");
this.handler.setResourceResolvers(Arrays.asList(versionResolver, new PathResourceResolver())); this.handler.setResourceResolvers(Arrays.asList(versionResolver, new PathResourceResolver()));
this.handler.afterPropertiesSet(); this.handler.afterPropertiesSet();
this.handler.afterSingletonsInstantiated();
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "versionString/foo.css"); this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "versionString/foo.css");
this.handler.handleRequest(this.request, this.response); this.handler.handleRequest(this.request, this.response);
@ -255,7 +253,6 @@ public class ResourceHttpRequestHandlerTests {
handler.setLocations(paths); handler.setLocations(paths);
handler.setContentNegotiationManager(manager); handler.setContentNegotiationManager(manager);
handler.afterPropertiesSet(); handler.afterPropertiesSet();
handler.afterSingletonsInstantiated();
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css"); this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css");
handler.handleRequest(this.request, this.response); handler.handleRequest(this.request, this.response);
@ -277,7 +274,6 @@ public class ResourceHttpRequestHandlerTests {
handler.setLocations(paths); handler.setLocations(paths);
handler.setContentNegotiationManager(manager); handler.setContentNegotiationManager(manager);
handler.afterPropertiesSet(); handler.afterPropertiesSet();
handler.afterSingletonsInstantiated();
this.request.addHeader("Accept", "application/json,text/plain,*/*"); this.request.addHeader("Accept", "application/json,text/plain,*/*");
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.html"); this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.html");
@ -306,7 +302,6 @@ public class ResourceHttpRequestHandlerTests {
handler.setServletContext(servletContext); handler.setServletContext(servletContext);
handler.setLocations(paths); handler.setLocations(paths);
handler.afterPropertiesSet(); handler.afterPropertiesSet();
handler.afterSingletonsInstantiated();
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css"); this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css");
handler.handleRequest(this.request, this.response); handler.handleRequest(this.request, this.response);
@ -421,7 +416,6 @@ public class ResourceHttpRequestHandlerTests {
handler.setServletContext(new MockServletContext()); handler.setServletContext(new MockServletContext());
handler.setLocations(Arrays.asList(location1, location2)); handler.setLocations(Arrays.asList(location1, location2));
handler.afterPropertiesSet(); handler.afterPropertiesSet();
handler.afterSingletonsInstantiated();
Resource[] locations = pathResolver.getAllowedLocations(); Resource[] locations = pathResolver.getAllowedLocations();
assertEquals(1, locations.length); assertEquals(1, locations.length);