src/java.management.rest/share/classes/com/oracle/jmx/remote/rest/http/HttpResponse.java
branchjmx-rest-api
changeset 56026 bd531f08d7c7
parent 56007 d6cbabcaf518
child 56027 81372436b79e
equal deleted inserted replaced
56007:d6cbabcaf518 56026:bd531f08d7c7
     1 /*
       
     2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package com.oracle.jmx.remote.rest.http;
       
    27 
       
    28 import javax.management.AttributeNotFoundException;
       
    29 import javax.management.InstanceNotFoundException;
       
    30 import javax.management.ReflectionException;
       
    31 import com.oracle.jmx.remote.rest.json.JSONElement;
       
    32 import com.oracle.jmx.remote.rest.json.JSONObject;
       
    33 import com.oracle.jmx.remote.rest.json.JSONPrimitive;
       
    34 import com.oracle.jmx.remote.rest.mapper.JSONDataException;
       
    35 import com.oracle.jmx.remote.rest.mapper.JSONMappingException;
       
    36 import com.oracle.jmx.remote.rest.json.parser.ParseException;
       
    37 import java.io.PrintWriter;
       
    38 import java.io.StringWriter;
       
    39 import java.net.HttpURLConnection;
       
    40 
       
    41 /**
       
    42  * @author harsha
       
    43  */
       
    44 public class HttpResponse {
       
    45 
       
    46     public static final HttpResponse OK = new HttpResponse(HttpURLConnection.HTTP_OK, "Success");
       
    47     public static final HttpResponse SERVER_ERROR = new HttpResponse(HttpURLConnection.HTTP_INTERNAL_ERROR, "Internal server error");
       
    48     public static final HttpResponse METHOD_NOT_ALLOWED = new HttpResponse(HttpURLConnection.HTTP_BAD_METHOD, "Method not allowed");
       
    49     public static final HttpResponse BAD_REQUEST = new HttpResponse(HttpURLConnection.HTTP_BAD_REQUEST, "Bad request");
       
    50     public static final HttpResponse REQUEST_NOT_FOUND = new HttpResponse(HttpURLConnection.HTTP_NOT_FOUND, "Request not found");
       
    51 
       
    52     private final int code;
       
    53     private final String message;
       
    54     private final String body;
       
    55 
       
    56     public HttpResponse(int code, String message) {
       
    57         this(code, message, "");
       
    58     }
       
    59 
       
    60     public HttpResponse(String message) {
       
    61         this(200,message,"");
       
    62     }
       
    63 
       
    64     public HttpResponse(int code, String message, String detail) {
       
    65         this.code = code;
       
    66         this.message = message;
       
    67 
       
    68         if (code != HttpURLConnection.HTTP_OK) {
       
    69             JSONObject jobj = new JSONObject();
       
    70             jobj.put("status", new JSONPrimitive(code));
       
    71             jobj.put("message", new JSONPrimitive(message));
       
    72             if (detail != null && !detail.isEmpty()) {
       
    73                 jobj.put("details", new JSONPrimitive(detail));
       
    74             }
       
    75             this.body = jobj.toJsonString();
       
    76         } else {
       
    77             this.body = message;
       
    78         }
       
    79     }
       
    80 
       
    81     public HttpResponse(HttpResponse response, String detail) {
       
    82         this(response.code, response.message, detail);
       
    83     }
       
    84 
       
    85     public int getCode() {
       
    86         return code;
       
    87     }
       
    88 
       
    89     public String getBody() {
       
    90         return body;
       
    91     }
       
    92 
       
    93     static int getHttpErrorCode(Exception ex) {
       
    94         if (ex instanceof JSONDataException
       
    95                 || ex instanceof ParseException || ex instanceof IllegalArgumentException) {
       
    96             return HttpURLConnection.HTTP_BAD_REQUEST;
       
    97         } else if (ex instanceof JSONMappingException) {
       
    98             return HttpURLConnection.HTTP_INTERNAL_ERROR;
       
    99         } else if (ex instanceof AttributeNotFoundException
       
   100                 || ex instanceof InstanceNotFoundException
       
   101                 || ex instanceof ReflectionException) {
       
   102             return HttpURLConnection.HTTP_NOT_FOUND;
       
   103         }
       
   104         return HttpURLConnection.HTTP_BAD_REQUEST;
       
   105     }
       
   106 
       
   107     public static String getErrorMessage(Exception ex) {
       
   108         StringWriter sw = new StringWriter();
       
   109         PrintWriter pw = new PrintWriter(sw);
       
   110         ex.printStackTrace(pw);
       
   111         return sw.toString();
       
   112     }
       
   113 
       
   114     static JSONObject getJsonObject(int code, JSONElement request, JSONElement response) {
       
   115         JSONObject jobj = new JSONObject();
       
   116         jobj.put("status", new JSONPrimitive(code));
       
   117         jobj.put("request", request);
       
   118         jobj.put("response", response);
       
   119         return jobj;
       
   120     }
       
   121 }