View Javadoc

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.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       * (non-Javadoc)
86       * 
87       * @see org.apache.turbine.modules.screens.RawScreen#getContentType(org.apache.turbine.util.RunData)
88       */
89      protected String getContentType(RunData data)
90      {
91          return JSONRPC_CONTENT_TYPE;
92      }
93  
94      /*
95       * (non-Javadoc)
96       * 
97       * @see org.apache.turbine.modules.screens.RawScreen#doOutput(org.apache.turbine.util.RunData)
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         //String charset = request.getCharacterEncoding();
111         //if(charset == null)
112         //{
113         //    charset = "UTF-8";
114         //}
115         //BufferedReader in = new BufferedReader(new InputStreamReader(request.getInputStream(), charset));
116         BufferedReader in = new BufferedReader(new InputStreamReader(request.getInputStream()));
117 
118         // Read the request
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         // Find the JSONRPCBridge for this session or create one
128         // if it doesn't exist
129         JSONRPCBridge json_bridge = TurbineJsonRpc.getBridge(data.getSession());
130 
131         // Process the request
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 }