package edu.washington.escience.myriad.api;

import java.io.PrintWriter;
import java.io.StringWriter;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.Response.Status;

/**
 * A helper function to easily create HTTP error responses in the API server.
 * 
 * @author dhalperi
 * 
 */
public final class MyriaApiException extends WebApplicationException {

  /** Required for Java serialization. */
  private static final long serialVersionUID = 1L;

  /**
   * Construct a MyriaApiException from the given status and cause. The entity of the HTTP Response includes the given
   * HTTP status code and the body is the exception cause. If the status code is 500 INTERNAL SERVER ERROR then we
   * return the entire stack trace. Otherwise, we just return the message.
   * 
   * @param status the HTTP status code used for the HTTP response.
   * @param cause the Exception, whose message is used to explain the exception in the HTTP response.
   */
  public MyriaApiException(final Status status, final Throwable cause) {
    super(getResponse(status, cause));
  }

  /**
   * Construct a MyriaApiException from the given status and explanation. The entity of the HTTP Response includes the
   * given HTTP status code and the body is the provided explanation string.
   * 
   * @param status the HTTP status code used for the HTTP response.
   * @param explanation the message is used to explain the exception in the HTTP response.
   */
  public MyriaApiException(final Status status, final String explanation) {
    super(getResponse(status, explanation));
  }

  /**
   * Get the HTTP response for this status and cause. If the Status code is 500 (Internal Server Error) then we return
   * the entire stack trace. Otherwise, we just return the message.
   * 
   * @param status the HTTP status code to be returned.
   * @param cause the reason for the exception.
   * @return the HTTP Response.
   */
  private static Response getResponse(final Status status, final Throwable cause) {
    /* Pass MyriaApiException objects right on through. */
    if (cause instanceof MyriaApiException) {
      return ((MyriaApiException) cause).getResponse();
    }
    ResponseBuilder ret = Response.status(status);
    if (status.equals(Status.INTERNAL_SERVER_ERROR) || cause.getMessage() == null || cause.getMessage().length() == 0) {
      StringWriter stringWriter = new StringWriter();
      cause.printStackTrace(new PrintWriter(stringWriter));
      String stackTrace = stringWriter.toString();
      ret.entity(stackTrace);
    } else {
      ret.entity(cause.getMessage());
    }
    return ret.build();
  }

  /**
   * Get the HTTP response for this status and explanation.
   * 
   * @param status the HTTP status code of the response.
   * @param explanation the explanation of the HTTP error.
   * @return the Response.
   */
  private static Response getResponse(final Status status, final String explanation) {
    Response ret = Response.status(status).entity(explanation).build();
    return ret;
  }
}

@@ -266,16 +266,49 @@
 tatus;%0A%0A
+import org.slf4j.LoggerFactory;%0A%0A
 /**%0A * A
@@ -577,16 +577,151 @@
 ID = 1L;
+%0A  /** The logger for this class. */%0A  private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(MyriaApiException.class);
 %0A%0A  /**%0A
@@ -1319,24 +1319,97 @@
 s, cause));%0A
+    LOGGER.trace(%22In Status:Throwable (log will be after getResponse)%22);%0A
   %7D%0A%0A  /**%0A 
@@ -1895,16 +1895,86 @@
 tion));%0A
+    LOGGER.trace(%22In Status:String (log will be after getResponse)%22);%0A
   %7D%0A%0A  /
@@ -2496,24 +2496,85 @@
 xception) %7B%0A
+      LOGGER.trace(%22Here in getResponse::Status:Throwable%22);%0A
       return
@@ -3043,24 +3043,78 @@
 e());%0A    %7D%0A
+    LOGGER.trace(%22In getResponse::Status:Throwable%22);%0A
     return r
@@ -3434,24 +3434,24 @@
 lanation) %7B%0A
-
     Response
@@ -3506,24 +3506,75 @@
 n).build();%0A
+    LOGGER.trace(%22In getResponse::Status:String%22);%0A
     return r

add logging