blogger templates blogger widgets
This is part of a list of blog posts.
To browse the contents go to

LDAP Operation: Read all attributes


import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
.....
  try {
   //In the main() method of the program, create an initial directory context. 
   //This is similar to creating an initial context in the previous naming example, 
   //except that you use the constructor for InitialDirContext
   Hashtable env = new Hashtable();
   env.put(Context.INITIAL_CONTEXT_FACTORY,
       "com.sun.jndi.ldap.LdapCtxFactory");
   env.put(Context.PROVIDER_URL, "ldap://localhost:10389/o=csRepository");
 
   DirContext ctx = new InitialDirContext(env);
   
   // Ask for all attributes of the object 
      Attributes attrs = ctx.getAttributes("cn=John Eipe, ou=employees");
     
      // Iterate and print all values
      for (NamingEnumeration ae = attrs.getAll(); ae.hasMore();) {
          Attribute attr = (Attribute)ae.next();
          System.out.println("attribute: " + attr.getID());
          /* Print each value */
          for (NamingEnumeration e = attr.getAll(); e.hasMore();
        System.out.println("value: " + e.next()));
      }
  }catch(NamingException e){
   e.printStackTrace();
  }
......

Output:
Output:
attribute: mail
value: john77eipe@gmail.com
attribute: userpassword
value: [B@414d0e66
attribute: description
value: the fist and the single employee as of now
attribute: objectclass
value: organizationalPerson
value: person
value: inetOrgPerson
value: top
attribute: sn
value: Eipe
attribute: cn
value: John Eipe

Continue reading: LDAP Operations

No comments:

Post a Comment