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.BufferedReader;
23  import java.io.CharArrayWriter;
24  import java.io.InputStreamReader;
25  import java.io.OutputStreamWriter;
26  import java.io.PrintWriter;
27  import java.nio.charset.Charset;
28  import java.nio.charset.StandardCharsets;
29  
30  import javax.servlet.http.HttpServletRequest;
31  
32  import org.apache.turbine.annotation.TurbineService;
33  import org.apache.turbine.pipeline.PipelineData;
34  import org.apache.turbine.services.jsonrpc.JsonRpcService;
35  import org.apache.turbine.util.RunData;
36  import org.jabsorb.JSONRPCBridge;
37  
38  
39  /**
40   * A Screen class for dealing with JSON-RPC requests.  Typically you would
41   * extend this class and override the doOutput() method to use TurbineJsonRpc
42   * to register the POJOs that will provide the functions you are making
43   * available via JSON-RPC.  Use JSONSecureScreen if you need the user to be
44   * logged in prior to executing the functions you provide.
45   *
46   * <p>Here is an example from a superclass:
47   * <code>
48   * public void doOutput(PipelineData data) throws Exception
49   * {
50   *     User user = data.getUser();
51   *
52   *     MyJsonFunctions myFunctions = new MyJsonFunctions(user.getName());
53   *
54   *     // Session specific
55   *     TurbineJsonRpc.registerObject(data.getSession(), "myFunctions", myFunctions);
56   *
57   *     // Global
58   *     //TurbineJsonRpc.registerObjectGlobal("testGlobal", testObject);
59   *
60   *     super.doOutput(data);
61   * }
62   * </code>
63   *
64   * <p>The class MyFunctions would be something like:
65   * <code>
66   * public class MyJsonFunctions
67   * {
68   *     private String getHello(String clientParameter)
69   *     {
70   *         return "Hello " + clientParameter;
71   *     }
72   * }
73   * </code>
74   *
75   * <p>This code is derived from the com.metaparadigm.jsonrpc.JSONRPCServlet and (after upgrade) checked against
76   * org.jabsorb.JSONRPCServlet.
77   *
78   * @author brad@folkens.com
79   * @author <a href="mailto:seade@backstagetech.com.au">Scott Eade</a>
80   * @version $Id$
81   */
82  public class JSONScreen extends RawScreen
83  {
84      protected static final String JSONRPC_CONTENT_TYPE = "application/json;charset=utf-8";
85  
86      protected final static int BUFFER_SIZE = 4096;
87  
88      /** Injected service instance */
89      @TurbineService
90      protected JsonRpcService jsonRpcService;
91  
92      /**
93       * @see org.apache.turbine.modules.screens.RawScreen#getContentType(org.apache.turbine.pipeline.PipelineData)
94       */
95      @Override
96      protected String getContentType(PipelineData pipelineData)
97      {
98          return JSONRPC_CONTENT_TYPE;
99      }
100 
101     /**
102      * Output the dynamic content.
103      *
104      * Encoding is UTF-8. @{@link #JSONRPC_CONTENT_TYPE}: {@value #JSONRPC_CONTENT_TYPE}.
105      *
106      * @param pipelineData The PipelineData object.
107      */
108     @Override
109     protected void doOutput(PipelineData pipelineData) throws Exception
110     {
111         RunData data = pipelineData.getRunData();
112         data.declareDirectResponse();
113         HttpServletRequest request = data.getRequest();
114 
115         // we require utf-8, cft
116         Charset charset =  StandardCharsets.UTF_8; //request.getCharacterEncoding();
117         BufferedReader in = new BufferedReader(new InputStreamReader(request.getInputStream(), charset));
118 
119         // Read the request
120         CharArrayWriter cdata = new CharArrayWriter();
121         char buf[] = new char[BUFFER_SIZE];
122         int ret;
123         while ((ret = in.read(buf, 0, BUFFER_SIZE)) != -1)
124         {
125             cdata.write(buf, 0, ret);
126         }
127 
128         // Find the JSONRPCBridge for this session or create one
129         // if it doesn't exist
130         JSONRPCBridge json_bridge = jsonRpcService.getBridge(data.getSession());
131 
132         // Process the request
133         Object json_res = jsonRpcService.processCall(cdata, json_bridge, request);
134 
135         try (PrintWriter out = new PrintWriter(
136                 new OutputStreamWriter(
137                     data.getResponse().getOutputStream(), charset)))
138         {
139             out.print(json_res.toString());
140             out.flush();
141         }
142     }
143 }