1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.cubby.action;
17
18 import java.io.Writer;
19 import java.util.Collection;
20 import java.util.Map;
21
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
24
25 import org.seasar.cubby.internal.util.StringUtils;
26 import org.seasar.cubby.spi.JsonProvider;
27 import org.seasar.cubby.spi.ProviderFactory;
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 public class Json implements ActionResult {
74
75
76 private final Object bean;
77
78
79 private final JsonProvider jsonProvider;
80
81
82 private final String calllback;
83
84
85 private String contentType = "text/javascript";
86
87
88 private String encoding = "utf-8";
89
90
91 private boolean xjson = false;
92
93
94
95
96
97
98
99 public Json(final Object bean) {
100 this(bean, null, ProviderFactory.get(JsonProvider.class));
101 }
102
103
104
105
106
107
108
109
110
111 public Json(final Object bean, final JsonProvider jsonProvider) {
112 this(bean, null, jsonProvider);
113 }
114
115
116
117
118
119
120
121
122
123 public Json(final Object bean, final String callback) {
124 this(bean, callback, ProviderFactory.get(JsonProvider.class));
125 }
126
127
128
129
130
131
132
133
134
135
136
137 public Json(final Object bean, final String callback,
138 final JsonProvider jsonProvider) {
139 if (jsonProvider == null) {
140 throw new NullPointerException("jsonProvider");
141 }
142 this.bean = bean;
143 this.calllback = callback;
144 this.jsonProvider = jsonProvider;
145 }
146
147
148
149
150
151
152 public Object getBean() {
153 return this.bean;
154 }
155
156
157
158
159
160
161 public String getCallback() {
162 return this.calllback;
163 }
164
165
166
167
168
169
170
171
172 public Json contentType(final String contentType) {
173 this.contentType = contentType;
174 return this;
175 }
176
177
178
179
180
181
182 public String getContentType() {
183 return this.contentType;
184 }
185
186
187
188
189
190
191
192
193
194
195
196 public Json encoding(final String encoding) {
197 this.encoding = encoding;
198 return this;
199 }
200
201
202
203
204
205
206 public String getEncoding() {
207 return this.encoding;
208 }
209
210
211
212
213
214
215
216
217
218
219 public void xjson() {
220 this.xjson = true;
221 }
222
223
224
225
226
227
228
229 public boolean isXjson() {
230 return xjson;
231 }
232
233
234
235
236 public void execute(final ActionContext actionContext,
237 final HttpServletRequest request, final HttpServletResponse response)
238 throws Exception {
239 response.setCharacterEncoding(this.encoding);
240 response
241 .setContentType(this.contentType + "; charset=" + this.encoding);
242 response.setHeader("Cache-Control", "no-cache");
243 response.setHeader("Pragma", "no-cache");
244
245 final String script;
246 if (isJsonp()) {
247 script = appendCallbackFunction(jsonProvider.toJson(bean),
248 calllback);
249 } else {
250 script = jsonProvider.toJson(bean);
251 }
252
253 if (xjson) {
254 response.setHeader("X-JSON", script);
255 } else {
256 final Writer writer = response.getWriter();
257 writer.write(script);
258 writer.flush();
259 }
260 }
261
262
263
264
265
266
267 private boolean isJsonp() {
268 return !StringUtils.isEmpty(calllback);
269 }
270
271
272
273
274
275
276
277
278
279
280 private static String appendCallbackFunction(final String script,
281 final String callback) {
282 final StringBuilder builder = new StringBuilder(script.length()
283 + callback.length() + 10);
284 builder.append(callback);
285 builder.append("(");
286 builder.append(script);
287 builder.append(");");
288 return builder.toString();
289 }
290
291 }