Fix package tangle in spring-boot-actuator

Introduce ManagementServletContext interface as a facade for
ManagementServerProperties to resolve the package tangle between
`o.s.boot.actuate.endpoint.mvc` and `o.s.boot.actuate`

Fixes gh-3887
pull/3741/merge
Phillip Webb 9 years ago
parent da0d85c30e
commit 60b8cb8a47

@ -34,6 +34,7 @@ import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.actuate.endpoint.Endpoint; 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.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
@ -120,6 +121,19 @@ public class EndpointWebMvcAutoConfiguration implements ApplicationContextAware,
return new ManagementContextResolver(this.applicationContext); return new ManagementContextResolver(this.applicationContext);
} }
@Bean
public ManagementServletContext managementServletContext(
final ManagementServerProperties properties) {
return new ManagementServletContext() {
@Override
public String getContextPath() {
return properties.getContextPath();
}
};
}
@Override @Override
public void afterSingletonsInstantiated() { public void afterSingletonsInstantiated() {
ManagementServerPort managementPort = ManagementServerPort.DIFFERENT; ManagementServerPort managementPort = ManagementServerPort.DIFFERENT;

@ -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.ActuatorHalBrowserEndpoint;
import org.springframework.boot.actuate.endpoint.mvc.ActuatorHalJsonEndpoint; import org.springframework.boot.actuate.endpoint.mvc.ActuatorHalJsonEndpoint;
import org.springframework.boot.actuate.endpoint.mvc.HypermediaDisabled; 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.MvcEndpoint;
import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoints; import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoints;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
@ -87,22 +88,36 @@ import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
@EnableConfigurationProperties(ResourceProperties.class) @EnableConfigurationProperties(ResourceProperties.class)
public class EndpointWebMvcHypermediaManagementContextConfiguration { 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) @ConditionalOnProperty(prefix = "endpoints.actuator", name = "enabled", matchIfMissing = true)
@Bean @Bean
public ActuatorHalJsonEndpoint actuatorMvcEndpoint( public ActuatorHalJsonEndpoint actuatorMvcEndpoint(
ManagementServerProperties management, ResourceProperties resources, ManagementServletContext managementServletContext,
ResourceLoader resourceLoader) { ResourceProperties resources, ResourceLoader resourceLoader) {
if (ActuatorHalBrowserEndpoint.getHalBrowserLocation(resourceLoader) != null) { if (ActuatorHalBrowserEndpoint.getHalBrowserLocation(resourceLoader) != null) {
return new ActuatorHalBrowserEndpoint(management); return new ActuatorHalBrowserEndpoint(managementServletContext);
} }
return new ActuatorHalJsonEndpoint(management); return new ActuatorHalJsonEndpoint(managementServletContext);
} }
@Bean @Bean
@ConditionalOnProperty(prefix = "endpoints.docs", name = "enabled", matchIfMissing = true) @ConditionalOnProperty(prefix = "endpoints.docs", name = "enabled", matchIfMissing = true)
@ConditionalOnResource(resources = "classpath:/META-INF/resources/spring-boot-actuator/docs/index.html") @ConditionalOnResource(resources = "classpath:/META-INF/resources/spring-boot-actuator/docs/index.html")
public ActuatorDocsEndpoint actuatorDocsEndpoint(ManagementServerProperties management) { public ActuatorDocsEndpoint actuatorDocsEndpoint(
return new ActuatorDocsEndpoint(management); ManagementServletContext managementServletContext) {
return new ActuatorDocsEndpoint(managementServletContext);
} }
@Bean @Bean

@ -16,7 +16,6 @@
package org.springframework.boot.actuate.endpoint.mvc; 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.actuate.endpoint.Endpoint;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
@ -39,7 +38,7 @@ public class ActuatorDocsEndpoint extends WebMvcConfigurerAdapter implements Mvc
private boolean sensitive; private boolean sensitive;
private ManagementServerProperties management; private final ManagementServletContext managementServletContext;
private Curies curies = new Curies(); private Curies curies = new Curies();
@ -47,23 +46,26 @@ public class ActuatorDocsEndpoint extends WebMvcConfigurerAdapter implements Mvc
return this.curies; return this.curies;
} }
public ActuatorDocsEndpoint(ManagementServerProperties management) { public ActuatorDocsEndpoint(ManagementServletContext managementServletContext) {
this.management = management; this.managementServletContext = managementServletContext;
} }
@RequestMapping(value = "/", produces = MediaType.TEXT_HTML_VALUE) @RequestMapping(value = "/", produces = MediaType.TEXT_HTML_VALUE)
public String browse() { 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) @RequestMapping(value = "", produces = MediaType.TEXT_HTML_VALUE)
public String redirect() { public String redirect() {
return "redirect:" + this.management.getContextPath() + this.path + "/"; return "redirect:" + this.managementServletContext.getContextPath() + this.path
+ "/";
} }
@Override @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) { public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler(this.management.getContextPath() + this.path + "/**") registry.addResourceHandler(
this.managementServletContext.getContextPath() + this.path + "/**")
.addResourceLocations(DOCS_LOCATION); .addResourceLocations(DOCS_LOCATION);
} }

