]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/jsonrpc/JsonRpcResponse.java
Implement jsonRpc command
[signal-cli] / src / main / java / org / asamk / signal / jsonrpc / JsonRpcResponse.java
1 package org.asamk.signal.jsonrpc;
2
3 import com.fasterxml.jackson.annotation.JsonInclude;
4 import com.fasterxml.jackson.databind.JsonNode;
5 import com.fasterxml.jackson.databind.node.ValueNode;
6
7 /**
8 * Represents a JSON-RPC response.
9 * https://www.jsonrpc.org/specification#response_object
10 */
11 public class JsonRpcResponse extends JsonRpcMessage {
12
13 /**
14 * A String specifying the version of the JSON-RPC protocol. MUST be exactly "2.0".
15 */
16 String jsonrpc;
17
18 /**
19 * This member is REQUIRED on success.
20 * This member MUST NOT exist if there was an error invoking the method.
21 * The value of this member is determined by the method invoked on the Server.
22 */
23 @JsonInclude(JsonInclude.Include.NON_NULL)
24 JsonNode result;
25
26 /**
27 * This member is REQUIRED on error.
28 * This member MUST NOT exist if there was no error triggered during invocation.
29 * The value for this member MUST be an Object as defined in section 5.1.
30 */
31 @JsonInclude(JsonInclude.Include.NON_NULL)
32 Error error;
33
34 /**
35 * This member is REQUIRED.
36 * It MUST be the same as the value of the id member in the Request Object.
37 * If there was an error in detecting the id in the Request object (e.g. Parse error/Invalid Request), it MUST be Null.
38 */
39 ValueNode id;
40
41 public static JsonRpcResponse forSuccess(JsonNode result, ValueNode id) {
42 return new JsonRpcResponse("2.0", result, null, id);
43 }
44
45 public static JsonRpcResponse forError(Error error, ValueNode id) {
46 return new JsonRpcResponse("2.0", null, error, id);
47 }
48
49 private JsonRpcResponse() {
50 }
51
52 private JsonRpcResponse(final String jsonrpc, final JsonNode result, final Error error, final ValueNode id) {
53 this.jsonrpc = jsonrpc;
54 this.result = result;
55 this.error = error;
56 this.id = id;
57 }
58
59 public String getJsonrpc() {
60 return jsonrpc;
61 }
62
63 public JsonNode getResult() {
64 return result;
65 }
66
67 public Error getError() {
68 return error;
69 }
70
71 public ValueNode getId() {
72 return id;
73 }
74
75 public static class Error {
76
77 public static final int PARSE_ERROR = -32700;
78 public static final int INVALID_REQUEST = -32600;
79 public static final int METHOD_NOT_FOUND = -32601;
80 public static final int INVALID_PARAMS = -32602;
81 public static final int INTERNAL_ERROR = -32603;
82
83 /**
84 * A Number that indicates the error type that occurred.
85 * This MUST be an integer.
86 */
87 int code;
88
89 /**
90 * A String providing a short description of the error.
91 * The message SHOULD be limited to a concise single sentence.
92 */
93 String message;
94
95 /**
96 * A Primitive or Structured value that contains additional information about the error.
97 * This may be omitted.
98 * The value of this member is defined by the Server (e.g. detailed error information, nested errors etc.).
99 */
100 JsonNode data;
101
102 public Error(final int code, final String message, final JsonNode data) {
103 this.code = code;
104 this.message = message;
105 this.data = data;
106 }
107
108 public int getCode() {
109 return code;
110 }
111
112 public String getMessage() {
113 return message;
114 }
115
116 public JsonNode getData() {
117 return data;
118 }
119 }
120 }