Extract AbstractMvcEndpoint class

Extract common functionality from Spring MVC only endpoints.

See gh-5670
pull/6350/head
Lari Hotari 9 years ago committed by Phillip Webb
parent b732aeb453
commit e1893f66ce

@ -0,0 +1,113 @@
/*
* Copyright 2012-2016 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;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.actuate.endpoint.EndpointProperties;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
* Abstract base class for {@link MvcEndpoint} implementations without a backing
* {@link Endpoint}.
*
* @author Phillip Webb
* @author Lari Hotari
* @since 1.4.0
*/
public abstract class AbstractMvcEndpoint extends WebMvcConfigurerAdapter
implements MvcEndpoint, EnvironmentAware {
private Environment environment;
/**
* Endpoint URL path.
*/
@NotNull
@Pattern(regexp = "/.*|^$", message = "Path must start with /")
private String path;
/**
* Enable the endpoint.
*/
private Boolean enabled;
/**
* Mark if the endpoint exposes sensitive information.
*/
private Boolean sensitive;
private final boolean sensitiveDefault;
public AbstractMvcEndpoint(String path, boolean sensitive) {
this.path = path;
this.sensitiveDefault = sensitive;
}
public AbstractMvcEndpoint(String path, boolean sensitive, boolean enabled) {
this.path = path;
this.sensitiveDefault = sensitive;
this.enabled = enabled;
}
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
protected final Environment getEnvironment() {
return this.environment;
}
@Override
public String getPath() {
return this.path;
}
public void setPath(String path) {
this.path = path;
}
public boolean isEnabled() {
return EndpointProperties.isEnabled(this.environment, this.enabled);
}
public void setEnabled(Boolean enabled) {
this.enabled = enabled;
}
@Override
public boolean isSensitive() {
return EndpointProperties.isSensitive(this.environment, this.sensitive,
this.sensitiveDefault);
}
public void setSensitive(Boolean sensitive) {
this.sensitive = sensitive;
}
@Override
@SuppressWarnings("rawtypes")
public Class<? extends Endpoint> getEndpointType() {
return null;
}
}

