ResourceHttpRequestHandler initializes PathExtensionContentNegotiationStrategy in afterPropertiesSet

Issue: SPR-14851
(cherry picked from commit b7d3a96)
This commit is contained in:
Juergen Hoeller
2016-10-28 15:13:37 +02:00
parent 1e3012cb49
commit 05ff83f95a
3 changed files with 19 additions and 30 deletions

View File

@@ -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);

View File

@@ -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<String, MediaType> mediaTypes = null;
if (getContentNegotiationManager() != null) {
PathExtensionContentNegotiationStrategy strategy =
@@ -307,9 +303,9 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
mediaTypes = new HashMap<String, MediaType>(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);
}
/**

View File

@@ -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);