##### Coverage Report - org.apache.turbine.modules.screens.JSONScreen

Classes in this File | Line Coverage | Branch Coverage | Complexity
JSONScreen |
0% | 0/18
0% | 0/2
| 1.5;1,5

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