Introduce AbstractEndpointMvcAdapter
Pull up functionality from EndpointMvcAdapter to a new AbstractEndpointMvcAdapter which doesn't define any @RequestMappings and update HealthMvcEndpoint to make use of it.
This commit is contained in:
parent
c3b7764b72
commit
e272b3a957
|
@ -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 <E> The delegate endpoint
|
||||
* @author Dave Syer
|
||||
* @author Andy Wilkinson
|
||||
* @author Phillip Webb
|
||||
* @since 1.3.0
|
||||
*/
|
||||
public abstract class AbstractEndpointMvcAdapter<E extends Endpoint<?>> 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<? extends Endpoint> 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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
|||
* @author Dave Syer
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
public class EndpointMvcAdapter implements MvcEndpoint {
|
||||
|
||||
private final Endpoint<?> delegate;
|
||||
|
||||
private String path;
|
||||
public class EndpointMvcAdapter extends AbstractEndpointMvcAdapter<Endpoint<?>> {
|
||||
|
||||
/**
|
||||
* 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<? extends Endpoint> 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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
|||
* @author Phillip Webb
|
||||
* @since 1.1.0
|
||||
*/
|
||||
public class HealthMvcEndpoint implements MvcEndpoint, EnvironmentAware {
|
||||
|
||||
private final HealthEndpoint delegate;
|
||||
public class HealthMvcEndpoint extends AbstractEndpointMvcAdapter<HealthEndpoint>
|
||||
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 class HealthMvcEndpoint implements MvcEndpoint, EnvironmentAware {
|
|||
@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<Map<String, String>>(Collections.singletonMap(
|
||||
"message", "This endpoint is disabled"), HttpStatus.NOT_FOUND);
|
||||
return getDisabledResponse();
|
||||
}
|
||||
Health health = getHealth(principal);
|
||||
HttpStatus status = getStatus(health);
|
||||
|
@ -163,7 +156,7 @@ public class HealthMvcEndpoint implements MvcEndpoint, EnvironmentAware {
|
|||
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 @@ public class HealthMvcEndpoint implements MvcEndpoint, EnvironmentAware {
|
|||
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 @@ public class HealthMvcEndpoint implements MvcEndpoint, EnvironmentAware {
|
|||
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<? extends Endpoint> getEndpointType() {
|
||||
return this.delegate.getClass();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue