src/jdk.management.rest/share/classes/jdk/internal/management/remote/rest/http/HttpResponse.java
branchjmx-rest-api
changeset 56026 bd531f08d7c7
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 jdk.internal.management.remote.rest.http;
       
    27 
       
    28 import jdk.internal.management.remote.rest.json.JSONObject;
       
    29 import jdk.internal.management.remote.rest.json.JSONPrimitive;
       
    30 
       
    31 import java.io.PrintWriter;
       
    32 import java.io.StringWriter;
       
    33 import java.net.HttpURLConnection;
       
    34 
       
    35 /**
       
    36  * Container class for standard HTTP reponses. Returns a JSON string for status
       
    37  */
       
    38 public class HttpResponse {
       
    39 
       
    40     public static final HttpResponse OK = new HttpResponse(HttpURLConnection.HTTP_OK, "Success");
       
    41     public static final HttpResponse SERVER_ERROR = new HttpResponse(HttpURLConnection.HTTP_INTERNAL_ERROR, "Internal server error");
       
    42     public static final HttpResponse METHOD_NOT_ALLOWED = new HttpResponse(HttpURLConnection.HTTP_BAD_METHOD, "Method not allowed");
       
    43     public static final HttpResponse BAD_REQUEST = new HttpResponse(HttpURLConnection.HTTP_BAD_REQUEST, "Bad request");
       
    44     public static final HttpResponse REQUEST_NOT_FOUND = new HttpResponse(HttpURLConnection.HTTP_NOT_FOUND, "Request not found");
       
    45 
       
    46     private final int code;
       
    47     private final String message;
       
    48     private final String body;
       
    49 
       
    50     public HttpResponse(int code, String message) {
       
    51         this(code, message, "");
       
    52     }
       
    53 
       
    54     public HttpResponse(String message) {
       
    55         this(200,message,"");
       
    56     }
       
    57 
       
    58     public HttpResponse(int code, String message, String detail) {
       
    59         this.code = code;
       
    60         this.message = message;
       
    61 
       
    62         if (code != HttpURLConnection.HTTP_OK) {
       
    63             JSONObject jobj = new JSONObject();
       
    64             jobj.put("status", new JSONPrimitive(code));
       
    65             jobj.put("message", new JSONPrimitive(message));
       
    66             if (detail != null && !detail.isEmpty()) {
       
    67                 jobj.put("details", new JSONPrimitive(detail));
       
    68             }
       
    69             this.body = jobj.toJsonString();
       
    70         } else {
       
    71             this.body = message;
       
    72         }
       
    73     }
       
    74 
       
    75     public HttpResponse(HttpResponse response, String detail) {
       
    76         this(response.code, response.message, detail);
       
    77     }
       
    78 
       
    79     public int getCode() {
       
    80         return code;
       
    81     }
       
    82 
       
    83     public String getBody() {
       
    84         return body;
       
    85     }
       
    86 
       
    87     public static String getErrorMessage(Exception ex) {
       
    88         StringWriter sw = new StringWriter();
       
    89         PrintWriter pw = new PrintWriter(sw);
       
    90         ex.printStackTrace(pw);
       
    91         return sw.toString();
       
    92     }
       
    93 }