⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ptbrequestgenerator.java

📁 一个完整的XACML工程,学习XACML技术的好例子!
💻 JAVA
字号:
/*
* Copyright (c) 2006, University of Kent
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without 
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this 
* list of conditions and the following disclaimer.
* 
* 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. 
*
* 1. Neither the name of the University of Kent nor the names of its 
* contributors may be used to endorse or promote products derived from this 
* software without specific prior written permission. 
*
* 2. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS  
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
* PURPOSE ARE DISCLAIMED. 
*
* 3. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 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.
*
* 4. YOU AGREE THAT THE EXCLUSIONS IN PARAGRAPHS 2 AND 3 ABOVE ARE REASONABLE
* IN THE CIRCUMSTANCES.  IN PARTICULAR, YOU ACKNOWLEDGE (1) THAT THIS
* SOFTWARE HAS BEEN MADE AVAILABLE TO YOU FREE OF CHARGE, (2) THAT THIS
* SOFTWARE IS NOT "PRODUCT" QUALITY, BUT HAS BEEN PRODUCED BY A RESEARCH
* GROUP WHO DESIRE TO MAKE THIS SOFTWARE FREELY AVAILABLE TO PEOPLE WHO WISH
* TO USE IT, AND (3) THAT BECAUSE THIS SOFTWARE IS NOT OF "PRODUCT" QUALITY
* IT IS INEVITABLE THAT THERE WILL BE BUGS AND ERRORS, AND POSSIBLY MORE
* SERIOUS FAULTS, IN THIS SOFTWARE.
*
* 5. This license is governed, except to the extent that local laws
* necessarily apply, by the laws of England and Wales.
*/
package issrg.test.ptb;

import issrg.pba.rbac.*;

import java.util.ArrayList;
import java.io.BufferedReader;

/**
 * This is the main class of the Permis Test Bench Request Generator Program. With this application you can
 * create, in an automated manner, request files from independent text files containing entries for
 * users, targets and actions. The generated request file can be used by the Permis Test Bench without further
 * editing.
 *
 * @author O Canovas
 * @author O Otenko
 * @version 0.1
 */

public class PTBRequestGenerator {
	protected ArrayList users;    //array of users
	protected ArrayList targets;  //array of targets
    protected ArrayList actions;  //array of actions

	public static void main(String [] args){
		System.setProperty("line.separator", "\r\n");

        PTBRequestGenerator generator = new PTBRequestGenerator();

        if (args.length != 4) { //The application needs 4 arguments
            printUsage();
            return;
        }
        // The first argument is the users file name
        if (!generator.loadUsersFile(args[0])) {
            System.out.println("**ERROR**: An error occurred while processing the file of users");
            return;
        }
        // The first argument is the targets file name
        if (!generator.loadTargetsFile(args[1])) {
            System.out.println("**ERROR**: An error occurred while processing the file of targets");
            return;
        }
        // The third argument is the actions file name
        if (!generator.loadActionsFile(args[2])) {
            System.out.println("**ERROR**: An error occurred while processing the file of actions");
            return;
        }
        // The requests are generated and stored in the file named args[3]
        generator.generateRequestFile(args[3]);
    }

    /**
     * Prints how to use this application (number and order of parameters)
     *
     */
    public static void printUsage()
    {
        System.out.println("Usage:");
        System.out.println("    PTBRequestGenerator <users_file> <targets_file> <actions_file> <output_file>");
    }


    /**
     * Constructs a Permis Test Bench Request Generator. It has no parameters, and
     * its main function is to initialise the Array Lists
     */
	public PTBRequestGenerator(){
        users = new ArrayList();
        targets = new ArrayList();
        actions = new ArrayList();
	}

    /**
     * Reads (attribute,value) pairs from a buffered reader. This method
     * processes each line of the buffered reader looking for the <code>PARAMETER=VALUE</code> pattern.
     * Once that pattern is found, it returns an array of Strings containing the name of the
     * parameter in the first element and the value in the second element. On the other hand,
     * when the end of the buffered reader is reached, it returns null.
     *
     * @param in is the buffered reader
     * @return String[2]: <code>[0]</code> is the name of the parameter; <code>[1]</code> is the value; <code>null if EOF</code>
     *
     */
    protected String[] loadVarValue(BufferedReader in) {
        String s = "";
        try {
            while (true) {
                s= in.readLine();
                if (s == null) return null;
                if (s.startsWith("#")) continue;
                int i = s.indexOf('=');	// find the assignment mark
                String varName = null;
                if (i>=0){
                    varName = s.substring(0, i).intern();
                    s = s.substring(i+1);
                    String[] result = new String[2];
                    result[0] = varName;
                    result[1] = s;
                    return result;
                }
                else continue;
            }
        }
        catch (Exception e) {
            return null;
        }
    }

