src/java.management.rest/share/classes/com/oracle/jmx/remote/rest/http/HttpResponse.java
author hb
Wed, 27 Dec 2017 14:44:48 +0530
branchjmx-rest-api
changeset 55995 a798bdd52997
parent 55994 9721e36abeb0
child 55997 f881344569d9
permissions -rw-r--r--
POST : Attribute update - working POST : Bulk operation for MBean - working Exception to HTTP error Mapping
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
     1
/*
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
     2
 * To change this template, choose Tools | Templates
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
     3
 * and open the template in the editor.
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
     4
 */
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
     5
package com.oracle.jmx.remote.rest.http;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
     6
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
     7
import javax.management.AttributeNotFoundException;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
     8
import javax.management.InstanceNotFoundException;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
     9
import javax.management.ReflectionException;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    10
import com.oracle.jmx.remote.rest.json.JSONElement;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    11
import com.oracle.jmx.remote.rest.json.JSONObject;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    12
import com.oracle.jmx.remote.rest.json.JSONPrimitive;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    13
import com.oracle.jmx.remote.rest.mapper.JSONDataException;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    14
import com.oracle.jmx.remote.rest.mapper.JSONMappingException;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    15
import com.oracle.jmx.remote.rest.json.parser.ParseException;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    16
import java.io.PrintWriter;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    17
import java.io.StringWriter;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    18
import java.net.HttpURLConnection;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    19
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    20
/**
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    21
 * @author harsha
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    22
 */
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    23
public class HttpResponse {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    24
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    25
    public static final HttpResponse OK = new HttpResponse(HttpURLConnection.HTTP_OK, "Success");
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    26
    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
    27
    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
    28
    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
    29
    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
    30
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    31
    private final int code;
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    32
    private final String message;
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    33
    private final String detail;
55995
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
    34
    private final String body;
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    35
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    36
    public HttpResponse(int code, String message) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    37
        this(code, message, "");
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    38
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    39
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    40
    public HttpResponse(int code, String message, String detail) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    41
        this.code = code;
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    42
        this.message = message;
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    43
        this.detail = detail;
55995
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
    44
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
    45
        if (code != HttpURLConnection.HTTP_OK) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
    46
            JSONObject jobj = new JSONObject();
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
    47
            jobj.put("status", new JSONPrimitive(code));
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
    48
            jobj.put("message", new JSONPrimitive(message));
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
    49
            if (detail != null && !detail.isEmpty()) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
    50
                jobj.put("details", new JSONPrimitive(detail));
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
    51
            }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
    52
            this.body = jobj.toJsonString();
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
    53
        } else {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
    54
            this.body = message;
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
    55
        }
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
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    58
    public HttpResponse(HttpResponse response, String detail) {
55995
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
    59
        this(response.code, response.message, detail);
55994
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
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    62
    public int getCode() {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    63
        return code;
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    64
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    65
55995
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
    66
    public String getBody() {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
    67
        return body;
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    68
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    69
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    70
    static int getHttpErrorCode(Exception ex) {
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    71
        if (ex instanceof JSONDataException
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    72
                || ex instanceof ParseException || ex instanceof IllegalArgumentException) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    73
            return HttpURLConnection.HTTP_BAD_REQUEST;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    74
        } else if (ex instanceof JSONMappingException) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    75
            return HttpURLConnection.HTTP_INTERNAL_ERROR;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    76
        } else if (ex instanceof AttributeNotFoundException
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    77
                || ex instanceof InstanceNotFoundException
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    78
                || ex instanceof ReflectionException) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    79
            return HttpURLConnection.HTTP_NOT_FOUND;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    80
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    81
        return HttpURLConnection.HTTP_BAD_REQUEST;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    82
    }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    83
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    84
    public static String getErrorMessage(Exception ex) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    85
        StringWriter sw = new StringWriter();
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    86
        PrintWriter pw = new PrintWriter(sw);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    87
        ex.printStackTrace(pw);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    88
        return sw.toString();
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    89
    }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    90
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    91
    static JSONObject getJsonObject(int code, JSONElement request, JSONElement response) {
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    92
        JSONObject jobj = new JSONObject();
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    93
        jobj.put("status", new JSONPrimitive(code));
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    94
        jobj.put("request", request);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    95
        jobj.put("response", response);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    96
        return jobj;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    97
    }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    98
}