List of usage examples for javax.naming.directory SearchResult getAttributes
public Attributes getAttributes()
From source file:org.apache.archiva.redback.users.ldap.ctl.DefaultLdapController.java
/** * @see org.apache.archiva.redback.users.ldap.ctl.LdapController#getUsers(javax.naming.directory.DirContext) *///from ww w . j av a 2 s. c o m public Collection<User> getUsers(DirContext context) throws LdapControllerException, MappingException { NamingEnumeration<SearchResult> results = null; try { results = searchUsers(context, null, null); Set<User> users = new LinkedHashSet<User>(); while (results.hasMoreElements()) { SearchResult result = results.nextElement(); users.add(mapper.getUser(result.getAttributes())); } return users; } catch (NamingException e) { String message = "Failed to retrieve ldap information for users."; throw new LdapControllerException(message, e); } finally { if (results != null) { try { results.close(); } catch (NamingException e) { log.warn("failed to close search results", e); } } } }
From source file:org.apache.archiva.redback.users.ldap.ctl.DefaultLdapController.java
/** * @see org.apache.archiva.redback.users.ldap.ctl.LdapController#getUsersByQuery(org.apache.archiva.redback.users.ldap.LdapUserQuery, javax.naming.directory.DirContext) */// w w w . java 2 s .c om public List<User> getUsersByQuery(LdapUserQuery query, DirContext context) throws LdapControllerException, MappingException { NamingEnumeration<SearchResult> results = null; try { results = searchUsers(context, null, query); List<User> users = new LinkedList<User>(); while (results.hasMoreElements()) { SearchResult result = results.nextElement(); users.add(mapper.getUser(result.getAttributes())); } return users; } catch (NamingException e) { String message = "Failed to retrieve ldap information for users."; throw new LdapControllerException(message, e); } finally { if (results != null) { try { results.close(); } catch (NamingException e) { log.warn("failed to close search results", e); } } } }
From source file:org.apache.geronimo.security.realm.providers.GenericHttpHeaderLdapLoginModule.java
protected ArrayList<String> getRoles(DirContext context, String dn, String username, ArrayList<String> list) throws NamingException { if (list == null) { list = new ArrayList<String>(); }//from w w w .j av a 2 s . c o m if (roleName == null || "".equals(roleName)) { return list; } String filter = roleSearchMatchingFormat.format(new String[] { doRFC2254Encoding(dn), username }); SearchControls constraints = new SearchControls(); if (roleSearchSubtreeBool) { constraints.setSearchScope(SearchControls.SUBTREE_SCOPE); } else { constraints.setSearchScope(SearchControls.ONELEVEL_SCOPE); } NamingEnumeration results = context.search(roleBase, filter, constraints); while (results.hasMore()) { SearchResult result = (SearchResult) results.next(); Attributes attrs = result.getAttributes(); if (attrs == null) { continue; } list = addAttributeValues(roleName, attrs, list); } return list; }
From source file:py.una.pol.karaku.util.LDAPUtil.java
/** * Recupera los usuarios de LDAP//from ww w .ja v a2s .c om * * @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:com.openkm.principal.LdapPrincipalAdapter.java
@SuppressWarnings("unchecked") private List<String> ldapSearch(List<String> searchBases, String searchFilter, String attribute) { log.debug("ldapSearch({}, {}, {})", new Object[] { searchBases, searchFilter, attribute }); List<String> al = new ArrayList<String>(); DirContext ctx = null;/*from w ww . j a v a2 s . c o m*/ Hashtable<String, String> env = getEnvironment(); try { ctx = new InitialDirContext(env); SearchControls searchCtls = new SearchControls(); searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE); for (String searchBase : searchBases) { NamingEnumeration<SearchResult> results = ctx.search(searchBase, searchFilter, searchCtls); while (results.hasMore()) { SearchResult searchResult = (SearchResult) results.next(); Attributes attributes = searchResult.getAttributes(); if (attribute.equals("")) { StringBuilder sb = new StringBuilder(); for (NamingEnumeration<?> ne = attributes.getAll(); ne.hasMore();) { Attribute attr = (Attribute) ne.nextElement(); sb.append(attr.toString()); sb.append("\n"); } al.add(sb.toString()); } else { Attribute attrib = attributes.get(attribute); if (attrib != null) { // Handle multi-value attributes for (NamingEnumeration<?> ne = attrib.getAll(); ne.hasMore();) { String value = (String) ne.nextElement(); // If FQDN get only main part if (value.startsWith("CN=") || value.startsWith("cn=")) { String cn = value.substring(3, value.indexOf(',')); log.debug("FQDN: {}, CN: {}", value, cn); al.add(cn); } else { al.add(value); } } } } } } } catch (ReferralException e) { log.error("ReferralException: {}", e.getMessage()); log.error("ReferralInfo: {}", e.getReferralInfo()); log.error("ResolvedObj: {}", e.getResolvedObj()); try { log.error("ReferralContext: {}", e.getReferralContext()); } catch (NamingException e1) { log.error("NamingException logging context: {}", e1.getMessage()); } } catch (NamingException e) { log.error("NamingException: {} (Base: {} - Filter: {} - Attribute: {})", new Object[] { e.getMessage(), searchBases, searchFilter, attribute }); } finally { try { if (ctx != null) { ctx.close(); } } catch (NamingException e) { log.error("NamingException closing context: {}", e.getMessage()); } } log.debug("ldapSearch: {}", al); return al; }
From source file:org.apache.zeppelin.realm.ActiveDirectoryGroupRealm.java
public List<String> searchForUserName(String containString, LdapContext ldapContext) throws NamingException { List<String> userNameList = new ArrayList<>(); SearchControls searchCtls = new SearchControls(); searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE); String searchFilter = "(&(objectClass=*)(userPrincipalName=*" + containString + "*))"; Object[] searchArguments = new Object[] { containString }; NamingEnumeration answer = ldapContext.search(searchBase, searchFilter, searchArguments, searchCtls); while (answer.hasMoreElements()) { SearchResult sr = (SearchResult) answer.next(); if (log.isDebugEnabled()) { log.debug("Retrieving userprincipalname names for user [" + sr.getName() + "]"); }//from www. j a v a2s . c o m Attributes attrs = sr.getAttributes(); if (attrs != null) { NamingEnumeration ae = attrs.getAll(); while (ae.hasMore()) { Attribute attr = (Attribute) ae.next(); if (attr.getID().toLowerCase().equals("cn")) { userNameList.addAll(LdapUtils.getAllAttributeValues(attr)); } } } } return userNameList; }
From source file:org.mule.module.ldap.api.jndi.PagedLDAPResultSet.java
/** * @return/* w w w .ja va2s .c om*/ * @throws LDAPException * @see org.mule.module.ldap.api.LDAPResultSet#next() */ @Override public LDAPEntry next() throws LDAPException { if (hasNext()) // Force navigating to next page { SearchResult searchResult = (SearchResult) this.entries.nextElement(); String entryDn; if (searchResult != null) { entryDn = searchResult.getName(); if (searchResult.isRelative()) { entryDn += "," + baseDn; } return LDAPJNDIUtils.buildEntry(entryDn, searchResult.getAttributes()); } } throw new NoSuchElementException(); }
From source file:org.apache.directory.studio.ldapbrowser.core.jobs.ExportDsmlRunnable.java
/** * Converts the given {@link SearchResult} to an {@link AddRequestDsml}. * * @param searchResult//from w ww . java 2s . co m * the {@link SearchResult} * @return * the associated {@link AddRequestDsml} * @throws LdapException */ private AddRequestDsml convertToAddRequestDsml(SearchResult searchResult) throws LdapException { AddRequestDsml ar = new AddRequestDsml(codec); Entry entry = AttributeUtils.toEntry(searchResult.getAttributes(), new Dn(searchResult.getNameInNamespace())); ar.setEntry(entry); return ar; }
From source file:org.apache.zeppelin.realm.ActiveDirectoryGroupRealm.java
private Set<String> getRoleNamesForUser(String username, LdapContext ldapContext) throws NamingException { Set<String> roleNames = new LinkedHashSet<>(); SearchControls searchCtls = new SearchControls(); searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE); String userPrincipalName = username; if (this.principalSuffix != null && userPrincipalName.indexOf('@') < 0) { userPrincipalName += principalSuffix; }//from ww w . ja v a2 s .c o m String searchFilter = "(&(objectClass=*)(userPrincipalName=" + userPrincipalName + "))"; Object[] searchArguments = new Object[] { userPrincipalName }; NamingEnumeration answer = ldapContext.search(searchBase, searchFilter, searchArguments, searchCtls); while (answer.hasMoreElements()) { SearchResult sr = (SearchResult) answer.next(); if (log.isDebugEnabled()) { log.debug("Retrieving group names for user [" + sr.getName() + "]"); } Attributes attrs = sr.getAttributes(); if (attrs != null) { NamingEnumeration ae = attrs.getAll(); while (ae.hasMore()) { Attribute attr = (Attribute) ae.next(); if (attr.getID().equals("memberOf")) { Collection<String> groupNames = LdapUtils.getAllAttributeValues(attr); if (log.isDebugEnabled()) { log.debug("Groups found for user [" + username + "]: " + groupNames); } Collection<String> rolesForGroups = getRoleNamesForGroups(groupNames); roleNames.addAll(rolesForGroups); } } } } return roleNames; }
From source file:org.apache.zeppelin.server.ActiveDirectoryGroupRealm.java
private Set<String> getRoleNamesForUser(String username, LdapContext ldapContext) throws NamingException { Set<String> roleNames = new LinkedHashSet<>(); SearchControls searchCtls = new SearchControls(); searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE); String userPrincipalName = username; if (principalSuffix != null) { userPrincipalName += principalSuffix; }//from www . j ava 2 s . c o m String searchFilter = "(&(objectClass=*)(userPrincipalName=" + userPrincipalName + "))"; Object[] searchArguments = new Object[] { userPrincipalName }; NamingEnumeration answer = ldapContext.search(searchBase, searchFilter, searchArguments, searchCtls); while (answer.hasMoreElements()) { SearchResult sr = (SearchResult) answer.next(); if (log.isDebugEnabled()) { log.debug("Retrieving group names for user [" + sr.getName() + "]"); } Attributes attrs = sr.getAttributes(); if (attrs != null) { NamingEnumeration ae = attrs.getAll(); while (ae.hasMore()) { Attribute attr = (Attribute) ae.next(); if (attr.getID().equals("memberOf")) { Collection<String> groupNames = LdapUtils.getAllAttributeValues(attr); if (log.isDebugEnabled()) { log.debug("Groups found for user [" + username + "]: " + groupNames); } Collection<String> rolesForGroups = getRoleNamesForGroups(groupNames); roleNames.addAll(rolesForGroups); } } } } return roleNames; }