01: package org.apache.turbine.services.jsonrpc;
02:
03: import java.io.CharArrayWriter;
04: import java.text.ParseException;
05:
06: import javax.servlet.http.HttpServletRequest;
07:
08: import org.apache.commons.logging.Log;
09: import org.apache.commons.logging.LogFactory;
10: import org.json.JSONArray;
11: import org.json.JSONObject;
12:
13: import com.metaparadigm.jsonrpc.JSONRPCBridge;
14: import com.metaparadigm.jsonrpc.JSONRPCResult;
15:
16: public class JSONProcessor {
17: /** Log. */
18: private static Log log = LogFactory.getLog(JSONProcessor.class);
19:
20: public static Object processCall(CharArrayWriter cdata,
21: JSONRPCBridge json_bridge, HttpServletRequest request) {
22: // Process the request
23: JSONObject json_req = null;
24: Object json_res = null;
25: try {
26: json_req = new JSONObject(cdata.toString());
27: if (log.isDebugEnabled()) {
28: String methodName = (String) json_req
29: .getString("method");
30: JSONArray arguments = json_req.getJSONArray("params");
31:
32: // If this a CallableReference it will have a non-zero objectID
33: int object_id = json_req.optInt("objectID");
34: StringBuffer sb = new StringBuffer(
35: ".doprocessCall(): call ");
36: if (object_id != 0) {
37: sb.append("objectID=").append(object_id)
38: .append(" ");
39: }
40: sb.append(methodName).append("(").append(arguments)
41: .append(")");
42: log.debug(sb.toString());
43: }
44: //json_res = json_bridge.call(new Object[] {request}, object_id, methodName, arguments);
45: json_res = json_bridge.call(new Object[] { request },
46: json_req);
47: } catch (ParseException e) {
48: log.error(".processCall(): can't parse call: " + cdata, e);
49: json_res = JSONRPCResult.MSG_ERR_PARSE;
50: }
51: // Write the response
52: if (log.isDebugEnabled()) {
53: log
54: .debug(".processCall(): returns "
55: + json_res.toString());
56: }
57: return json_res;
58: }
59:
60: }
|