List of usage examples for javax.naming.directory DirContext search
public NamingEnumeration<SearchResult> search(String name, Attributes matchingAttributes) throws NamingException;
From source file:Main.java
public static void main(String[] argv) throws Exception { Attributes matchAttrs = new BasicAttributes(true); matchAttrs.put(new BasicAttribute("sn", "YourName")); matchAttrs.put(new BasicAttribute("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("ou=People", matchAttrs); if (e.hasMore()) { SearchResult entry = (SearchResult) e.next(); // Abandon rest of results e.close();/*w ww .j a va 2s. c om*/ } }
From source file:Main.java
public static void main(String[] argv) throws Exception { 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); DirContext tedClasses = ctx.getSchemaClassDefinition("cn=YourName, ou=People"); NamingEnumeration e = tedClasses.search("", null); while (e.hasMore()) { DirContext entry = (DirContext) e.next(); System.out.println(entry); }/*from www. jav a 2s . c om*/ }
From source file:FullName.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 {/*w ww . j a va 2 s.c o m*/ // Create initial context DirContext ctx = new InitialDirContext(env); NamingEnumeration answer = ctx.search("ou=People", null); // Print the answer printSearchEnumeration(answer); // Close the context when we're done ctx.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:RetrievingLdapName.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 a2 s. com*/ // Create initial context DirContext ctx = new InitialDirContext(env); NamingEnumeration answer = ctx.search("ou=People", null); // Print the answer while (answer.hasMore()) { SearchResult sr = (SearchResult) answer.next(); String name = sr.getNameInNamespace(); System.out.println(name); LdapName dn = new LdapName(name); // do something with the dn } // Close the context when we're done ctx.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:AttributeExample.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); Attributes attrs = new BasicAttributes(true); attrs.put(new BasicAttribute("email")); attrs.put(new BasicAttribute("website", "www.pri.com")); NamingEnumeration result = dctx.search("ou=People", attrs); while (result.hasMore()) { SearchResult sr = (SearchResult) result.next(); System.out.println("Result = " + sr.getName()); Attributes srchAttrs = sr.getAttributes(); NamingEnumeration attributes = srchAttrs.getAll(); while (attributes.hasMore()) { Attribute attr = (Attribute) attributes.next(); System.out.println("Attribute: " + attr.getID()); NamingEnumeration values = attr.getAll(); while (values.hasMore()) { Object value = values.next(); System.out.println("Value = " + value); }/* w ww . ja va2 s . c om*/ } } }
From source file:py.una.pol.karaku.util.LDAPUtil.java
/** * Recupera los usuarios de LDAP/*from w w w . j av a 2 s. com*/ * * @return Una lista con los usuarios de LDAP */ public List<User> getUsers() { List<User> users = new ArrayList<User>(); try { DirContext ctx = createInitialDirContext(); Attributes matchAttrs = new BasicAttributes(true); matchAttrs.put(new BasicAttribute("uid")); NamingEnumeration<SearchResult> answer = ctx.search("ou=users", matchAttrs); while (answer.hasMore()) { SearchResult sr = answer.next(); String uid = sr.getName().substring(4); // No se retornan los usuarios especiales if (!uid.startsWith(LDAP_SPECIAL_USER_PREFIX) && !ListHelper.contains(EXCLUDED_USERS, uid)) { User user = new User(); user.setUid(uid); Attributes atributos = sr.getAttributes(); String cn = atributos.get("cn").toString().substring(4); user.setCn(cn); users.add(user); } } } catch (NamingException e) { throw new KarakuRuntimeException(e.getMessage(), e); } return users; }
From source file:py.una.pol.karaku.security.KarakuUserService.java
private List<KarakuPermission> loadAuthoritiesByDn(String uid) { List<KarakuPermission> listaRoles = new ArrayList<KarakuPermission>(); try {//w w w . j av a2 s . c o m DirContext ctx = getInitialDirContext(propertiesUtil.get(LDAP_ADMIN_KEY), propertiesUtil.get(LDAP_ADMIN_PASS_KEY)); Attributes matchAttrs = new BasicAttributes(true); matchAttrs.put(new BasicAttribute("member", getRealUsername(uid))); NamingEnumeration<SearchResult> answer = ctx.search("ou=permissions", matchAttrs); while (answer.hasMore()) { SearchResult searchResult = answer.next(); Attributes attributes = searchResult.getAttributes(); Attribute attr = attributes.get("cn"); String rol = (String) attr.get(); KarakuPermission grantedAuthority = new KarakuPermission(rol); listaRoles.add(grantedAuthority); } return listaRoles; } catch (NamingException e) { LOG.warn("Can't create Ldap Context", e); return Collections.emptyList(); } }
From source file:de.sub.goobi.helper.ldap.Ldap.java
/** * check if User already exists on system. * * @param inLogin//from w w w. j a va 2 s.c om * String * @return path as string */ public boolean isUserAlreadyExists(String inLogin) { Hashtable<String, String> env = getLdapConnectionSettings(); env.put(Context.SECURITY_PRINCIPAL, ConfigCore.getParameter("ldap_adminLogin")); env.put(Context.SECURITY_CREDENTIALS, ConfigCore.getParameter("ldap_adminPassword")); DirContext ctx; boolean rueckgabe = false; try { ctx = new InitialDirContext(env); Attributes matchAttrs = new BasicAttributes(true); NamingEnumeration<SearchResult> answer = ctx.search("ou=users,dc=gdz,dc=sub,dc=uni-goettingen,dc=de", matchAttrs); rueckgabe = answer.hasMoreElements(); while (answer.hasMore()) { SearchResult sr = answer.next(); if (logger.isDebugEnabled()) { logger.debug(">>>" + sr.getName()); } Attributes attrs = sr.getAttributes(); String givenName = " "; String surName = " "; String mail = " "; String cn = " "; String hd = " "; try { givenName = attrs.get("givenName").toString(); } catch (Exception err) { givenName = " "; } try { surName = attrs.get("sn").toString(); } catch (Exception e2) { surName = " "; } try { mail = attrs.get("mail").toString(); } catch (Exception e3) { mail = " "; } try { cn = attrs.get("cn").toString(); } catch (Exception e4) { cn = " "; } try { hd = attrs.get("homeDirectory").toString(); } catch (Exception e4) { hd = " "; } logger.debug(givenName); logger.debug(surName); logger.debug(mail); logger.debug(cn); logger.debug(hd); } ctx.close(); } catch (NamingException e) { logger.error(e); } return rueckgabe; }
From source file:org.apache.archiva.redback.users.ldap.LdapUserManagerTest.java
private void assertExist(DirContext context, String dn, String attribute, String value) throws NamingException { SearchControls ctls = new SearchControls(); ctls.setDerefLinkFlag(true);//w w w . java2 s . c om ctls.setSearchScope(SearchControls.ONELEVEL_SCOPE); ctls.setReturningAttributes(new String[] { "*" }); BasicAttributes matchingAttributes = new BasicAttributes(); matchingAttributes.put(attribute, value); BasicAttribute objectClass = new BasicAttribute("objectClass"); objectClass.add("inetOrgPerson"); matchingAttributes.put(objectClass); NamingEnumeration<SearchResult> results = context.search(suffix, matchingAttributes); // NamingEnumeration<SearchResult> results = context.search( suffix, "(" + attribute + "=" + value + ")", ctls // ); assertTrue(results.hasMoreElements()); SearchResult result = results.nextElement(); Attributes attrs = result.getAttributes(); Attribute testAttr = attrs.get(attribute); assertEquals(value, testAttr.get()); }
From source file:org.apache.archiva.redback.common.ldap.role.TestLdapRoleMapper.java
private void assertExist(DirContext context, String dn, String attribute, String value) throws NamingException { SearchControls ctls = new SearchControls(); ctls.setDerefLinkFlag(true);// www.ja v a 2s . c o m ctls.setSearchScope(SearchControls.ONELEVEL_SCOPE); ctls.setReturningAttributes(new String[] { "*" }); BasicAttributes matchingAttributes = new BasicAttributes(); matchingAttributes.put(attribute, value); BasicAttribute objectClass = new BasicAttribute("objectClass"); objectClass.add("inetOrgPerson"); matchingAttributes.put(objectClass); NamingEnumeration<SearchResult> results = context.search(suffix, matchingAttributes); assertTrue(results.hasMoreElements()); SearchResult result = results.nextElement(); Attributes attrs = result.getAttributes(); Attribute testAttr = attrs.get(attribute); assertEquals(value, testAttr.get()); }