3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.remote
.view
;
12 import java
.io
.PrintWriter
;
13 import java
.util
.Collection
;
15 import java
.util
.regex
.Matcher
;
16 import java
.util
.regex
.Pattern
;
18 import javax
.servlet
.http
.HttpServletRequest
;
19 import javax
.servlet
.http
.HttpServletResponse
;
21 import net
.sf
.json
.JSON
;
22 import net
.sf
.json
.JSONArray
;
23 import net
.sf
.json
.JSONObject
;
24 import net
.sf
.json
.JsonConfig
;
25 import net
.sf
.json
.xml
.XMLSerializer
;
27 import org
.apache
.log4j
.Logger
;
28 import org
.springframework
.web
.servlet
.View
;
31 public class JsonView
extends BaseView
implements View
{
33 public static final Logger logger
= Logger
.getLogger(JsonView
.class);
35 private JsonConfig jsonConfig
;
38 JSON("application/json"),
39 XML("application/xml");
41 private String contentType
;
43 Type(String contentType
){
44 this.contentType
= contentType
;
47 public String
getContentType(){
52 private Type type
= Type
.JSON
;
54 private String xsl
= null;
56 public void setXsl(String xsl
) {
60 public String
getXsl() {
64 public Type
getType() {
69 * Default is Type.JSON
72 public void setType(Type type
) {
76 public void setJsonConfig(JsonConfig jsonConfig
) {
77 this.jsonConfig
= jsonConfig
;
82 * @see org.springframework.web.servlet.View#getContentType()
84 public String
getContentType() {
85 return type
.getContentType();
88 public void render(Object entity
, PrintWriter writer
, String documentContextPath
, String jsonpCallback
) throws Exception
{
90 if(jsonConfig
== null){
91 logger
.error("The jsonConfig must not be null. It must be set in the applicationContext.");
95 boolean isCollectionType
= false;
98 jsonObj
= JSONObject
.fromObject("{}");
99 } else if(Collection
.class.isAssignableFrom(entity
.getClass())){
100 isCollectionType
= true;
101 jsonObj
= JSONArray
.fromObject(entity
, jsonConfig
);
102 }else if(entity
instanceof String
){
103 jsonObj
= JSONObject
.fromObject("{\"String\":\""+entity
.toString().replace("\"", "\\\"")+"\"}");
104 } else if(entity
instanceof Integer
){
105 jsonObj
= JSONObject
.fromObject("{\"Integer\":"+((Integer
)entity
).intValue()+"}");
106 } else if(entity
instanceof Boolean
){
107 jsonObj
= JSONObject
.fromObject("{\"Boolean\":"+((Boolean
)entity
).toString()+"}");
109 jsonObj
= JSONObject
.fromObject(entity
, jsonConfig
);
112 if(type
.equals(Type
.XML
)){
113 XMLSerializer xmlSerializer
= new XMLSerializer();
114 if(isCollectionType
){
115 xmlSerializer
.setArrayName(entity
.getClass().getSimpleName());
116 Class elementType
= Object
.class;
117 Collection c
= (Collection
)entity
;
119 elementType
= c
.iterator().next().getClass();
121 xmlSerializer
.setObjectName(elementType
.getSimpleName());
122 } else if(entity
!= null){
123 xmlSerializer
.setObjectName(entity
.getClass().getSimpleName());
125 String xml
= xmlSerializer
.write( jsonObj
);
126 if(type
.equals(Type
.XML
) && xsl
!= null){
127 if(documentContextPath
== null){
128 documentContextPath
= "";
130 String replace
= "\r\n<?xml-stylesheet type=\"text/xsl\" href=\"" + documentContextPath
+ "/" + xsl
+ "\"?>\r\n";
131 xml
= xml
.replaceFirst("\r\n", replace
);
136 if(jsonpCallback
!= null){
137 writer
.append(jsonpCallback
).append("(").append(jsonObj
.toString()).append(")");
139 writer
.append(jsonObj
.toString());
142 //TODO resp.setContentType(type);
148 * @see org.springframework.web.servlet.View#render(java.util.Map, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
150 public void render(Map model
, HttpServletRequest request
, HttpServletResponse response
) throws Exception
{
152 // Retrieve data from model
153 Object entity
= getResponseData(model
);
156 response
.setContentType(type
.getContentType());
158 PrintWriter writer
= response
.getWriter();
160 // read jsonp parameter from query string
161 String
[] tokens
= request
.getQueryString().split("&", 0);
162 String jsonpParamName
= "callback";
163 String jsonpCallback
= null;
164 for (int i
= 0; i
< tokens
.length
; i
++) {
165 if(tokens
[i
].startsWith(jsonpParamName
)){
166 jsonpCallback
= tokens
[i
].substring(jsonpParamName
.length() + 1);
172 render(entity
, writer
, request
.getContextPath(), jsonpCallback
);