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

📄 jndiops.java

📁 JAVA开源LDAP浏览器jxplorer的源码!
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
    {
        BasicAttributes atts = new BasicAttributes();
        atts.put(a);
        modifyAttributes(dn, DirContext.REMOVE_ATTRIBUTE, atts);
    }

    /**
     * deletes a set of attribute-s from an object
     *
     * @param dn distinguished name of object
     * @param a  the Attributes object containing the
     *           list of attribute-s to delete
     */


    public void deleteAttributes(String dn, Attributes a)
            throws NamingException
    {
        modifyAttributes(dn, DirContext.REMOVE_ATTRIBUTE, a);
    }

    /**
     * updates an Attribute with a new value set
     *
     * @param dn distinguished name of object
     * @param a  the attribute to modify
     */

    public void updateAttribute(String dn, Attribute a)
            throws NamingException
    {
        BasicAttributes atts = new BasicAttributes();
        atts.put(a);
        modifyAttributes(dn, DirContext.REPLACE_ATTRIBUTE, atts);
    }

    /**
     * updates a set of Attribute-s.
     *
     * @param dn distinguished name of object
     * @param a  an Attributes object containing the attribute-s to modify
     */

    public void updateAttributes(String dn, Attributes a)
            throws NamingException
    {
        modifyAttributes(dn, DirContext.REPLACE_ATTRIBUTE, a);
    }

    /**
     * Adds a new attribute to a particular dn.
     *
     * @param dn distinguished name of object
     * @param a  the attribute to modify
     */

    public void addAttribute(String dn, Attribute a)
            throws NamingException
    {
        BasicAttributes atts = new BasicAttributes();
        atts.put(a);
        modifyAttributes(dn, DirContext.ADD_ATTRIBUTE, atts);
    }

    /**
     * Adds a set of attributes to a particular dn.
     *
     * @param dn distinguished name of object
     * @param a  the Attributes (set of attribute-s) to add
     */

    public void addAttributes(String dn, Attributes a)
            throws NamingException
    {
        modifyAttributes(dn, DirContext.ADD_ATTRIBUTE, a);
    }


    /**
     * returns the next level of a directory tree, returning
     * a Enumeration of the results, *relative* to the SearchBase (i.e. not as
     * absolute DNs), along with their object classes if possible.
     * <p/>
     * <p>WARNING - due to jndi wierdness, sometimes the entries are *not* relative, but are full DNs.</p>
     *
     * @param searchbase the node in the tree to expand
     * @return list of results (NameClassPair); the next layer of the tree...
     */

    public NamingEnumeration list(String searchbase)
            throws NamingException
    {
        //    Attempt to read the names of the next level of subentries along with their object
        //    classes.  Failing that, try to just read their names.

        return rawSearchOneLevel(nameParser.parse(searchbase), "(objectclass=*)", 0, 0, new String[]{"1.1"});
    }

    /**
     * Method that calls the actual search on the jndi context.
     *
     *   @param searchbase the domain name (relative to initial context in ldap) to seach from.
     *   @param filter the non-null filter to use for the search
     *   @param limit the maximum number of results to return
     *   @param timeout the maximum time to wait before abandoning the search
     *   @param returnAttributes an array of strings containing the names of attributes to search. (null = all, empty array = none)
     * @return
     * @throws NamingException
     */
