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

📄 permistestbench.java

📁 一个完整的XACML工程,学习XACML技术的好例子!
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                    additionalInfo = pe.getMessage();
                } catch (Throwable th){
                    code = 3;
                    resultInfo = "Run-time error";
                    additionalInfo = th.getMessage();
                }
                // Now we need to put the decision in the output file
                if (!writeDecisionData(out,rqNumber,new Integer(code).toString(),resultInfo,additionalInfo))
                    break;
            }
            out.flush(); out.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    /**
     * Reads the decision header. These are the fields read by
     * this method:
     * <p>
     * <ul>
     *   <li> <code>REQUEST_FILE="name of the file containing the requests"; NOE [1,1]</code>
     *   <li> <code>REQUEST_FILE_HASH="hash value of the request file"; NOE [1,1]</code>
     *   <li> <code>EVALUATION_DATE="date on which the decisions were taken"; NOE [1,1]</code>
     * </ul>
     *
     * @param in is buffered reader related to the decision file
     * @return String[3] containing: [0] name of the request file, [1] hash value, [2] date; null if EOF
     */
    protected String[] loadDecisionHeader(java.io.BufferedReader in) {
        String[] varValue;
        String requestFile = "";
        String requestHash = "";
        String evaluationDate = "";

        varValue = loadVarValue(in);
        if (varValue == null) return null;

        if (varValue[0].intern() == "REQUEST_FILE") {
            requestFile =  varValue[1];
        }

        varValue = loadVarValue(in);
        if (varValue == null) return null;

        if (varValue[0].intern() == "REQUEST_FILE_HASH") {
            requestHash =  varValue[1];
        }

        varValue = loadVarValue(in);
        if (varValue == null) return null;

        if (varValue[0].intern() == "EVALUATION_DATE") {
            evaluationDate =  varValue[1];
        }
        String[] result = new String[3];
        result[0] = requestFile;
        result[1] = requestHash;
        result[2] = evaluationDate;
        return result;
    }

    /**
     * Reads the information related to a decision contained in a decision file.
     * These are the fields read by this method:
     * <p>
     * <ul>
     *   <li> <code>RQ_NUMBER="number of the request"; NOE [1,1]</code>
     *   <li> <code>RESULT_CODE="0: allowed; 1: not allowed; 2: Invalid input; 3: Run-time error"; NOE [1,1]</code>
     *   <li> <code>RESULT_INFO="code description"; NOE [1,1]</code>
     *   <li> <code>ADDITIONAL_INFO="additional info about exceptions or errors"; NOE [1,1]</code>
     * </ul>
     *
     * @param in is buffered reader related to the decision file
     * @return String[4]: [0] request number; [1] result code; [2] info; [3] additional info; null if EOF
     */
    protected String[] loadDecision(BufferedReader in) {
        String[] varValue;
        String rqNumber = "";
        String resultCode = "";
        String resultInfo = "";
        String additionalInfo = "";

        varValue = loadVarValue(in);
        if (varValue == null) return null;

        if (varValue[0].intern() == "RQ_NUMBER") {
            rqNumber =  varValue[1];
        } else return null;

        varValue = loadVarValue(in);
        if (varValue == null) return null;

        if (varValue[0].intern() == "RESULT_CODE") {
            resultCode =  varValue[1];
        } else return null;

        varValue = loadVarValue(in);
        if (varValue == null) return null;

        if (varValue[0].intern() == "RESULT_INFO") {
            resultInfo =  varValue[1];
        } else return null;

        varValue = loadVarValue(in);
        if (varValue == null) return null;

        if (varValue[0].intern() == "ADDITIONAL_INFO") {
            additionalInfo =  varValue[1];
        }
        String[] result = new String[4];
        result[0] = rqNumber;
        result[1] = resultCode;
        result[2] = resultInfo;
        result[3] = additionalInfo;
        return result;
    }

    /**
     * Compares two decision files. As a result, a diff file is generated
     * according to the following format:
     * <p>
     * HEADER: (see writeDiffHeader)
     * <p>
     * FOR EACH DECISION:
     * <p>
     * <ul>
     *   <li> <code>[Checking request "number of request"]</code>
     *   <li> <code>  [(OK) Decision codes are equal || (WN) Decision codes differ "code1" VS "code2"]</code>
     *   <li> <code>  [(OK) Messages are the same || (WN) Messages Differ]</code>
     *   <li> <code>  [(OK) No additional information provided || (OK) Additional notes are the same || (WN) Notes differ]</code>
     * </ul>
     *
     * @param f1 is one of the decision files
     * @param f2 is the other decision file
     * @param diff is the name of the file which is going to contain the differences
     */
    public void checkDecisionFiles(String f1,String f2, String diff) {
        try {
            java.io.BufferedReader in1 = new java.io.BufferedReader(new java.io.FileReader(f1));
            java.io.BufferedReader in2 = new java.io.BufferedReader(new java.io.FileReader(f2));
            java.io.BufferedWriter out = new java.io.BufferedWriter(new java.io.FileWriter(diff));

            // First we read both decision headers
            String[] header1 = loadDecisionHeader(in1);
            String[] header2 = loadDecisionHeader(in2);

            if (header1 == null || header2 == null) return;
            // Then we analyse those headers and we write the analysis in diff
            //writeDiffHeader(out,header1,header2,new File(f1).getName(),new File(f2).getName());

            String[] decision1 = null;
            String[] decision2 = null;

            //For each decision in the two files
            decision1 = loadDecision(in1);
            decision2 = loadDecision(in2);
            while ((decision1 != null) && (decision2 != null)) {
                //We check whether both decisions refer to the same request number
                if (decision1[0].intern() == decision2[0].intern()) {
                    out.write("[Checking request " + decision1[0]+"]"); out.newLine();
                    //We check whether both decision codes are equal
                    if (decision1[1].intern() == decision2[1].intern()) {
                        out.write("  (OK) Decision codes are equal."); out.newLine();
                    }
                    else {
                        out.write("  (WN) Decision codes differ (" + decision1[1] + " VS " + decision2[1] + ")");
                        out.newLine();
                    }
                    //We check whether both code descriptions are equal
                    if (decision1[2].intern() == decision2[2].intern()) {
                        out.write("  (OK) Messages are the same."); out.newLine();
                    }
                    else {
                        out.write("  (WN) Messages differ:"); out.newLine();
                        out.write("    1. " + decision1[2]); out.newLine();
                        out.write("    2. " + decision2[2]); out.newLine();
                    }
                    if ((decision1[3].intern() == "") && (decision2[3].intern() == "")) {
                        out.write("  (OK) No additional information provided."); out.newLine();
                    }
                    else
                        if (decision1[3].intern() == decision2[3].intern()) {
                            out.write("  (OK) Additional notes are the same.");
                            out.newLine();
                        } else {
                            out.write("  (WN) Additional notes differ:");
                            out.newLine();
                            out.write("    1. " + decision1[3]);
                            out.newLine();
                            out.write("    2. " + decision2[3]);
                            out.newLine();
                        }
                }
                else break;
                decision1 = loadDecision(in1);
                decision2 = loadDecision(in2);
            }
            if ((decision1 != null) && (decision2 != null)) {
                out.write("**ERROR**: Check the sequences of request numbers in both files, they differ.");
                out.newLine();
            }
            else if ((decision1 != null) || (decision2 != null)) {
                if (decision1 == null) {
                    out.write("**WARNING**: The first file is shorter than the second one");
                    out.newLine();
                }
                else {
                    out.write("**WARNING**: The second file is shorter than the first one");
                    out.newLine();
                }
            }
            in1.close();in2.close();
            out.flush(); out.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    /**
     * Obtains the hash value of a request file
     *
     * @param rqFile is name of the request file
     * @return byte[] containing the SHA-1 hash of the file
     */
    protected byte[] getRequestHash(String rqFile) {

        try {
            MessageDigest md = MessageDigest.getInstance("SHA");
            FileInputStream fis = new FileInputStream(rqFile);
            byte[] content = new byte[fis.available()];
            int nb = fis.read(content);
            while (nb != -1) {
                md.update(content,0,nb);
                nb = fis.read(content);
            }
            byte[] result = md.digest();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * Reads the decision header. These are the fields read by
     * this method:
     * <p>
     * <ul>
     *   <li> <code>REQUEST_FILE="name of the file containing the requests"; NOE [1,1]</code>
     *   <li> <code>REQUEST_FILE_HASH="hash value of the request file"; NOE [1,1]</code>
     *   <li> <code>EVALUATION_DATE="date on which the decisions were taken"; NOE [1,1]</code>
     * </ul>
     *
     * @param out is buffered writer related to the diff file
     * @param h1 contains the 3 fields of the header included in the first decision file
     * @param h2 contains the 3 fields of the header included in the second decision file
     * @param f1 is the name of the first decision file
     * @param f2 is the name of the second decision file
     * @return true if the diff header was successfully written
     */
    protected boolean writeDiffHeader(java.io.BufferedWriter out, String[] h1, String[] h2, String f1, String f2) {
        try {
            out.write("CHECKING " + f1 + " against " + f2); out.newLine();
            if (h1[1].intern() == h2[1].intern()) {
                out.write("Both files are related to the same requests, i.e. same hash: <"+h1[1]+">");
                out.newLine();
            }
            else {
                out.write("WARNING: Decisions correspond to different request files");
                out.newLine();
                out.write("  Hash of File 1 <"+h1[1]+">"); out.newLine();
                out.write("  Hash of File 2 <"+h2[1]+">"); out.newLine();
            }

            java.util.Date date1 = new java.text.SimpleDateFormat().parse(h1[2]);
            java.util.Date date2 = new java.text.SimpleDateFormat().parse(h2[2]);

            if (date1.before(date2)) {
                out.write("File "+ f2 + " is newer than file " + f1); out.newLine();
            }
            else {
                out.write("File "+ f1 + " is newer than file " + f2); out.newLine();
            }
            return true;
        }
        catch (Exception e) {
            return false;
        }
    }

    protected void authenticate(java.security.Principal user){
	}
}

⌨️ 快捷键说明

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