Java DirContext filter

Description

Java DirContext filter

import javax.naming.NamingEnumeration;
import javax.naming.directory.Attribute;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;

public class Main {
   public static void main(String[] args) {
      try {/*w w  w  .j  a v  a 2 s. c om*/
         DirContext ctx = new InitialDirContext();
         SearchControls ctls = new SearchControls();
         String filter = "(&(o=JU)(objectclass=organization))";
         NamingEnumeration result = ctx.search(args[0], filter, ctls);
         while (result.hasMore()) {
            SearchResult sr = (SearchResult) result.next();
            System.out.println("name: " + sr.getName());
            for (NamingEnumeration ne = sr.getAttributes().getAll(); ne.hasMore();) {
               Attribute attr = (Attribute) ne.next();
               System.out.println("attribute: " + attr.getID());
               for (NamingEnumeration e = attr.getAll(); e.hasMore();)
                  System.out.println("value: " + e.next());
            }
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}



PreviousNext

Related