/*
     private NamingEnumeration rawOneLevelSearch(String searchbase, String filter, int limit,
                 int timeout, String[] returnAttributes ) throws NamingException
     {
         // specify search constraints to search one level
         SearchControls constraints = new SearchControls();

         constraints.setSearchScope(SearchControls.ONELEVEL_SCOPE);
         constraints.setCountLimit(limit);
         constraints.setTimeLimit(timeout);

         constraints.setReturningAttributes(returnAttributes);

         NamingEnumeration results = ctx.search(searchbase, filter, constraints);

         return results;

     }
*/



    /**
     * Shuts down the current context.<p>
     * nb. It is not an error to call this method multiple times.
     */

    public void close()
            throws NamingException
    {
        if (ctx == null) return;  // it is not an error to multiply disconnect.
        nameParser = null;
        ctx.close();
        //TODO: decide if we should set ctx to null at this point
    }

    /**
     * This method allows an object to be renamed, while also specifying
     * the exact fate of the old name.
     *
     * @param OldDN        the original name to be changed
     * @param NewDN        the new name
     * @param deleteOldRDN whether the rdn of the old name should be removed,
     *                     or retained as a second attribute value.
     */

    public void renameEntry(String OldDN, String NewDN, boolean deleteOldRDN)
            throws NamingException
    {
        String value = (deleteOldRDN) ? "true" : "false";
        try
        {
            ctx.addToEnvironment("java.naming.ldap.deleteRDN", value);

            renameEntry(OldDN, NewDN);

            ctx.addToEnvironment("java.naming.ldap.deleteRDN", "false");  // reset to default of 'false' afterwards.
        }
        catch (NamingException e)
        {
            ctx.addToEnvironment("java.naming.ldap.deleteRDN", "false");  // reset to default of 'false' afterwards.
            throw e;  // rethrow exception...
        }
    }

    //	-----------------------------------------------------------------------
    //	Name related magic - put here for now, but probably should go elsewhere

    private static Properties nameParserSyntax = null;

    /**
     * setupLDAPSyntax
     * <p/>
     * Set up the syntax rules for parsing LDAP DNs when creating Name objects.
     */
    private static void setupLDAPSyntax()
    {
        nameParserSyntax = new Properties();

        nameParserSyntax.put("jndi.syntax.direction", "right_to_left");
        nameParserSyntax.put("jndi.syntax.separator", ",");
        nameParserSyntax.put("jndi.syntax.escape", "\\");

        // Not currently used, as the parser seems to preferentially quote rather than use escape chars.  May be an issue with LDAPv2
        // quoted RDNs.
        // 		nameParserSyntax.put("jndi.syntax.beginquote", "\"")
        // 		nameParserSyntax.put("jndi.syntax.endquote", "\"");

        nameParserSyntax.put("jndi.syntax.trimblanks", "true");
        nameParserSyntax.put("jndi.syntax.separator.typeval", "=");
    }

    /**
     * getNameFromString
     * <p/>
     * Convert DN String into JNDI Name,
     *
     * @param iDN DN in String.
     * @return	the resulting name
     */
    //TODO: decide if this method should be static or not - should use the syntax of the current connection, after all
    public static Name getNameFromString(String iDN)
            throws NamingException
    {
        // iDN is assumed to either:
        // a) contain an LDAP DN, without either server/port information or
        // namespace identifier ('ldap://').
        // or:
        // b) Contain a full URL ('ldap://server:port/o=...').

        // Parse it and return it.

        String DN = iDN;
        Name CompositeFormDN = null;
        CompoundName CompoundFormDN = null;

        if (iDN.indexOf("ldap://") != -1)
        {
            // iDN contains the string 'ldap://', and therefore has
            // at least 2 name spaces.  Instantiate a Composite name
            // object and strip off the name we want.
            CompositeFormDN = new CompositeName(iDN);
            if (CompositeFormDN.size() != 0)
                DN = CompositeFormDN.get(CompositeFormDN.size() - 1);
        }

        if (nameParserSyntax == null)
            setupLDAPSyntax();

        CompoundFormDN = new CompoundName(DN, nameParserSyntax);

        return CompoundFormDN;
    }

    /**
     * getNameFromSearchResult
     * <p/>
     * Given a SearchResult object and Base DN, work out the complete DN of the entry, parse it into a Name object and return it.
     *
     * @param iDirectoryEntry JNDI SearchResult object containing a Directory entry.
     * @param iBaseDN         Name object with the Base DN used for the search (may be empty).
     * @return Name object containing the complete DN of the entry.
     */
    //TODO: decide if this method should be static or not - should use the syntax of the current connection, after all
    public static Name getNameFromSearchResult(SearchResult iDirectoryEntry, Name iBaseDN)
            throws InvalidNameException, NamingException
    {
        // Get RDN from a string.  Parse it and if required add the base DN to it, and
        // then return it as a JNDI Name object.
        // Tim Bentley
        // 20010404

        // Take care of the JNDI trailing whitespace problem:
        String RDN = applyJNDIRDNBugWorkAround(iDirectoryEntry.getName());

        Name JNDIRDN = getNameFromString(RDN);

        if (JNDIRDN != null)
		{	// if the name is relative, insert the base DN
            if (iDirectoryEntry.isRelative())
                JNDIRDN.addAll(0, iBaseDN);
		}
		else
			JNDIRDN = (Name) iBaseDN.clone();				// if the RDN is null, use the base DN

        return JNDIRDN;
    }

    /**
     * applyJNDIRDNBugWorkAround
     * <p/>
     * Cope with escaping bug in JNDI RDN handling.
     *
     * @param iRDN String containing RDN to check escaping on.
     * @return String containing correctly escaped RDN.
     */
    private static String applyJNDIRDNBugWorkAround(String iRDN)
    {

        // Tim Bentley
        // 20010328
        // JNDI's SearchResult.getName() removes any trailing space character from the
        // RDN without also removing the LDAP escaping character ('\') - in fact it
        // then escapes the '\' character, resulting in '\\' at the end of the RDN.
        // Parse the passed in RDN and if the last two chars are '\'s, remove
        // them.

//		int 			SlashPos = iRDN.indexOf("\\");
        int SlashPos = iRDN.lastIndexOf("\\\\");	// AJR: need LAST occurrence, and need to escape backslashes
        String ReturnString;

        if (SlashPos == iRDN.length() - 2)
            ReturnString = iRDN.substring(0, SlashPos);
        else
            ReturnString = iRDN;

        return ReturnString;
    }


    public DirContext getContext()
    {
        return ctx;
    }

    public void setContext(DirContext ctx)
    {
        this.ctx = ctx;

        try
        {
            nameParser = ctx.getNameParser("");
        }
        catch (NamingException e)
        {
            // TODO: add logging to this class :-)
            System.out.println("Error initialising name parser " + e);
        }
    }


}

⌨️ 快捷键说明

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