Move enablement of endpoints into AbstractEndpoint

This commit is contained in:
Christian Dupuis
2013-11-29 14:57:29 +01:00
parent f1026b1202
commit 2c67e06b47
14 changed files with 50 additions and 42 deletions

View File

@@ -47,13 +47,24 @@ public abstract class AbstractEndpoint<T> implements Endpoint<T> {
private boolean sensitive;
private boolean enabled = true;
public AbstractEndpoint(String path) {
this(path, true);
this(path, true, true);
}
public AbstractEndpoint(String path, boolean sensitive) {
public AbstractEndpoint(String path, boolean sensitive, boolean enabled) {
this.path = path;
this.sensitive = sensitive;
this.enabled = enabled;
}
public boolean isEnabled() {
return this.enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
@Override
@@ -83,4 +94,14 @@ public abstract class AbstractEndpoint<T> implements Endpoint<T> {
public HttpMethod[] methods() {
return GET_HTTP_METHOD;
}
@Override
public final T invoke() {
if (this.enabled) {
return doInvoke();
}
throw new EndpointDisabledException();
}
protected abstract T doInvoke();
}

View File

@@ -52,7 +52,7 @@ public class AutoConfigurationReportEndpoint extends AbstractEndpoint<Report> {
}
@Override
public Report invoke() {
protected Report doInvoke() {
return new Report(this.autoConfigurationReport);
}

View File

@@ -56,7 +56,7 @@ public class BeansEndpoint extends AbstractEndpoint<String> implements
}
@Override
public String invoke() {
protected String doInvoke() {
return this.liveBeansView.getSnapshotAsJson();
}
}

View File

@@ -39,7 +39,7 @@ public class DumpEndpoint extends AbstractEndpoint<List<ThreadInfo>> {
}
@Override
public List<ThreadInfo> invoke() {
protected List<ThreadInfo> doInvoke() {
return Arrays.asList(ManagementFactory.getThreadMXBean().dumpAllThreads(true,
true));
}

View File

@@ -47,7 +47,7 @@ public class EnvironmentEndpoint extends AbstractEndpoint<Map<String, Object>> i
}
@Override
public Map<String, Object> invoke() {
protected Map<String, Object> doInvoke() {
Map<String, Object> result = new LinkedHashMap<String, Object>();
result.put("profiles", this.environment.getActiveProfiles());
for (PropertySource<?> source : getPropertySources()) {

View File

@@ -36,13 +36,13 @@ public class HealthEndpoint<T> extends AbstractEndpoint<T> {
* @param indicator the health indicator
*/
public HealthEndpoint(HealthIndicator<? extends T> indicator) {
super("/health", false);
super("/health", false, true);
Assert.notNull(indicator, "Indicator must not be null");
this.indicator = indicator;
}
@Override
public T invoke() {
protected T doInvoke() {
return this.indicator.health();
}

View File

@@ -39,13 +39,13 @@ public class InfoEndpoint extends AbstractEndpoint<Map<String, Object>> {
* @param info the info to expose
*/
public InfoEndpoint(Map<String, ? extends Object> info) {
super("/info", false);
super("/info", false, true);
Assert.notNull(info, "Info must not be null");
this.info = info;
}
@Override
public Map<String, Object> invoke() {
protected Map<String, Object> doInvoke() {
Map<String, Object> info = new LinkedHashMap<String, Object>(this.info);
info.putAll(getAdditionalInfo());
return info;

View File

@@ -28,14 +28,12 @@ import org.springframework.http.HttpMethod;
@ConfigurationProperties(name = "endpoints.jolokia", ignoreUnknownFields = false)
public class JolokiaEndpoint extends AbstractEndpoint<String> {
private boolean enabled = true;
public JolokiaEndpoint() {
super("/jolokia");
}
@Override
public String invoke() {
protected String doInvoke() {
return null;
}
@@ -44,11 +42,4 @@ public class JolokiaEndpoint extends AbstractEndpoint<String> {
return NO_HTTP_METHOD;
}
public boolean isEnabled() {
return this.enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}

View File

@@ -45,7 +45,7 @@ public class MetricsEndpoint extends AbstractEndpoint<Map<String, Object>> {
}
@Override
public Map<String, Object> invoke() {
protected Map<String, Object> doInvoke() {
Map<String, Object> result = new LinkedHashMap<String, Object>();
for (Metric metric : this.metrics.metrics()) {
result.put(metric.getName(), metric.getValue());

View File

@@ -38,22 +38,15 @@ public class ShutdownEndpoint extends AbstractEndpoint<Map<String, Object>> impl
private ConfigurableApplicationContext context;
private boolean enabled = false;
/**
* Create a new {@link ShutdownEndpoint} instance.
*/
public ShutdownEndpoint() {
super("/shutdown");
super("/shutdown", true, false);
}
@Override
public Map<String, Object> invoke() {
if (!this.enabled) {
return Collections.<String, Object> singletonMap("message",
"Shutdown not enabled, sorry.");
}
protected Map<String, Object> doInvoke() {
if (this.context == null) {
return Collections.<String, Object> singletonMap("message",
@@ -89,11 +82,4 @@ public class ShutdownEndpoint extends AbstractEndpoint<Map<String, Object>> impl
return POST_HTTP_METHOD;
}
public boolean isEnabled() {
return this.enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}

View File

@@ -45,7 +45,7 @@ public class TraceEndpoint extends AbstractEndpoint<List<Trace>> {
}
@Override
public List<Trace> invoke() {
protected List<Trace> doInvoke() {
return this.repository.findAll();
}
}

View File

@@ -29,6 +29,7 @@ import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.actuate.endpoint.EndpointDisabledException;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
@@ -40,6 +41,7 @@ import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor;
import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException;
import com.fasterxml.jackson.databind.SerializationFeature;
@@ -88,7 +90,15 @@ public final class EndpointHandlerAdapter implements HandlerAdapter {
private void handle(HttpServletRequest request, HttpServletResponse response,
Endpoint<?> endpoint) throws Exception {
Object result = endpoint.invoke();
Object result = null;
try {
result = endpoint.invoke();
}
catch (EndpointDisabledException e) {
// Disabled endpoints should get mapped to a HTTP 404
throw new NoSuchRequestHandlingMethodException(request);
}
Class<?> resultClass = result.getClass();
List<MediaType> mediaTypes = getMediaTypes(request, endpoint, resultClass);

View File

@@ -195,9 +195,9 @@ public class EndpointWebMvcAutoConfigurationTests {
@Bean
public Endpoint<String> testEndpoint() {
return new AbstractEndpoint<String>("/endpoint", false) {
return new AbstractEndpoint<String>("/endpoint", false, true) {
@Override
public String invoke() {
public String doInvoke() {
return "endpointoutput";
}
};

View File

@@ -105,7 +105,7 @@ public class EndpointHandlerMappingTests {
}
@Override
public Object invoke() {
public Object doInvoke() {
return null;
}