head	1.4;
access;
symbols;
locks; strict;
comment	@# @;


1.4
date	2001.07.28.00.56.06;	author stack;	state Exp;
branches;
next	1.3;

1.3
date	2001.07.27.19.34.02;	author leonardr;	state Exp;
branches;
next	1.2;

1.2
date	2001.07.27.19.32.21;	author stack;	state Exp;
branches;
next	1.1;

1.1
date	2001.07.26.20.21.46;	author stack;	state Exp;
branches;
next	;


desc
@@


1.4
log
@Synced this sample/test client w/ latest server changes.
@
text
@/* ================================================================
 * Copyright (c) 2001 CollabNet.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * 3. The end-user documentation included with the redistribution, if
 * any, must include the following acknowlegement: "This product includes
 * software developed by CollabNet <http://www.collab.net/>."
 * Alternately, this acknowlegement may appear in the software itself, if
 * and wherever such third-party acknowlegements normally appear.
 *
 * 4. The hosted project names must not be used to endorse or promote
 * products derived from this software without prior written
 * permission. For written permission, please contact info@@collab.net.
 *
 * 5. Products derived from this software may not use the "Tigris" or
 * "Helm" names nor may "Tigris" or "Helm" appear in their names without
 * prior written permission of CollabNet.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL COLLABNET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of CollabNet.
 */
import helma.xmlrpc.*;
import java.util.*;


/**
 * Harness for XML-RPC permission checker.
 *
 * Asks turbine permission questions using the tigris xmlrcp service. 
 * Make sure to hit the front page of the xmlrpc server before running 
 * your client: hitting the front page starts the xmlrpc server.
 *
 * @@author <a href="mailto:leonardr@@collab.net">Leonard Richardson</a>
 * @@author <a href="mailto:lucas@@collab.net">Josh Lucas</a>
 * @@author <a href="mailto:stack@@collab.net">St.Ack</a>
 *
 * @@see org.tigris.helm.security.PermissionHandler
 * @@see <a href="http://helm.tigris.org/docs/services_xmlrpc.html">Helm Services: XMLRPC</a>
 *
 * @@version $Id: TestXMLRPCClient.java,v 1.3 2001/07/27 19:34:02 leonardr Exp $
 */
public class TestXMLRPCClient
{
    private static final String USER_INFO_FROM_ID_ACTION
        = "userInfoFromID";

    private static final String USER_INFO_FROM_IDS_ACTION
        = "userInfoFromIDs";

    private static final String VALID_LOGIN_ACTION
        = "validLogin";

    private static final String USER_INFO_FROM_LOGIN_NAME_ACTION
        = "userInfoFromLoginName";

    private static final String PERMISSION_SET_ACTION 
        = "permissionSet";

    private static final String RESOURCES_FOR_ACTION 
        = "resourcesForAction";

    private static final String USERS_WITH_ACTION 
        = "usersWithAction";


    /** Main.
     *
     * @@param args Command-line argumnets.
     *
     * @@exception General exception.
     */
    public static void main(String argv[]) 
        throws Exception
    {
        XmlRpc.setDriver("org.apache.xerces.parsers.SAXParser");

        if( argv.length == 0 || argv.length < 4 )
        {
            // special case : show help info
            //
            showUsage();
        }
        else
        {
            try
            {
                processCommand(argv);
            }
            catch (Exception ex)
            { 
                ex.printStackTrace(); 
            }
        }
    }

