Skip to content

Commit

Permalink
Relax ServletContext check for resource handling
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rstoyanchev committed Aug 30, 2016
1 parent 5075dd4 commit 815a3ad
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
Expand Up @@ -32,6 +32,7 @@
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;
Expand Down Expand Up @@ -91,7 +92,7 @@
* @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 =
Expand All @@ -112,7 +113,7 @@ public class ResourceHttpRequestHandler extends WebContentGenerator

private ContentNegotiationManager contentNegotiationManager;

private ServletPathExtensionContentNegotiationStrategy pathExtensionStrategy;
private PathExtensionContentNegotiationStrategy pathExtensionStrategy;

private ServletContext servletContext;

Expand Down Expand Up @@ -270,7 +271,6 @@ public void afterPropertiesSet() throws Exception {
if (this.resourceRegionHttpMessageConverter == null) {
this.resourceRegionHttpMessageConverter = new ResourceRegionHttpMessageConverter();
}
this.pathExtensionStrategy = initPathExtensionStrategy();
}

/**
Expand All @@ -293,7 +293,12 @@ protected void initAllowedLocations() {
}
}

protected ServletPathExtensionContentNegotiationStrategy initPathExtensionStrategy() {
@Override
public void afterSingletonsInstantiated() {
this.pathExtensionStrategy = initPathExtensionStrategy();
}

protected PathExtensionContentNegotiationStrategy initPathExtensionStrategy() {
Map<String, MediaType> mediaTypes = null;
if (getContentNegotiationManager() != null) {
PathExtensionContentNegotiationStrategy strategy =
Expand All @@ -302,7 +307,9 @@ protected ServletPathExtensionContentNegotiationStrategy initPathExtensionStrate
mediaTypes = new HashMap<String, MediaType>(strategy.getMediaTypes());
}
}
return new ServletPathExtensionContentNegotiationStrategy(this.servletContext, mediaTypes);
return (getServletContext() != null) ?
new ServletPathExtensionContentNegotiationStrategy(getServletContext(), mediaTypes) :
new PathExtensionContentNegotiationStrategy(mediaTypes);
}


Expand Down
Expand Up @@ -79,6 +79,8 @@ public void mapPathToLocation() throws Exception {
request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "/testStylesheet.css");

ResourceHttpRequestHandler handler = getHandler("/resources/**");
handler.afterPropertiesSet();
handler.afterSingletonsInstantiated();
handler.handleRequest(request, this.response);

assertEquals("test stylesheet content", this.response.getContentAsString());
Expand Down
Expand Up @@ -81,6 +81,7 @@ public void setUp() throws Exception {
this.handler.setCacheSeconds(3600);
this.handler.setServletContext(new TestServletContext());
this.handler.afterPropertiesSet();
this.handler.afterSingletonsInstantiated();

this.request = new MockHttpServletRequest("GET", "");
this.response = new MockHttpServletResponse();
Expand Down Expand Up @@ -147,6 +148,7 @@ public void getVersionedResource() throws Exception {
.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);
Expand Down Expand Up @@ -253,6 +255,7 @@ public void getResourceWithRegisteredMediaType() throws Exception {
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);
Expand All @@ -274,6 +277,7 @@ public void getMediaTypeWithFavorPathExtensionOff() throws Exception {
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");
Expand Down Expand Up @@ -302,6 +306,7 @@ public String getVirtualServerName() {
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);
Expand Down Expand Up @@ -416,6 +421,7 @@ public void initAllowedLocationsWithExplicitConfiguration() throws Exception {
handler.setServletContext(new MockServletContext());
handler.setLocations(Arrays.asList(location1, location2));
handler.afterPropertiesSet();
handler.afterSingletonsInstantiated();

Resource[] locations = pathResolver.getAllowedLocations();
assertEquals(1, locations.length);
Expand Down

0 comments on commit 815a3ad

Please sign in to comment.