diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/AbstractEndpointMvcAdapter.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/AbstractEndpointMvcAdapter.java new file mode 100644 index 000000000000..500cf1e02d79 --- /dev/null +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/AbstractEndpointMvcAdapter.java @@ -0,0 +1,96 @@ +/* + * 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; + +import org.springframework.boot.actuate.endpoint.Endpoint; +import org.springframework.http.ResponseEntity; +import org.springframework.util.Assert; + +/** + * Abstract base class for {@link MvcEndpoint} implementations. + * + * @param The delegate endpoint + * @author Dave Syer + * @author Andy Wilkinson + * @author Phillip Webb + * @since 1.3.0 + */ +public abstract class AbstractEndpointMvcAdapter> implements + MvcEndpoint { + + private final E delegate; + + private String path; + + /** + * Create a new {@link EndpointMvcAdapter}. + * @param delegate the underlying {@link Endpoint} to adapt. + */ + public AbstractEndpointMvcAdapter(E delegate) { + Assert.notNull(delegate, "Delegate must not be null"); + this.delegate = delegate; + } + + protected Object invoke() { + if (!this.delegate.isEnabled()) { + // Shouldn't happen - shouldn't be registered when delegate's disabled + return getDisabledResponse(); + } + return this.delegate.invoke(); + } + + public E getDelegate() { + return this.delegate; + } + + @Override + public String getPath() { + return (this.path != null ? this.path : "/" + this.delegate.getId()); + } + + public void setPath(String path) { + while (path.endsWith("/")) { + path = path.substring(0, path.length() - 1); + } + if (!path.startsWith("/")) { + path = "/" + path; + } + this.path = path; + } + + @Override + public boolean isSensitive() { + return this.delegate.isSensitive(); + } + + @Override + @SuppressWarnings("rawtypes") + public Class getEndpointType() { + return this.delegate.getClass(); + } + + /** + * Returns the response that should be returned when the endpoint is disabled. + * @return The response to be returned when the endpoint is disabled + * @since 1.2.4 + * @see Endpoint#isEnabled() + */ + protected ResponseEntity getDisabledResponse() { + return MvcEndpoint.DISABLED_RESPONSE; + } + +} diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/EndpointMvcAdapter.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/EndpointMvcAdapter.java index 47db4d8874c8..134364935190 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/EndpointMvcAdapter.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/EndpointMvcAdapter.java @@ -17,8 +17,6 @@ package org.springframework.boot.actuate.endpoint.mvc; import org.springframework.boot.actuate.endpoint.Endpoint; -import org.springframework.http.ResponseEntity; -import org.springframework.util.Assert; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; @@ -29,69 +27,21 @@ * @author Dave Syer * @author Andy Wilkinson */ -public class EndpointMvcAdapter implements MvcEndpoint { - - private final Endpoint delegate; - - private String path; +public class EndpointMvcAdapter extends AbstractEndpointMvcAdapter> { /** * Create a new {@link EndpointMvcAdapter}. * @param delegate the underlying {@link Endpoint} to adapt. */ public EndpointMvcAdapter(Endpoint delegate) { - Assert.notNull(delegate, "Delegate must not be null"); - this.delegate = delegate; + super(delegate); } + @Override @RequestMapping(method = RequestMethod.GET) @ResponseBody public Object invoke() { - if (!this.delegate.isEnabled()) { - // Shouldn't happen - shouldn't be registered when delegate's disabled - return getDisabledResponse(); - } - return this.delegate.invoke(); - } - - public Endpoint getDelegate() { - return this.delegate; - } - - @Override - public String getPath() { - return (this.path != null ? this.path : "/" + this.delegate.getId()); - } - - public void setPath(String path) { - while (path.endsWith("/")) { - path = path.substring(0, path.length() - 1); - } - if (!path.startsWith("/")) { - path = "/" + path; - } - this.path = path; - } - - @Override - public boolean isSensitive() { - return this.delegate.isSensitive(); - } - - @Override - @SuppressWarnings("rawtypes") - public Class getEndpointType() { - return this.delegate.getClass(); - } - - /** - * Returns the response that should be returned when the endpoint is disabled. - * @return The response to be returned when the endpoint is disabled - * @since 1.2.4 - * @see Endpoint#isEnabled() - */ - protected ResponseEntity getDisabledResponse() { - return MvcEndpoint.DISABLED_RESPONSE; + return super.invoke(); } } diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/HealthMvcEndpoint.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/HealthMvcEndpoint.java index ea528ce1bc40..819817fd23e8 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/HealthMvcEndpoint.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/HealthMvcEndpoint.java @@ -17,11 +17,9 @@ package org.springframework.boot.actuate.endpoint.mvc; import java.security.Principal; -import java.util.Collections; import java.util.HashMap; import java.util.Map; -import org.springframework.boot.actuate.endpoint.Endpoint; import org.springframework.boot.actuate.endpoint.HealthEndpoint; import org.springframework.boot.actuate.health.Health; import org.springframework.boot.actuate.health.Status; @@ -47,9 +45,8 @@ * @author Phillip Webb * @since 1.1.0 */ -public class HealthMvcEndpoint implements MvcEndpoint, EnvironmentAware { - - private final HealthEndpoint delegate; +public class HealthMvcEndpoint extends AbstractEndpointMvcAdapter + implements EnvironmentAware { private final boolean secure; @@ -63,15 +60,12 @@ public class HealthMvcEndpoint implements MvcEndpoint, EnvironmentAware { private Health cached; - private String path; - public HealthMvcEndpoint(HealthEndpoint delegate) { this(delegate, true); } public HealthMvcEndpoint(HealthEndpoint delegate, boolean secure) { - Assert.notNull(delegate, "Delegate must not be null"); - this.delegate = delegate; + super(delegate); this.secure = secure; setupDefaultStatusMapping(); } @@ -132,10 +126,9 @@ public void addStatusMapping(String statusCode, HttpStatus httpStatus) { @RequestMapping @ResponseBody public Object invoke(Principal principal) { - if (!this.delegate.isEnabled()) { + if (!getDelegate().isEnabled()) { // Shouldn't happen because the request mapping should not be registered - return new ResponseEntity>(Collections.singletonMap( - "message", "This endpoint is disabled"), HttpStatus.NOT_FOUND); + return getDisabledResponse(); } Health health = getHealth(principal); HttpStatus status = getStatus(health); @@ -163,7 +156,7 @@ private Health getHealth(Principal principal) { long accessTime = System.currentTimeMillis(); if (isCacheStale(accessTime)) { this.lastAccess = accessTime; - this.cached = this.delegate.invoke(); + this.cached = getDelegate().invoke(); } if (exposeHealthDetails(principal)) { return this.cached; @@ -175,7 +168,7 @@ private boolean isCacheStale(long accessTime) { if (this.cached == null) { return true; } - return (accessTime - this.lastAccess) >= this.delegate.getTimeToLive(); + return (accessTime - this.lastAccess) >= getDelegate().getTimeToLive(); } private boolean exposeHealthDetails(Principal principal) { @@ -209,30 +202,4 @@ private boolean isUnrestricted() { return !this.secure && !Boolean.TRUE.equals(sensitive); } - @Override - public String getPath() { - return (this.path != null ? this.path : "/" + this.delegate.getId()); - } - - public void setPath(String path) { - while (path.endsWith("/")) { - path = path.substring(0, path.length() - 1); - } - if (!path.startsWith("/")) { - path = "/" + path; - } - this.path = path; - } - - @Override - public boolean isSensitive() { - return this.delegate.isSensitive(); - } - - @Override - @SuppressWarnings("rawtypes") - public Class getEndpointType() { - return this.delegate.getClass(); - } - }