Skip to content

Commit

Permalink
Introduce AbstractEndpointMvcAdapter
Browse files Browse the repository at this point in the history
Pull up functionality from EndpointMvcAdapter to a new
AbstractEndpointMvcAdapter which doesn't define any @RequestMappings and
update HealthMvcEndpoint to make use of it.
  • Loading branch information
philwebb committed Oct 2, 2015
1 parent c3b7764 commit e272b3a
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 94 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<HealthEndpoint>
implements EnvironmentAware {

private final boolean secure;

Expand All @@ -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();
}
Expand Down Expand Up @@ -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<Map<String, String>>(Collections.singletonMap(
"message", "This endpoint is disabled"), HttpStatus.NOT_FOUND);
return getDisabledResponse();
}
Health health = getHealth(principal);
HttpStatus status = getStatus(health);
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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<? extends Endpoint> getEndpointType() {
return this.delegate.getClass();
}

}

0 comments on commit e272b3a

Please sign in to comment.