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