|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2021 the original author or authors. |
| 2 | + * Copyright 2012-2023 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
110 | 110 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
|
111 | 111 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
112 | 112 | import org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver;
|
| 113 | +import org.springframework.web.servlet.handler.AbstractUrlHandlerMapping; |
113 | 114 | import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
|
114 | 115 | import org.springframework.web.servlet.i18n.FixedLocaleResolver;
|
115 | 116 | import org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver;
|
@@ -453,12 +454,29 @@ public RequestMappingHandlerMapping requestMappingHandlerMapping(
|
453 | 454 | @Bean
|
454 | 455 | public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext,
|
455 | 456 | FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {
|
456 |
| - WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping( |
457 |
| - new TemplateAvailabilityProviders(applicationContext), applicationContext, getWelcomePage(), |
458 |
| - this.mvcProperties.getStaticPathPattern()); |
459 |
| - welcomePageHandlerMapping.setInterceptors(getInterceptors(mvcConversionService, mvcResourceUrlProvider)); |
460 |
| - welcomePageHandlerMapping.setCorsConfigurations(getCorsConfigurations()); |
461 |
| - return welcomePageHandlerMapping; |
| 457 | + return createWelcomePageHandlerMapping(applicationContext, mvcConversionService, mvcResourceUrlProvider, |
| 458 | + WelcomePageHandlerMapping::new); |
| 459 | + } |
| 460 | + |
| 461 | + @Bean |
| 462 | + public WelcomePageNotAcceptableHandlerMapping welcomePageNotAcceptableHandlerMapping( |
| 463 | + ApplicationContext applicationContext, FormattingConversionService mvcConversionService, |
| 464 | + ResourceUrlProvider mvcResourceUrlProvider) { |
| 465 | + return createWelcomePageHandlerMapping(applicationContext, mvcConversionService, mvcResourceUrlProvider, |
| 466 | + WelcomePageNotAcceptableHandlerMapping::new); |
| 467 | + } |
| 468 | + |
| 469 | + private <T extends AbstractUrlHandlerMapping> T createWelcomePageHandlerMapping( |
| 470 | + ApplicationContext applicationContext, FormattingConversionService mvcConversionService, |
| 471 | + ResourceUrlProvider mvcResourceUrlProvider, WelcomePageHandlerMappingFactory<T> factory) { |
| 472 | + TemplateAvailabilityProviders templateAvailabilityProviders = new TemplateAvailabilityProviders( |
| 473 | + applicationContext); |
| 474 | + String staticPathPattern = this.mvcProperties.getStaticPathPattern(); |
| 475 | + T handlerMapping = factory.create(templateAvailabilityProviders, applicationContext, getIndexHtmlResource(), |
| 476 | + staticPathPattern); |
| 477 | + handlerMapping.setInterceptors(getInterceptors(mvcConversionService, mvcResourceUrlProvider)); |
| 478 | + handlerMapping.setCorsConfigurations(getCorsConfigurations()); |
| 479 | + return handlerMapping; |
462 | 480 | }
|
463 | 481 |
|
464 | 482 | @Override
|
@@ -493,25 +511,25 @@ public FlashMapManager flashMapManager() {
|
493 | 511 | return super.flashMapManager();
|
494 | 512 | }
|
495 | 513 |
|
496 |
| - private Resource getWelcomePage() { |
| 514 | + private Resource getIndexHtmlResource() { |
497 | 515 | for (String location : this.resourceProperties.getStaticLocations()) {
|
498 |
| - Resource indexHtml = getIndexHtml(location); |
| 516 | + Resource indexHtml = getIndexHtmlResource(location); |
499 | 517 | if (indexHtml != null) {
|
500 | 518 | return indexHtml;
|
501 | 519 | }
|
502 | 520 | }
|
503 | 521 | ServletContext servletContext = getServletContext();
|
504 | 522 | if (servletContext != null) {
|
505 |
| - return getIndexHtml(new ServletContextResource(servletContext, SERVLET_LOCATION)); |
| 523 | + return getIndexHtmlResource(new ServletContextResource(servletContext, SERVLET_LOCATION)); |
506 | 524 | }
|
507 | 525 | return null;
|
508 | 526 | }
|
509 | 527 |
|
510 |
| - private Resource getIndexHtml(String location) { |
511 |
| - return getIndexHtml(this.resourceLoader.getResource(location)); |
| 528 | + private Resource getIndexHtmlResource(String location) { |
| 529 | + return getIndexHtmlResource(this.resourceLoader.getResource(location)); |
512 | 530 | }
|
513 | 531 |
|
514 |
| - private Resource getIndexHtml(Resource location) { |
| 532 | + private Resource getIndexHtmlResource(Resource location) { |
515 | 533 | try {
|
516 | 534 | Resource resource = location.createRelative("index.html");
|
517 | 535 | if (resource.exists() && (resource.getURL() != null)) {
|
@@ -626,6 +644,15 @@ ResourceChainResourceHandlerRegistrationCustomizer resourceHandlerRegistrationCu
|
626 | 644 |
|
627 | 645 | }
|
628 | 646 |
|
| 647 | + @FunctionalInterface |
| 648 | + interface WelcomePageHandlerMappingFactory<T extends AbstractUrlHandlerMapping> { |
| 649 | + |
| 650 | + T create(TemplateAvailabilityProviders templateAvailabilityProviders, ApplicationContext applicationContext, |
| 651 | + Resource indexHtmlResource, String staticPathPattern); |
| 652 | + |
| 653 | + } |
| 654 | + |
| 655 | + @FunctionalInterface |
629 | 656 | interface ResourceHandlerRegistrationCustomizer {
|
630 | 657 |
|
631 | 658 | void customize(ResourceHandlerRegistration registration);
|
|
0 commit comments