    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;
    025import javax.servlet.http.HttpSession;
    026
    027import org.apache.turbine.services.TurbineBaseService;
    028import org.jabsorb.JSONRPCBridge;
    029
    030
    031/**
    032 * This is a service that will respond to JSON-RPC calls.
    033 *
    034 * @author <a href="mailto:seade@backstagetech.com.au">Scott Eade</a>
    035 * @version $Id: TurbineJsonRpcService.java 1706239 2015-10-01 13:18:35Z tv $
    036 */
    037public class TurbineJsonRpcService
    038        extends TurbineBaseService
    039        implements JsonRpcService
    040{
    041    /** The key used to store the bridge in the session. */
    042    public static final String JSON_BRIDGE_KEY = "JSONRPCBridge";
    043
    044    @Override
    045    public Object processCall(CharArrayWriter cdata,
    046            JSONRPCBridge json_bridge, HttpServletRequest request)
    047    {
    048        return JSONProcessor.processCall(cdata, json_bridge, request);
    049    }
    050
    051    @Override
    052    public void registerObjectGlobal(String key, Object value)
    053    {
    054        JSONRPCBridge.getGlobalBridge().registerObject(key, value);
    055    }
    056
    057    @Override
    058    public void registerObject(HttpSession session, String key, Object value)
    059    {
    060        JSONRPCBridge json_bridge = getBridge(session);
    061        json_bridge.registerObject(key, value);
    062    }
    063
    064    @Override
    065    public JSONRPCBridge getBridge(HttpSession session)
    066    {
    067        JSONRPCBridge json_bridge = (JSONRPCBridge) session.getAttribute(JSON_BRIDGE_KEY);
    068        if (json_bridge == null)
    069        {
    070            json_bridge = new JSONRPCBridge();
    071            session.setAttribute(JSON_BRIDGE_KEY, json_bridge);
    072        }
    073        return json_bridge;
    074    }
    075
    076    @Override
    077    public void clearBridge(HttpSession session)
    078    {
    079        session.removeAttribute(JSON_BRIDGE_KEY);
    080    }
    081
    082// The following is modeled on XmlRpcSercice.
    083//    /**
    084//     * Initialize the JsonRpcService.
    085//     *
    086//     * @throws InitializationException Something went wrong in the init stage.
    087//     */
    088//    public void init() throws InitializationException
    089//    {
    090//        //Configuration conf = getConfiguration();
    091//        setInit(true);
    092//    }
    093//
    094//    /**
    095//     * Shuts down this service, stopping running threads.
    096//     */
    097//    public void shutdown()
    098//    {
    099//        setInit(false);
    100//    }
    101
    102}