1 package org.apache.turbine.modules.screens;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.InputStreamReader;
23 import java.io.OutputStreamWriter;
24 import java.io.BufferedReader;
25 import java.io.PrintWriter;
26 import java.io.CharArrayWriter;
27
28 import javax.servlet.http.HttpServletRequest;
29
30 import org.apache.turbine.modules.screens.RawScreen;
31 import org.apache.turbine.services.jsonrpc.TurbineJsonRpc;
32 import org.apache.turbine.util.RunData;
33
34 import com.metaparadigm.jsonrpc.JSONRPCBridge;
35
36 /***
37 * A Screen class for dealing with JSON-RPC requests. Typically you would
38 * extend this class and override the doOutput() method to use TurbineJsonRpc
39 * to register the POJOs that will provide the functions you are making
40 * available via JSON-RPC. Use JSONSecureScreen if you need the user to be
41 * logged in prior to executing the functions you provide.
42 *
43 * <p>Here is an example from a superclass:
44 * <code>
45 * public void doOutput(RunData data) throws Exception
46 * {
47 * User user = data.getUser();
48 *
49 * MyJsonFunctions myFunctions = new MyJsonFunctions(user.getName());
50 *
51 * // Session specific
52 * TurbineJsonRpc.registerObject(data.getSession(), "myFunctions", myFunctions);
53 *
54 * // Global
55 * //TurbineJsonRpc.registerObjectGlobal("testGlobal", testObject);
56 *
57 * super.doOutput(data);
58 * }
59 * </code>
60 *
61 * <p>The class MyFunctions would be something like:
62 * <code>
63 * public class MyJsonFunctions
64 * {
65 * private String getHello(String clientParameter)
66 * {
67 * return "Hello " + clientParameter;
68 * }
69 * }
70 * </code>
71 *
72 * <p>This code is derived from the com.metaparadigm.jsonrpc.JSONRPCServlet
73 *
74 * @author brad@folkens.com
75 * @author <a href="mailto:seade@backstagetech.com.au">Scott Eade</a>
76 * @version $Id$
77 */
78 public class JSONScreen extends RawScreen
79 {
80 protected static final String JSONRPC_CONTENT_TYPE = "text/plain";
81
82 protected final static int BUFFER_SIZE = 4096;
83
84
85
86
87
88
89 protected String getContentType(RunData data)
90 {
91 return JSONRPC_CONTENT_TYPE;
92 }
93
94
95
96
97
98
99
100 /***
101 * Output the dynamic content.
102 *
103 * @param data The RunData object.
104 */
105 protected void doOutput(RunData data) throws Exception
106 {
107 data.declareDirectResponse();
108 HttpServletRequest request = data.getRequest();
109
110
111
112
113
114
115
116 BufferedReader in = new BufferedReader(new InputStreamReader(request.getInputStream()));
117
118
119 CharArrayWriter cdata = new CharArrayWriter();
120 char buf[] = new char[BUFFER_SIZE];
121 int ret;
122 while ((ret = in.read(buf, 0, BUFFER_SIZE)) != -1)
123 {
124 cdata.write(buf, 0, ret);
125 }
126
127
128
129 JSONRPCBridge json_bridge = TurbineJsonRpc.getBridge(data.getSession());
130
131
132 Object json_res = TurbineJsonRpc.processCall(cdata, json_bridge, request);
133
134 PrintWriter out = new PrintWriter(
135 new OutputStreamWriter(data.getResponse().getOutputStream()));
136 out.print(json_res.toString());
137 out.flush();
138 out.close();
139 }
140 }