001package org.apache.turbine.modules.screens;
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 org.apache.turbine.pipeline.PipelineData;
023
024/**
025 * An extension to JSONScreen that performs a Security Check before invoking
026 * doBuildTemplate().  You should extend this class and add the specific
027 * security check needed.  If you have a number of screens that need to perform
028 * the same check, you could make a base screen by extending this class and
029 * implementing the isAuthorized().  Then each screen that needs to perform the
030 * same check could extend your base screen.
031 *
032 * <p>Typically you would extend this class and override the doOutput() method
033 * to use TurbineJsonRpc to register the POJOs that will provide the functions
034 * you are making available via JSON-RPC.  Use JSONScreen if you <b>do not</b>
035 * need the user to be logged in prior to executing the functions you provide.</p>
036 *
037 * <p>Here is an example from a superclass:
038 * <code>
039 * public void doOutput(PipelineData data) throws Exception
040 * {
041 *     User user = data.getUser();
042 *
043 *     MySecureJsonFunctions myFunctions
044 *             = new MySecureJsonFunctions(user.getName());
045 *
046 *     // Session specific
047 *     TurbineJsonRpc.registerObject(data.getSession(), "myFunctions", myFunctions);
048 *
049 *     // Global
050 *     //TurbineJsonRpc.registerObjectGlobal("testGlobal", testObject);
051 *
052 *     super.doOutput(data);
053 * }
054 * </code></p>
055 *
056 * <p>The class MyFunctions would be something like:
057 * <code>
058 * public class MySecureJsonFunctions
059 * {
060 *     private final String name;
061 *
062 *     public MySecureJsonFunctions(String name)
063 *     {
064 *         this.name = name;
065 *     }
066 *
067 *     private String getName(String clientParameter)
068 *     {
069 *         return "Client " + clientParameter + " says Hello World to " + name;
070 *     }
071 * }
072 * </code></p>
073 *
074 * @author <a href="mailto:seade@policypoint.net">Scott Eade</a>
075 * @version $Id$
076 */
077public abstract class JSONSecureScreen extends JSONScreen
078{
079    /**
080     * This method overrides the method in JSONScreen to perform a security
081     * check prior to producing the output.
082     *
083     * @param pipelineData Turbine information.
084     * @throws Exception a generic exception.
085     */
086    @Override
087    protected void doOutput(PipelineData pipelineData) throws Exception
088    {
089        if (isAuthorized(pipelineData))
090        {
091            super.doOutput(pipelineData);
092        }
093    }
094
095    /**
096     * Override this method to perform the necessary security checks.
097     *
098     * @param pipelineData Turbine information.
099     * @return <code>true</code> if the user is authorized to access the screen.
100     * @throws Exception A generic exception.
101     */
102    protected abstract boolean isAuthorized(PipelineData pipelineData)
103            throws Exception;
104}