Skip to content

Commit af9cd8c

Browse files
committed
Merge branch '2.7.x' into 3.0.x
Closes gh-35553
2 parents b6e56c8 + 418dd1b commit af9cd8c

File tree

8 files changed

+356
-42
lines changed

8 files changed

+356
-42
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.java

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
110110
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
111111
import org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver;
112+
import org.springframework.web.servlet.handler.AbstractUrlHandlerMapping;
112113
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
113114
import org.springframework.web.servlet.i18n.FixedLocaleResolver;
114115
import org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver;
@@ -434,12 +435,29 @@ protected RequestMappingHandlerAdapter createRequestMappingHandlerAdapter() {
434435
@Bean
435436
public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext,
436437
FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {
437-
WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(
438-
new TemplateAvailabilityProviders(applicationContext), applicationContext, getWelcomePage(),
439-
this.mvcProperties.getStaticPathPattern());
440-
welcomePageHandlerMapping.setInterceptors(getInterceptors(mvcConversionService, mvcResourceUrlProvider));
441-
welcomePageHandlerMapping.setCorsConfigurations(getCorsConfigurations());
442-
return welcomePageHandlerMapping;
438+
return createWelcomePageHandlerMapping(applicationContext, mvcConversionService, mvcResourceUrlProvider,
439+
WelcomePageHandlerMapping::new);
440+
}
441+
442+
@Bean
443+
public WelcomePageNotAcceptableHandlerMapping welcomePageNotAcceptableHandlerMapping(
444+
ApplicationContext applicationContext, FormattingConversionService mvcConversionService,
445+
ResourceUrlProvider mvcResourceUrlProvider) {
446+
return createWelcomePageHandlerMapping(applicationContext, mvcConversionService, mvcResourceUrlProvider,
447+
WelcomePageNotAcceptableHandlerMapping::new);
448+
}
449+
450+
private <T extends AbstractUrlHandlerMapping> T createWelcomePageHandlerMapping(
451+
ApplicationContext applicationContext, FormattingConversionService mvcConversionService,
452+
ResourceUrlProvider mvcResourceUrlProvider, WelcomePageHandlerMappingFactory<T> factory) {
453+
TemplateAvailabilityProviders templateAvailabilityProviders = new TemplateAvailabilityProviders(
454+
applicationContext);
455+
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
456+
T handlerMapping = factory.create(templateAvailabilityProviders, applicationContext, getIndexHtmlResource(),
457+
staticPathPattern);
458+
handlerMapping.setInterceptors(getInterceptors(mvcConversionService, mvcResourceUrlProvider));
459+
handlerMapping.setCorsConfigurations(getCorsConfigurations());
460+
return handlerMapping;
443461
}
444462

445463
@Override
@@ -470,25 +488,25 @@ public FlashMapManager flashMapManager() {
470488
return super.flashMapManager();
471489
}
472490

473-
private Resource getWelcomePage() {
491+
private Resource getIndexHtmlResource() {
474492
for (String location : this.resourceProperties.getStaticLocations()) {
475-
Resource indexHtml = getIndexHtml(location);
493+
Resource indexHtml = getIndexHtmlResource(location);
476494
if (indexHtml != null) {
477495
return indexHtml;
478496
}
479497
}
480498
ServletContext servletContext = getServletContext();
481499
if (servletContext != null) {
482-
return getIndexHtml(new ServletContextResource(servletContext, SERVLET_LOCATION));
500+
return getIndexHtmlResource(new ServletContextResource(servletContext, SERVLET_LOCATION));
483501
}
484502
return null;
485503
}
486504

487-
private Resource getIndexHtml(String location) {
488-
return getIndexHtml(this.resourceLoader.getResource(location));
505+
private Resource getIndexHtmlResource(String location) {
506+
return getIndexHtmlResource(this.resourceLoader.getResource(location));
489507
}
490508

491-
private Resource getIndexHtml(Resource location) {
509+
private Resource getIndexHtmlResource(Resource location) {
492510
try {
493511
Resource resource = location.createRelative("index.html");
494512
if (resource.exists() && (resource.getURL() != null)) {
@@ -602,6 +620,15 @@ ResourceChainResourceHandlerRegistrationCustomizer resourceHandlerRegistrationCu
602620

603621
}
604622

623+
@FunctionalInterface
624+
interface WelcomePageHandlerMappingFactory<T extends AbstractUrlHandlerMapping> {
625+
626+
T create(TemplateAvailabilityProviders templateAvailabilityProviders, ApplicationContext applicationContext,
627+
Resource indexHtmlResource, String staticPathPattern);
628+
629+
}
630+
631+
@FunctionalInterface
605632
interface ResourceHandlerRegistrationCustomizer {
606633

607634
void customize(ResourceHandlerRegistration registration);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.web.servlet;
18+
19+
import org.springframework.boot.autoconfigure.template.TemplateAvailabilityProviders;
20+
import org.springframework.context.ApplicationContext;
21+
import org.springframework.core.io.Resource;
22+
23+
/**
24+
* Details for a welcome page resolved from a resource or a template.
25+
*
26+
* @author Phillip Webb
27+
*/
28+
final class WelcomePage {
29+
30+
/**
31+
* Value used for an unresolved welcome page.
32+
*/
33+
static final WelcomePage UNRESOLVED = new WelcomePage(null, false);
34+
35+
private final String viewName;
36+
37+
private final boolean templated;
38+
39+
private WelcomePage(String viewName, boolean templated) {
40+
this.viewName = viewName;
41+
this.templated = templated;
42+
}
43+
44+
/**
45+
* Return the view name of the welcome page.
46+
* @return the view name
47+
*/
48+
String getViewName() {
49+
return this.viewName;
50+
}
51+
52+
/**
53+
* Return if the welcome page is from a template.
54+
* @return if the welcome page is templated
55+
*/
56+
boolean isTemplated() {
57+
return this.templated;
58+
}
59+
60+
/**
61+
* Resolve the {@link WelcomePage} to use.
62+
* @param templateAvailabilityProviders the template availability providers
63+
* @param applicationContext the application context
64+
* @param indexHtmlResource the index HTML resource to use or {@code null}
65+
* @param staticPathPattern the static path pattern being used
66+
* @return a resolved {@link WelcomePage} instance or {@link #UNRESOLVED}
67+
*/
68+
static WelcomePage resolve(TemplateAvailabilityProviders templateAvailabilityProviders,
69+
ApplicationContext applicationContext, Resource indexHtmlResource, String staticPathPattern) {
70+
if (indexHtmlResource != null && "/**".equals(staticPathPattern)) {
71+
return new WelcomePage("forward:index.html", false);
72+
}
73+
if (templateAvailabilityProviders.getProvider("index", applicationContext) != null) {
74+
return new WelcomePage("index", true);
75+
}
76+
return UNRESOLVED;
77+
}
78+
79+
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WelcomePageHandlerMapping.java

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,19 +26,21 @@
2626
import org.springframework.boot.autoconfigure.template.TemplateAvailabilityProviders;
2727
import org.springframework.context.ApplicationContext;
2828
import org.springframework.core.io.Resource;
29+
import org.springframework.core.log.LogMessage;
2930
import org.springframework.http.HttpHeaders;
3031
import org.springframework.http.MediaType;
3132
import org.springframework.util.StringUtils;
3233
import org.springframework.web.servlet.handler.AbstractUrlHandlerMapping;
3334
import org.springframework.web.servlet.mvc.ParameterizableViewController;
3435

3536
/**
36-
* An {@link AbstractUrlHandlerMapping} for an application's welcome page. Supports both
37-
* static and templated files. If both a static and templated index page are available,
38-
* the static page is preferred.
37+
* An {@link AbstractUrlHandlerMapping} for an application's HTML welcome page. Supports
38+
* both static and templated files. If both a static and templated index page are
39+
* available, the static page is preferred.
3940
*
4041
* @author Andy Wilkinson
4142
* @author Bruce Brouwer
43+
* @see WelcomePageNotAcceptableHandlerMapping
4244
*/
4345
final class WelcomePageHandlerMapping extends AbstractUrlHandlerMapping {
4446

@@ -47,37 +49,31 @@ final class WelcomePageHandlerMapping extends AbstractUrlHandlerMapping {
4749
private static final List<MediaType> MEDIA_TYPES_ALL = Collections.singletonList(MediaType.ALL);
4850

4951
WelcomePageHandlerMapping(TemplateAvailabilityProviders templateAvailabilityProviders,
50-
ApplicationContext applicationContext, Resource welcomePage, String staticPathPattern) {
51-
if (welcomePage != null && "/**".equals(staticPathPattern)) {
52-
logger.info("Adding welcome page: " + welcomePage);
53-
setRootViewName("forward:index.html");
54-
}
55-
else if (welcomeTemplateExists(templateAvailabilityProviders, applicationContext)) {
56-
logger.info("Adding welcome page template: index");
57-
setRootViewName("index");
58-
}
59-
}
60-
61-
private boolean welcomeTemplateExists(TemplateAvailabilityProviders templateAvailabilityProviders,
62-
ApplicationContext applicationContext) {
63-
return templateAvailabilityProviders.getProvider("index", applicationContext) != null;
64-
}
65-
66-
private void setRootViewName(String viewName) {
67-
ParameterizableViewController controller = new ParameterizableViewController();
68-
controller.setViewName(viewName);
69-
setRootHandler(controller);
52+
ApplicationContext applicationContext, Resource indexHtmlResource, String staticPathPattern) {
7053
setOrder(2);
54+
WelcomePage welcomePage = WelcomePage.resolve(templateAvailabilityProviders, applicationContext,
55+
indexHtmlResource, staticPathPattern);
56+
if (welcomePage != WelcomePage.UNRESOLVED) {
57+
logger.info(LogMessage.of(() -> (!welcomePage.isTemplated()) ? "Adding welcome page: " + indexHtmlResource
58+
: "Adding welcome page template: index"));
59+
ParameterizableViewController controller = new ParameterizableViewController();
60+
controller.setViewName(welcomePage.getViewName());
61+
setRootHandler(controller);
62+
}
7163
}
7264

7365
@Override
7466
public Object getHandlerInternal(HttpServletRequest request) throws Exception {
67+
return (!isHtmlTextAccepted(request)) ? null : super.getHandlerInternal(request);
68+
}
69+
70+
private boolean isHtmlTextAccepted(HttpServletRequest request) {
7571
for (MediaType mediaType : getAcceptedMediaTypes(request)) {
7672
if (mediaType.includes(MediaType.TEXT_HTML)) {
77-
return super.getHandlerInternal(request);
73+
return true;
7874
}
7975
}
80-
return null;
76+
return false;
8177
}
8278

8379
private List<MediaType> getAcceptedMediaTypes(HttpServletRequest request) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.web.servlet;
18+
19+
import javax.servlet.http.HttpServletRequest;
20+
import javax.servlet.http.HttpServletResponse;
21+
22+
import org.springframework.boot.autoconfigure.template.TemplateAvailabilityProviders;
23+
import org.springframework.context.ApplicationContext;
24+
import org.springframework.core.io.Resource;
25+
import org.springframework.http.HttpStatus;
26+
import org.springframework.web.servlet.ModelAndView;
27+
import org.springframework.web.servlet.handler.AbstractUrlHandlerMapping;
28+
import org.springframework.web.servlet.mvc.Controller;
29+
30+
/**
31+
* An {@link AbstractUrlHandlerMapping} for an application's welcome page that was
32+
* ultimately not accepted.
33+
*
34+
* @author Phillip Webb
35+
*/
36+
class WelcomePageNotAcceptableHandlerMapping extends AbstractUrlHandlerMapping {
37+
38+
WelcomePageNotAcceptableHandlerMapping(TemplateAvailabilityProviders templateAvailabilityProviders,
39+
ApplicationContext applicationContext, Resource indexHtmlResource, String staticPathPattern) {
40+
setOrder(LOWEST_PRECEDENCE - 10); // Before ResourceHandlerRegistry
41+
WelcomePage welcomePage = WelcomePage.resolve(templateAvailabilityProviders, applicationContext,
42+
indexHtmlResource, staticPathPattern);
43+
if (welcomePage != WelcomePage.UNRESOLVED) {
44+
setRootHandler((Controller) this::handleRequest);
45+
}
46+
}
47+
48+
private ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {
49+
response.setStatus(HttpStatus.NOT_ACCEPTABLE.value());
50+
return null;
51+
}
52+
53+
@Override
54+
protected Object getHandlerInternal(HttpServletRequest request) throws Exception {
55+
return super.getHandlerInternal(request);
56+
}
57+
58+
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ void handlerAdaptersCreated() {
165165

166166
@Test
167167
void handlerMappingsCreated() {
168-
this.contextRunner.run((context) -> assertThat(context).getBeans(HandlerMapping.class).hasSize(5));
168+
this.contextRunner.run((context) -> assertThat(context).getBeans(HandlerMapping.class).hasSize(6));
169169
}
170170

171171
@Test
@@ -687,8 +687,8 @@ void welcomePageHandlerMappingIsAutoConfigured() {
687687
this.contextRunner.withPropertyValues("spring.web.resources.static-locations:classpath:/welcome-page/")
688688
.run((context) -> {
689689
assertThat(context).hasSingleBean(WelcomePageHandlerMapping.class);
690-
WelcomePageHandlerMapping bean = context.getBean(WelcomePageHandlerMapping.class);
691-
assertThat(bean.getRootHandler()).isNotNull();
690+
assertThat(context.getBean(WelcomePageHandlerMapping.class).getRootHandler()).isNotNull();
691+
assertThat(context.getBean(WelcomePageNotAcceptableHandlerMapping.class).getRootHandler()).isNotNull();
692692
});
693693
}
694694

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WelcomePageHandlerMappingTests.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ void handlesRequestWithEmptyAcceptHeader() {
115115
.perform(get("/").header(HttpHeaders.ACCEPT, ""))
116116
.andExpect(status().isOk())
117117
.andExpect(forwardedUrl("index.html")));
118-
119118
}
120119

121120
@Test

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WelcomePageIntegrationTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.boot.test.web.server.LocalServerPort;
3030
import org.springframework.context.annotation.Configuration;
3131
import org.springframework.context.annotation.Import;
32+
import org.springframework.http.HttpStatus;
3233
import org.springframework.http.MediaType;
3334
import org.springframework.http.RequestEntity;
3435
import org.springframework.http.ResponseEntity;
@@ -57,6 +58,16 @@ void contentStrategyWithWelcomePage() throws Exception {
5758
.build();
5859
ResponseEntity<String> content = this.template.exchange(entity, String.class);
5960
assertThat(content.getBody()).contains("/custom-");
61+
assertThat(content.getStatusCode()).isEqualTo(HttpStatus.OK);
62+
}
63+
64+
@Test
65+
void notAcceptableWelcomePage() throws Exception {
66+
RequestEntity<?> entity = RequestEntity.get(new URI("http://localhost:" + this.port + "/"))
67+
.header("Accept", "spring/boot")
68+
.build();
69+
ResponseEntity<String> content = this.template.exchange(entity, String.class);
70+
assertThat(content.getStatusCode()).isEqualTo(HttpStatus.NOT_ACCEPTABLE);
6071
}
6172

6273
@Configuration

0 commit comments

Comments
 (0)