Support relaxed HealthMvcEndpoint status mappings

Update HealthMvcEndpoint so that relaxed names can be used as keys in
the `endpoints.health.mapping` property.

Fixes gh-2465
pull/4042/head
Phillip Webb 9 years ago
parent 49583c0aa3
commit 3b93a82dd6

@ -25,6 +25,7 @@ import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.actuate.endpoint.HealthEndpoint; import org.springframework.boot.actuate.endpoint.HealthEndpoint;
import org.springframework.boot.actuate.health.Health; import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.Status; import org.springframework.boot.actuate.health.Status;
import org.springframework.boot.bind.RelaxedNames;
import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware; import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
@ -128,13 +129,27 @@ public class HealthMvcEndpoint implements MvcEndpoint, EnvironmentAware {
"message", "This endpoint is disabled"), HttpStatus.NOT_FOUND); "message", "This endpoint is disabled"), HttpStatus.NOT_FOUND);
} }
Health health = getHealth(principal); Health health = getHealth(principal);
HttpStatus status = this.statusMapping.get(health.getStatus().getCode()); HttpStatus status = getStatus(health);
if (status != null) { if (status != null) {
return new ResponseEntity<Health>(health, status); return new ResponseEntity<Health>(health, status);
} }
return health; return health;
} }
private HttpStatus getStatus(Health health) {
String code = health.getStatus().getCode();
if (code != null) {
code = code.toLowerCase().replace("_", "-");
for (String candidate : RelaxedNames.forCamelCase(code)) {
HttpStatus status = this.statusMapping.get(candidate);
if (status != null) {
return status;
}
}
}
return null;
}
private Health getHealth(Principal principal) { private Health getHealth(Principal principal) {
long accessTime = System.currentTimeMillis(); long accessTime = System.currentTimeMillis();
if (isCacheStale(accessTime)) { if (isCacheStale(accessTime)) {

@ -91,8 +91,8 @@ public class HealthMvcEndpointTests {
assertEquals(HttpStatus.SERVICE_UNAVAILABLE, response.getStatusCode()); assertEquals(HttpStatus.SERVICE_UNAVAILABLE, response.getStatusCode());
} }
@SuppressWarnings("unchecked")
@Test @Test
@SuppressWarnings("unchecked")
public void customMapping() { public void customMapping() {
given(this.endpoint.invoke()).willReturn( given(this.endpoint.invoke()).willReturn(
new Health.Builder().status("OK").build()); new Health.Builder().status("OK").build());
@ -105,6 +105,20 @@ public class HealthMvcEndpointTests {
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
} }
@Test
@SuppressWarnings("unchecked")
public void customMappingWithRelaxedName() {
given(this.endpoint.invoke()).willReturn(
new Health.Builder().outOfService().build());
this.mvc.setStatusMapping(Collections.singletonMap("out-of-service",
HttpStatus.INTERNAL_SERVER_ERROR));
Object result = this.mvc.invoke(null);
assertTrue(result instanceof ResponseEntity);
ResponseEntity<Health> response = (ResponseEntity<Health>) result;
assertTrue(response.getBody().getStatus().equals(Status.OUT_OF_SERVICE));
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
}
@Test @Test
public void secure() { public void secure() {
given(this.endpoint.invoke()).willReturn( given(this.endpoint.invoke()).willReturn(

Loading…
Cancel
Save