List of usage examples for javax.naming.directory SearchControls SearchControls
public SearchControls()
From source file:org.apache.zeppelin.rest.GetUserList.java
/** * function to extract users from Zeppelin LdapRealm *///from ww w . j ava 2 s .c o m public List<String> getUserList(LdapRealm r, String searchText) { List<String> userList = new ArrayList<>(); if (LOG.isDebugEnabled()) { LOG.debug("SearchText: " + searchText); } String userAttribute = r.getUserSearchAttributeName(); String userSearchRealm = r.getUserSearchBase(); String userObjectClass = r.getUserObjectClass(); JndiLdapContextFactory CF = (JndiLdapContextFactory) r.getContextFactory(); try { LdapContext ctx = CF.getSystemLdapContext(); SearchControls constraints = new SearchControls(); constraints.setSearchScope(SearchControls.SUBTREE_SCOPE); String[] attrIDs = { userAttribute }; constraints.setReturningAttributes(attrIDs); NamingEnumeration result = ctx.search(userSearchRealm, "(&(objectclass=" + userObjectClass + ")(" + userAttribute + "=" + searchText + "))", constraints); while (result.hasMore()) { Attributes attrs = ((SearchResult) result.next()).getAttributes(); if (attrs.get(userAttribute) != null) { String currentUser; if (r.getUserLowerCase()) { LOG.debug("userLowerCase true"); currentUser = ((String) attrs.get(userAttribute).get()).toLowerCase(); } else { LOG.debug("userLowerCase false"); currentUser = (String) attrs.get(userAttribute).get(); } if (LOG.isDebugEnabled()) { LOG.debug("CurrentUser: " + currentUser); } userList.add(currentUser.trim()); } } } catch (Exception e) { LOG.error("Error retrieving User list from Ldap Realm", e); } return userList; }
From source file:nl.knaw.dans.common.ldap.repo.AbstractLdapUserRepo.java
/** * {@inheritDoc}//from w w w. j a v a 2s . c o m */ public Map<String, String> findByCommonNameStub(String stub, long maxCount) throws RepositoryException { Map<String, String> idNameMap = new LinkedHashMap<String, String>(); String text = censorHumanoidSearchPhrase(stub); String filter = "(&(objectClass=" + getObjectClassName() + ")(cn=" + text + "*))"; SearchControls ctls = new SearchControls(); ctls.setSearchScope(SearchControls.ONELEVEL_SCOPE); ctls.setCountLimit(maxCount); ctls.setReturningAttributes(new String[] { "cn", "uid" }); try { NamingEnumeration<SearchResult> resultEnum = getClient().search(getContext(), filter, ctls); while (resultEnum.hasMoreElements()) { SearchResult result = resultEnum.next(); Attributes attrs = result.getAttributes(); idNameMap.put((String) attrs.get("uid").get(), (String) attrs.get("cn").get()); } } catch (NamingException e) { throw new RepositoryException(e); } return idNameMap; }
From source file:org.springframework.ldap.samples.article.dao.TraditionalPersonDaoImpl.java
public List findAll() { DirContext ctx = createAnonymousContext(); LinkedList list = new LinkedList(); NamingEnumeration results = null; try {/*w w w . ja v a2s . com*/ SearchControls controls = new SearchControls(); controls.setSearchScope(SearchControls.SUBTREE_SCOPE); results = ctx.search("", "(objectclass=person)", controls); while (results.hasMore()) { SearchResult searchResult = (SearchResult) results.next(); String dn = searchResult.getName(); Attributes attributes = searchResult.getAttributes(); list.add(mapToPerson(dn, attributes)); } } catch (NamingException e) { throw new RuntimeException(e); } finally { if (results != null) { try { results.close(); } catch (Exception e) { // Never mind this. } } if (ctx != null) { try { ctx.close(); } catch (Exception e) { // Never mind this. } } } return list; }
From source file:org.apache.lens.server.user.LDAPBackedDatabaseUserConfigLoader.java
/** * Find account by account name./* www . j ava 2 s . c om*/ * * @param accountName the account name * @return the search result * @throws NamingException the naming exception */ protected SearchResult findAccountByAccountName(String accountName) throws NamingException { String searchFilter = String.format(searchFilterPattern, accountName); SearchControls searchControls = new SearchControls(); searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE); InitialLdapContext ctx = new InitialLdapContext(env, null); try { NamingEnumeration<SearchResult> results = ctx.search(searchBase, searchFilter, searchControls); if (!results.hasMoreElements()) { throw new UserConfigLoaderException("LDAP Search returned no accounts"); } SearchResult searchResult = results.nextElement(); if (results.hasMoreElements()) { throw new UserConfigLoaderException("More than one account found in ldap search"); } return searchResult; } finally { ctx.close(); } }
From source file:net.identio.server.service.authentication.ldap.LdapConnectionFactory.java
@Override public boolean validateObject(PooledObject<InitialLdapContext> p) { LOG.debug("Validating connection to LDAP directory {}", ldapAuthMethod.getName()); SearchControls controls = new SearchControls(); controls.setSearchScope(SearchControls.SUBTREE_SCOPE); controls.setCountLimit(1);//w ww. j a v a 2 s. c om controls.setTimeLimit(500); try { p.getObject().search("", ldapAuthMethod.getPoolConfig().getTestRequestFilter(), controls); } catch (NamingException e) { LOG.error("Validation of connection to LDAP directory {} failed", ldapAuthMethod.getName()); return false; } return true; }
From source file:org.springframework.ldap.demo.dao.PersonDaoImpl.java
public List<Person> findAll() { DirContext ctx = createAnonymousContext(); LinkedList<Person> list = new LinkedList<Person>(); NamingEnumeration<?> results = null; try {/*from w w w .ja v a 2s . c o m*/ SearchControls controls = new SearchControls(); controls.setSearchScope(SearchControls.SUBTREE_SCOPE); results = ctx.search("", "(objectclass=person)", controls); while (results.hasMore()) { SearchResult searchResult = (SearchResult) results.next(); String dn = searchResult.getName(); Attributes attributes = searchResult.getAttributes(); list.add(mapToPerson(dn, attributes)); } } catch (NamingException e) { throw new RuntimeException(e); } finally { if (results != null) { try { results.close(); } catch (Exception e) { // Never mind this. } } if (ctx != null) { try { ctx.close(); } catch (Exception e) { // Never mind this. } } } return list; }
From source file:org.apache.archiva.redback.common.ldap.role.DefaultLdapRoleMapper.java
public List<String> getAllGroups(DirContext context) throws MappingException { NamingEnumeration<SearchResult> namingEnumeration = null; try {/*from w ww . j a v a 2 s. co m*/ SearchControls searchControls = new SearchControls(); searchControls.setDerefLinkFlag(true); searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE); String filter = "objectClass=" + getLdapGroupClass(); if (!StringUtils.isEmpty(this.groupFilter)) { filter = "(&(" + filter + ")(" + this.groupFilter + "))"; } namingEnumeration = context.search(getGroupsDn(), filter, searchControls); List<String> allGroups = new ArrayList<String>(); while (namingEnumeration.hasMore()) { SearchResult searchResult = namingEnumeration.next(); String groupName = searchResult.getName(); // cn=blabla we only want bla bla groupName = StringUtils.substringAfter(groupName, "="); log.debug("found groupName: '{}", groupName); allGroups.add(groupName); } return allGroups; } catch (LdapException e) { throw new MappingException(e.getMessage(), e); } catch (NamingException e) { throw new MappingException(e.getMessage(), e); } finally { close(namingEnumeration); } }
From source file:egovframework.com.ext.ldapumt.service.impl.DeptManageLdapDAO.java
/** * ?./* w ww . ja v a 2 s. c om*/ * @param vo vo */ public boolean hasChildren(String dn) throws NamingException { ContextSource contextSource = ldapTemplate.getContextSource(); DirContext ctx = contextSource.getReadOnlyContext(); String filter = "objectclass=*"; SearchControls control = new SearchControls(); control.setSearchScope(SearchControls.ONELEVEL_SCOPE); NamingEnumeration<SearchResult> n = ctx.search(dn, filter, control); if (n != null && n.hasMore()) { return true; } return false; }
From source file:org.jasig.portlet.contacts.adapters.impl.ldap.LdapSearchAdapter.java
/** * Construct a new search controls object for our search *///from www. j a v a 2 s . co m protected SearchControls getSearchControls() { SearchControls searchControls = new SearchControls(); searchControls.setTimeLimit(timeLimit); searchControls.setCountLimit(countLimit); searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE); return searchControls; }
From source file:jp.ikedam.jenkins.plugins.ldap_sasl.SearchGroupResolver.java
/** * Resolves groups by querying the LDAP directory. * // w w w .j a v a2 s. c om * Never return null in any case. Returns empty list instead. * * @param ctx * @param dn * @param username * @return List of authorities (not null) * @see jp.ikedam.jenkins.plugins.ldap_sasl.GroupResolver#resolveGroup(javax.naming.ldap.LdapContext, java.lang.String, java.lang.String) */ @Override public List<GrantedAuthority> resolveGroup(LdapContext ctx, String dn, String username) { List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(); Logger logger = getLogger(); if (dn == null) { logger.warning("Group cannot be resolved: DN of the user is not resolved!"); return authorities; } try { SearchControls searchControls = new SearchControls(); searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE); logger.fine(String.format("Searching groups base=%s, dn=%s", getSearchBase(), dn)); NamingEnumeration<SearchResult> entries = ctx.search((getSearchBase() != null) ? getSearchBase() : "", getGroupSearchQuery(dn), searchControls); while (entries.hasMore()) { SearchResult entry = entries.next(); String groupName = entry.getAttributes().get("cn").get().toString(); if (getPrefix() != null) { groupName = getPrefix() + groupName; } authorities.add(new GrantedAuthorityImpl(groupName)); logger.fine(String.format("group: %s", groupName)); } entries.close(); } catch (NamingException e) { logger.log(Level.WARNING, "Failed to search groups", e); } return authorities; }