@ -21,7 +21,6 @@ import java.nio.charset.Charset;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import org.springframework.boot.actuate.autoconfigure.ManagementServerProperties;
import org.springframework.context.ResourceLoaderAware; import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.ResourceLoader;
@ -47,13 +46,13 @@ public class ActuatorHalBrowserEndpoint extends ActuatorHalJsonEndpoint implemen
private HalBrowserLocation location; private HalBrowserLocation location;
public ActuatorHalBrowserEndpoint(ManagementServerProperties management) { public ActuatorHalBrowserEndpoint(ManagementServletContext managementServletContext) {
super(management); super(managementServletContext);
} }
@RequestMapping(produces = MediaType.TEXT_HTML_VALUE) @RequestMapping(produces = MediaType.TEXT_HTML_VALUE)
public String browse(HttpServletRequest request) { public String browse(HttpServletRequest request) {
String contextPath = getManagement().getContextPath() String contextPath = getManagementServletContext().getContextPath()
+ (getPath().endsWith("/") ? getPath() : getPath() + "/"); + (getPath().endsWith("/") ? getPath() : getPath() + "/");
if (request.getRequestURI().endsWith("/")) { if (request.getRequestURI().endsWith("/")) {
return "forward:" + contextPath + this.location.getHtmlFile(); 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 // 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 // and add a transformer to set the initial link
if (this.location != null) { if (this.location != null) {
String start = getManagement().getContextPath() + getPath(); String start = getManagementServletContext().getContextPath() + getPath();
registry.addResourceHandler(start + "/", start + "/**") registry.addResourceHandler(start + "/", start + "/**")
.addResourceLocations(this.location.getResourceLocation()) .addResourceLocations(this.location.getResourceLocation())
.setCachePeriod(0).resourceChain(true) .setCachePeriod(0).resourceChain(true)
@ -138,9 +137,8 @@ public class ActuatorHalBrowserEndpoint extends ActuatorHalJsonEndpoint implemen
private Resource replaceInitialLink(Resource resource) throws IOException { private Resource replaceInitialLink(Resource resource) throws IOException {
byte[] bytes = FileCopyUtils.copyToByteArray(resource.getInputStream()); byte[] bytes = FileCopyUtils.copyToByteArray(resource.getInputStream());
String content = new String(bytes, DEFAULT_CHARSET); String content = new String(bytes, DEFAULT_CHARSET);
String initialLink = getManagement().getContextPath() + getPath(); String initial = getManagementServletContext().getContextPath() + getPath();
content = content.replace("entryPoint: '/'", "entryPoint: '" + initialLink content = content.replace("entryPoint: '/'", "entryPoint: '" + initial + "'");
+ "'");
return new TransformedResource(resource, content.getBytes(DEFAULT_CHARSET)); return new TransformedResource(resource, content.getBytes(DEFAULT_CHARSET));
} }

@ -19,7 +19,6 @@ package org.springframework.boot.actuate.endpoint.mvc;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern; import javax.validation.constraints.Pattern;
import org.springframework.boot.actuate.autoconfigure.ManagementServerProperties;
import org.springframework.boot.actuate.endpoint.Endpoint; import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.hateoas.ResourceSupport; import org.springframework.hateoas.ResourceSupport;
@ -58,16 +57,18 @@ public class ActuatorHalJsonEndpoint extends WebMvcConfigurerAdapter implements
*/ */
private boolean enabled = true; private boolean enabled = true;
private final ManagementServerProperties management; private final ManagementServletContext managementServletContext;
public ActuatorHalJsonEndpoint(ManagementServerProperties management) { public ActuatorHalJsonEndpoint(ManagementServletContext managementServletContext) {
this.management = management; this.managementServletContext = managementServletContext;
if (StringUtils.hasText(management.getContextPath())) { this.path = getDefaultPath(managementServletContext);
this.path = ""; }
}
else { private String getDefaultPath(ManagementServletContext managementServletContext) {
this.path = "/actuator"; if (StringUtils.hasText(managementServletContext.getContextPath())) {
return this.path = "";
} }
return "/actuator";
} }
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE) @RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE)
@ -107,8 +108,8 @@ public class ActuatorHalJsonEndpoint extends WebMvcConfigurerAdapter implements
return null; return null;
} }
protected final ManagementServerProperties getManagement() { protected final ManagementServletContext getManagementServletContext() {
return this.management; return this.managementServletContext;
} }
} }

@ -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();
}
Loading…
Cancel
Save