Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
JSONScreen |
|
| 1.3333333333333333;1,333 |
1 | package org.apache.turbine.modules.screens; | |
2 | ||
3 | /* | |
4 | * Licensed to the Apache Software Foundation (ASF) under one | |
5 | * or more contributor license agreements. See the NOTICE file | |
6 | * distributed with this work for additional information | |
7 | * regarding copyright ownership. The ASF licenses this file | |
8 | * to you under the Apache License, Version 2.0 (the | |
9 | * "License"); you may not use this file except in compliance | |
10 | * with the License. You may obtain a copy of the License at | |
11 | * | |
12 | * http://www.apache.org/licenses/LICENSE-2.0 | |
13 | * | |
14 | * Unless required by applicable law or agreed to in writing, | |
15 | * software distributed under the License is distributed on an | |
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
17 | * KIND, either express or implied. See the License for the | |
18 | * specific language governing permissions and limitations | |
19 | * under the License. | |
20 | */ | |
21 | ||
22 | import java.io.BufferedReader; | |
23 | import java.io.CharArrayWriter; | |
24 | import java.io.InputStreamReader; | |
25 | import java.io.OutputStreamWriter; | |
26 | import java.io.PrintWriter; | |
27 | ||
28 | import javax.servlet.http.HttpServletRequest; | |
29 | ||
30 | import org.apache.turbine.pipeline.PipelineData; | |
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: JSONScreen.java 958672 2010-06-28 18:42:04Z tv $ | |
77 | */ | |
78 | 0 | 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 | * @see org.apache.turbine.modules.screens.RawScreen#getContentType(org.apache.turbine.util.RunData) | |
86 | * @deprecated Use PipelineData version instead. | |
87 | */ | |
88 | protected String getContentType(RunData data) | |
89 | { | |
90 | 0 | return JSONRPC_CONTENT_TYPE; |
91 | } | |
92 | ||
93 | /** | |
94 | * @see org.apache.turbine.modules.screens.RawScreen#getContentType(org.apache.turbine.pipeline.PipelineData) | |
95 | */ | |
96 | protected String getContentType(PipelineData pipelineData) | |
97 | { | |
98 | 0 | return JSONRPC_CONTENT_TYPE; |
99 | } | |
100 | ||
101 | /** | |
102 | * @see org.apache.turbine.modules.screens.RawScreen#doOutput(org.apache.turbine.util.RunData) | |
103 | */ | |
104 | ||
105 | /** | |
106 | * Output the dynamic content. | |
107 | * | |
108 | * @param data The RunData object. | |
109 | * @deprecated Use PipelineData version instead. | |
110 | */ | |
111 | protected void doOutput(RunData data) throws Exception | |
112 | { | |
113 | 0 | data.declareDirectResponse(); |
114 | 0 | HttpServletRequest request = data.getRequest(); |
115 | ||
116 | //String charset = request.getCharacterEncoding(); | |
117 | //if(charset == null) | |
118 | //{ | |
119 | // charset = "UTF-8"; | |
120 | //} | |
121 | //BufferedReader in = new BufferedReader(new InputStreamReader(request.getInputStream(), charset)); | |
122 | 0 | BufferedReader in = new BufferedReader(new InputStreamReader(request.getInputStream())); |
123 | ||
124 | // Read the request | |
125 | 0 | CharArrayWriter cdata = new CharArrayWriter(); |
126 | 0 | char buf[] = new char[BUFFER_SIZE]; |
127 | int ret; | |
128 | 0 | while ((ret = in.read(buf, 0, BUFFER_SIZE)) != -1) |
129 | { | |
130 | 0 | cdata.write(buf, 0, ret); |
131 | } | |
132 | ||
133 | // Find the JSONRPCBridge for this session or create one | |
134 | // if it doesn't exist | |
135 | 0 | JSONRPCBridge json_bridge = TurbineJsonRpc.getBridge(data.getSession()); |
136 | ||
137 | // Process the request | |
138 | 0 | Object json_res = TurbineJsonRpc.processCall(cdata, json_bridge, request); |
139 | ||
140 | 0 | PrintWriter out = new PrintWriter( |
141 | new OutputStreamWriter(data.getResponse().getOutputStream())); | |
142 | 0 | out.print(json_res.toString()); |
143 | 0 | out.flush(); |
144 | 0 | out.close(); |
145 | 0 | } |
146 | } |