Relax ServletContext check for resource handling

This is a follow-up on commit fe4046 relaxing the expectation that a
ServletContext is present. Instead we check defensively and fall back
on PathExtensionContentNegotiationStrategy which can use JAF.

Issue: SPR-14577
This commit is contained in:
Rossen Stoyanchev
2016-08-30 12:57:35 -04:00
parent 5075dd4dfa
commit 815a3ad0de
3 changed files with 20 additions and 5 deletions

View File

@@ -32,6 +32,7 @@ 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;
@@ -91,7 +92,7 @@ import org.springframework.web.servlet.support.WebContentGenerator;
* @since 3.0.4
*/
public class ResourceHttpRequestHandler extends WebContentGenerator
implements HttpRequestHandler, InitializingBean, CorsConfigurationSource {
implements HttpRequestHandler, InitializingBean, SmartInitializingSingleton, CorsConfigurationSource {
// Servlet 3.1 setContentLengthLong(long) available?
private static final boolean contentLengthLongAvailable =
@@ -112,7 +113,7 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
private ContentNegotiationManager contentNegotiationManager;
private ServletPathExtensionContentNegotiationStrategy pathExtensionStrategy;
private PathExtensionContentNegotiationStrategy pathExtensionStrategy;
private ServletContext servletContext;
@@ -270,7 +271,6 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
if (this.resourceRegionHttpMessageConverter == null) {
this.resourceRegionHttpMessageConverter = new ResourceRegionHttpMessageConverter();
}
this.pathExtensionStrategy = initPathExtensionStrategy();
}
/**
@@ -293,7 +293,12 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
}
}
protected ServletPathExtensionContentNegotiationStrategy initPathExtensionStrategy() {
@Override
public void afterSingletonsInstantiated() {
this.pathExtensionStrategy = initPathExtensionStrategy();
}
protected PathExtensionContentNegotiationStrategy initPathExtensionStrategy() {
Map<String, MediaType> mediaTypes = null;
if (getContentNegotiationManager() != null) {
PathExtensionContentNegotiationStrategy strategy =
@@ -302,7 +307,9 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
mediaTypes = new HashMap<String, MediaType>(strategy.getMediaTypes());
}
}
return new ServletPathExtensionContentNegotiationStrategy(this.servletContext, mediaTypes);
return (getServletContext() != null) ?
new ServletPathExtensionContentNegotiationStrategy(getServletContext(), mediaTypes) :
new PathExtensionContentNegotiationStrategy(mediaTypes);
}