001package org.apache.turbine.services.jsonrpc;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.CharArrayWriter;
023
024import javax.servlet.http.HttpServletRequest;
025
026import org.apache.logging.log4j.LogManager;
027import org.apache.logging.log4j.Logger;
028import org.jabsorb.JSONRPCBridge;
029import org.jabsorb.JSONRPCResult;
030import org.json.JSONArray;
031import org.json.JSONException;
032import org.json.JSONObject;
033
034/**
035 * Process a JSON RPC call
036 *
037 * @author <a href="mailto:seade@backstagetech.com.au">Scott Eade</a>
038 */
039public class JSONProcessor
040{
041    /** Log. */
042    private static Logger log = LogManager.getLogger(JSONProcessor.class);
043
044    /**
045     * Process a JSON RPC call
046     * @param cdata the JSON data
047     * @param json_bridge the {@link JSONRPCBridge} object
048     * @param request the request
049     * @return the return object of the JSON RPC call
050     */
051    public static Object processCall(CharArrayWriter cdata, JSONRPCBridge json_bridge, HttpServletRequest request)
052    {
053        // Process the request
054        JSONObject json_req = null;
055        Object json_res = null;
056        try
057        {
058            json_req = new JSONObject(cdata.toString());
059            if (log.isDebugEnabled())
060            {
061                String methodName = json_req.getString("method");
062                JSONArray arguments = json_req.getJSONArray("params");
063
064                // If this a CallableReference it will have a non-zero objectID
065                int object_id = json_req.optInt("objectID");
066                StringBuilder sb = new StringBuilder(".doprocessCall(): call ");
067                if (object_id != 0)
068                {
069                    sb.append("objectID=").append(object_id).append(" ");
070                }
071                sb.append(methodName).append("(").append(arguments).append(")");
072                log.debug(sb.toString());
073            }
074            //json_res = json_bridge.call(new Object[] {request}, object_id, methodName, arguments);
075            json_res = json_bridge.call(new Object[] {request}, json_req);
076        }
077        catch (JSONException e)
078        {
079            log.error(".processCall(): can't parse call: {}", cdata, e);
080            json_res = JSONRPCResult.MSG_ERR_PARSE;
081        }
082        // Write the response
083        log.debug(".processCall():  returns ", json_res::toString);
084        return json_res;
085    }
086
087}