View Javadoc

    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  public class JSONProcessor
    17  {
    18      /** Log. */
    19      private static Log log = LogFactory.getLog(JSONProcessor.class);
    20
    21      public static Object processCall(CharArrayWriter cdata, JSONRPCBridge json_bridge, HttpServletRequest request)
    22      {
    23          // Process the request
    24          JSONObject json_req = null;
    25          Object json_res = null;
    26          try
    27          {
    28              json_req = new JSONObject(cdata.toString());
    29              if (log.isDebugEnabled())
    30              {
    31                  String methodName = json_req.getString("method");
    32                  JSONArray arguments = json_req.getJSONArray("params");
    33
    34                  // If this a CallableReference it will have a non-zero objectID
    35                  int object_id = json_req.optInt("objectID");
    36                  StringBuffer sb = new StringBuffer(".doprocessCall(): call ");
    37                  if (object_id != 0)
    38                  {
    39                      sb.append("objectID=").append(object_id).append(" ");
    40                  }
    41                  sb.append(methodName).append("(").append(arguments).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}, json_req);
    46          }
    47          catch (ParseException e)
    48          {
    49              log.error(".processCall(): can't parse call: " + cdata, e);
    50              json_res = JSONRPCResult.MSG_ERR_PARSE;
    51          }
    52          // Write the response
    53          if (log.isDebugEnabled())
    54          {
    55              log.debug(".processCall():  returns " + json_res.toString());
    56          }
    57          return json_res;
    58      }
    59
    60  }