Performing a Basic Directory Search : Directory « JNDI LDAP « Java






Performing a Basic Directory Search

 


import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchResult;

public class Main {
  public static void main(String[] argv) throws Exception {
    String[] attrIDs = { "sn", "number", "value", "mail" };

    Attributes matchAttrs = new BasicAttributes(true); 
    matchAttrs.put(new BasicAttribute("sn", "YourName"));
    matchAttrs.put(new BasicAttribute("mail"));

    Object obj = "yourObject";
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "yourURL");

    DirContext ctx = new InitialDirContext(env);

    NamingEnumeration e = ctx.search("ou=People", matchAttrs, attrIDs);

    while (e.hasMore()) {
      SearchResult entry = (SearchResult) e.next();
      System.out.println(entry.getName());
    }
  }
}

   
  








Related examples in the same category

1.Creating an Initial Context to a Directory
2.Reading an Object's Attributes from the Directory
3.Modifying an Object's Attributes in the Directory
4.Creating a Directory Entry
5.Adding a Binding with Attributes to the Directory
6.Searching the Directory by Using a Search Filter
7.Searching a Subtree in the Directory
8.Cancelling a Directory Search
9.Getting an Object's Schema from the Directory
10.Getting an Attribute's Schema from the Directory
11.Authenticating to the Directory
12.Registering for Namespace Changes in the Directory
13.Registering for Object Changes in the Directory