LDAP操作之java篇(2)

LDAP操作之java篇(2

       续接上一篇我们将举例来讲解对节点的操作(也就是entry),包括查询、增加、修改、删除节点。

         增加节点

/**

*entry :包含新增节点的所有信息

/*

public boolean addInformation(Entry entry)

       {

              try

              {

                     Attributes attrs = new BasicAttributes();

                     Enumeration attrEnum = entry.keys();

                     while (attrEnum.hasMoreElements())

                     {

                            String type = (String) attrEnum.nextElement();

                            Attribute oneAttr = new BasicAttribute(type);

                            Vector vals = (Vector) entry.get(type);

                            Enumeration valEnum = vals.elements();

                            while (valEnum.hasMoreElements())

                            {

                                   oneAttr.add((String) valEnum.nextElement());

                            }

                            attrs.put(oneAttr);

                     }

                     dc.createSubcontext(entry.getDN(), attrs);

                     return true;

              } catch (Exception ne) {

                     //System.err.println("Error: " + ne.getMessage());

                     ne.printStackTrace();

                     return false;

              }

       }

      

       举一个构造entry的例子:

       Entry en    = new Entry();

       String enDN = "cn=dsafa,dc=gsmd,dc=com";

       Vector employeeName = new Vector();

       Vector objectClass = new Vector();

              Vector mailAddress = new Vector();

              Vector mobilePhone = new Vector();

              en.setDN(enDN);

              employeeName.add("liufu");

              en.put("employeeName",employeeName);

              objectClass.add("cssisMail");

              en.put("objectClass",objectClass);

              mailAddress.add("dsfsa");

              en.put("mailAddress",mailAddress);

              mobilePhone.add("1234");

              en.put("mobilePhone",mobilePhone);

      

         修改节点的信息

每个节点相当于一个实体,每个实体有很多属性,当然我们要改的就是某些或某个属性的值。

       /**

       *dn :节点的位置 如:ou = kk, dc = china ,dc=com

       /*

       public boolean modifyInformation(String dn)

       {

              try {

                     ModificationItem[] mods = new ModificationItem[2];

                     Attribute mobilePhone = new BasicAttribute("mobilePhone",

                     "12315");

                     Attribute employeeName = new BasicAttribute("employeeName","Smith");

                     mods[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE,

                                   mobilePhone);

                     mods[1] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,

                                   employeeName);

                     dc.modifyAttributes(dn,mods);

                     return true;

              } catch (NamingException ne) {

                     System.err.println("Error: " + ne.getMessage());

                     return false;

              }

       }

      

         删除指定的节点

这个简单没啥好说的把:)

public boolean deleteInformation(String dn)

       {

              try {

                     dc.destroySubcontext("cn=asdf,ou=mail,dc=gsmd,dc=com");

                     return true;

              } catch (NamingException ne) {

                     System.err.println("Error: " + ne.getMessage());

                     return false;

              }

       }

 

         修改节点的名字(dn

这个好像只能修改叶子节点的,有没有人找到可以修改非叶子节点dn的方法,告诉[email protected]

              public boolean renameEntry(String oldDN,String newDN)

              {

                     try {

                            dc.rename(oldDN,newDN);

                            return true;

                     } catch (NamingException ne) {

                            System.err.println("Error: " + ne.getMessage());

                            return false;

                     }

              }

这两篇文章就LDAP的一些操作进行了简单的讲解,ldap远远不止这些,偶还是一只小小菜鸟,不断在学习中,我会留下偶学习的痕迹的,希望能与各位大大交流,有啥问题也可以一起探讨,[email protected]

你可能感兴趣的:(LDAP)