List of usage examples for javax.naming.directory SearchControls SearchControls
public SearchControls()
From source file:org.infoscoop.account.ldap.LDAPAccountManager.java
public IAccount getUser(String uid) throws NamingException { SearchControls searchControls = new SearchControls(); searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE); NamingEnumeration searchResultEnum; Map filters = new HashMap(); String uidAttrName = "uid"; if (this.propAttrMap.containsKey("user_id")) { try {//from w w w . j a v a2 s . c o m uidAttrName = (String) this.propAttrMap.get("user_id"); } catch (Exception ex) { //ignore } } if (uid != null && !"".equals(uid)) filters.put(uidAttrName, uid); DirContext context = null; try { context = this.initContext(); searchResultEnum = context.search(userBase, buildFilterByUid(filters), searchControls); //roop of retrieval result while (searchResultEnum.hasMore()) { SearchResult searchResult = (SearchResult) searchResultEnum.next(); String dn = searchResult.getName() + "," + userBase; LDAPAccount user = createLDAPUser(dn, searchResult.getAttributes()); setGroup(context, user); return user; } return null; } finally { if (context != null) context.close(); } }
From source file:se.inera.axel.shs.broker.directory.internal.LdapDirectoryService.java
/** * Finds all entries matching filter, mapped with the mapper. * If organization is given, it is used as a search base. * For instance: list all addresses under a given organization. * * At most 'limit' entries are returned. * * @param organization//from w w w . j a va2s. c om * @param filter * @param mapper * @param limit * @param dirContextProcessor * @param <T> * @return * @throws DirectoryException */ private <T> List<T> findAll(Organization organization, AndFilter filter, ParameterizedContextMapper<T> mapper, long limit, DirContextProcessor dirContextProcessor) throws DirectoryException { List<T> entries = new ArrayList<T>(); String base = ""; try { SearchControls ctrl = new SearchControls(); ctrl.setSearchScope(SearchControls.SUBTREE_SCOPE); ctrl.setReturningObjFlag(true); ctrl.setCountLimit(limit); if (organization != null) { base = "o=" + organization.getOrgName(); } entries = ldapTemplate.search(base, filter.encode(), ctrl, mapper, dirContextProcessor); // Remove duplicates... HashSet<T> set = new HashSet<T>(entries); entries = new ArrayList<T>(set); } catch (NameNotFoundException e) { log.warn("not found in ldap directory: " + base + "," + filter.encode()); } catch (RuntimeException e) { log.error("error during looking-up", e); throw new DirectoryException("error during looking-up", e); } return entries; }
From source file:org.nuxeo.ecm.directory.ldap.LDAPDirectoryTestCase.java
protected void destroyRecursively(String dn, DirContext ctx, int limit) throws NamingException { if (limit == 0) { log.warn("Reach recursion limit, stopping deletion at" + dn); return;//from ww w.java2 s . co m } SearchControls scts = new SearchControls(); scts.setSearchScope(SearchControls.ONELEVEL_SCOPE); String providerUrl = (String) ctx.getEnvironment().get(Context.PROVIDER_URL); NamingEnumeration<SearchResult> children = ctx.search(dn, "(objectClass=*)", scts); try { while (children.hasMore()) { SearchResult child = children.next(); String subDn = child.getName(); if (!USE_EXTERNAL_TEST_LDAP_SERVER && subDn.endsWith(providerUrl)) { subDn = subDn.substring(0, subDn.length() - providerUrl.length() - 1); } else { subDn = subDn + ',' + dn; } destroyRecursively(subDn, ctx, limit); } } catch (SizeLimitExceededException e) { log.warn("SizeLimitExceededException: trying again on partial results " + dn); if (limit == -1) { limit = 100; } destroyRecursively(dn, ctx, limit - 1); } ctx.destroySubcontext(dn); }
From source file:org.sipfoundry.sipxconfig.bulk.ldap.LdapManagerImpl.java
/** * Connects to LDAP to retrieve the namingContexts attribute from root. Good * way to verify if LDAP is accessible. Command line anologue is: * * ldapsearch -x -b '' -s base '(objectclass=*)' namingContexts * * @param attrNames/*from w w w .j a v a2s . co m*/ * TODO * * @return namingContext value - can be used as the search base for user if * nothing more specific is provided * @throws NamingException */ private Map<String, String> retrieveDefaultSearchBase(LdapConnectionParams params, String[] attrNames) throws NamingException { SearchControls cons = new SearchControls(); cons.setReturningAttributes(attrNames); cons.setSearchScope(SearchControls.OBJECT_SCOPE); cons.setTimeLimit(30000); List<Map<String, String>> results = m_templateFactory.getLdapTemplate(params).search("", FILTER_ALL_CLASSES, cons, new AttributesToValues(attrNames), NULL_PROCESSOR); // only interested in the first result if (results.size() > 0) { return results.get(0); } return null; }
From source file:org.wso2.carbon.directory.server.manager.internal.LDAPServerStoreManager.java
public boolean isExistingServiceUid(String uid) throws DirectoryServerManagerException { DirContext dirContext;//from w w w . ja v a 2 s .c o m try { dirContext = this.connectionSource.getContext(); } catch (UserStoreException e) { log.error("Unable to retrieve directory context.", e); throw new DirectoryServerManagerException("Unable to retrieve directory context.", e); } //first search the existing user entry. String searchBase = realmConfiguration.getUserStoreProperty(LDAPConstants.USER_SEARCH_BASE); String filter = "(&(" + LDAPServerManagerConstants.LDAP_UID + "=" + uid + ")" + getServerPrincipleIncludeString() + ")"; SearchControls searchControls = new SearchControls(); searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE); searchControls.setReturningAttributes(new String[] { LDAPServerManagerConstants.LDAP_UID }); try { NamingEnumeration<SearchResult> namingEnumeration = dirContext.search(searchBase, filter, searchControls); return namingEnumeration.hasMore(); } catch (NamingException e) { log.error("Unable to check whether service exists in directory server. UID - " + uid, e); throw new DirectoryServerManagerException("Can not access the directory service", e); } finally { try { JNDIUtil.closeContext(dirContext); } catch (UserStoreException e) { log.error("Unable to close directory context.", e); } } }
From source file:es.udl.asic.user.OpenLdapDirectoryProvider.java
protected boolean userExists(String id) { env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_CREDENTIALS, "secret"); try {// ww w.ja v a 2s . c o m DirContext ctx = new InitialDirContext(env); /* * Setup subtree scope to tell LDAP to recursively descend directory structure during searches. */ SearchControls searchControls = new SearchControls(); searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE); /* * Setup the directory entry attributes we want to search for. In this case it is the user's ID. */ String filter = "(&(objectclass=person)(uid=" + escapeSearchFilterTerm(id) + "))"; /* Execute the search, starting at the directory level of Users */ NamingEnumeration hits = ctx.search(getBasePath(), filter, searchControls); /* All we need to know is if there were any hits at all. */ if (hits.hasMore()) { hits.close(); ctx.close(); return true; } else { hits.close(); ctx.close(); return false; } } catch (Exception e) { e.printStackTrace(); return false; } }
From source file:it.infn.ct.security.utilities.LDAPUtils.java
public static List<Organization> getOrgList(String country) { List<Organization> OrgList = new ArrayList<Organization>(); NamingEnumeration resultCountries = null; DirContext ctx = null;//from ww w . jav a2s . com try { ctx = getContext(); SearchControls controls = new SearchControls(); controls.setSearchScope(SearchControls.SUBTREE_SCOPE); ResourceBundle rb = ResourceBundle.getBundle("ldap"); String filter; if (country == null) { filter = "(objectclass=country)"; } else { filter = "(&(objectclass=country)(c=" + country + "))"; } resultCountries = ctx.search(rb.getString("organisationsRoot"), filter, controls); while (resultCountries.hasMore()) { SearchResult searchResult = (SearchResult) resultCountries.next(); Attributes attributes = searchResult.getAttributes(); String countryCode = (String) attributes.get("c").get(); String countryName = (String) attributes.get("co").get(); NamingEnumeration resultsOrgs = ctx.search( "c=" + countryCode + "," + rb.getString("organisationsRoot"), "(objectclass=organization)", controls); while (resultsOrgs.hasMore()) { SearchResult srOrg = (SearchResult) resultsOrgs.next(); Attributes orgAttrs = srOrg.getAttributes(); String description = ""; if ((orgAttrs.get("description")) != null) { description = (String) orgAttrs.get("description").get(); } OrgList.add(new Organization((String) orgAttrs.get("o").get(), countryName, countryCode, description, srOrg.getNameInNamespace())); } resultsOrgs.close(); } } catch (NameNotFoundException ex) { _log.error(ex); } catch (NamingException e) { throw new RuntimeException(e); } finally { if (resultCountries != null) { try { resultCountries.close(); } catch (Exception e) { // Never mind this. } } if (ctx != null) { try { ctx.close(); } catch (Exception e) { // Never mind this. } } } Collections.sort(OrgList, new Comparator<Organization>() { public int compare(Organization o1, Organization o2) { return o1.getKey().compareTo(o2.getKey()); } }); return OrgList; }
From source file:org.apache.cloudstack.ldap.LdapUserManager.java
private LdapUser getUserForDn(String userdn, LdapContext context) throws NamingException { final SearchControls controls = new SearchControls(); controls.setSearchScope(_ldapConfiguration.getScope()); controls.setReturningAttributes(_ldapConfiguration.getReturnAttributes()); NamingEnumeration<SearchResult> result = context.search(userdn, "(objectClass=" + _ldapConfiguration.getUserObject() + ")", controls); if (result.hasMoreElements()) { return createUser(result.nextElement()); } else {/*from w w w .j a va 2s .co m*/ throw new NamingException("No user found for dn " + userdn); } }
From source file:org.tolven.gatekeeper.bean.LdapBean.java
/** * Find a TolvenPerson/*from w w w . ja v a 2 s. c om*/ * * @param uid * @param realm * @return */ @Override public TolvenPerson findTolvenPerson(String uid, String realm) { LdapContext ctx = null; try { LdapRealmContext ldapRealmContext = getLdapRealmContext(realm); ctx = getLadpContext(ldapRealmContext.getAnonymousUser(), ldapRealmContext.getAnonymousUserPassword().toCharArray(), realm); SearchControls ctls = new SearchControls(); ctls.setSearchScope(SearchControls.SUBTREE_SCOPE); ctls.setCountLimit(1); String principalLdapName = ldapRealmContext.getPrincipalName(uid); String basePeopleName = ldapRealmContext.getBasePeopleName(); List<TolvenPerson> tolvenPersons = findTolvenPerson(ctx, basePeopleName, principalLdapName, realm, 1, 1000); if (tolvenPersons.isEmpty()) { return null; } else { return tolvenPersons.get(0); } } catch (GatekeeperSecurityException ex) { throw ex; } catch (Exception ex) { throw new RuntimeException("Could not find user " + uid + " in realm " + realm, ex); } finally { close(ctx, realm); } }
From source file:org.apache.archiva.redback.common.ldap.role.DefaultLdapRoleMapper.java
public boolean hasRole(DirContext context, String roleName) throws MappingException { String groupName = findGroupName(roleName); if (groupName == null) { if (this.useDefaultRoleName) { groupName = roleName;// ww w .ja v a2 s. c o m } else { log.warn("skip group creation as no mapping for roleName:'{}'", roleName); return false; } } NamingEnumeration<SearchResult> namingEnumeration = null; try { SearchControls searchControls = new SearchControls(); searchControls.setDerefLinkFlag(true); searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE); String filter = "objectClass=" + getLdapGroupClass(); namingEnumeration = context.search("cn=" + groupName + "," + getGroupsDn(), filter, searchControls); return namingEnumeration.hasMore(); } catch (NameNotFoundException e) { log.debug("group {} for role {} not found", groupName, roleName); return false; } catch (LdapException e) { throw new MappingException(e.getMessage(), e); } catch (NamingException e) { throw new MappingException(e.getMessage(), e); } finally { close(namingEnumeration); } }