Commit 6de479fe authored by Phillip Webb's avatar Phillip Webb

Polish

parent 93046d05
...@@ -165,7 +165,8 @@ public final class EndpointRequest { ...@@ -165,7 +165,8 @@ public final class EndpointRequest {
} }
@Override @Override
protected void initialized(Supplier<WebApplicationContext> webApplicationContext) { protected void initialized(
Supplier<WebApplicationContext> webApplicationContext) {
this.delegate = createDelegate(webApplicationContext); this.delegate = createDelegate(webApplicationContext);
} }
...@@ -173,8 +174,10 @@ public final class EndpointRequest { ...@@ -173,8 +174,10 @@ public final class EndpointRequest {
Supplier<WebApplicationContext> webApplicationContext) { Supplier<WebApplicationContext> webApplicationContext) {
try { try {
WebApplicationContext context = webApplicationContext.get(); WebApplicationContext context = webApplicationContext.get();
PathMappedEndpoints pathMappedEndpoints = context.getBean(PathMappedEndpoints.class); PathMappedEndpoints pathMappedEndpoints = context
DispatcherServletPathProvider pathProvider = context.getBean(DispatcherServletPathProvider.class); .getBean(PathMappedEndpoints.class);
DispatcherServletPathProvider pathProvider = context
.getBean(DispatcherServletPathProvider.class);
return createDelegate(pathMappedEndpoints, pathProvider.getServletPath()); return createDelegate(pathMappedEndpoints, pathProvider.getServletPath());
} }
catch (NoSuchBeanDefinitionException ex) { catch (NoSuchBeanDefinitionException ex) {
...@@ -182,18 +185,20 @@ public final class EndpointRequest { ...@@ -182,18 +185,20 @@ public final class EndpointRequest {
} }
} }
private RequestMatcher createDelegate(PathMappedEndpoints pathMappedEndpoints, String servletPath) { private RequestMatcher createDelegate(PathMappedEndpoints pathMappedEndpoints,
String servletPath) {
Set<String> paths = new LinkedHashSet<>(); Set<String> paths = new LinkedHashSet<>();
if (this.includes.isEmpty()) { if (this.includes.isEmpty()) {
paths.addAll(pathMappedEndpoints.getAllPaths()); paths.addAll(pathMappedEndpoints.getAllPaths());
} }
streamPaths(this.includes, pathMappedEndpoints).forEach(paths::add); streamPaths(this.includes, pathMappedEndpoints).forEach(paths::add);
streamPaths(this.excludes, pathMappedEndpoints).forEach(paths::remove); streamPaths(this.excludes, pathMappedEndpoints).forEach(paths::remove);
List<RequestMatcher> delegateMatchers = getDelegateMatchers(servletPath, paths); List<RequestMatcher> delegateMatchers = getDelegateMatchers(servletPath,
paths);
if (this.includeLinks if (this.includeLinks
&& StringUtils.hasText(pathMappedEndpoints.getBasePath())) { && StringUtils.hasText(pathMappedEndpoints.getBasePath())) {
delegateMatchers.add( delegateMatchers.add(new AntPathRequestMatcher(
new AntPathRequestMatcher(servletPath + pathMappedEndpoints.getBasePath())); servletPath + pathMappedEndpoints.getBasePath()));
} }
return new OrRequestMatcher(delegateMatchers); return new OrRequestMatcher(delegateMatchers);
} }
...@@ -221,8 +226,10 @@ public final class EndpointRequest { ...@@ -221,8 +226,10 @@ public final class EndpointRequest {
return annotation.id(); return annotation.id();
} }
private List<RequestMatcher> getDelegateMatchers(String servletPath, Set<String> paths) { private List<RequestMatcher> getDelegateMatchers(String servletPath,
return paths.stream().map((path) -> new AntPathRequestMatcher(servletPath + path + "/**")) Set<String> paths) {
return paths.stream()
.map((path) -> new AntPathRequestMatcher(servletPath + path + "/**"))
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
...@@ -247,11 +254,14 @@ public final class EndpointRequest { ...@@ -247,11 +254,14 @@ public final class EndpointRequest {
} }
@Override @Override
protected void initialized(Supplier<WebApplicationContext> webApplicationContext) { protected void initialized(
Supplier<WebApplicationContext> webApplicationContext) {
try { try {
WebApplicationContext context = webApplicationContext.get(); WebApplicationContext context = webApplicationContext.get();
WebEndpointProperties properties = context.getBean(WebEndpointProperties.class); WebEndpointProperties properties = context
DispatcherServletPathProvider pathProvider = context.getBean(DispatcherServletPathProvider.class); .getBean(WebEndpointProperties.class);
DispatcherServletPathProvider pathProvider = context
.getBean(DispatcherServletPathProvider.class);
this.delegate = createDelegate(pathProvider.getServletPath(), properties); this.delegate = createDelegate(pathProvider.getServletPath(), properties);
} }
catch (NoSuchBeanDefinitionException ex) { catch (NoSuchBeanDefinitionException ex) {
...@@ -259,7 +269,8 @@ public final class EndpointRequest { ...@@ -259,7 +269,8 @@ public final class EndpointRequest {
} }
} }
private RequestMatcher createDelegate(String path, WebEndpointProperties properties) { private RequestMatcher createDelegate(String path,
WebEndpointProperties properties) {
if (StringUtils.hasText(properties.getBasePath())) { if (StringUtils.hasText(properties.getBasePath())) {
return new AntPathRequestMatcher(path + properties.getBasePath()); return new AntPathRequestMatcher(path + properties.getBasePath());
} }
......
...@@ -65,9 +65,11 @@ public class WebMvcEndpointChildContextConfigurationTests { ...@@ -65,9 +65,11 @@ public class WebMvcEndpointChildContextConfigurationTests {
@Test @Test
public void contextShouldConfigureDispatcherServletPathProviderWithEmptyPath() { public void contextShouldConfigureDispatcherServletPathProviderWithEmptyPath() {
this.contextRunner.withUserConfiguration(WebMvcEndpointChildContextConfiguration.class) this.contextRunner
.run((context) -> assertThat(context.getBean(DispatcherServletPathProvider.class) .withUserConfiguration(WebMvcEndpointChildContextConfiguration.class)
.getServletPath()).isEmpty()); .run((context) -> assertThat(context
.getBean(DispatcherServletPathProvider.class).getServletPath())
.isEmpty());
} }
static class ExistingConfig { static class ExistingConfig {
......
...@@ -90,7 +90,8 @@ public class DispatcherServletAutoConfiguration { ...@@ -90,7 +90,8 @@ public class DispatcherServletAutoConfiguration {
private final ServerProperties serverProperties; private final ServerProperties serverProperties;
public DispatcherServletConfiguration(WebMvcProperties webMvcProperties, ServerProperties serverProperties) { public DispatcherServletConfiguration(WebMvcProperties webMvcProperties,
ServerProperties serverProperties) {
this.webMvcProperties = webMvcProperties; this.webMvcProperties = webMvcProperties;
this.serverProperties = serverProperties; this.serverProperties = serverProperties;
} }
...@@ -117,7 +118,8 @@ public class DispatcherServletAutoConfiguration { ...@@ -117,7 +118,8 @@ public class DispatcherServletAutoConfiguration {
@Bean @Bean
public DispatcherServletPathProvider mainDispatcherServletPathProvider() { public DispatcherServletPathProvider mainDispatcherServletPathProvider() {
return () -> DispatcherServletConfiguration.this.serverProperties.getServlet().getPath(); return () -> DispatcherServletConfiguration.this.serverProperties.getServlet()
.getPath();
} }
} }
......
...@@ -19,8 +19,8 @@ package org.springframework.boot.autoconfigure.web.servlet; ...@@ -19,8 +19,8 @@ package org.springframework.boot.autoconfigure.web.servlet;
import org.springframework.web.servlet.DispatcherServlet; import org.springframework.web.servlet.DispatcherServlet;
/** /**
* Interface that provides the path of the {@link DispatcherServlet} in * Interface that provides the path of the {@link DispatcherServlet} in an application
* an application context. * context.
* *
* @author Madhura Bhave * @author Madhura Bhave
* @since 2.0.2 * @since 2.0.2
...@@ -31,4 +31,3 @@ public interface DispatcherServletPathProvider { ...@@ -31,4 +31,3 @@ public interface DispatcherServletPathProvider {
String getServletPath(); String getServletPath();
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment