KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > common > web > view > JsonView


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.common.web.view;
25
26 import java.io.PrintWriter JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletResponse JavaDoc;
31
32 import net.sf.json.JSONObject;
33
34 import org.springframework.web.servlet.view.AbstractView;
35
36 /**
37  * A View that renders the model as JSON object.
38  */

39 public class JsonView extends AbstractView {
40     
41     private static final String JavaDoc DEFAULT_CONTENT_TYPE =
42             "text/plain; charset=UTF-8";
43     
44     private static final String JavaDoc JSON_HEADER = "X-JSON";
45
46     private String JavaDoc characterEncoding = "UTF-8";
47
48     private boolean sendAsHeader;
49     
50     private String JavaDoc headerName = JSON_HEADER;
51     
52     public JsonView() {
53         this(false);
54     }
55     
56     public JsonView(boolean sendAsHeader) {
57         this.sendAsHeader = sendAsHeader;
58         setContentType(DEFAULT_CONTENT_TYPE);
59     }
60     
61     public void setSendAsHeader(boolean sendAsHeader) {
62         this.sendAsHeader = sendAsHeader;
63     }
64     
65     public void setHeaderName(String JavaDoc headerName) {
66         this.headerName = headerName;
67     }
68
69     public void setCharacterEncoding(String JavaDoc characterEncoding) {
70         this.characterEncoding = characterEncoding;
71     }
72
73     protected void renderMergedOutputModel(Map JavaDoc model,
74             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
75             throws Exception JavaDoc {
76         
77         response.setCharacterEncoding(characterEncoding);
78         JSONObject jsonObject = JSONObject.fromObject(model);
79         if (sendAsHeader) {
80             response.setHeader(headerName, jsonObject.toString());
81         }
82         else {
83             PrintWriter JavaDoc out = response.getWriter();
84             out.write('(');
85             out.write(jsonObject.toString());
86             out.write(')');
87         }
88     }
89 }
Popular Tags