tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Java > LDAP > How to Query default attributes of LDAP Entry

How to Query default attributes of LDAP Entry 

LDAP (Lightweight Directory Access Protocol) is based on X.500 standard. Its a hierarchical data structure with Entries organized in a tree like structure called Directory Information Tree (DIT). The following example shows retrieving the default attributes of all entries having objectclass as 'person'.

directory.png

File Name  :  
com/bethecoder/tutorials/ldap/QueryDefaultAttributesTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.ldap;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.NameNotFoundException;
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;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;


public class QueryDefaultAttributesTest {

  /**
   @param args
   */
  public static void main(String[] args) {

    //Setup the environment to login as anonymous user
    Hashtable<String, String> environment = new Hashtable<String, String>();
    environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    environment.put(Context.PROVIDER_URL, "ldap://localhost:389/dc=test,dc=com");

    DirContext dirContext = null;
    NamingEnumeration<?> results = null;
    
    try {
      dirContext = new InitialDirContext(environment);

      /**
       * Retrieve the default attributes 
       */
      SearchControls controls = new SearchControls();
      controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
      
      //Get entries having objectclass=person
      String filter = "(objectclass=person)";
      results = dirContext.search("", filter, controls);

      while (results.hasMore()) {
        SearchResult searchResult = (SearchResultresults.next();
        Attributes attributes = searchResult.getAttributes();
        
        NamingEnumeration<? extends Attribute> attrs = attributes.getAll();

        while (attrs.hasMore()) {
          System.out.println(attrs.next());
        }
        
        System.out.println();
      }

    catch (NameNotFoundException e) {
      e.printStackTrace();
    catch (NamingException e) {
      e.printStackTrace();
    finally {
      if (results != null) {
        try {
          results.close();
        catch (Exception e) {
        }
      }

      if (dirContext != null) {
        try {
          dirContext.close();
        catch (Exception e) {
        }
      }
    }

  }

}
   

It gives the following output,
uid: svenkata
objectClass: top, person, organizationalPerson, inetorgperson
givenName: sudhakar
sn: venkata
cn: sudhakar venkata

uid: aradha
objectClass: top, person, organizationalPerson, inetorgperson
givenName: anu
sn: radha
cn: anu radha

uid: sram
objectClass: top, person, organizationalPerson, inetorgperson
givenName: sri
sn: ram
cn: sri ram



 
  


  
bl  br