/** * Appends a collection of sub-string attribute values to a list. * <br/>The sub-attributes are determined by attribute.getAll(). * <br/>Only sub-attributes of type String will be appended. * @param attribute the attribute containing values to append (from) * @param values the list of values to populate (to) * @throws NamingException if an exception occurs */ protected void appendSubStringValues(Attribute attribute, StringSet values) throws NamingException { NamingEnumeration<?> enAttr = null; try { if (attribute != null) { enAttr = attribute.getAll(); while (enAttr.hasMore()) { Object oAttr = enAttr.next(); if (oAttr instanceof String) { values.add((String)oAttr); } } } } catch (PartialResultException pre) { LogUtil.getLogger().finer(pre.toString()); } catch (LimitExceededException lee) { LogUtil.getLogger().finer(lee.toString()); } finally { closeEnumeration(enAttr); } }
/** * Reads the attribute values associated with an attribute name. * @param dirContext the directory context * @param attrubuteName attribute name. * @param objectDN the distinguished name of the object * @return the list attribute values (strings only are returned) * @throws NamingException if an exception occurs */ protected StringSet readAttribute(DirContext dirContext, String objectDN, String attrubuteName) throws NamingException { StringSet values = new StringSet(); try{ if ((objectDN.length() > 0) && (attrubuteName.length() > 0)) { String[] aReturn = new String[1]; aReturn[0] = attrubuteName; try{ Attributes attributes = dirContext.getAttributes(objectDN,aReturn); if (attributes != null) { appendSubStringValues(attributes.get(attrubuteName),values); } }catch(NameNotFoundException nnfe){ LogUtil.getLogger().finer(nnfe.toString()); } } } catch (PartialResultException pre) { LogUtil.getLogger().finer(pre.toString()); } catch (LimitExceededException lee) { LogUtil.getLogger().finer(lee.toString()); } return values; }
/** * Appends attribute values to a map (keyed on attribute id). * @param attributes the attributes to append (from) * @param values the map of values to populate (to) * @param stringsOnly if true, only attributes values of type * String will be appended * @throws NamingException if an exception occurs */ protected void appendAttributeValues(Attributes attributes, Map<String,Object> values, boolean stringsOnly) throws NamingException { NamingEnumeration<?> enAttr = null; try { if (attributes != null) { enAttr = attributes.getAll(); while (enAttr.hasMore()) { Object oAttr = enAttr.next(); if (oAttr instanceof Attribute) { Attribute attr = (Attribute)oAttr; String sId = attr.getID(); Object oVal = attr.get(); if (!stringsOnly || (oVal instanceof String)) { values.put(sId,oVal); } else if (stringsOnly && (oVal == null)) { //values.put(sId,""); } //System.err.println(sId+"="+oVal+" cl="+oVal.getClass().getName()); } } enAttr.close(); } }catch (PartialResultException pre) { LogUtil.getLogger().finer(pre.toString()); } catch (LimitExceededException lee) { LogUtil.getLogger().finer(lee.toString()); } finally { closeEnumeration(enAttr); } }
/** * Returns a list of distinguished names resulting from a search. * <br/>The search is executed with SearchControls.SUBTREE_SCOPE. * @param dirContext the directory context * @param baseDN the baseBN for the search * @param filter the filter for the search * @return a collection of distinguished names * @throws NamingException if an exception occurs */ protected StringSet searchDNs(DirContext dirContext, String baseDN, String filter) throws NamingException { StringSet names = new StringSet(false,false,true); NamingEnumeration<SearchResult> enSearch = null; try { baseDN = Val.chkStr(baseDN); filter = Val.chkStr(filter); if (filter.length() > 0) { SearchControls controls = new SearchControls(); controls.setSearchScope(SearchControls.SUBTREE_SCOPE); enSearch = dirContext.search(baseDN,filter,controls); try { while (enSearch.hasMore()) { SearchResult result = (SearchResult)enSearch.next(); names.add(buildFullDN(result.getName(),baseDN)); } } catch (PartialResultException pre) { LogUtil.getLogger().finer(pre.toString()); } catch (LimitExceededException lee) { LogUtil.getLogger().finer(lee.toString()); } } } finally { closeEnumeration(enSearch); } return names; }
private DirContext getReferralContext(ReferralException e) throws LimitExceededException, NamingException { int limit = 0; if (env.get("java.naming.ldap.referral.limit") != null) { limit = Integer.valueOf( (String) env.get("java.naming.ldap.referral.limit")) .intValue(); } if (limit == -1) { throw new LimitExceededException(Messages.getString("ldap.25")); //$NON-NLS-1$ } if (limit == 1) { limit = -1; } else if (limit != 0) { limit -= 1; } Hashtable<Object, Object> newEnv = (Hashtable<Object, Object>) env .clone(); newEnv.put("java.naming.ldap.referral.limit", String.valueOf(limit)); DirContext referralContext = null; while (true) { try { referralContext = (DirContext) e.getReferralContext(newEnv); break; } catch (NamingException ex) { if (e.skipReferral()) { continue; } throw ex; } } return referralContext; }
/** * Builds list of ldap users matching filter. * @param dirContext the directory context * @param filter the user search filter for ldap * @return the list of users matching filter * @throws NamingException if an LDAP naming exception occurs */ protected Users readUsers(DirContext dirContext,String filter, String attributeName) throws NamingException{ Users users = new Users(); NamingEnumeration<SearchResult> enSearch = null; try{ LdapUserProperties userProps = getConfiguration().getUserProperties(); String sNameAttribute = userProps.getUserDisplayNameAttribute(); String sBaseDN = userProps.getUserSearchDIT(); String sFilter = userProps.returnUserLoginSearchFilter(filter); if(attributeName != null){ sFilter = userProps.returnUserNewRequestSearchFilter(filter, attributeName); } SearchControls controls = new SearchControls(); controls.setSearchScope(SearchControls.SUBTREE_SCOPE); if (sNameAttribute.length() > 0) { String[] aReturn = new String[1]; aReturn[0] = sNameAttribute; controls.setReturningAttributes(aReturn); } enSearch = dirContext.search(sBaseDN,sFilter,controls); try { while (enSearch.hasMore()) { SearchResult result = (SearchResult)enSearch.next(); String sDN = buildFullDN(result.getName(),sBaseDN); if (sDN.length() > 0) { String sName = ""; if (sNameAttribute.length() > 0) { Attribute attrName = result.getAttributes().get(sNameAttribute); if ((attrName != null) && (attrName.size() > 0)) { sName = Val.chkStr(attrName.get(0).toString()); } } User user = new User(); user.setDistinguishedName(sDN); user.setKey(user.getDistinguishedName()); user.setName(sName); users.add(user); } } } catch (PartialResultException pre) { LogUtil.getLogger().finer(pre.toString()); } catch (LimitExceededException lee) { LogUtil.getLogger().finer(lee.toString()); } }finally { closeEnumeration(enSearch); } return users; }
/** * Builds list of ldap groups matching filter. * @param dirContext the directory context * @param filter the group search filter for ldap * @return the list of groups matching filter * @throws NamingException if an LDAP naming exception occurs */ protected Groups readGroups(DirContext dirContext,String filter) throws NamingException{ Groups groups = new Groups(); NamingEnumeration<SearchResult> enSearch = null; try{ LdapGroupProperties groupProps = getConfiguration().getGroupProperties(); String sNameAttribute = groupProps.getGroupDisplayNameAttribute(); String sBaseDN = groupProps.getGroupSearchDIT(); String sFilter = groupProps.returnGroupNameSearchFilter(filter); SearchControls controls = new SearchControls(); controls.setSearchScope(SearchControls.SUBTREE_SCOPE); if (sNameAttribute.length() > 0) { String[] aReturn = new String[1]; aReturn[0] = sNameAttribute; controls.setReturningAttributes(aReturn); } enSearch = dirContext.search(sBaseDN,sFilter,controls); try { while (enSearch.hasMore()) { SearchResult result = (SearchResult)enSearch.next(); String sDN = buildFullDN(result.getName(),sBaseDN); if (sDN.length() > 0) { String sName = ""; if (sNameAttribute.length() > 0) { Attribute attrName = result.getAttributes().get(sNameAttribute); if ((attrName != null) && (attrName.size() > 0)) { sName = Val.chkStr(attrName.get(0).toString()); } } Group group = new Group(); group.setDistinguishedName(sDN); group.setKey(group.getDistinguishedName()); group.setName(sName); groups.add(group); } } } catch (PartialResultException pre) { LogUtil.getLogger().finer(pre.toString()); } catch (LimitExceededException lee) { LogUtil.getLogger().finer(lee.toString()); } }finally { closeEnumeration(enSearch); } return groups; }