src/java.management.rest/share/classes/com/oracle/jmx/remote/rest/http/HttpResponse.java
branchjmx-rest-api
changeset 55995 a798bdd52997
parent 55994 9721e36abeb0
child 55997 f881344569d9
equal deleted inserted replaced
55994:9721e36abeb0 55995:a798bdd52997
    29     public static final HttpResponse REQUEST_NOT_FOUND = new HttpResponse(HttpURLConnection.HTTP_NOT_FOUND, "Request not found");
    29     public static final HttpResponse REQUEST_NOT_FOUND = new HttpResponse(HttpURLConnection.HTTP_NOT_FOUND, "Request not found");
    30 
    30 
    31     private final int code;
    31     private final int code;
    32     private final String message;
    32     private final String message;
    33     private final String detail;
    33     private final String detail;
       
    34     private final String body;
    34 
    35 
    35     public HttpResponse(int code, String message) {
    36     public HttpResponse(int code, String message) {
    36         this(code, message, "");
    37         this(code, message, "");
    37     }
    38     }
    38 
    39 
    39     public HttpResponse(int code, String message, String detail) {
    40     public HttpResponse(int code, String message, String detail) {
    40         this.code = code;
    41         this.code = code;
    41         this.message = message;
    42         this.message = message;
    42         this.detail = detail;
    43         this.detail = detail;
       
    44 
       
    45         if (code != HttpURLConnection.HTTP_OK) {
       
    46             JSONObject jobj = new JSONObject();
       
    47             jobj.put("status", new JSONPrimitive(code));
       
    48             jobj.put("message", new JSONPrimitive(message));
       
    49             if (detail != null && !detail.isEmpty()) {
       
    50                 jobj.put("details", new JSONPrimitive(detail));
       
    51             }
       
    52             this.body = jobj.toJsonString();
       
    53         } else {
       
    54             this.body = message;
       
    55         }
    43     }
    56     }
    44 
    57 
    45     public HttpResponse(HttpResponse response, String detail) {
    58     public HttpResponse(HttpResponse response, String detail) {
    46         this.code = response.code;
    59         this(response.code, response.message, detail);
    47         this.message = response.message;
       
    48         this.detail = detail;
       
    49     }
    60     }
    50 
    61 
    51     public int getCode() {
    62     public int getCode() {
    52         return code;
    63         return code;
    53     }
    64     }
    54 
    65 
    55     public String getResponse() {
    66     public String getBody() {
    56         if(code != HttpURLConnection.HTTP_OK) {
    67         return body;
    57             JSONObject jobj = new JSONObject();
       
    58             jobj.put("status",new JSONPrimitive(code));
       
    59             jobj.put("message",new JSONPrimitive(message));
       
    60             if(detail != null && !detail.isEmpty()) {
       
    61                 jobj.put("details", new JSONPrimitive(detail));
       
    62             }
       
    63             return jobj.toJsonString();
       
    64         }
       
    65         return message;
       
    66     }
    68     }
    67 
    69 
    68     static int getHttpErrorCode(Exception ex) {
    70     static int getHttpErrorCode(Exception ex) {
    69         if (ex instanceof JSONDataException
    71         if (ex instanceof JSONDataException
    70                 || ex instanceof ParseException || ex instanceof IllegalArgumentException) {
    72                 || ex instanceof ParseException || ex instanceof IllegalArgumentException) {