Skip to content

Commit 815a3ad

Browse files
committed
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
1 parent 5075dd4 commit 815a3ad

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.apache.commons.logging.LogFactory;
3333

3434
import org.springframework.beans.factory.InitializingBean;
35+
import org.springframework.beans.factory.SmartInitializingSingleton;
3536
import org.springframework.core.io.Resource;
3637
import org.springframework.core.io.support.ResourceRegion;
3738
import org.springframework.http.HttpHeaders;
@@ -91,7 +92,7 @@
9192
* @since 3.0.4
9293
*/
9394
public class ResourceHttpRequestHandler extends WebContentGenerator
94-
implements HttpRequestHandler, InitializingBean, CorsConfigurationSource {
95+
implements HttpRequestHandler, InitializingBean, SmartInitializingSingleton, CorsConfigurationSource {
9596

9697
// Servlet 3.1 setContentLengthLong(long) available?
9798
private static final boolean contentLengthLongAvailable =
@@ -112,7 +113,7 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
112113

113114
private ContentNegotiationManager contentNegotiationManager;
114115

115-
private ServletPathExtensionContentNegotiationStrategy pathExtensionStrategy;
116+
private PathExtensionContentNegotiationStrategy pathExtensionStrategy;
116117

117118
private ServletContext servletContext;
118119

@@ -270,7 +271,6 @@ public void afterPropertiesSet() throws Exception {
270271
if (this.resourceRegionHttpMessageConverter == null) {
271272
this.resourceRegionHttpMessageConverter = new ResourceRegionHttpMessageConverter();
272273
}
273-
this.pathExtensionStrategy = initPathExtensionStrategy();
274274
}
275275

276276
/**
@@ -293,7 +293,12 @@ protected void initAllowedLocations() {
293293
}
294294
}
295295

296-
protected ServletPathExtensionContentNegotiationStrategy initPathExtensionStrategy() {
296+
@Override
297+
public void afterSingletonsInstantiated() {
298+
this.pathExtensionStrategy = initPathExtensionStrategy();
299+
}
300+
301+
protected PathExtensionContentNegotiationStrategy initPathExtensionStrategy() {
297302
Map<String, MediaType> mediaTypes = null;
298303
if (getContentNegotiationManager() != null) {
299304
PathExtensionContentNegotiationStrategy strategy =
@@ -302,7 +307,9 @@ protected ServletPathExtensionContentNegotiationStrategy initPathExtensionStrate
302307
mediaTypes = new HashMap<String, MediaType>(strategy.getMediaTypes());
303308
}
304309
}
305-
return new ServletPathExtensionContentNegotiationStrategy(this.servletContext, mediaTypes);
310+
return (getServletContext() != null) ?
311+
new ServletPathExtensionContentNegotiationStrategy(getServletContext(), mediaTypes) :
312+
new PathExtensionContentNegotiationStrategy(mediaTypes);
306313
}
307314

308315

spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistryTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ public void mapPathToLocation() throws Exception {
7979
request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "/testStylesheet.css");
8080

8181
ResourceHttpRequestHandler handler = getHandler("/resources/**");
82+
handler.afterPropertiesSet();
83+
handler.afterSingletonsInstantiated();
8284
handler.handleRequest(request, this.response);
8385

8486
assertEquals("test stylesheet content", this.response.getContentAsString());

spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public void setUp() throws Exception {
8181
this.handler.setCacheSeconds(3600);
8282
this.handler.setServletContext(new TestServletContext());
8383
this.handler.afterPropertiesSet();
84+
this.handler.afterSingletonsInstantiated();
8485

8586
this.request = new MockHttpServletRequest("GET", "");
8687
this.response = new MockHttpServletResponse();
@@ -147,6 +148,7 @@ public void getVersionedResource() throws Exception {
147148
.addFixedVersionStrategy("versionString", "/**");
148149
this.handler.setResourceResolvers(Arrays.asList(versionResolver, new PathResourceResolver()));
149150
this.handler.afterPropertiesSet();
151+
this.handler.afterSingletonsInstantiated();
150152

151153
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "versionString/foo.css");
152154
this.handler.handleRequest(this.request, this.response);
@@ -253,6 +255,7 @@ public void getResourceWithRegisteredMediaType() throws Exception {
253255
handler.setLocations(paths);
254256
handler.setContentNegotiationManager(manager);
255257
handler.afterPropertiesSet();
258+
handler.afterSingletonsInstantiated();
256259

257260
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css");
258261
handler.handleRequest(this.request, this.response);
@@ -274,6 +277,7 @@ public void getMediaTypeWithFavorPathExtensionOff() throws Exception {
274277
handler.setLocations(paths);
275278
handler.setContentNegotiationManager(manager);
276279
handler.afterPropertiesSet();
280+
handler.afterSingletonsInstantiated();
277281

278282
this.request.addHeader("Accept", "application/json,text/plain,*/*");
279283
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.html");
@@ -302,6 +306,7 @@ public String getVirtualServerName() {
302306
handler.setServletContext(servletContext);
303307
handler.setLocations(paths);
304308
handler.afterPropertiesSet();
309+
handler.afterSingletonsInstantiated();
305310

306311
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css");
307312
handler.handleRequest(this.request, this.response);
@@ -416,6 +421,7 @@ public void initAllowedLocationsWithExplicitConfiguration() throws Exception {
416421
handler.setServletContext(new MockServletContext());
417422
handler.setLocations(Arrays.asList(location1, location2));
418423
handler.afterPropertiesSet();
424+
handler.afterSingletonsInstantiated();
419425

420426
Resource[] locations = pathResolver.getAllowedLocations();
421427
assertEquals(1, locations.length);

0 commit comments

Comments
 (0)