SirenResponse.java
4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.servlet.http.HttpServletResponse
* org.apache.sling.servlets.post.AbstractPostResponse
* org.json.JSONArray
* org.json.JSONException
* org.json.JSONObject
*/
package com.adobe.granite.rest.impl.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import javax.servlet.http.HttpServletResponse;
import org.apache.sling.servlets.post.AbstractPostResponse;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class SirenResponse
extends AbstractPostResponse {
private static final String PROP_PROPERTIES = "properties";
private static final String PROP_CHANGES = "changes";
private static final String PROP_TYPE = "type";
private static final String PROP_ARGUMENT = "argument";
private JSONObject json = new JSONObject();
private JSONObject properties = new JSONObject();
private JSONArray changes = new JSONArray();
private Throwable error;
public SirenResponse() throws JSONResponseException {
try {
this.json = new JSONObject();
this.json.put("class", (Object)new String[]{"core/response"});
this.properties = new JSONObject();
this.properties.put("changes", (Object)this.changes);
this.json.put("properties", (Object)this.properties);
}
catch (Exception e) {
throw new JSONResponseException(this, e);
}
}
public /* varargs */ void onChange(String type, String ... arguments) throws JSONResponseException {
try {
JSONObject change = new JSONObject();
change.put("type", (Object)type);
for (String argument : arguments) {
change.accumulate("argument", (Object)argument);
}
this.changes.put((Object)change);
}
catch (JSONException e) {
throw new JSONResponseException(this, (Throwable)e);
}
}
public void setError(Throwable error) {
try {
this.error = error;
JSONObject jsonError = new JSONObject();
jsonError.put("class", (Object)error.getClass().getName());
jsonError.put("message", (Object)error.getMessage());
this.properties.put("error", (Object)jsonError);
}
catch (JSONException e) {
throw new JSONResponseException(this, (Throwable)e);
}
}
public Throwable getError() {
return this.error;
}
public void setProperty(String name, Object value) {
try {
this.properties.put(name, value);
}
catch (Exception e) {
throw new JSONResponseException(this, "Error setting JSON property '" + name + "' to '" + value + "'", e);
}
}
public Object getProperty(String name) throws JSONResponseException {
try {
if (this.properties.has(name)) {
return this.properties.get(name);
}
return null;
}
catch (JSONException e) {
throw new JSONResponseException(this, "Error getting JSON property '" + name + "'", (Throwable)e);
}
}
protected void doSend(HttpServletResponse response) throws IOException {
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
try {
this.json.write((Writer)response.getWriter());
}
catch (JSONException e) {
IOException ioe = new IOException("Error creating JSON response");
ioe.initCause((Throwable)e);
throw ioe;
}
}
JSONObject getJson() {
return this.json;
}
public class JSONResponseException
extends RuntimeException {
final /* synthetic */ SirenResponse this$0;
public JSONResponseException(SirenResponse this$0, String message, Throwable exception) {
this.this$0 = this$0;
super(message, exception);
}
public JSONResponseException(SirenResponse this$0, Throwable e) {
this.this$0 = this$0;
super("Error building JSON response", e);
}
}
}