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

📄 testjndiops.java

📁 JAVA开源LDAP浏览器jxplorer的源码!
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
    /**
     * Reads input as an ldif entry at a time
     * (i.e. as a series of colon seperated att:value pairs, until
     * a blank line or eof is read).
     */

    public void processInput()
    {
        // start the infinite loop...
        try
        {
            DXEntry commandSet;
            while ((commandSet = ldifutil.readLdifEntry(in)) != null)
            {
                if (commandSet == null)
                    error("internal error in jnditest - parsed ldif command set null", null);

                if (processCommand(commandSet) == FINISHED)
                    return;
            }
        }
        catch (Exception e)
        {
            error("error reading input - program stopped." + "\n  ", e);
            System.exit(-1);
        }
    }

    /**
     * This checks that an entry is valid, and
     * then passes it to the appropriate command handler.
     *
     * @param entry the DXEntry object containing a dn attribute,
     *              a changetype attribute, and any data attributes required
     *              by the ldif command.
     * @return code - 0 = normal, 1 = error, -1 = finished.
     */

    public int processCommand(DXEntry entry)
    {
        // do some sanity checking
        if (entry.size() == 0)
        {
            if (debug) out.println("\n\nEnding on double blank line");
            return FINISHED;
        }

        if (entry.get("version") != null)
            entry.remove("version");         // strip out version number.

        if (entry.getDN().size() == 0)
        {
            DXAttribute temp = (DXAttribute) entry.get("changetype");
            String test = "";
            try
            {
                test = temp.get().toString();
            }
            catch (Exception e)
            {
            } // never happen
            // two commands do not require a dn...
            if ((test.equalsIgnoreCase("connect") == false) &&
                    (test.equalsIgnoreCase("disconnect") == false))
            {
                error("error reading input - no dn attribute in entry!\n*******\n" + entry + "******\n", null);
                return ERROR;
            }
        }

        DXAttribute command = (DXAttribute) entry.get("changetype");
        if (command == null || command.size() == 0)
        {
            error("error reading input - no 'changetype' attribute in entry.\n******\n" + entry + "******\n", null);
            return ERROR;
        }

        String commandString = "";

        try
        {
            commandString = command.get().toString();
        }
        catch (NamingException e)
        {
            error("internal error in processCommand()\n  ", e);
            return ERROR;
        }    // never happen :-)

        if (debug) System.out.println("\n\nCOMMAND= " + commandString);

        entry.remove("changetype");

        if ((myOps == null) && !(commandString.equalsIgnoreCase("connect") || commandString.equalsIgnoreCase("disconnect")))
            error("Attempting operation " + commandString + " without an open connection!", null);

        // branch to appropriate handler
        try
        {
            if (commandString.equalsIgnoreCase("add"))
                addEntry(entry, returnType(entry));
            else if (commandString.equalsIgnoreCase("delete"))
                deleteEntry(entry, returnType(entry));
            else if (commandString.equalsIgnoreCase("modrdn"))
                modrdnEntry(entry, modRDN(entry));
            else if (commandString.equalsIgnoreCase("modify"))
                modifyEntry(entry, returnType(entry));
            else if (commandString.equalsIgnoreCase("list"))
                listEntry(entry, list(entry));
            else if (commandString.equalsIgnoreCase("search"))
                searchEntry(entry, search(entry));
            else if (commandString.equalsIgnoreCase("searchOneLevel"))
                searchOneLevelEntry(entry, search(entry));
            else if (commandString.equalsIgnoreCase("read"))
                readEntry(entry, read(entry));
            else if (commandString.equalsIgnoreCase("connect"))
                connect(entry);
            else if (commandString.equalsIgnoreCase("disconnect"))
                disconnect(entry);
            else if (commandString.equalsIgnoreCase("copy"))
                copy(entry, copy(entry, command));
            else if (commandString.equalsIgnoreCase("cut"))
                cut(entry, cut(entry, command));
        }
        catch (NamingException e)
        {
            error("Naming Exception in " + commandString + "\n  ", e);
            return ERROR;
        }

        return OK;
    }


    /**
     * Gets the 'returntype' value from the test ldif file and parses it into a string.
     * 'returntype' is a boolean value pertaining to the add, delete or modify 'changetype'
     * functions in the ldif file. i.e. it signifies if an entry should actually be
     * added or deleted. Changes the value to boolean. Removes the 'returntype' attribute.
     *
     * @param entry the DXEntry object containing a dn attribute, a changetype attribute, and any data attributes required.
     * @return expectedVal a boolean value that represents the success or failure of the modification.
     */

    public boolean returnType(DXEntry entry)
    {

        DXAttribute returnType = (DXAttribute) entry.get("returntype");

        String myType = "";
        try
        {
            myType = returnType.get().toString();
        }
        catch (Exception e)
        {
        }

        boolean expectedVal = "true".equalsIgnoreCase(myType);

        if (returnType != null)
        {
            if (debug) System.out.println("\n\nparsed returntype: " + myType + "\n as: " + expectedVal + "\n");
            entry.remove("returntype");
        }
        return expectedVal;
    }


    /**
     * Add an entry to a directory.  Compares the 'returntype' value in the ldif file (if
     * present) to the result of the add operation.  If the two don't match the program exits.
     *
     * @param entry         contains the att/val pairs to be added, and the dn to add them to.
     * @param expectedValue a flag that indicates the modification success.
     */

    public void addEntry(DXEntry entry, boolean expectedValue)
    {
        if (debug) System.out.println("\nADD: " + entry);

        boolean ret = false;

        try
        {
            myOps.addEntry(entry.getDN(), entry);
            ret = true;
        }
        catch (NamingException e)
        {
        }

        //TE: flag to test if the modification succeeded or not.

        if (ret != expectedValue)                   //TE: tests if the modification status is what we expected.
        {
            if (debug) System.out.println("\nret equals expectedValue: " + (ret == expectedValue) + "\n");
            if (debug) System.out.println("\n&&&& RET: " + ret + "  &&&& EXPECTEDVALUE: " + expectedValue);
            error("\nadd operation failed for: " + entry + "\n", null);
        }
    }


    /**
     * Delete an entry from a directory. Compares the 'returntype' value in the ldif file (if
     * present) to the result of the delete operation.  If the two don't match the program exits.
     *
     * @param entry         contains dn to delete.
     * @param expectedValue exptectedValue a flag that indicates the modification success.
     */

    public void deleteEntry(DXEntry entry, boolean expectedValue)
    {
        if (debug) System.out.println("\nDELETE: " + entry);

        boolean ret = false;

        try
        {
            myOps.deleteEntry(entry.getDN());
            ret = true;
        }
        catch (NamingException e)
        {
        }

        //TE: flag to test if the modification succeeded or not.

        if (ret != expectedValue)                   //TE: tests if the modification status is what we expected.
        {
            if (debug) System.out.println("\n\nRET  " + ret + "  EXPECTEDVALUE  " + expectedValue);
            if (debug) System.out.println("\nret equals expectedValue: " + (ret == expectedValue) + "\n");
            error("\ndelete operation failed for: " + entry + "\n", null);
        }
    }


    /**
     * Gets the 'modrdnresult' value from the test ldif file and parses into a string.
     * 'modrdnresult' is the 'cn' attribute of the entry after the rename.
     * Removes the 'modrdnresult' attribute. Note: 'modrdnresult' can be configured to
     * represent any string attribute.
     *
     * @param entry contains the dn to start searching from, and a 'filter' attribute with a search filter.
     * @return myModRdnCn
     */

    public String modRDN(DXEntry entry)
    {

        DXAttribute modRdnResult = (DXAttribute) entry.get("modrdnresult");

        String myModRdnCn = "";
        try
        {
            myModRdnCn = modRdnResult.get().toString();
        }
        catch (Exception e)
        {
        }

        if (modRdnResult != null)
        {
            if (debug) System.out.println("\n\nparsed modrdnresult (DXAttribute): " + modRdnResult + "\n to mymodrdnCn(String): " + myModRdnCn);
            entry.remove("modrdnresult");
        }
        return myModRdnCn;
    }


    /**
     * Modifies the rdn of a directory.  Compares the 'modrdnresult' value in the ldif file (if
     * present) to the rdn of the entry.  If the two don't match the program exits.
     *
     * @param entry          contains dn to be modified.
     * @param expectedModrdn a flag that indicates the value that the modify is expected to return.
     */

    public void modrdnEntry(DXEntry entry, String expectedModrdn)
    {
        String newDNString = entry.getString("newrdn");
        DN newDN = new DN(newDNString);
        if (debug) System.out.println("modrdn: " + entry.getDN() + "\n  to: " + newDN);

        boolean ret = false;

        try
        {
            myOps.renameEntry(entry.getDN(), newDN);
            ret = true;
        }
        catch (NamingException e)
        {
        }

        if (ret == false)
            error("modrdn operation failed for: " + entry, null);

        int compare = -2;                           //TE: the flag to compare the expected DN with the actual DN.

        compare = expectedModrdn.compareTo(newDNString);    //TE: the compare of the expected DN with the actual DN.

        if (compare != 0)                            //TE: if 0, the two results are the same, therefore the modify performed as expected.
        {
            if (debug) System.out.println("\n\nnewDN CN String: " + newDNString);
            if (debug) System.out.println("EXPECTEDVALUE  : " + expectedModrdn);
            error("\nmodrdn operation failed for: " + entry + "\nExpected read result for cn: " + expectedModrdn + "\nActual result for cn: " + newDNString, null);
        }
    }


    /**
     * Modifies the attributes of an entry in a directory.  Compares the 'returntype' value in the ldif file (if
     * present) to the result of the modify operation.  If the two don't match the program exits.
     *
     * @param entry          contains the entry to be modified, and the list of att/val modification pairs (see ldif spec.).
     * @param expectedValue a flag that indicates the modification success.
     */

    public void modifyEntry(DXEntry entry, boolean expectedValue)
    {
        if (debug) System.out.println("modify: " + entry);

        Name myDN = entry.getDN();


        /*TE:   The three operations to modify an entry are:
                                add
                                delete
                                replace
                A test is done to see which operation is to be carried out.

                Note: within one ldif entry at this stage we can't do multiple operations
                on the same attribute or the same operations on multiple attributes
                because of the way the parser works,
                for example:
                                dn: cn=T,ou=Applications,ou=Customer,o=Democorp,c=AU
                                changetype: modify
                                add: drink
                                drink: red wine
                                drink: white wine
                                -
                                delete: drink
                                drink: red wine

                would result in both the 'drink' attributes being deleted.  To delete just the
                'red wine' attribute, write another ldif entry,
                for example:
                                dn: cn=T,ou=Applications,ou=Customer,o=Democorp,c=AU
                                changetype: modify
                                delete: drink
                                drink: red wine

                however, deleting another attribute not used by the add operation should work fine
                for example:
                                dn: cn=T,ou=Applications,ou=Customer,o=Democorp,c=AU
                                changetype: modify
                                add: drink
                                drink: red wine
                                drink: white wine
                                -
                                delete: cn
                                cn: TEST
        */

        DXAttribute add = null;
        DXAttribute delete = null;
        DXAttribute replace = null;

        add = (DXAttribute) entry.get("add");
        delete = (DXAttribute) entry.get("delete");
        replace = (DXAttribute) entry.get("replace");

⌨️ 快捷键说明

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