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