1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.googlecode.android_scripting.jsonrpc;
18 
19 import com.googlecode.android_scripting.Sl4aErrors;
20 import com.googlecode.android_scripting.Sl4aException;
21 
22 import org.json.JSONException;
23 import org.json.JSONObject;
24 
25 /**
26  * Represents a JSON RPC result.
27  *
28  * @see http://json-rpc.org/wiki/specification
29  */
30 public class JsonRpcResult {
31 
JsonRpcResult()32     private JsonRpcResult() {
33         // Utility class.
34     }
35 
36     /**
37      * Returns a JSON-RPC 1.0 formatted request response.
38      *
39      * @param id   the id given by the request
40      * @param data the data to pass into the result field
41      * @return a JSON-RPC 1.0 formatted request response
42      * @throws JSONException if JsonBuilder.build() cannot correctly convert the object into JSON
43      */
result(Object id, Object data)44     public static JSONObject result(Object id, Object data) throws JSONException {
45         JSONObject response = new JSONObject();
46         response.put("id", id);
47         response.put("result", JsonBuilder.build(data));
48         response.put("error", JSONObject.NULL);
49         return response;
50     }
51 
52     /**
53      * Returns a JSON-RPC 2.0 formatted error response.
54      *
55      * @param id      the id given by the request
56      * @param message the error message to send
57      * @return a JSON-RPC 2.0 formatted request response
58      * @throws JSONException if the world is ending
59      */
error(Object id, Object message)60     public static JSONObject error(Object id, Object message) throws JSONException {
61         JSONObject response = new JSONObject();
62         if (id == null) {
63             id = JSONObject.NULL;
64         }
65         response.put("id", id);
66         response.put("result", JSONObject.NULL);
67         response.put("error", message.toString());
68         return response;
69     }
70 
71     /**
72      * Returns a JSON-RPC 2.0 formatted error response.
73      *
74      * @param id            the id given by the request
75      * @param sl4aException the Sl4aException to send over JSON-RPC
76      * @return a JSON-RPC 2.0 formatted error response
77      * @throws JSONException if the world is ending
78      */
error(Object id, Sl4aException sl4aException)79     public static JSONObject error(Object id, Sl4aException sl4aException) throws JSONException {
80         JSONObject response = new JSONObject();
81         if (id == null) {
82             id = JSONObject.NULL;
83         }
84         response.put("id", id);
85         response.put("result", JSONObject.NULL);
86         JSONObject error = new JSONObject();
87         error.put("code", sl4aException.getErrorCode());
88         error.put("message", sl4aException.getErrorMessage());
89         response.put("error", error);
90         return response;
91     }
92 
93     /**
94      * Returns a JSON-RPC 2.0 formatted error response.
95      *
96      * @param id        the id given by the request
97      * @param sl4aError the Sl4aErrors object to send over JSON-RPC
98      * @param details   additional data associated with the error
99      * @return a JSON-RPC 1.0 formatted error response
100      * @throws JSONException if details cannot be converted to JSON
101      */
error(Object id, Sl4aErrors sl4aError, Object details)102     public static JSONObject error(Object id, Sl4aErrors sl4aError, Object details)
103             throws JSONException {
104         return error(id, sl4aError.getError(), details);
105     }
106 
107 
108     /**
109      * Returns a JSON-RPC 2.0 formatted error response.
110      *
111      * @param id            the id given by the request
112      * @param sl4aException the Sl4aException to send over JSON-RPC
113      * @param details       additional data associated with the error
114      * @return a JSON-RPC 2.0 formatted error response
115      * @throws JSONException if the world is ending
116      */
error(Object id, Sl4aException sl4aException, Object details)117     public static JSONObject error(Object id, Sl4aException sl4aException, Object details)
118             throws JSONException {
119         JSONObject returnValue = error(id, sl4aException);
120         returnValue.getJSONObject("error").put("data", details);
121         return returnValue;
122     }
123 
124     /**
125      * A function that returns a valid JSON-RPC error as a string when all else has failed.
126      *
127      * @return a String representation of {@see Sl4aErrors.JSON_RPC_UNKNOWN_EXCEPTION} in JSON
128      */
wtf()129     public static String wtf() {
130         Sl4aException exception = Sl4aErrors.JSON_RPC_UNKNOWN_EXCEPTION.getError();
131         return "{\"id\":null,"
132                 + "\"error\":{"
133                 + "\"code\":" + exception.getErrorCode() + ","
134                 + "\"message\":\"" + exception.getErrorMessage() + "\"}}";
135     }
136 }
137