    /**
     * Reads the list of users contained in a users file. Each user entry
     * has the following format:
     * <p>
     * <ul>
     *   <li> <code>(USER_DN || USER)="distinguished name of the requestor"</code>
     * </ul>
     * <p>
     * If a line starts with #, it will be considered as a comment (it is ignored).
     * <p>
     *
     * @param file is the name of the file containing the set of users
     * @return true if <code>file</code> was successfully read
     */
    public boolean loadUsersFile(String file) {

        try {
            java.io.BufferedReader in = new java.io.BufferedReader(new java.io.FileReader(file));
            String[] varValue = null;

            while ((varValue = loadVarValue(in)) != null) {
                if ((varValue[0].intern() == "USER_DN") || (varValue[0].intern() == "USER")) {
                    users.add(new String(varValue[1]));
                }
            }
            return (users.size() != 0);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * Reads the list of targets contained in a targets file. Each target entry
     * has the following format:
     * <p>
     * <ul>
     *   <li> <code>(TARGET_DN || TARGET)="name of the requested resource (DN or URI)"</code>
     * </ul>
     * <p>
     * If a line starts with #, it will be considered as a comment (it is ignored).
     * <p>
     *
     *
     * @param file is the name of the file containing the set of targets
     * @return true if <code>file</code> was successfully read
     */
    public boolean loadTargetsFile(String file) {

        try {
            java.io.BufferedReader in = new java.io.BufferedReader(new java.io.FileReader(file));
            String[] varValue = null;

            while ((varValue = loadVarValue(in)) != null) {
                if ((varValue[0].intern() == "TARGET_DN") || (varValue[0].intern() == "TARGET")) {
                    targets.add(new String(varValue[1]));
                }
            }
            return (targets.size() != 0);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * Reads the list of actions contained in a actions file. Each action
     * has the following format (the number of entries, NOE, specifies
     * the minimun and maximun number):
     * <p>
     * <ul>
     *   <li> <code>ACTION="action being requested"; NOE [1,1]</code>
     *   <li> <code>ARG_TYPE="type of the argument"; NOE [0,N]</code>
     *   <li> <code>ARG_VALUE="value of the argument"; NOE [0,N]</code>
     * </ul>
     * <p>
     * If a line starts with #, it will be considered as a comment (it is ignored).
     * <p>
     *
     *
     * @param file is the name of the file containing the set of actions
     * @return true if <code>file</code> was successfully read
     */
    protected boolean loadActionsFile(String file) {
        String[] varValue;
        String action = "";
        PermisAction permisAction = null;
        java.io.BufferedReader in;

        try {
            in = new java.io.BufferedReader(new java.io.FileReader(file));
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return false;
        }

        while (true) {
            varValue = loadVarValue(in);
            if (varValue == null) break;

            if (varValue[0].intern() == "ACTION") {
                action =  varValue[1];
            } else continue;

            String value = "";
            String type = "";
            int argN;
            ArrayList arguments = new ArrayList();
            argN = -1;
            do {
                argN++;
                try {
                    in.mark(1024);
                }
                catch (Exception e)
                {
                    return false;
                }
                varValue = loadVarValue(in);
                if (varValue == null) break;

                if (varValue[0].intern() == "ACTION") {
                    try {
                        in.reset();
                        break;
                    }
                    catch (Exception e) {
                        return false;
                    }
                }

                if (varValue[0].intern() == "ARG_TYPE") {
                    type = varValue[1];
                    varValue = loadVarValue(in);
                    if (varValue == null) return false;
                    if (varValue[0].intern() == "ARG_VALUE") {
                        value = varValue[1];
                    } else return false;
                }
                arguments.add(argN, new PermisArgument(type, value));
            } while (true);

            if (argN > 0) {
                PermisArgument[] permisArguments = new PermisArgument[arguments.size()];
                permisArguments = (PermisArgument[]) arguments.toArray(permisArguments);
                permisAction = new PermisAction(action, permisArguments);
            } else
                permisAction = new PermisAction(action);
            actions.add(permisAction);
        }
        return (actions.size() != 0);
    }

    /**
     * Generates the request file according to the format expected by the Permis Test Bench
     *
     * @param file is the name which is going to contain the requests
     */
    protected void generateRequestFile(String file) {
        try {
            java.io.BufferedWriter out = new java.io.BufferedWriter(new java.io.FileWriter(file));
            // Number of the current request
            int requestN = 1;
            // Number of users
            int noUsers = users.size();
            // Number of targets
            int noTargets = targets.size();
            // Number of actions
            int noActions = actions.size();
            // The generated requests are derived from all the combinations of users, targets and actions
            for (int i = 0; i < noUsers; i++) {
                String user = (String) users.get(i);
                for (int j = 0; j < noTargets; j++) {
                    String target = (String) targets.get(j);
                    for (int k = 0; k < noActions; k++) {
                        PermisAction permisAction = (PermisAction) actions.get(k);
                        out.write("RQ_NUMBER="+new Integer(requestN).toString());
                        out.newLine();
                        out.write("USER="+user);
                        out.newLine();
                        out.write("TARGET="+target);
                        out.newLine();
                        out.write("ACTION="+permisAction.getActionName());
                        out.newLine();

                        PermisArgument[] arguments = new PermisArgument[permisAction.getArguments().length];
                        if (arguments != null) {
                            for (int l = 0; l  < arguments.length; l++) {
                                String type = ((permisAction.getArguments())[l]).getType();
                                String value = ((permisAction.getArguments())[l]).getValue();
                                out.write("ARG_TYPE="+type);
                                out.newLine();
                                out.write("ARG_VALUE="+value);
                                out.newLine();
                            }
                        }
                        requestN++; //New request
                    }
                }
            }
            out.flush();
            out.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -