package keywhiz.service.resources;

import com.codahale.metrics.annotation.ExceptionMetered;
import com.codahale.metrics.annotation.Timed;
import com.codahale.metrics.health.HealthCheck;
import com.codahale.metrics.health.HealthCheckRegistry;
import com.fasterxml.jackson.annotation.JsonProperty;
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.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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;
  private static final Logger logger = LoggerFactory.getLogger(SecretDeliveryResource.class);

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

  private static class StatusResponse {
    public String status;
    public String message;
    public SortedMap<String, HealthCheck.Result> results;

    StatusResponse(String status, String message, SortedMap<String, HealthCheck.Result> results) {
      this.status = status;
      this.message = message;
      this.results = results;
    }
  }

  @Timed @ExceptionMetered
  @GET
  public Response 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()) {
      logger.warn("Health checks failed: {}", results);
      String message = "failing health checks: " + Arrays.toString(failing.toArray());
      StatusResponse sr = new StatusResponse("critical", message, results);
      return Response.serverError().entity(sr).build();
    }
    StatusResponse sr = new StatusResponse("ok", "ok", results);
    return Response.ok(sr).build();
  }
}

@@ -240,62 +240,8 @@
 ry;%0A
-import com.fasterxml.jackson.annotation.JsonProperty;%0A
 impo
@@ -1019,30 +1019,29 @@
 nt;%0A  %7D%0A%0A  p
-rivate
+ublic
  static clas
@@ -1075,39 +1075,134 @@
 ic S
-tring status;%0A    public String
+ortedMap%3CString, HealthCheck.Result%3E getResults() %7B%0A      return results;%0A    %7D%0A%0A    public String getMessage() %7B%0A      return
  mes
@@ -1203,24 +1203,31 @@
 rn message;%0A
+    %7D%0A%0A
     public S
@@ -1217,32 +1217,148 @@
   %7D%0A%0A    public 
+String getStatus() %7B%0A      return status;%0A    %7D%0A%0A    private String status;%0A    private String message;%0A    private 
 SortedMap%3CString
@@ -1581,16 +1581,259 @@
 s;%0A    %7D
+%0A%0A    @Override public String toString() %7B%0A      return %22StatusResponse%7B%22 +%0A          %22status='%22 + this.getStatus() + '%5C'' +%0A          %22, message='%22 + this.getMessage() + '%5C'' +%0A          %22, results=%22 + this.getResults() +%0A          '%7D';%0A    %7D
 %0A  %7D%0A%0A  

add a toString for convenience. Make methods private and use getters intead. Because findbugs told me to.