List of usage examples for javax.naming.directory DirContext search
public NamingEnumeration<SearchResult> search(String name, String filter, SearchControls cons) throws NamingException;
From source file:Main.java
public static void main(String[] argv) throws Exception { SearchControls ctls = new SearchControls(); ctls.setSearchScope(SearchControls.SUBTREE_SCOPE); String filter = "(&(sn=YourName)(mail=*))"; 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("", filter, ctls); while (e.hasMore()) { SearchResult entry = (SearchResult) e.next(); System.out.println(entry.getName()); }// w w w . j ava2 s . c o m }
From source file:Main.java
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()); }/*w ww. jav a 2s. c o m*/ }
From source file:FilterExample.java
public static void main(String[] argc) throws Exception { Hashtable env = new Hashtable(11); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://MyHost/o=JNDIExample"); DirContext dctx = new InitialDirContext(env); String filter = "(&(cn=E*)(account>1005))"; NamingEnumeration result = dctx.search("ou=People", filter, null); while (result.hasMore()) { SearchResult sr = (SearchResult) result.next(); System.out.println("Result = " + sr.getName()); }/*from ww w . jav a 2 s . co m*/ }
From source file:Main.java
public static void main(String args[]) throws Exception { Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, INITCTX); env.put(Context.PROVIDER_URL, MY_HOST); DirContext ctx = new InitialDirContext(env); SearchControls constraints = new SearchControls(); constraints.setSearchScope(SearchControls.SUBTREE_SCOPE); NamingEnumeration results = ctx.search(MY_SEARCHBASE, MY_FILTER, constraints); while (results != null && results.hasMore()) { SearchResult sr = (SearchResult) results.next(); String dn = sr.getName() + ", " + MY_SEARCHBASE; System.out.println("Distinguished Name is " + dn); Attributes ar = ctx.getAttributes(dn, MY_ATTRS); if (ar == null) { System.out.println("Entry " + dn + " has none of the specified attributes\n"); return; }//from w w w. j a v a2 s . c o m for (int i = 0; i < MY_ATTRS.length; i++) { Attribute attr = ar.get(MY_ATTRS[i]); if (attr == null) { continue; } System.out.println(MY_ATTRS[i] + ":"); for (Enumeration vals = attr.getAll(); vals.hasMoreElements();) { System.out.println("\t" + vals.nextElement()); } } } }
From source file:SearchControlsExample.java
public static void main(String[] argc) throws Exception { Hashtable env = new Hashtable(11); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://MyHost/o=JNDIExample"); DirContext dctx = new InitialDirContext(env); String filter = "(&(cn=S*)(account>1005))"; String[] attrIDs = { "cn", "email" }; SearchControls sc = new SearchControls(); sc.setReturningAttributes(attrIDs);/*www. j a v a2 s .c o m*/ NamingEnumeration result = dctx.search("ou=People", filter, sc); while (result.hasMore()) { SearchResult sr = (SearchResult) result.next(); System.out.println("Result = " + sr.getName()); } }
From source file:SearchScopeExample.java
public static void main(String[] argc) { Hashtable env = new Hashtable(11); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://MyHost/o=JNDIExample"); try {// www. ja va2s . c om DirContext dctx = new InitialDirContext(env); String filter = "(&(cn=S*)(account>1000))"; String[] attrIDs = { "cn", "email" }; SearchControls sc = new SearchControls(); sc.setReturningAttributes(attrIDs); sc.setSearchScope(SearchControls.SUBTREE_SCOPE); NamingEnumeration result = dctx.search("ou=People", filter, sc); while (result.hasMore()) { SearchResult sr = (SearchResult) result.next(); System.out.println("Result = " + sr.getName()); } } catch (NamingException e) { System.out.println(e); } }
From source file:SearchCountLimit.java
public static void main(String[] args) { // Set up the environment for creating the initial context Hashtable<String, Object> env = new Hashtable<String, Object>(11); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial"); try {/*www . j a v a2 s. c om*/ // Create initial context DirContext ctx = new InitialDirContext(env); // Set search controls to limit count to 'expected' SearchControls ctls = new SearchControls(); ctls.setCountLimit(expected); // Search for objects with those matching attributes NamingEnumeration answer = ctx.search("ou=People", "(sn=M*)", ctls); // Print the answer printSearchEnumeration(answer); // Close the context when we're done ctx.close(); } catch (Exception e) { System.err.println(e); } }
From source file:LdapSearch.java
public static void main(String[] args) throws Exception { Hashtable env = new Hashtable(); String sp = "com.sun.jndi.ldap.LdapCtxFactory"; env.put(Context.INITIAL_CONTEXT_FACTORY, sp); String ldapUrl = "ldap://localhost:389/dc=yourName, dc=com"; env.put(Context.PROVIDER_URL, ldapUrl); DirContext dctx = new InitialDirContext(env); String base = "ou=People"; SearchControls sc = new SearchControls(); String[] attributeFilter = { "cn", "mail" }; sc.setReturningAttributes(attributeFilter); sc.setSearchScope(SearchControls.SUBTREE_SCOPE); String filter = "(&(sn=W*)(l=Criteria*))"; NamingEnumeration results = dctx.search(base, filter, sc); while (results.hasMore()) { SearchResult sr = (SearchResult) results.next(); Attributes attrs = sr.getAttributes(); Attribute attr = attrs.get("cn"); System.out.print(attr.get() + ": "); attr = attrs.get("mail"); System.out.println(attr.get()); }/*from w ww . j a v a2 s. c om*/ dctx.close(); }
From source file:SearchTimeLimit.java
public static void main(String[] args) { // Set up the environment for creating the initial context Hashtable<String, Object> env = new Hashtable<String, Object>(11); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial"); try {/*from www .j a va 2 s.co m*/ // Create initial context DirContext ctx = new InitialDirContext(env); // Set search controls to limit count to 'timeout' SearchControls ctls = new SearchControls(); ctls.setSearchScope(SearchControls.SUBTREE_SCOPE); ctls.setTimeLimit(timeout); // // Search for objects with those matching attributes NamingEnumeration answer = ctx.search("", "(objectclass=*)", ctls); // Print the answer printSearchEnumeration(answer); // Close the context when we're done ctx.close(); } catch (TimeLimitExceededException e) { System.out.println("time limit exceeded"); } catch (Exception e) { System.err.println(e); } }
From source file:SearchWithFilterRetAll.java
public static void main(String[] args) { // Set up the environment for creating the initial context Hashtable<String, Object> env = new Hashtable<String, Object>(11); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial"); try {//from w w w .j a v a 2 s. c o m // Create initial context DirContext ctx = new InitialDirContext(env); // Create default search controls SearchControls ctls = new SearchControls(); // Specify the search filter to match // Ask for objects with attribute sn == Geisel and which have // the "mail" attribute. String filter = "(&(sn=Geisel)(mail=*))"; // Search for objects using filter NamingEnumeration answer = ctx.search("ou=People", filter, ctls); // Print the answer // Search.printSearchEnumeration(answer); // Close the context when we're done ctx.close(); } catch (Exception e) { e.printStackTrace(); } }