1 package org.apache.turbine.services.jsonrpc;
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.CharArrayWriter;
23
24 import javax.servlet.http.HttpServletRequest;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.jabsorb.JSONRPCBridge;
29 import org.jabsorb.JSONRPCResult;
30 import org.json.JSONArray;
31 import org.json.JSONException;
32 import org.json.JSONObject;
33
34 /**
35 * Process a JSON RPC call
36 *
37 * @author <a href="mailto:seade@backstagetech.com.au">Scott Eade</a>
38 */
39 public class JSONProcessor
40 {
41 /** Log. */
42 private static Log log = LogFactory.getLog(JSONProcessor.class);
43
44 /**
45 * Process a JSON RPC call
46 * @param cdata the JSON data
47 * @param json_bridge the {@link JSONRPCBridge} object
48 * @param request the request
49 * @return the return object of the JSON RPC call
50 */
51 public static Object processCall(CharArrayWriter cdata, JSONRPCBridge json_bridge, HttpServletRequest request)
52 {
53 // Process the request
54 JSONObject json_req = null;
55 Object json_res = null;
56 try
57 {
58 json_req = new JSONObject(cdata.toString());
59 if (log.isDebugEnabled())
60 {
61 String methodName = json_req.getString("method");
62 JSONArray arguments = json_req.getJSONArray("params");
63
64 // If this a CallableReference it will have a non-zero objectID
65 int object_id = json_req.optInt("objectID");
66 StringBuilder sb = new StringBuilder(".doprocessCall(): call ");
67 if (object_id != 0)
68 {
69 sb.append("objectID=").append(object_id).append(" ");
70 }
71 sb.append(methodName).append("(").append(arguments).append(")");
72 log.debug(sb.toString());
73 }
74 //json_res = json_bridge.call(new Object[] {request}, object_id, methodName, arguments);
75 json_res = json_bridge.call(new Object[] {request}, json_req);
76 }
77 catch (JSONException e)
78 {
79 log.error(".processCall(): can't parse call: " + cdata, e);
80 json_res = JSONRPCResult.MSG_ERR_PARSE;
81 }
82 // Write the response
83 if (log.isDebugEnabled())
84 {
85 log.debug(".processCall(): returns " + json_res.toString());
86 }
87 return json_res;
88 }
89
90 }