DerivedUnitFacade Portal controller
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / view / JsonView.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
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
29 public class JsonView extends BaseView implements View{
30
31 public static final Logger logger = Logger.getLogger(JsonView.class);
32
33 private JsonConfig jsonConfig;
34
35 public enum Type{
36 JSON("application/json"),
37 XML("application/xml");
38
39 private String contentType;
40
41 Type(String contentType){
42 this.contentType = contentType;
43 }
44
45 public String getContentType(){
46 return contentType;
47 }
48 }
49
50 private Type type = Type.JSON;
51
52 private String xsl;
53
54 public void setXsl(String xsl) {
55 this.xsl = xsl;
56 }
57
58 public Type getType() {
59 return type;
60 }
61
62 /**
63 * Default is Type.JSON
64 * @param type
65 */
66 public void setType(Type type) {
67 this.type = type;
68 }
69
70 public void setJsonConfig(JsonConfig jsonConfig) {
71 this.jsonConfig = jsonConfig;
72 }
73
74 /*
75 * (non-Javadoc)
76 * @see org.springframework.web.servlet.View#getContentType()
77 */
78 public String getContentType() {
79 return type.getContentType();
80 }
81
82 public void render(Object entity, PrintWriter writer) throws Exception {
83
84
85 // create JSON Object
86 boolean isCollectionType = false;
87 JSON jsonObj;
88 if (entity == null){
89 jsonObj = JSONObject.fromObject("{}");
90 } else if(Collection.class.isAssignableFrom(entity.getClass())){
91 isCollectionType = true;
92 jsonObj = JSONArray.fromObject(entity, jsonConfig);
93 }else if(entity instanceof String){
94 jsonObj = JSONObject.fromObject("{\"String\":\""+entity.toString().replace("\"", "\\\"")+"\"}");
95 } else if(entity instanceof Integer){
96 jsonObj = JSONObject.fromObject("{\"Integer\":"+((Integer)entity).intValue()+"}");
97 } else if(entity instanceof Boolean){
98 jsonObj = JSONObject.fromObject("{\"Boolean\":"+((Boolean)entity).toString()+"}");
99 } else {
100 jsonObj = JSONObject.fromObject(entity, jsonConfig);
101 }
102
103 if(type.equals(Type.XML)){
104 XMLSerializer xmlSerializer = new XMLSerializer();
105 if(isCollectionType){
106 xmlSerializer.setArrayName(entity.getClass().getSimpleName());
107 Class elementType = Object.class;
108 Collection c = (Collection)entity;
109 if(c.size() > 0){
110 elementType = c.iterator().next().getClass();
111 }
112 xmlSerializer.setObjectName(elementType.getSimpleName());
113 } else if(entity != null){
114 xmlSerializer.setObjectName(entity.getClass().getSimpleName());
115 }
116 String xml = xmlSerializer.write( jsonObj );
117 if(xsl != null){
118 String xslInclude = "\r\n<?xml-stylesheet type=\"text/xsl\" href=\"human.xsl\"?>\r\n";
119 xml = xml.replaceFirst("\r\n", xslInclude);
120 }
121 writer.append(xml);
122 } else {
123 // assuming json
124 writer.append(jsonObj.toString());
125 }
126 //TODO resp.setContentType(type);
127 writer.flush();
128 }
129
130 /*
131 * (non-Javadoc)
132 * @see org.springframework.web.servlet.View#render(java.util.Map, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
133 */
134 public void render(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception {
135
136 // Retrieve data from model
137 Object entity = getResponseData(model);
138
139 // set content type
140 response.setContentType(type.getContentType());
141
142 // render
143 render(entity, response.getWriter());
144
145 }
146 }