diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfiguration.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfiguration.java index 79601b0d8dd..165a80d6066 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfiguration.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfiguration.java @@ -34,6 +34,7 @@ import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.boot.actuate.endpoint.Endpoint; +import org.springframework.boot.actuate.endpoint.mvc.ManagementServletContext; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; @@ -120,6 +121,19 @@ public class EndpointWebMvcAutoConfiguration implements ApplicationContextAware, return new ManagementContextResolver(this.applicationContext); } + @Bean + public ManagementServletContext managementServletContext( + final ManagementServerProperties properties) { + return new ManagementServletContext() { + + @Override + public String getContextPath() { + return properties.getContextPath(); + } + + }; + } + @Override public void afterSingletonsInstantiated() { ManagementServerPort managementPort = ManagementServerPort.DIFFERENT; diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcHypermediaManagementContextConfiguration.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcHypermediaManagementContextConfiguration.java index 61d3cce84e8..2073c8701a8 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcHypermediaManagementContextConfiguration.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcHypermediaManagementContextConfiguration.java @@ -28,6 +28,7 @@ import org.springframework.boot.actuate.endpoint.mvc.ActuatorDocsEndpoint; import org.springframework.boot.actuate.endpoint.mvc.ActuatorHalBrowserEndpoint; import org.springframework.boot.actuate.endpoint.mvc.ActuatorHalJsonEndpoint; import org.springframework.boot.actuate.endpoint.mvc.HypermediaDisabled; +import org.springframework.boot.actuate.endpoint.mvc.ManagementServletContext; import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint; import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoints; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; @@ -87,22 +88,36 @@ import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo; @EnableConfigurationProperties(ResourceProperties.class) public class EndpointWebMvcHypermediaManagementContextConfiguration { + @Bean + public ManagementServletContext managementServletContext( + final ManagementServerProperties properties) { + return new ManagementServletContext() { + + @Override + public String getContextPath() { + return properties.getContextPath(); + } + + }; + } + @ConditionalOnProperty(prefix = "endpoints.actuator", name = "enabled", matchIfMissing = true) @Bean public ActuatorHalJsonEndpoint actuatorMvcEndpoint( - ManagementServerProperties management, ResourceProperties resources, - ResourceLoader resourceLoader) { + ManagementServletContext managementServletContext, + ResourceProperties resources, ResourceLoader resourceLoader) { if (ActuatorHalBrowserEndpoint.getHalBrowserLocation(resourceLoader) != null) { - return new ActuatorHalBrowserEndpoint(management); + return new ActuatorHalBrowserEndpoint(managementServletContext); } - return new ActuatorHalJsonEndpoint(management); + return new ActuatorHalJsonEndpoint(managementServletContext); } @Bean @ConditionalOnProperty(prefix = "endpoints.docs", name = "enabled", matchIfMissing = true) @ConditionalOnResource(resources = "classpath:/META-INF/resources/spring-boot-actuator/docs/index.html") - public ActuatorDocsEndpoint actuatorDocsEndpoint(ManagementServerProperties management) { - return new ActuatorDocsEndpoint(management); + public ActuatorDocsEndpoint actuatorDocsEndpoint( + ManagementServletContext managementServletContext) { + return new ActuatorDocsEndpoint(managementServletContext); } @Bean diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/ActuatorDocsEndpoint.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/ActuatorDocsEndpoint.java index 397e8713caf..947da269042 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/ActuatorDocsEndpoint.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/ActuatorDocsEndpoint.java @@ -16,7 +16,6 @@ package org.springframework.boot.actuate.endpoint.mvc; -import org.springframework.boot.actuate.autoconfigure.ManagementServerProperties; import org.springframework.boot.actuate.endpoint.Endpoint; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.http.MediaType; @@ -39,7 +38,7 @@ public class ActuatorDocsEndpoint extends WebMvcConfigurerAdapter implements Mvc private boolean sensitive; - private ManagementServerProperties management; + private final ManagementServletContext managementServletContext; private Curies curies = new Curies(); @@ -47,23 +46,26 @@ public class ActuatorDocsEndpoint extends WebMvcConfigurerAdapter implements Mvc return this.curies; } - public ActuatorDocsEndpoint(ManagementServerProperties management) { - this.management = management; + public ActuatorDocsEndpoint(ManagementServletContext managementServletContext) { + this.managementServletContext = managementServletContext; } @RequestMapping(value = "/", produces = MediaType.TEXT_HTML_VALUE) public String browse() { - return "forward:" + this.management.getContextPath() + this.path + "/index.html"; + return "forward:" + this.managementServletContext.getContextPath() + this.path + + "/index.html"; } @RequestMapping(value = "", produces = MediaType.TEXT_HTML_VALUE) public String redirect() { - return "redirect:" + this.management.getContextPath() + this.path + "/"; + return "redirect:" + this.managementServletContext.getContextPath() + this.path + + "/"; } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { - registry.addResourceHandler(this.management.getContextPath() + this.path + "/**") + registry.addResourceHandler( + this.managementServletContext.getContextPath() + this.path + "/**") .addResourceLocations(DOCS_LOCATION); } diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/ActuatorHalBrowserEndpoint.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/ActuatorHalBrowserEndpoint.java index c407379c2e3..9bce5578b0a 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/ActuatorHalBrowserEndpoint.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/ActuatorHalBrowserEndpoint.java @@ -21,7 +21,6 @@ import java.nio.charset.Charset; import javax.servlet.http.HttpServletRequest; -import org.springframework.boot.actuate.autoconfigure.ManagementServerProperties; import org.springframework.context.ResourceLoaderAware; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; @@ -47,13 +46,13 @@ public class ActuatorHalBrowserEndpoint extends ActuatorHalJsonEndpoint implemen private HalBrowserLocation location; - public ActuatorHalBrowserEndpoint(ManagementServerProperties management) { - super(management); + public ActuatorHalBrowserEndpoint(ManagementServletContext managementServletContext) { + super(managementServletContext); } @RequestMapping(produces = MediaType.TEXT_HTML_VALUE) public String browse(HttpServletRequest request) { - String contextPath = getManagement().getContextPath() + String contextPath = getManagementServletContext().getContextPath() + (getPath().endsWith("/") ? getPath() : getPath() + "/"); if (request.getRequestURI().endsWith("/")) { return "forward:" + contextPath + this.location.getHtmlFile(); @@ -71,7 +70,7 @@ public class ActuatorHalBrowserEndpoint extends ActuatorHalJsonEndpoint implemen // Make sure the root path is not cached so the browser comes back for the JSON // and add a transformer to set the initial link if (this.location != null) { - String start = getManagement().getContextPath() + getPath(); + String start = getManagementServletContext().getContextPath() + getPath(); registry.addResourceHandler(start + "/", start + "/**") .addResourceLocations(this.location.getResourceLocation()) .setCachePeriod(0).resourceChain(true) @@ -138,9 +137,8 @@ public class ActuatorHalBrowserEndpoint extends ActuatorHalJsonEndpoint implemen private Resource replaceInitialLink(Resource resource) throws IOException { byte[] bytes = FileCopyUtils.copyToByteArray(resource.getInputStream()); String content = new String(bytes, DEFAULT_CHARSET); - String initialLink = getManagement().getContextPath() + getPath(); - content = content.replace("entryPoint: '/'", "entryPoint: '" + initialLink - + "'"); + String initial = getManagementServletContext().getContextPath() + getPath(); + content = content.replace("entryPoint: '/'", "entryPoint: '" + initial + "'"); return new TransformedResource(resource, content.getBytes(DEFAULT_CHARSET)); } diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/ActuatorHalJsonEndpoint.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/ActuatorHalJsonEndpoint.java index 378f8061365..ea7db45d567 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/ActuatorHalJsonEndpoint.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/ActuatorHalJsonEndpoint.java @@ -19,7 +19,6 @@ package org.springframework.boot.actuate.endpoint.mvc; import javax.validation.constraints.NotNull; import javax.validation.constraints.Pattern; -import org.springframework.boot.actuate.autoconfigure.ManagementServerProperties; import org.springframework.boot.actuate.endpoint.Endpoint; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.hateoas.ResourceSupport; @@ -58,16 +57,18 @@ public class ActuatorHalJsonEndpoint extends WebMvcConfigurerAdapter implements */ private boolean enabled = true; - private final ManagementServerProperties management; + private final ManagementServletContext managementServletContext; - public ActuatorHalJsonEndpoint(ManagementServerProperties management) { - this.management = management; - if (StringUtils.hasText(management.getContextPath())) { - this.path = ""; - } - else { - this.path = "/actuator"; + public ActuatorHalJsonEndpoint(ManagementServletContext managementServletContext) { + this.managementServletContext = managementServletContext; + this.path = getDefaultPath(managementServletContext); + } + + private String getDefaultPath(ManagementServletContext managementServletContext) { + if (StringUtils.hasText(managementServletContext.getContextPath())) { + return this.path = ""; } + return "/actuator"; } @RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE) @@ -107,8 +108,8 @@ public class ActuatorHalJsonEndpoint extends WebMvcConfigurerAdapter implements return null; } - protected final ManagementServerProperties getManagement() { - return this.management; + protected final ManagementServletContext getManagementServletContext() { + return this.managementServletContext; } } diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/ManagementServletContext.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/ManagementServletContext.java new file mode 100644 index 00000000000..ca4c592fe3a --- /dev/null +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/ManagementServletContext.java @@ -0,0 +1,33 @@ +/* + * Copyright 2012-2015 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.actuate.endpoint.mvc; + +/** + * Provides information about the management servlet context for MVC controllers to use. + * + * @author Phillip Webb + * @since 1.3.0 + */ +public interface ManagementServletContext { + + /** + * Return the context path of the management server. + * @return the context path + */ + String getContextPath(); + +}