Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Json |
|
| 1.2666666666666666;1.267 |
1 | /* | |
2 | * Copyright 2004-2009 the Seasar Foundation and the Others. | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | |
13 | * either express or implied. See the License for the specific language | |
14 | * governing permissions and limitations under the License. | |
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 | * JSON 形式のレスポンスを返す {@link ActionResult} です。 | |
31 | * <p> | |
32 | * アクションメソッドの戻り値としてこのインスタンスを指定することで、指定された JavaBean を JSON/JSONP | |
33 | * 形式に変換してレスポンスを返します。 ブラウザの JavaScript から発行されたリクエストを処理する場合等に使用してください。 JavaBean/ | |
34 | * {@link Map}/配列/{@link Collection}などがコンストラクタに渡すことができます。 | |
35 | * </p> | |
36 | * <p> | |
37 | * 使用例1 : JSON 形式のレスポンスを返す | |
38 | * | |
39 | * <pre> | |
40 | * MyBean bean = ...; | |
41 | * return new Json(bean); | |
42 | * </pre> | |
43 | * | |
44 | * </p> | |
45 | * <p> | |
46 | * 使用例2 : コールバック関数名を指定して JSONP 形式のレスポンスを返す | |
47 | * | |
48 | * <pre> | |
49 | * MyBean bean = ...; | |
50 | * return new Json(bean, "callback"); | |
51 | * </pre> | |
52 | * | |
53 | * </p> | |
54 | * <p> | |
55 | * 使用例3 : MIME タイプと文字コードを指定して JSON 形式のレスポンスを返す。<br> | |
56 | * 設定される MIME タイプは"text/javascript+json; charset=Shift_JIS"になります。 | |
57 | * | |
58 | * <pre> | |
59 | * MyBean bean = ...; | |
60 | * return new Json(bean).contentType("text/javascript+json").encoding("Shift_JIS"); | |
61 | * </pre> | |
62 | * | |
63 | * </p> | |
64 | * | |
65 | * @see <a | |
66 | * href="http://www.json.org/">JSON(JavaScript Object Notation)</a> | |
67 | * @see <a | |
68 | * href="http://ajaxian.com/archives/jsonp-json-with-padding">JSONP(JSON with Padding)</a> | |
69 | * @see JsonProvider#toJson(Object) | |
70 | * @author baba | |
71 | * @author agata | |
72 | * @since 1.0.0 | |
73 | */ | |
74 | public class Json implements ActionResult { | |
75 | ||
76 | /** 変換対象のオブジェクト。 */ | |
77 | private final Object bean; | |
78 | ||
79 | /** 変換に使用する {@link JsonProvider}。 */ | |
80 | private final JsonProvider jsonProvider; | |
81 | ||
82 | /** コールバック関数名。 */ | |
83 | private final String calllback; | |
84 | ||
85 | /** MIME タイプ。 */ | |
86 | 6 | private String contentType = "text/javascript"; |
87 | ||
88 | /** エンコーディング。 */ | |
89 | 6 | private String encoding = "utf-8"; |
90 | ||
91 | /** X-JSON 応答ヘッダを使用するか。 */ | |
92 | 6 | private boolean xjson = false; |
93 | ||
94 | /** | |
95 | * JSON 形式でレスポンスを返すインスタンスを生成します。 | |
96 | * | |
97 | * @param bean | |
98 | * JSON 形式に変換するオブジェクト | |
99 | */ | |
100 | public Json(final Object bean) { | |
101 | 6 | this(bean, null, ProviderFactory.get(JsonProvider.class)); |
102 | 6 | } |
103 | ||
104 | /** | |
105 | * JSON 形式でレスポンスを返すインスタンスを生成します。 | |
106 | * | |
107 | * @param bean | |
108 | * JSON 形式に変換するオブジェクト | |
109 | * @param jsonProvider | |
110 | * JSON のプロバイダ | |
111 | */ | |
112 | public Json(final Object bean, final JsonProvider jsonProvider) { | |
113 | 0 | this(bean, null, jsonProvider); |
114 | 0 | } |
115 | ||
116 | /** | |
117 | * JSONP 形式でレスポンスを返すインスタンスを生成します。 | |
118 | * | |
119 | * @param bean | |
120 | * JSONP 形式に変換するオブジェクト | |
121 | * @param callback | |
122 | * コールバック関数名 | |
123 | */ | |
124 | public Json(final Object bean, final String callback) { | |
125 | 0 | this(bean, callback, ProviderFactory.get(JsonProvider.class)); |
126 | 0 | } |
127 | ||
128 | /** | |
129 | * JSONP 形式でレスポンスを返すインスタンスを生成します。 | |
130 | * | |
131 | * @param bean | |
132 | * JSONP 形式に変換するオブジェクト | |
133 | * @param callback | |
134 | * コールバック関数名 | |
135 | * @param jsonProvider | |
136 | * JSON のプロバイダ | |
137 | */ | |
138 | public Json(final Object bean, final String callback, | |
139 | 6 | final JsonProvider jsonProvider) { |
140 | 6 | if (jsonProvider == null) { |
141 | 0 | throw new NullPointerException("jsonProvider"); |
142 | } | |
143 | 6 | this.bean = bean; |
144 | 6 | this.calllback = callback; |
145 | 6 | this.jsonProvider = jsonProvider; |
146 | 6 | } |
147 | ||
148 | /** | |
149 | * JSON 形式に変換する JavaBeanを取得します。 | |
150 | * | |
151 | * @return JSON 形式に変換する JavaBean | |
152 | */ | |
153 | public Object getBean() { | |
154 | 0 | return this.bean; |
155 | } | |
156 | ||
157 | /** | |
158 | * コールバック関数名を取得します。 | |
159 | * | |
160 | * @return コールバック関数名 | |
161 | */ | |
162 | public String getCallback() { | |
163 | 0 | return this.calllback; |
164 | } | |
165 | ||
166 | /** | |
167 | * MIME タイプを設定します。 | |
168 | * | |
169 | * @param contentType | |
170 | * MIME タイプ (例:"text/javascript+json") | |
171 | * @return {@link Json} | |
172 | */ | |
173 | public Json contentType(final String contentType) { | |
174 | 3 | this.contentType = contentType; |
175 | 3 | return this; |
176 | } | |
177 | ||
178 | /** | |
179 | * MIME タイプを取得します。 | |
180 | * | |
181 | * @return MIME タイプ | |
182 | */ | |
183 | public String getContentType() { | |
184 | 0 | return this.contentType; |
185 | } | |
186 | ||
187 | /** | |
188 | * エンコーディングを設定します。 | |
189 | * <p> | |
190 | * 設定されたエンコーディングは MIME タイプの charset として使用されます。 | |
191 | * </p> | |
192 | * | |
193 | * @param encoding | |
194 | * エンコーディング (例:"Shift_JIS") | |
195 | * @return {@link Json} | |
196 | */ | |
197 | public Json encoding(final String encoding) { | |
198 | 3 | this.encoding = encoding; |
199 | 3 | return this; |
200 | } | |
201 | ||
202 | /** | |
203 | * エンコーディングを取得します。 | |
204 | * | |
205 | * @return エンコーディング | |
206 | */ | |
207 | public String getEncoding() { | |
208 | 0 | return this.encoding; |
209 | } | |
210 | ||
211 | /** | |
212 | * JSON 文字列を応答ボディではなく X-JSON 応答ヘッダに設定することを指定します。 | |
213 | * <p> | |
214 | * prototype.js の <code>Ajax.Request</code> を使うときに使用してください。 | |
215 | * </p> | |
216 | * | |
217 | * @see <a | |
218 | * href="http://www.prototypejs.org/api/ajax/options">www.prototypejs.org - Ajax Options</a> | |
219 | */ | |
220 | public void xjson() { | |
221 | 0 | this.xjson = true; |
222 | 0 | } |
223 | ||
224 | /** | |
225 | * JSON 文字列を X-JOSN 応答ヘッダに設定するかを示します。 | |
226 | * | |
227 | * @return JSON 文字列を X-JOSN 応答ヘッダに設定する場合は <code>true</code>、そうでない場合は | |
228 | * <code>false</code> | |
229 | */ | |
230 | public boolean isXjson() { | |
231 | 0 | return xjson; |
232 | } | |
233 | ||
234 | /** | |
235 | * {@inheritDoc} | |
236 | */ | |
237 | public void execute(final ActionContext actionContext, | |
238 | final HttpServletRequest request, final HttpServletResponse response) | |
239 | throws Exception { | |
240 | 6 | response.setCharacterEncoding(this.encoding); |
241 | 6 | response |
242 | .setContentType(this.contentType + "; charset=" + this.encoding); | |
243 | 6 | response.setHeader("Cache-Control", "no-cache"); |
244 | 6 | response.setHeader("Pragma", "no-cache"); |
245 | ||
246 | final String script; | |
247 | 6 | if (isJsonp()) { |
248 | 0 | script = appendCallbackFunction(jsonProvider.toJson(bean), |
249 | calllback); | |
250 | } else { | |
251 | 6 | script = jsonProvider.toJson(bean); |
252 | } | |
253 | ||
254 | 6 | if (xjson) { |
255 | 0 | response.setHeader("X-JSON", script); |
256 | } else { | |
257 | 6 | final Writer writer = response.getWriter(); |
258 | 6 | writer.write(script); |
259 | 6 | writer.flush(); |
260 | } | |
261 | 6 | } |
262 | ||
263 | /** | |
264 | * JSONP 形式に変換するかどうかを示します。 | |
265 | * | |
266 | * @return JSONP 形式に変換する場合は <code>true</code>、そうでない場合は <code>false</code> | |
267 | */ | |
268 | private boolean isJsonp() { | |
269 | 6 | return !StringUtils.isEmpty(calllback); |
270 | } | |
271 | ||
272 | /** | |
273 | * JSON 形式のスクリプトに指定されたコールバック関数を付加します。 | |
274 | * | |
275 | * @param script | |
276 | * JSON 形式のスクリプト | |
277 | * @param callback | |
278 | * コールバック関数名 | |
279 | * @return コールバック関数が追加された JSON 形式のスクリプト | |
280 | */ | |
281 | private static String appendCallbackFunction(final String script, | |
282 | final String callback) { | |
283 | 0 | final StringBuilder builder = new StringBuilder(script.length() |
284 | + callback.length() + 10); | |
285 | 0 | builder.append(callback); |
286 | 0 | builder.append("("); |
287 | 0 | builder.append(script); |
288 | 0 | builder.append(");"); |
289 | 0 | return builder.toString(); |
290 | } | |
291 | ||
292 | } |