    /**
     * Processes an RPC command.
     * 
     * @@param args Command-line argumnets.
     *
     * @@exception General exception.
     */
    public static void processCommand(String[] args) 
        throws Exception 
    {
        int offset = 0;

        String action = args[offset++];
        if(action.equalsIgnoreCase("-h") 
            || action.equalsIgnoreCase("--help"))
        {
            showUsage( );
        }

        String url = args[offset++];
        String auth = args[offset++];
        XmlRpcClient client = new XmlRpcClient(url);

        if (USER_INFO_FROM_ID_ACTION.equals(action))
        {                
            String userID = args[offset++];
            userInfoFromID(client, auth, userID);
        }
        else if( USER_INFO_FROM_IDS_ACTION.equals(action))
        {
            List idsList = 
                Arrays.asList(args).subList(offset++, args.length);
            Vector idsVector = new Vector(idsList.size());
            for (Iterator i = idsList.iterator(); i.hasNext();)
            {
                idsVector.addElement(i.next());
            }
            userInfoFromIDs(client, auth, idsVector);
        }
        else if (USER_INFO_FROM_LOGIN_NAME_ACTION.equals(action))
        {
            String domainName = args[offset++];
            String loginName = args[offset++];
            userInfoFromLoginName(client, auth, domainName, loginName);
        }
        else if (VALID_LOGIN_ACTION.equals(action))
        {
            String domainName = args[offset++];
            String loginName = args[offset++];
            String passwordName = args[offset++];
            validLogin(client, auth, domainName, loginName, passwordName );
        }
        else if (PERMISSION_SET_ACTION.equals(action))
        {
            String userID = args[offset++];
            String projectID = args[offset++];
            List actionList = 
                Arrays.asList(args).subList(offset++, args.length);
            Vector actionVector = new Vector(actionList.size());
            for (Iterator i = actionList.iterator(); i.hasNext();)
            {
                actionVector.addElement(i.next());
            }

            permissionSet(client, auth, userID, projectID, actionVector);
        }
        else if (RESOURCES_FOR_ACTION.equals(action))
        {
            String domainName = args[offset++];
            String projectName = args[offset++];
            String userName = args[offset++];
            action = args[offset++];
            resourcesForAction(client, auth, domainName, projectName, userName, 
                action);
        }
        else if (USERS_WITH_ACTION.equals(action))
        {
            String projectID = args[offset++];
            action = args[offset++];
            usersWithAction(client, auth, projectID, action);
        }
        else
        {
            System.out.println( "Action " + action + " is unsupported." );
        }
    }

    private static void showUsage()
    {
        System.out.print("Usage: java TestXMLRPCClient ACTION URL AUTH ARGS..."
            + "\n\n"
            + "Params:\n"
            + " ACTION   The xmlrpc client method to call." 
            + " URL      URL of the xmlrpc server.\n"
            + " AUTH     Authentication to pass the xmlrpc server. Try"
            + " DB password.\n"
            + " ARGS     Each ACTION takes arguments. Depends upon the action"
            + " specified.\n"
            + "          For ACTION=userInfoFromID, pass a UserID\n"
            + "          For ACTION=userInfoFromIDs, pass a list of UserIDs\n"
            + "          etc. Read the server code to determine arguments.\n\n"
            + "Example usage:\n"
            + " % java TestXMLRPCClient userInfoFromIDs "
            + "http://localhost:16500 \\\n"
            + "      euphrates marx.sp.collab.net 3 4 5\n\n"
            + " % java TestXMLRPCClient userInfoFromLoginName "
            + "http://localhost:16500 \\\n"
            + "      euphrates marx.sp.collab.net root\n\n"
            + "Notes:\n"
            + " + You must get a page from the server to start the xmlrpc"
            + " server.\n"
            + " + See http://helm.tigris.org/docs/services_xmlrpc.html\n\n");
    }

    private static  void userInfoFromID(XmlRpcClient client, String auth, 
                                        String userID)
        throws helma.xmlrpc.XmlRpcException,
            java.io.IOException
    {
        Vector v = new Vector();
        v.addElement(auth);
        v.addElement(new Integer(userID));
        Hashtable result = 
            (Hashtable)client.execute("user.getUserInfo", v);
        describeHash(result);
    }

    private static  void userInfoFromIDs(XmlRpcClient client, String auth, 
            Vector inIDs)
        throws helma.xmlrpc.XmlRpcException,
            java.io.IOException
    {
        Vector v = new Vector();
        v.addElement(auth);
        v.addElement(inIDs);
        Vector result = (Vector)client.execute("user.getUserInfo", v);
        for( Iterator i = result.iterator( ); i.hasNext( ); )
            describeHash((Hashtable)i.next( ));
    }

    private static void userInfoFromLoginName( XmlRpcClient client, String auth, 
            String domainName, String loginName)
        throws helma.xmlrpc.XmlRpcException,      
            java.io.IOException
    {
        Vector v = new Vector();
        v.addElement(auth);
        v.addElement(domainName);
        v.addElement(loginName);
        Hashtable result = 
            (Hashtable)client.execute("user.getUserInfo", v);
        describeHash(result);
    }
    
