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