|
17 | 17 | package org.springframework.boot.autoconfigure.web.servlet;
|
18 | 18 |
|
19 | 19 | import java.time.Duration;
|
20 |
| -import java.util.HashSet; |
21 | 20 | import java.util.List;
|
22 | 21 | import java.util.ListIterator;
|
23 | 22 | import java.util.Locale;
|
24 | 23 | import java.util.Map;
|
25 |
| -import java.util.Set; |
| 24 | +import java.util.function.Consumer; |
26 | 25 |
|
27 | 26 | import javax.servlet.Servlet;
|
28 | 27 | import javax.servlet.ServletContext;
|
|
95 | 94 | import org.springframework.web.servlet.DispatcherServlet;
|
96 | 95 | import org.springframework.web.servlet.FlashMapManager;
|
97 | 96 | import org.springframework.web.servlet.HandlerExceptionResolver;
|
98 |
| -import org.springframework.web.servlet.HandlerMapping; |
99 | 97 | import org.springframework.web.servlet.LocaleResolver;
|
100 | 98 | import org.springframework.web.servlet.ThemeResolver;
|
101 | 99 | import org.springframework.web.servlet.View;
|
|
111 | 109 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
|
112 | 110 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
113 | 111 | import org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver;
|
114 |
| -import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping; |
115 | 112 | import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
|
116 | 113 | import org.springframework.web.servlet.i18n.FixedLocaleResolver;
|
117 | 114 | import org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver;
|
118 | 115 | import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
|
119 | 116 | import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
120 | 117 | import org.springframework.web.servlet.resource.EncodedResourceResolver;
|
121 |
| -import org.springframework.web.servlet.resource.ResourceHttpRequestHandler; |
122 | 118 | import org.springframework.web.servlet.resource.ResourceResolver;
|
123 | 119 | import org.springframework.web.servlet.resource.ResourceUrlProvider;
|
124 | 120 | import org.springframework.web.servlet.resource.VersionResourceResolver;
|
@@ -348,8 +344,6 @@ public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfigurat
|
348 | 344 |
|
349 | 345 | private ResourceLoader resourceLoader;
|
350 | 346 |
|
351 |
| - private final Set<String> autoConfiguredResourceHandlers = new HashSet<>(); |
352 |
| - |
353 | 347 | @SuppressWarnings("deprecation")
|
354 | 348 | public EnableWebMvcConfiguration(
|
355 | 349 | org.springframework.boot.autoconfigure.web.ResourceProperties resourceProperties,
|
@@ -402,59 +396,37 @@ public RequestMappingHandlerMapping requestMappingHandlerMapping(
|
402 | 396 | resourceUrlProvider);
|
403 | 397 | }
|
404 | 398 |
|
405 |
| - @Bean |
406 |
| - @Override |
407 |
| - public HandlerMapping resourceHandlerMapping(ContentNegotiationManager contentNegotiationManager, |
408 |
| - FormattingConversionService conversionService, ResourceUrlProvider resourceUrlProvider) { |
409 |
| - HandlerMapping mapping = super.resourceHandlerMapping(contentNegotiationManager, conversionService, |
410 |
| - resourceUrlProvider); |
411 |
| - if (mapping instanceof SimpleUrlHandlerMapping) { |
412 |
| - addServletContextResourceHandlerMapping((SimpleUrlHandlerMapping) mapping); |
413 |
| - } |
414 |
| - return mapping; |
415 |
| - } |
416 |
| - |
417 |
| - private void addServletContextResourceHandlerMapping(SimpleUrlHandlerMapping mapping) { |
418 |
| - Map<String, ?> urlMap = mapping.getUrlMap(); |
419 |
| - String pattern = this.mvcProperties.getStaticPathPattern(); |
420 |
| - Object handler = urlMap.get(pattern); |
421 |
| - if (handler instanceof ResourceHttpRequestHandler |
422 |
| - && this.autoConfiguredResourceHandlers.contains(pattern)) { |
423 |
| - addServletContextResourceHandlerMapping((ResourceHttpRequestHandler) handler); |
424 |
| - } |
425 |
| - } |
426 |
| - |
427 |
| - private void addServletContextResourceHandlerMapping(ResourceHttpRequestHandler handler) { |
428 |
| - ServletContext servletContext = getServletContext(); |
429 |
| - if (servletContext != null) { |
430 |
| - List<Resource> locations = handler.getLocations(); |
431 |
| - locations.add(new ServletContextResource(servletContext, SERVLET_LOCATION)); |
432 |
| - } |
433 |
| - } |
434 |
| - |
435 | 399 | @Override
|
436 | 400 | protected void addResourceHandlers(ResourceHandlerRegistry registry) {
|
437 | 401 | super.addResourceHandlers(registry);
|
438 | 402 | if (!this.resourceProperties.isAddMappings()) {
|
439 | 403 | logger.debug("Default resource handling disabled");
|
440 | 404 | return;
|
441 | 405 | }
|
| 406 | + ServletContext servletContext = getServletContext(); |
442 | 407 | addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
|
443 |
| - addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), |
444 |
| - this.resourceProperties.getStaticLocations()); |
445 |
| - |
| 408 | + addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> { |
| 409 | + registration.addResourceLocations(this.resourceProperties.getStaticLocations()); |
| 410 | + if (servletContext != null) { |
| 411 | + registration.addResourceLocations(new ServletContextResource(servletContext, SERVLET_LOCATION)); |
| 412 | + } |
| 413 | + }); |
446 | 414 | }
|
447 | 415 |
|
448 | 416 | private void addResourceHandler(ResourceHandlerRegistry registry, String pattern, String... locations) {
|
| 417 | + addResourceHandler(registry, pattern, (registration) -> registration.addResourceLocations(locations)); |
| 418 | + } |
| 419 | + |
| 420 | + private void addResourceHandler(ResourceHandlerRegistry registry, String pattern, |
| 421 | + Consumer<ResourceHandlerRegistration> customizer) { |
449 | 422 | if (registry.hasMappingForPattern(pattern)) {
|
450 | 423 | return;
|
451 | 424 | }
|
452 | 425 | ResourceHandlerRegistration registration = registry.addResourceHandler(pattern);
|
453 |
| - registration.addResourceLocations(locations); |
| 426 | + customizer.accept(registration); |
454 | 427 | registration.setCachePeriod(getSeconds(this.resourceProperties.getCache().getPeriod()));
|
455 | 428 | registration.setCacheControl(this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl());
|
456 | 429 | customizeResourceHandlerRegistration(registration);
|
457 |
| - this.autoConfiguredResourceHandlers.add(pattern); |
458 | 430 | }
|
459 | 431 |
|
460 | 432 | private Integer getSeconds(Duration cachePeriod) {
|
|
0 commit comments