    private static void validLogin(XmlRpcClient client, String auth, 
            String domainName, String loginName, String passwordName )
        throws helma.xmlrpc.XmlRpcException,      
            java.io.IOException
    {
        Vector v = new Vector();
        v.addElement(auth);
        v.addElement(domainName);
        v.addElement(loginName);
        v.addElement(passwordName);
        Boolean result = 
            (Boolean)client.execute("user.isValidLogin", v);
        System.out.println( 
            ( result.booleanValue( ) )? "Valid" : "Not Valid" );
    }

    private static void permissionSet(XmlRpcClient client, String auth, 
                                      String userID, String projectID, 
                                      Vector actions)
        throws helma.xmlrpc.XmlRpcException,      
            java.io.IOException
    {
        Vector v = new Vector();
        v.addElement(auth);
        v.addElement(new Integer(userID));
        v.addElement(new Integer(projectID));
        v.addElement(actions);
        Vector result = 
            (Vector)client.execute("permission.getPermissionSet", v);

        System.out.println("User " + userID 
                           + " has the following actions in project " 
                           + projectID + ":");
        for (Iterator i = result.iterator(); i.hasNext();)
        {
            String action = (String)i.next();            
            System.out.println(" " + action);
        }
    }

    private static void resourcesForAction(XmlRpcClient client, String auth, 
            String domainName, String projectName, String userName, 
            String action)
        throws helma.xmlrpc.XmlRpcException,      
            java.io.IOException
    {
        Vector v = new Vector();
        v.addElement(auth);
        v.addElement(domainName);
        v.addElement(projectName);
        v.addElement(userName);
        v.addElement(action);
        Vector result = 
            (Vector)client.execute("permission.getResourcesForAction", v);
        System.out.println("The following users have action \"" + action
                           + "\" in project " + projectName + ":");
        for (Iterator i = result.iterator(); i.hasNext();)
        {
            //Hashtable user = (Hashtable)i.next();            
            //describeHash(user);
            System.out.println( i.next( ) );
            System.out.println("-------------------");
        }
    }

    private static void usersWithAction(XmlRpcClient client, String auth, 
            String projectID, String action)
        throws helma.xmlrpc.XmlRpcException,      
            java.io.IOException
    {
        Vector v = new Vector();
        v.addElement(auth);
        v.addElement(new Integer(projectID));
        v.addElement(action);
        Vector result = 
            (Vector)client.execute("permission.getUsersWithAction", v);
        System.out.println("The following users have action \"" + action
                           + "\" in project " + projectID + ":");
        for (Iterator i = result.iterator(); i.hasNext();)
        {
            Hashtable user = (Hashtable)i.next();            
            describeHash(user);
            System.out.println("-------------------");
        }
    }

    private static void describeHash(Hashtable hash)
    {
        Enumeration keys = hash.keys();
        while (keys.hasMoreElements())
        {
            String key = (String)keys.nextElement();
            String value = (String)hash.get(key);
            System.out.println("key -> " + key);
            System.out.println("value -> " + value);
        }
    }
}
@


1.3
log
@Changed package names of XML-RPC methods.
@
text
@d62 1
d64 1
a64 1
 * @@version $Id: TestXMLRPCClient.java,v 1.2 2001/07/27 19:32:21 stack Exp $
d71 6
d83 3
d148 11
d165 7
d186 9
d212 1
a212 2
            + " ACTION   The xmlrpc client method to call. Read this client"
            + " code for list.\n"
d217 4
a220 1
            + " specified.\n\n"
d222 3
d230 2
a231 1
            + " server.\n\n");
d247 13
d273 16
d314 25
a349 1

@


1.2
log
@Usage string had wrong class name.

Also fixed test for argument count.
@
text
@d63 1
a63 1
 * @@version $Id: TestXMLRPCClient.java,v 1.1 2001/07/26 20:21:46 stack Exp $
d200 1
a200 1
            (Hashtable)client.execute("permission.getUserInfo", v);
d214 1
a214 1
            (Hashtable)client.execute("permission.getUserInfo", v);
@


1.1
log
@Client to exercise the tigris permission checker xmlrpc service.
@
text
@d63 1
a63 1
 * @@version $Id: TestRpc.java,v 1.31 2001/05/14 12:40:12 edk Exp $
d91 1
a91 1
        if( argv.length == 0 || argv.length < 5 )
d172 2
a173 1
        System.out.print("Usage: java TestXMLRPC ACTION URL AUTH ARGS...\n\n"
d183 1
a183 1
            + " % java TestXMLRPC userInfoFromLoginName "
@

