Skip to content

Commit 05ff83f

Browse files
committed
ResourceHttpRequestHandler initializes PathExtensionContentNegotiationStrategy in afterPropertiesSet
Issue: SPR-14851 (cherry picked from commit b7d3a96)
1 parent 1e3012c commit 05ff83f

File tree

3 files changed

+19
-30
lines changed

3 files changed

+19
-30
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistry.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,8 @@ protected AbstractHandlerMapping getHandlerMapping() {
145145
handler.setContentNegotiationManager(this.contentNegotiationManager);
146146
try {
147147
handler.afterPropertiesSet();
148-
handler.afterSingletonsInstantiated();
149148
}
150-
catch (Exception ex) {
149+
catch (Throwable ex) {
151150
throw new BeanInitializationException("Failed to init ResourceHttpRequestHandler", ex);
152151
}
153152
urlMap.put(pathPattern, handler);

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

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.HashMap;
2323
import java.util.List;
2424
import java.util.Map;
25-
import javax.servlet.ServletContext;
2625
import javax.servlet.ServletException;
2726
import javax.servlet.ServletResponse;
2827
import javax.servlet.http.HttpServletRequest;
@@ -32,7 +31,6 @@
3231
import org.apache.commons.logging.LogFactory;
3332

3433
import org.springframework.beans.factory.InitializingBean;
35-
import org.springframework.beans.factory.SmartInitializingSingleton;
3634
import org.springframework.core.io.Resource;
3735
import org.springframework.core.io.support.ResourceRegion;
3836
import org.springframework.http.HttpHeaders;
@@ -92,7 +90,7 @@
9290
* @since 3.0.4
9391
*/
9492
public class ResourceHttpRequestHandler extends WebContentGenerator
95-
implements HttpRequestHandler, InitializingBean, SmartInitializingSingleton, CorsConfigurationSource {
93+
implements HttpRequestHandler, InitializingBean, CorsConfigurationSource {
9694

9795
// Servlet 3.1 setContentLengthLong(long) available?
9896
private static final boolean contentLengthLongAvailable =
@@ -113,9 +111,7 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
113111

114112
private ContentNegotiationManager contentNegotiationManager;
115113

116-
private PathExtensionContentNegotiationStrategy pathExtensionStrategy;
117-
118-
private ServletContext servletContext;
114+
private PathExtensionContentNegotiationStrategy contentNegotiationStrategy;
119115

120116
private CorsConfiguration corsConfiguration;
121117

@@ -190,7 +186,7 @@ public void setResourceHttpMessageConverter(ResourceHttpMessageConverter resourc
190186
}
191187

192188
/**
193-
* Return the list of configured resource converters.
189+
* Return the configured resource converter.
194190
* @since 4.3
195191
*/
196192
public ResourceHttpMessageConverter getResourceHttpMessageConverter() {
@@ -207,7 +203,7 @@ public void setResourceRegionHttpMessageConverter(ResourceRegionHttpMessageConve
207203
}
208204

209205
/**
210-
* Return the list of configured resource region converters.
206+
* Return the configured resource region converter.
211207
* @since 4.3
212208
*/
213209
public ResourceRegionHttpMessageConverter getResourceRegionHttpMessageConverter() {
@@ -249,28 +245,27 @@ public CorsConfiguration getCorsConfiguration(HttpServletRequest request) {
249245
return this.corsConfiguration;
250246
}
251247

252-
@Override
253-
protected void initServletContext(ServletContext servletContext) {
254-
this.servletContext = servletContext;
255-
}
256-
257248

258249
@Override
259250
public void afterPropertiesSet() throws Exception {
260251
if (logger.isWarnEnabled() && CollectionUtils.isEmpty(this.locations)) {
261252
logger.warn("Locations list is empty. No resources will be served unless a " +
262253
"custom ResourceResolver is configured as an alternative to PathResourceResolver.");
263254
}
255+
264256
if (this.resourceResolvers.isEmpty()) {
265257
this.resourceResolvers.add(new PathResourceResolver());
266258
}
267259
initAllowedLocations();
260+
268261
if (this.resourceHttpMessageConverter == null) {
269262
this.resourceHttpMessageConverter = new ResourceHttpMessageConverter();
270263
}
271264
if (this.resourceRegionHttpMessageConverter == null) {
272265
this.resourceRegionHttpMessageConverter = new ResourceRegionHttpMessageConverter();
273266
}
267+
268+
this.contentNegotiationStrategy = initContentNegotiationStrategy();
274269
}
275270

276271
/**
@@ -293,12 +288,13 @@ protected void initAllowedLocations() {
293288
}
294289
}
295290

296-
@Override
297-
public void afterSingletonsInstantiated() {
298-
this.pathExtensionStrategy = initPathExtensionStrategy();
299-
}
300-
301-
protected PathExtensionContentNegotiationStrategy initPathExtensionStrategy() {
291+
/**
292+
* Initialize the content negotiation strategy depending on the {@code ContentNegotiationManager}
293+
* setup and the availability of a {@code ServletContext}.
294+
* @see ServletPathExtensionContentNegotiationStrategy
295+
* @see PathExtensionContentNegotiationStrategy
296+
*/
297+
protected PathExtensionContentNegotiationStrategy initContentNegotiationStrategy() {
302298
Map<String, MediaType> mediaTypes = null;
303299
if (getContentNegotiationManager() != null) {
304300
PathExtensionContentNegotiationStrategy strategy =
@@ -307,9 +303,9 @@ protected PathExtensionContentNegotiationStrategy initPathExtensionStrategy() {
307303
mediaTypes = new HashMap<String, MediaType>(strategy.getMediaTypes());
308304
}
309305
}
310-
return (getServletContext() != null) ?
306+
return (getServletContext() != null ?
311307
new ServletPathExtensionContentNegotiationStrategy(getServletContext(), mediaTypes) :
312-
new PathExtensionContentNegotiationStrategy(mediaTypes);
308+
new PathExtensionContentNegotiationStrategy(mediaTypes));
313309
}
314310

315311

@@ -528,7 +524,7 @@ protected MediaType getMediaType(HttpServletRequest request, Resource resource)
528524
if (mediaType != null) {
529525
return mediaType;
530526
}
531-
return this.pathExtensionStrategy.getMediaTypeForResource(resource);
527+
return this.contentNegotiationStrategy.getMediaTypeForResource(resource);
532528
}
533529

534530
/**

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

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

8685
this.request = new MockHttpServletRequest("GET", "");
8786
this.response = new MockHttpServletResponse();
@@ -148,7 +147,6 @@ public void getVersionedResource() throws Exception {
148147
.addFixedVersionStrategy("versionString", "/**");
149148
this.handler.setResourceResolvers(Arrays.asList(versionResolver, new PathResourceResolver()));
150149
this.handler.afterPropertiesSet();
151-
this.handler.afterSingletonsInstantiated();
152150

153151
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "versionString/foo.css");
154152
this.handler.handleRequest(this.request, this.response);
@@ -255,7 +253,6 @@ public void getResourceWithRegisteredMediaType() throws Exception {
255253
handler.setLocations(paths);
256254
handler.setContentNegotiationManager(manager);
257255
handler.afterPropertiesSet();
258-
handler.afterSingletonsInstantiated();
259256

260257
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css");
261258
handler.handleRequest(this.request, this.response);
@@ -277,7 +274,6 @@ public void getMediaTypeWithFavorPathExtensionOff() throws Exception {
277274
handler.setLocations(paths);
278275
handler.setContentNegotiationManager(manager);
279276
handler.afterPropertiesSet();
280-
handler.afterSingletonsInstantiated();
281277

282278
this.request.addHeader("Accept", "application/json,text/plain,*/*");
283279
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.html");
@@ -306,7 +302,6 @@ public String getVirtualServerName() {
306302
handler.setServletContext(servletContext);
307303
handler.setLocations(paths);
308304
handler.afterPropertiesSet();
309-
handler.afterSingletonsInstantiated();
310305

311306
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css");
312307
handler.handleRequest(this.request, this.response);
@@ -421,7 +416,6 @@ public void initAllowedLocationsWithExplicitConfiguration() throws Exception {
421416
handler.setServletContext(new MockServletContext());
422417
handler.setLocations(Arrays.asList(location1, location2));
423418
handler.afterPropertiesSet();
424-
handler.afterSingletonsInstantiated();
425419

426420
Resource[] locations = pathResolver.getAllowedLocations();
427421
assertEquals(1, locations.length);

0 commit comments

Comments
 (0)