package keywhiz.service.resources;

import com.codahale.metrics.annotation.Timed;
import com.codahale.metrics.annotation.ExceptionMetered;
import com.codahale.metrics.health.HealthCheck;
import com.codahale.metrics.health.HealthCheckRegistry;
import io.dropwizard.setup.Environment;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.InternalServerErrorException;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import static java.util.stream.Collectors.toList;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;

/** Serve status information */
@Path("/_status")
@Produces(APPLICATION_JSON)
public class StatusResource {
  private Environment environment;

  @Inject public StatusResource(Environment environment) {
    this.environment = environment;
  }

  @Timed @ExceptionMetered
  @GET
  public boolean get() {
    HealthCheckRegistry checks = this.environment.healthChecks();
    SortedMap<String, HealthCheck.Result> results = checks.runHealthChecks();

    List<String> failing = results.entrySet().stream()
        .filter(r -> !r.getValue().isHealthy())
        .map(Map.Entry::getKey)
        .collect(toList());

    if (!failing.isEmpty()) {
      throw new InternalServerErrorException(
          "failing health checks: " + Arrays.toString(failing.toArray()));
    }

    return true;
  }
}

@@ -68,19 +68,30 @@
 otation.
-Tim
+ExceptionMeter
 ed;%0Aimpo
@@ -121,38 +121,27 @@
 .annotation.
-ExceptionMeter
+Tim
 ed;%0Aimport c
@@ -236,16 +236,70 @@
 gistry;%0A
+import com.fasterxml.jackson.annotation.JsonProperty;%0A
 import i
@@ -484,57 +484,8 @@
 ET;%0A
-import javax.ws.rs.InternalServerErrorException;%0A
 impo
@@ -529,24 +529,115 @@
 rs.Produces;
+%0Aimport javax.ws.rs.core.Response;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Aimport sta
@@ -878,16 +878,110 @@
 ronment;
+%0A  private static final Logger logger = LoggerFactory.getLogger(SecretDeliveryResource.class);
 %0A%0A  @Inj
@@ -1074,16 +1074,365 @@
 t;%0A  %7D%0A%0A
+  public class StatusResponse %7B%0A    public String status;%0A    public String message;%0A    public SortedMap%3CString, HealthCheck.Result%3E results;%0A%0A    public StatusResponse(String status, String message, SortedMap%3CString, HealthCheck.Result%3E results) %7B%0A      this.status = status;%0A      this.message = message;%0A      this.results = results;%0A    %7D%0A  %7D%0A%0A
   @Timed
@@ -1470,15 +1470,16 @@
 lic 
-boolean
+Response
  get
@@ -1832,145 +1832,382 @@
     
-throw new InternalServerErrorException(%0A          %22failing health checks: %22 + Arrays.toString(failing.toArray()));%0A    %7D%0A%0A    return true
+logger.warn(%22Health checks failed: %7B%7D%22, results);%0A      String message = %22failing health checks: %22 + Arrays.toString(failing.toArray());%0A      StatusResponse sr = new StatusResponse(%22critical%22, message, results);%0A      return Response.serverError().entity(sr).build();%0A    %7D%0A    StatusResponse sr = new StatusResponse(%22ok%22, %22ok%22, results);%0A    return Response.ok(sr).build()
 ;%0A  

Make /_status always return JSON