1 | |
package org.apache.turbine.services.jsonrpc; |
2 | |
|
3 | |
import java.io.CharArrayWriter; |
4 | |
|
5 | |
import javax.servlet.http.HttpServletRequest; |
6 | |
|
7 | |
import org.apache.commons.logging.Log; |
8 | |
import org.apache.commons.logging.LogFactory; |
9 | |
import org.jabsorb.JSONRPCBridge; |
10 | |
import org.jabsorb.JSONRPCResult; |
11 | |
import org.json.JSONArray; |
12 | |
import org.json.JSONException; |
13 | |
import org.json.JSONObject; |
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | 0 | public class JSONProcessor |
21 | |
{ |
22 | |
|
23 | 0 | private static Log log = LogFactory.getLog(JSONProcessor.class); |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
public static Object processCall(CharArrayWriter cdata, JSONRPCBridge json_bridge, HttpServletRequest request) |
33 | |
{ |
34 | |
|
35 | 0 | JSONObject json_req = null; |
36 | 0 | Object json_res = null; |
37 | |
try |
38 | |
{ |
39 | 0 | json_req = new JSONObject(cdata.toString()); |
40 | 0 | if (log.isDebugEnabled()) |
41 | |
{ |
42 | 0 | String methodName = json_req.getString("method"); |
43 | 0 | JSONArray arguments = json_req.getJSONArray("params"); |
44 | |
|
45 | |
|
46 | 0 | int object_id = json_req.optInt("objectID"); |
47 | 0 | StringBuilder sb = new StringBuilder(".doprocessCall(): call "); |
48 | 0 | if (object_id != 0) |
49 | |
{ |
50 | 0 | sb.append("objectID=").append(object_id).append(" "); |
51 | |
} |
52 | 0 | sb.append(methodName).append("(").append(arguments).append(")"); |
53 | 0 | log.debug(sb.toString()); |
54 | |
} |
55 | |
|
56 | 0 | json_res = json_bridge.call(new Object[] {request}, json_req); |
57 | |
} |
58 | 0 | catch (JSONException e) |
59 | |
{ |
60 | 0 | log.error(".processCall(): can't parse call: " + cdata, e); |
61 | 0 | json_res = JSONRPCResult.MSG_ERR_PARSE; |
62 | 0 | } |
63 | |
|
64 | 0 | if (log.isDebugEnabled()) |
65 | |
{ |
66 | 0 | log.debug(".processCall(): returns " + json_res.toString()); |
67 | |
} |
68 | 0 | return json_res; |
69 | |
} |
70 | |
|
71 | |
} |