1 |
2ce08010
|
Andreas Kohlbecker
|
// $Id$
|
2 |
|
|
/**
|
3 |
|
|
* Copyright (C) 2007 EDIT
|
4 |
045a007b
|
Andreas Kohlbecker
|
* European Distributed Institute of Taxonomy
|
5 |
2ce08010
|
Andreas Kohlbecker
|
* http://www.e-taxonomy.eu
|
6 |
045a007b
|
Andreas Kohlbecker
|
*
|
7 |
2ce08010
|
Andreas Kohlbecker
|
* 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.
|
9 |
|
|
*/
|
10 |
|
|
package eu.etaxonomy.cdm.remote.view;
|
11 |
|
|
|
12 |
|
|
import java.io.PrintWriter;
|
13 |
|
|
import java.util.Collection;
|
14 |
|
|
import java.util.Map;
|
15 |
|
|
|
16 |
|
|
import javax.servlet.http.HttpServletRequest;
|
17 |
|
|
import javax.servlet.http.HttpServletResponse;
|
18 |
|
|
|
19 |
|
|
import net.sf.json.JSON;
|
20 |
|
|
import net.sf.json.JSONArray;
|
21 |
|
|
import net.sf.json.JSONObject;
|
22 |
|
|
import net.sf.json.JsonConfig;
|
23 |
|
|
import net.sf.json.xml.XMLSerializer;
|
24 |
|
|
|
25 |
|
|
import org.apache.log4j.Logger;
|
26 |
|
|
import org.springframework.web.servlet.View;
|
27 |
|
|
|
28 |
86f9ed88
|
Andreas Kohlbecker
|
import eu.etaxonomy.cdm.remote.config.DataSourceProperties;
|
29 |
|
|
|
30 |
2ce08010
|
Andreas Kohlbecker
|
|
31 |
|
|
public class JsonView extends BaseView implements View{
|
32 |
045a007b
|
Andreas Kohlbecker
|
|
33 |
|
|
public static final Logger logger = Logger.getLogger(JsonView.class);
|
34 |
|
|
|
35 |
|
|
private JsonConfig jsonConfig;
|
36 |
|
|
|
37 |
86f9ed88
|
Andreas Kohlbecker
|
private DataSourceProperties dataSourceProperties;
|
38 |
|
|
|
39 |
|
|
public DataSourceProperties getDataSourceProperties() {
|
40 |
|
|
return dataSourceProperties;
|
41 |
|
|
}
|
42 |
|
|
|
43 |
|
|
public void setDataSourceProperties(DataSourceProperties dataSourceProperties) {
|
44 |
|
|
this.dataSourceProperties = dataSourceProperties;
|
45 |
|
|
}
|
46 |
|
|
|
47 |
045a007b
|
Andreas Kohlbecker
|
public enum Type{
|
48 |
|
|
JSON("application/json"),
|
49 |
|
|
XML("application/xml");
|
50 |
|
|
|
51 |
ad875511
|
Andreas Kohlbecker
|
private final String contentType;
|
52 |
045a007b
|
Andreas Kohlbecker
|
|
53 |
|
|
Type(String contentType){
|
54 |
|
|
this.contentType = contentType;
|
55 |
|
|
}
|
56 |
|
|
|
57 |
|
|
public String getContentType(){
|
58 |
|
|
return contentType;
|
59 |
|
|
}
|
60 |
|
|
}
|
61 |
|
|
|
62 |
|
|
private Type type = Type.JSON;
|
63 |
|
|
|
64 |
|
|
private String xsl = null;
|
65 |
|
|
|
66 |
|
|
public void setXsl(String xsl) {
|
67 |
|
|
this.xsl = xsl;
|
68 |
|
|
}
|
69 |
|
|
|
70 |
|
|
public String getXsl() {
|
71 |
|
|
return xsl;
|
72 |
|
|
}
|
73 |
|
|
|
74 |
|
|
public Type getType() {
|
75 |
|
|
return type;
|
76 |
|
|
}
|
77 |
|
|
|
78 |
|
|
/**
|
79 |
|
|
* Default is Type.JSON
|
80 |
|
|
* @param type
|
81 |
|
|
*/
|
82 |
|
|
public void setType(Type type) {
|
83 |
|
|
this.type = type;
|
84 |
|
|
}
|
85 |
|
|
|
86 |
|
|
public void setJsonConfig(JsonConfig jsonConfig) {
|
87 |
|
|
this.jsonConfig = jsonConfig;
|
88 |
|
|
}
|
89 |
|
|
|
90 |
|
|
/*
|
91 |
|
|
* (non-Javadoc)
|
92 |
|
|
* @see org.springframework.web.servlet.View#getContentType()
|
93 |
|
|
*/
|
94 |
ad875511
|
Andreas Kohlbecker
|
@Override
|
95 |
045a007b
|
Andreas Kohlbecker
|
public String getContentType() {
|
96 |
|
|
return type.getContentType();
|
97 |
|
|
}
|
98 |
|
|
|
99 |
f27c30c3
|
Andreas Kohlbecker
|
/* (non-Javadoc)
|
100 |
|
|
* @see eu.etaxonomy.cdm.remote.view.BaseView#render(java.lang.Object, java.io.PrintWriter, java.lang.String, java.lang.String)
|
101 |
|
|
*/
|
102 |
ad875511
|
Andreas Kohlbecker
|
@Override
|
103 |
f27c30c3
|
Andreas Kohlbecker
|
public void render(Object entity, PrintWriter writer, String jsonpCallback, HttpServletRequest request, HttpServletResponse response) throws Exception {
|
104 |
|
|
|
105 |
9a230c10
|
l.morris
|
String contextPath = null;
|
106 |
ad875511
|
Andreas Kohlbecker
|
|
107 |
9a230c10
|
l.morris
|
if (request != null)
|
108 |
|
|
{
|
109 |
|
|
contextPath = request.getContextPath();
|
110 |
|
|
}
|
111 |
045a007b
|
Andreas Kohlbecker
|
|
112 |
|
|
if(jsonConfig == null){
|
113 |
|
|
logger.error("The jsonConfig must not be null. It must be set in the applicationContext.");
|
114 |
|
|
}
|
115 |
|
|
|
116 |
02d9659c
|
Andreas Kohlbecker
|
// option to skip json processing for debugging purposes, see #4925
|
117 |
|
|
if(System.getProperty("SkipJSON") == null) {
|
118 |
|
|
|
119 |
045a007b
|
Andreas Kohlbecker
|
// create JSON Object
|
120 |
256c8f84
|
Andreas Kohlbecker
|
|
121 |
f36a0784
|
Andreas Kohlbecker
|
// long start = System.currentTimeMillis();
|
122 |
045a007b
|
Andreas Kohlbecker
|
boolean isCollectionType = false;
|
123 |
|
|
JSON jsonObj;
|
124 |
|
|
if (entity == null){
|
125 |
|
|
jsonObj = JSONObject.fromObject("{}");
|
126 |
|
|
} else if(Collection.class.isAssignableFrom(entity.getClass())){
|
127 |
|
|
isCollectionType = true;
|
128 |
|
|
jsonObj = JSONArray.fromObject(entity, jsonConfig);
|
129 |
|
|
}else if(entity instanceof String){
|
130 |
|
|
jsonObj = JSONObject.fromObject("{\"String\":\""+entity.toString().replace("\"", "\\\"")+"\"}");
|
131 |
|
|
} else if(entity instanceof Integer){
|
132 |
|
|
jsonObj = JSONObject.fromObject("{\"Integer\":"+((Integer)entity).intValue()+"}");
|
133 |
|
|
} else if(entity instanceof Boolean){
|
134 |
|
|
jsonObj = JSONObject.fromObject("{\"Boolean\":"+((Boolean)entity).toString()+"}");
|
135 |
|
|
} else {
|
136 |
|
|
jsonObj = JSONObject.fromObject(entity, jsonConfig);
|
137 |
|
|
}
|
138 |
f36a0784
|
Andreas Kohlbecker
|
// System.err.println("create JSON Object " + (System.currentTimeMillis() - start));
|
139 |
045a007b
|
Andreas Kohlbecker
|
|
140 |
|
|
if(type.equals(Type.XML)){
|
141 |
|
|
XMLSerializer xmlSerializer = new XMLSerializer();
|
142 |
|
|
if(isCollectionType){
|
143 |
|
|
xmlSerializer.setArrayName(entity.getClass().getSimpleName());
|
144 |
|
|
Class elementType = Object.class;
|
145 |
|
|
Collection c = (Collection)entity;
|
146 |
|
|
if(c.size() > 0){
|
147 |
|
|
elementType = c.iterator().next().getClass();
|
148 |
|
|
}
|
149 |
|
|
xmlSerializer.setObjectName(elementType.getSimpleName());
|
150 |
|
|
} else if(entity != null){
|
151 |
|
|
xmlSerializer.setObjectName(entity.getClass().getSimpleName());
|
152 |
|
|
}
|
153 |
|
|
String xml = xmlSerializer.write( jsonObj );
|
154 |
|
|
if(type.equals(Type.XML) && xsl != null){
|
155 |
86f9ed88
|
Andreas Kohlbecker
|
|
156 |
|
|
if(contextPath == null){
|
157 |
|
|
contextPath = "";
|
158 |
045a007b
|
Andreas Kohlbecker
|
}
|
159 |
86f9ed88
|
Andreas Kohlbecker
|
String basepath = dataSourceProperties.getXslBasePath(contextPath + "/xsl");
|
160 |
|
|
String replace = "\r\n<?xml-stylesheet type=\"text/xsl\" href=\"" + basepath + "/" + xsl + "\"?>\r\n";
|
161 |
045a007b
|
Andreas Kohlbecker
|
xml = xml.replaceFirst("\r\n", replace);
|
162 |
|
|
}
|
163 |
|
|
writer.append(xml);
|
164 |
|
|
} else {
|
165 |
|
|
// assuming json
|
166 |
d9f1f8e5
|
Andreas Kohlbecker
|
if(jsonpCallback != null){
|
167 |
|
|
writer.append(jsonpCallback).append("(").append(jsonObj.toString()).append(")");
|
168 |
|
|
} else {
|
169 |
|
|
writer.append(jsonObj.toString());
|
170 |
|
|
}
|
171 |
045a007b
|
Andreas Kohlbecker
|
}
|
172 |
|
|
//TODO resp.setContentType(type);
|
173 |
02d9659c
|
Andreas Kohlbecker
|
|
174 |
|
|
} else {
|
175 |
|
|
writer.append("SkipJSON mode detected, this is for debugging only! Please contact the adminitrator.");
|
176 |
|
|
// END SkipJSON
|
177 |
|
|
}
|
178 |
045a007b
|
Andreas Kohlbecker
|
writer.flush();
|
179 |
|
|
}
|
180 |
|
|
|
181 |
|
|
/*
|
182 |
|
|
* (non-Javadoc)
|
183 |
|
|
* @see org.springframework.web.servlet.View#render(java.util.Map, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
|
184 |
|
|
*/
|
185 |
ad875511
|
Andreas Kohlbecker
|
@Override
|
186 |
045a007b
|
Andreas Kohlbecker
|
public void render(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception {
|
187 |
|
|
|
188 |
|
|
// Retrieve data from model
|
189 |
|
|
Object entity = getResponseData(model);
|
190 |
|
|
|
191 |
|
|
// set content type
|
192 |
|
|
response.setContentType(type.getContentType());
|
193 |
|
|
|
194 |
|
|
PrintWriter writer = response.getWriter();
|
195 |
|
|
|
196 |
d9f1f8e5
|
Andreas Kohlbecker
|
// read jsonp parameter from query string
|
197 |
f27c30c3
|
Andreas Kohlbecker
|
String jsonpCallback = extractJsonpCallback(request);
|
198 |
|
|
|
199 |
|
|
// render
|
200 |
|
|
render(entity, writer, jsonpCallback, request, response);
|
201 |
|
|
}
|
202 |
|
|
|
203 |
|
|
/**
|
204 |
|
|
* @param request
|
205 |
|
|
* @return
|
206 |
|
|
*/
|
207 |
|
|
private String extractJsonpCallback(HttpServletRequest request) {
|
208 |
d9f1f8e5
|
Andreas Kohlbecker
|
String jsonpCallback= null;
|
209 |
024a38c9
|
Andreas Kohlbecker
|
String queryString = request.getQueryString();
|
210 |
|
|
if(queryString != null){
|
211 |
|
|
String[] tokens = request.getQueryString().split("&", 0);
|
212 |
|
|
String jsonpParamName = "callback";
|
213 |
|
|
for (int i = 0; i < tokens.length; i++) {
|
214 |
|
|
if(tokens[i].startsWith(jsonpParamName)){
|
215 |
|
|
jsonpCallback = tokens[i].substring(jsonpParamName.length() + 1);
|
216 |
|
|
break;
|
217 |
|
|
}
|
218 |
|
|
}
|
219 |
d9f1f8e5
|
Andreas Kohlbecker
|
}
|
220 |
f27c30c3
|
Andreas Kohlbecker
|
return jsonpCallback;
|
221 |
045a007b
|
Andreas Kohlbecker
|
}
|
222 |
2ce08010
|
Andreas Kohlbecker
|
}
|