@ -16,15 +16,10 @@
package org.springframework.boot.actuate.endpoint.mvc; package org.springframework.boot.actuate.endpoint.mvc;
import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.actuate.endpoint.EndpointProperties;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/** /**
* {@link MvcEndpoint} to expose actuator documentation. * {@link MvcEndpoint} to expose actuator documentation.
@ -33,95 +28,42 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
* @since 1.3.0 * @since 1.3.0
*/ */
@ConfigurationProperties("endpoints.docs") @ConfigurationProperties("endpoints.docs")
public class DocsMvcEndpoint extends WebMvcConfigurerAdapter public class DocsMvcEndpoint extends AbstractMvcEndpoint {
implements MvcEndpoint, EnvironmentAware {
private static final String DOCS_LOCATION = "classpath:/META-INF/resources/spring-boot-actuator/docs/"; private static final String DOCS_LOCATION = "classpath:/META-INF/resources/spring-boot-actuator/docs/";
private Environment environment;
/**
* Endpoint URL path.
*/
private String path = "/docs";
/**
* Enable the endpoint.
*/
private boolean enabled = true;
/**
* Mark if the endpoint exposes sensitive information.
*/
private Boolean sensitive;
private final ManagementServletContext managementServletContext; private final ManagementServletContext managementServletContext;
private Curies curies = new Curies(); private Curies curies = new Curies();
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
public Curies getCuries() { public Curies getCuries() {
return this.curies; return this.curies;
} }
public DocsMvcEndpoint(ManagementServletContext managementServletContext) { public DocsMvcEndpoint(ManagementServletContext managementServletContext) {
super("/docs", false);
this.managementServletContext = managementServletContext; 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.managementServletContext.getContextPath() + this.path return "forward:" + this.managementServletContext.getContextPath() + getPath()
+ "/index.html"; + "/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.managementServletContext.getContextPath() + this.path return "redirect:" + this.managementServletContext.getContextPath() + getPath()
+ "/"; + "/";
} }
@Override @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) { public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler( registry.addResourceHandler(
this.managementServletContext.getContextPath() + this.path + "/**") this.managementServletContext.getContextPath() + getPath() + "/**")
.addResourceLocations(DOCS_LOCATION); .addResourceLocations(DOCS_LOCATION);
} }
public void setPath(String path) {
this.path = path;
}
@Override
public String getPath() {
return this.path;
}
public boolean isEnabled() {
return this.enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
@Override
public boolean isSensitive() {
return EndpointProperties.isSensitive(this.environment, this.sensitive, false);
}
public void setSensitive(Boolean sensitive) {
this.sensitive = sensitive;
}
@Override
public Class<? extends Endpoint<?>> getEndpointType() {
return null;
}
/** /**
* Properties of the default CurieProvider (used for adding docs links). If enabled, * Properties of the default CurieProvider (used for adding docs links). If enabled,
* all unqualified rels will pick up a prefix and a curie template pointing to the * all unqualified rels will pick up a prefix and a curie template pointing to the

@ -16,20 +16,12 @@
package org.springframework.boot.actuate.endpoint.mvc; package org.springframework.boot.actuate.endpoint.mvc;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.actuate.endpoint.EndpointProperties;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.hateoas.ResourceSupport; import org.springframework.hateoas.ResourceSupport;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/** /**
* {@link MvcEndpoint} to expose HAL-formatted JSON. * {@link MvcEndpoint} to expose HAL-formatted JSON.
@ -40,41 +32,16 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
* @since 1.3.0 * @since 1.3.0
*/ */
@ConfigurationProperties("endpoints.actuator") @ConfigurationProperties("endpoints.actuator")
public class HalJsonMvcEndpoint extends WebMvcConfigurerAdapter public class HalJsonMvcEndpoint extends AbstractMvcEndpoint {
implements MvcEndpoint, EnvironmentAware {
private Environment environment;
/**
* Endpoint URL path.
*/
@NotNull
@Pattern(regexp = "^$|/.*", message = "Path must be empty or start with /")
private String path;
/**
* Enable the endpoint.
*/
private boolean enabled = true;
/**
* Mark if the endpoint exposes sensitive information.
*/
private Boolean sensitive;
private final ManagementServletContext managementServletContext; private final ManagementServletContext managementServletContext;
public HalJsonMvcEndpoint(ManagementServletContext managementServletContext) { public HalJsonMvcEndpoint(ManagementServletContext managementServletContext) {
super(getDefaultPath(managementServletContext), false);
this.managementServletContext = managementServletContext; this.managementServletContext = managementServletContext;
this.path = getDefaultPath(managementServletContext);
} }
@Override private static String getDefaultPath(
public void setEnvironment(Environment environment) { ManagementServletContext managementServletContext) {
this.environment = environment;
}
private String getDefaultPath(ManagementServletContext managementServletContext) {
if (StringUtils.hasText(managementServletContext.getContextPath())) { if (StringUtils.hasText(managementServletContext.getContextPath())) {
return ""; return "";
} }
@ -87,39 +54,7 @@ public class HalJsonMvcEndpoint extends WebMvcConfigurerAdapter
return new ResourceSupport(); return new ResourceSupport();
} }
public void setPath(String path) {
this.path = path;
}
@Override
public String getPath() {
return this.path;
}
public boolean isEnabled() {
return this.enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
@Override
public boolean isSensitive() {
return EndpointProperties.isSensitive(this.environment, this.sensitive, false);
}
public void setSensitive(Boolean sensitive) {
this.sensitive = sensitive;
}
@Override
public Class<? extends Endpoint<?>> getEndpointType() {
return null;
}
protected final ManagementServletContext getManagementServletContext() { protected final ManagementServletContext getManagementServletContext() {
return this.managementServletContext; return this.managementServletContext;
} }
} }

@ -22,20 +22,14 @@ import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper; import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import org.jolokia.http.AgentServlet; import org.jolokia.http.AgentServlet;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.actuate.endpoint.EndpointProperties;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.ServletContextAware; import org.springframework.web.context.ServletContextAware;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
@ -50,31 +44,12 @@ import org.springframework.web.util.UrlPathHelper;
*/ */
@ConfigurationProperties(prefix = "endpoints.jolokia", ignoreUnknownFields = false) @ConfigurationProperties(prefix = "endpoints.jolokia", ignoreUnknownFields = false)
@HypermediaDisabled @HypermediaDisabled
public class JolokiaMvcEndpoint implements MvcEndpoint, InitializingBean, public class JolokiaMvcEndpoint extends AbstractMvcEndpoint
ApplicationContextAware, ServletContextAware, EnvironmentAware { implements InitializingBean, ApplicationContextAware, ServletContextAware {
private Environment environment;
/**
* Endpoint URL path.
*/
@NotNull
@Pattern(regexp = "/.*", message = "Path must start with /")
private String path = "/jolokia";
/**
* Enable the endpoint.
*/
private boolean enabled = true;
/**
* Mark if the endpoint exposes sensitive information.
*/
private Boolean sensitive;
private final ServletWrappingController controller = new ServletWrappingController(); private final ServletWrappingController controller = new ServletWrappingController();
public JolokiaMvcEndpoint() { public JolokiaMvcEndpoint() {
super("/jolokia", true);
this.controller.setServletClass(AgentServlet.class); this.controller.setServletClass(AgentServlet.class);
this.controller.setServletName("jolokia"); this.controller.setServletName("jolokia");
} }
@ -99,43 +74,6 @@ public class JolokiaMvcEndpoint implements MvcEndpoint, InitializingBean,
this.controller.setApplicationContext(context); this.controller.setApplicationContext(context);
} }
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
public boolean isEnabled() {
return this.enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
@Override
public boolean isSensitive() {
return EndpointProperties.isSensitive(this.environment, this.sensitive, true);
}
public void setSensitive(Boolean sensitive) {
this.sensitive = sensitive;
}
@Override
public String getPath() {
return this.path;
}
public void setPath(String path) {
this.path = path;
}
@Override
@SuppressWarnings("rawtypes")
public Class<? extends Endpoint> getEndpointType() {
return null;
}
@RequestMapping("/**") @RequestMapping("/**")
public ModelAndView handle(HttpServletRequest request, HttpServletResponse response) public ModelAndView handle(HttpServletRequest request, HttpServletResponse response)
throws Exception { throws Exception {

@ -22,18 +22,12 @@ import java.io.IOException;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.actuate.endpoint.EndpointProperties;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.logging.LogFile; import org.springframework.boot.logging.LogFile;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
@ -52,64 +46,18 @@ import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
* @since 1.3.0 * @since 1.3.0
*/ */
@ConfigurationProperties(prefix = "endpoints.logfile") @ConfigurationProperties(prefix = "endpoints.logfile")
public class LogFileMvcEndpoint implements MvcEndpoint, EnvironmentAware { public class LogFileMvcEndpoint extends AbstractMvcEndpoint {
private static final Log logger = LogFactory.getLog(LogFileMvcEndpoint.class); private static final Log logger = LogFactory.getLog(LogFileMvcEndpoint.class);
/**
* Endpoint URL path.
*/
@NotNull
@Pattern(regexp = "/.*", message = "Path must start with /")
private String path = "/logfile";
/**
* Enable the endpoint.
*/
private boolean enabled = true;
/**
* Mark if the endpoint exposes sensitive information.
*/
private Boolean sensitive;
/** /**
* External Logfile to be accessed. Can be used if the logfile is written by output * External Logfile to be accessed. Can be used if the logfile is written by output
* redirect and not by the logging-system itself. * redirect and not by the logging-system itself.
*/ */
private File externalFile; private File externalFile;
private Environment environment; public LogFileMvcEndpoint() {
super("/logfile", true);
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
@Override
public String getPath() {
return this.path;
}
public void setPath(String path) {
this.path = path;
}
public boolean isEnabled() {
return this.enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
@Override
public boolean isSensitive() {
return EndpointProperties.isSensitive(this.environment, this.sensitive, true);
}
public void setSensitive(Boolean sensitive) {
this.sensitive = sensitive;
} }
public File getExternalFile() { public File getExternalFile() {
@ -120,12 +68,6 @@ public class LogFileMvcEndpoint implements MvcEndpoint, EnvironmentAware {
this.externalFile = externalFile; this.externalFile = externalFile;
} }
@Override
@SuppressWarnings("rawtypes")
public Class<? extends Endpoint> getEndpointType() {
return null;
}
@RequestMapping(method = { RequestMethod.GET, RequestMethod.HEAD }) @RequestMapping(method = { RequestMethod.GET, RequestMethod.HEAD })
public void invoke(HttpServletRequest request, HttpServletResponse response) public void invoke(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { throws ServletException, IOException {
@ -147,7 +89,7 @@ public class LogFileMvcEndpoint implements MvcEndpoint, EnvironmentAware {
if (this.externalFile != null) { if (this.externalFile != null) {
return new FileSystemResource(this.externalFile); return new FileSystemResource(this.externalFile);
} }
LogFile logFile = LogFile.get(this.environment); LogFile logFile = LogFile.get(getEnvironment());
if (logFile == null) { if (logFile == null) {
logger.debug("Missing 'logging.file' or 'logging.path' properties"); logger.debug("Missing 'logging.file' or 'logging.path' properties");
return null; return null;

Loading…
Cancel
Save