View Javadoc

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