22
22
import java .util .HashMap ;
23
23
import java .util .List ;
24
24
import java .util .Map ;
25
- import javax .servlet .ServletContext ;
26
25
import javax .servlet .ServletException ;
27
26
import javax .servlet .ServletResponse ;
28
27
import javax .servlet .http .HttpServletRequest ;
32
31
import org .apache .commons .logging .LogFactory ;
33
32
34
33
import org .springframework .beans .factory .InitializingBean ;
35
- import org .springframework .beans .factory .SmartInitializingSingleton ;
36
34
import org .springframework .core .io .Resource ;
37
35
import org .springframework .core .io .support .ResourceRegion ;
38
36
import org .springframework .http .HttpHeaders ;
92
90
* @since 3.0.4
93
91
*/
94
92
public class ResourceHttpRequestHandler extends WebContentGenerator
95
- implements HttpRequestHandler , InitializingBean , SmartInitializingSingleton , CorsConfigurationSource {
93
+ implements HttpRequestHandler , InitializingBean , CorsConfigurationSource {
96
94
97
95
// Servlet 3.1 setContentLengthLong(long) available?
98
96
private static final boolean contentLengthLongAvailable =
@@ -113,9 +111,7 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
113
111
114
112
private ContentNegotiationManager contentNegotiationManager ;
115
113
116
- private PathExtensionContentNegotiationStrategy pathExtensionStrategy ;
117
-
118
- private ServletContext servletContext ;
114
+ private PathExtensionContentNegotiationStrategy contentNegotiationStrategy ;
119
115
120
116
private CorsConfiguration corsConfiguration ;
121
117
@@ -190,7 +186,7 @@ public void setResourceHttpMessageConverter(ResourceHttpMessageConverter resourc
190
186
}
191
187
192
188
/**
193
- * Return the list of configured resource converters .
189
+ * Return the configured resource converter .
194
190
* @since 4.3
195
191
*/
196
192
public ResourceHttpMessageConverter getResourceHttpMessageConverter () {
@@ -207,7 +203,7 @@ public void setResourceRegionHttpMessageConverter(ResourceRegionHttpMessageConve
207
203
}
208
204
209
205
/**
210
- * Return the list of configured resource region converters .
206
+ * Return the configured resource region converter .
211
207
* @since 4.3
212
208
*/
213
209
public ResourceRegionHttpMessageConverter getResourceRegionHttpMessageConverter () {
@@ -249,28 +245,27 @@ public CorsConfiguration getCorsConfiguration(HttpServletRequest request) {
249
245
return this .corsConfiguration ;
250
246
}
251
247
252
- @ Override
253
- protected void initServletContext (ServletContext servletContext ) {
254
- this .servletContext = servletContext ;
255
- }
256
-
257
248
258
249
@ Override
259
250
public void afterPropertiesSet () throws Exception {
260
251
if (logger .isWarnEnabled () && CollectionUtils .isEmpty (this .locations )) {
261
252
logger .warn ("Locations list is empty. No resources will be served unless a " +
262
253
"custom ResourceResolver is configured as an alternative to PathResourceResolver." );
263
254
}
255
+
264
256
if (this .resourceResolvers .isEmpty ()) {
265
257
this .resourceResolvers .add (new PathResourceResolver ());
266
258
}
267
259
initAllowedLocations ();
260
+
268
261
if (this .resourceHttpMessageConverter == null ) {
269
262
this .resourceHttpMessageConverter = new ResourceHttpMessageConverter ();
270
263
}
271
264
if (this .resourceRegionHttpMessageConverter == null ) {
272
265
this .resourceRegionHttpMessageConverter = new ResourceRegionHttpMessageConverter ();
273
266
}
267
+
268
+ this .contentNegotiationStrategy = initContentNegotiationStrategy ();
274
269
}
275
270
276
271
/**
@@ -293,12 +288,13 @@ protected void initAllowedLocations() {
293
288
}
294
289
}
295
290
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 () {
302
298
Map <String , MediaType > mediaTypes = null ;
303
299
if (getContentNegotiationManager () != null ) {
304
300
PathExtensionContentNegotiationStrategy strategy =
@@ -307,9 +303,9 @@ protected PathExtensionContentNegotiationStrategy initPathExtensionStrategy() {
307
303
mediaTypes = new HashMap <String , MediaType >(strategy .getMediaTypes ());
308
304
}
309
305
}
310
- return (getServletContext () != null ) ?
306
+ return (getServletContext () != null ?
311
307
new ServletPathExtensionContentNegotiationStrategy (getServletContext (), mediaTypes ) :
312
- new PathExtensionContentNegotiationStrategy (mediaTypes );
308
+ new PathExtensionContentNegotiationStrategy (mediaTypes )) ;
313
309
}
314
310
315
311
@@ -528,7 +524,7 @@ protected MediaType getMediaType(HttpServletRequest request, Resource resource)
528
524
if (mediaType != null ) {
529
525
return mediaType ;
530
526
}
531
- return this .pathExtensionStrategy .getMediaTypeForResource (resource );
527
+ return this .contentNegotiationStrategy .getMediaTypeForResource (resource );
532
528
}
533
529
534
530
/**
0 commit comments