    001    package org.apache.turbine.services.jsonrpc;
    002
    003    import java.io.CharArrayWriter;
    004    import java.text.ParseException;
    005
    006    import javax.servlet.http.HttpServletRequest;
    007
    008    import org.apache.commons.logging.Log;
    009    import org.apache.commons.logging.LogFactory;
    010    import org.json.JSONArray;
    011    import org.json.JSONObject;
    012
    013    import com.metaparadigm.jsonrpc.JSONRPCBridge;
    014    import com.metaparadigm.jsonrpc.JSONRPCResult;
    015
    016    public class JSONProcessor
    017    {
    018        /** Log. */
    019        private static Log log = LogFactory.getLog(JSONProcessor.class);
    020
    021        public static Object processCall(CharArrayWriter cdata, JSONRPCBridge json_bridge, HttpServletRequest request)
    022        {
    023            // Process the request
    024            JSONObject json_req = null;
    025            Object json_res = null;
    026            try
    027            {
    028                json_req = new JSONObject(cdata.toString());
    029                if (log.isDebugEnabled())
    030                {
    031                    String methodName = json_req.getString("method");
    032                    JSONArray arguments = json_req.getJSONArray("params");
    033
    034                    // If this a CallableReference it will have a non-zero objectID
    035                    int object_id = json_req.optInt("objectID");
    036                    StringBuffer sb = new StringBuffer(".doprocessCall(): call ");
    037                    if (object_id != 0)
    038                    {
    039                        sb.append("objectID=").append(object_id).append(" ");
    040                    }
    041                    sb.append(methodName).append("(").append(arguments).append(")");
    042                    log.debug(sb.toString());
    043                }
    044                //json_res = json_bridge.call(new Object[] {request}, object_id, methodName, arguments);
    045                json_res = json_bridge.call(new Object[] {request}, json_req);
    046            }
    047            catch (ParseException e)
    048            {
    049                log.error(".processCall(): can't parse call: " + cdata, e);
    050                json_res = JSONRPCResult.MSG_ERR_PARSE;
    051            }
    052            // Write the response
    053            if (log.isDebugEnabled())
    054            {
    055                log.debug(".processCall():  returns " + json_res.toString());
    056            }
    057            return json_res;
    058        }
    059
    060    }