JSONResponse.java
3.91 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.servlet.http.HttpServletResponse
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.io.JSONWriter
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.crx.packaging;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
public class JSONResponse {
private static final Logger log = LoggerFactory.getLogger(JSONResponse.class);
public static final String TEXT_HTML_UTF8 = "text/html; charset=utf-8";
public static final String APPLICATION_JSON_UTF8 = "application/json; charset=utf-8";
private int status = 200;
private boolean success = true;
private String message = "";
private String path = "";
private String data = "";
private String location = "";
private String contentType = "application/json; charset=utf-8";
private boolean jsonInTextarea = false;
public JSONResponse() {
}
public JSONResponse(boolean success, String message) {
this.success = success;
this.message = message;
this.status = success ? 200 : 500;
}
public JSONResponse(boolean success, String message, String path) {
this.success = success;
this.message = message;
this.status = success ? 200 : 500;
this.path = path;
}
public int getStatus() {
return this.status;
}
public void setStatus(int status) {
this.status = status;
}
public boolean isSuccess() {
return this.success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getMessage() {
return this.message;
}
public void setMessage(String message) {
this.message = message;
}
public String getPath() {
return this.path;
}
public void setPath(String path) {
this.path = path;
}
public String getData() {
return this.data;
}
public void setData(String data) {
this.data = data;
}
public String getLocation() {
return this.location;
}
public void setLocation(String location) {
this.location = location;
}
public void writeJsonInTextarea(boolean v) {
if (v) {
this.contentType = "text/html; charset=utf-8";
this.jsonInTextarea = true;
}
}
public void send(HttpServletResponse response, boolean setStatus) throws IOException {
if (setStatus) {
response.setStatus(this.status);
if (this.status == 201) {
response.setHeader("Location", this.location);
}
}
response.setContentType(this.contentType);
PrintWriter out = response.getWriter();
if (this.jsonInTextarea) {
out.print("<textarea>");
}
JSONWriter w = new JSONWriter((Writer)out);
try {
w.object();
w.key("success").value(this.success);
if (this.message.length() > 0) {
w.key("msg").value((Object)this.message);
}
if (this.path.length() > 0) {
w.key("path").value((Object)this.path);
}
if (this.data.length() > 0) {
w.key("data").value((Object)this.data);
}
if (this.location.length() > 0) {
w.key("location").value((Object)this.location);
}
w.endObject();
}
catch (JSONException e) {
log.error("Error while writing json.");
throw new IOException(e.toString());
}
if (this.jsonInTextarea) {
out.print("</textarea>");
}
}
}