List<Result> query(DirContext ctx, Map<String, String> params) throws NamingException { final SearchControls sc = new SearchControls(); final NamingEnumeration<SearchResult> res; sc.setSearchScope(searchScope.scope()); sc.setReturningAttributes(returnAttributes); res = ctx.search(base, pattern.getRawPattern(), pattern.bind(params), sc); try { final List<Result> r = new ArrayList<>(); try { while (res.hasMore()) { r.add(new Result(res.next())); } } catch (PartialResultException e) { // Ignored } return r; } finally { res.close(); } }
/** * 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; }
private boolean isFollowReferral(ReferralException e) throws ReferralException, PartialResultException { // ignore referral String action = (String) env.get(Context.REFERRAL); if (action == null) { action = "ignore"; } if ("follow".equals(action)) { return true; } else if ("throw".equals(action)) { return false; } else if ("ignore".equals(action)) { // ldap.1A=[LDAP: error code 10 - Referral] throw new PartialResultException(Messages.getString("ldap.1A")); } else { throw new IllegalArgumentException(Messages.getString( "ldap.30", new Object[] { //$NON-NLS-1$ env.get(Context.REFERRAL), Context.REFERRAL })); } }
/** * Add values of a specified attribute to a list * * @param attrId Attribute name * @param attrs Attributes containing the new values * @param values ArrayList containing values found so far * * @exception NamingException if a directory server error occurs */ private ArrayList<String> addAttributeValues(String attrId, Attributes attrs, ArrayList<String> values) throws NamingException{ if (containerLog.isTraceEnabled()) containerLog.trace(" retrieving values for attribute " + attrId); if (attrId == null || attrs == null) return values; if (values == null) values = new ArrayList<String>(); Attribute attr = attrs.get(attrId); if (attr == null) return values; NamingEnumeration<?> e = attr.getAll(); try { while(e.hasMore()) { String value = (String)e.next(); values.add(value); } } catch (PartialResultException ex) { if (!adCompat) throw ex; } finally { e.close(); } return values; }
@Test public void search_withPartialResultException() throws Exception { // given initAttribute(SEARCH_ATTRIBUTES[0], aMock); initAttribute(SEARCH_ATTRIBUTES[1], aMock); initAttribute(SEARCH_ATTRIBUTES[2], aMock); when(Boolean.valueOf(neMock.hasMore())).thenReturn(Boolean.TRUE) .thenThrow(new PartialResultException()); Map<SettingType, String> map = new HashMap<SettingType, String>(); map.put(SettingType.LDAP_ATTR_UID, SEARCH_ATTRIBUTES[0]); map.put(SettingType.LDAP_ATTR_FIRST_NAME, SEARCH_ATTRIBUTES[1]); map.put(SettingType.LDAP_ATTR_EMAIL, SEARCH_ATTRIBUTES[2]); ILdapResultMapper<VOUserDetails> mapper = new LdapVOUserDetailsMapper( null, map); List<VOUserDetails> list = new ArrayList<VOUserDetails>(); // when list = bean.search(new Properties(), "baseDN", "filter", mapper, false); // then assertNotNull(list); assertEquals(1, list.size()); VOUserDetails det = list.get(0); assertEquals(SEARCH_ATTRIBUTES[0], det.getRealmUserId()); assertEquals(SEARCH_ATTRIBUTES[1], det.getFirstName()); assertEquals(SEARCH_ATTRIBUTES[2], det.getEMail()); }
private boolean hasMoreEnum(NamingEnumeration<SearchResult> namingEnum) throws NamingException { boolean hasMore = true; try { if (!namingEnum.hasMore()) { hasMore = false; } } catch (PartialResultException e) { hasMore = false; logger.logWarn(Log4jLogger.SYSTEM_LOG, e, LogMessageIdentifier.WARN_LDAP_PARTIAL_EXCEPTION); } return hasMore; }
/** * @return null if there are zero results */ private NamingEnumeration<SearchResult> search(DirContext ctx, Name base, String[] returnAttributes, Filter filter, boolean recurse) { SearchControls ctls = new SearchControls(); ctls.setCountLimit(filter.getLimit()); ctls.setReturningAttributes(returnAttributes); ctls.setSearchScope(recurse ? SearchControls.SUBTREE_SCOPE : SearchControls.ONELEVEL_SCOPE); try { // Search for objects using the filter String query = filter.toFilter(); if( LOGGER.isDebugEnabled() ) { LOGGER.debug("Query:" + query + " Base:" + base); } NamingEnumeration<SearchResult> ne = ctx.search(base, query, ctls); if( ne.hasMore() ) { return ne; } } catch( PartialResultException pre ) { LOGGER.info(pre); } catch( SizeLimitExceededException slee ) { LOGGER.info(slee); } catch( Exception e ) { LOGGER.warn(e); } return null; }
public <T> List<T> search(DirContext ctx, Name base, Filter filter, HitsCollector<T> collector, boolean recurse) { collector.setup(ctx, this); NamingEnumeration<SearchResult> ne = search(ctx, base, collector.getReturnAttributes(), filter, recurse); try { while( ne != null && ne.hasMore() ) { SearchResult sr = ne.next(); collector.addResult(sr, base); } } catch( PartialResultException pre ) { LOGGER.info(pre); } catch( SizeLimitExceededException slee ) { LOGGER.info(slee); } catch( Exception e ) { LOGGER.error("Error searching", e); } return collector.getResults(); }
/** * Add values of a specified attribute to a list * * @param attrId Attribute name * @param attrs Attributes containing the new values * @param values ArrayList containing values found so far * * @exception NamingException if a directory server error occurs */ private ArrayList<String> addAttributeValues(String attrId, Attributes attrs, ArrayList<String> values) throws NamingException{ if (containerLog.isTraceEnabled()) containerLog.trace(" retrieving values for attribute " + attrId); if (attrId == null || attrs == null) return values; if (values == null) values = new ArrayList<String>(); Attribute attr = attrs.get(attrId); if (attr == null) return (values); NamingEnumeration e = attr.getAll(); try { while(e.hasMore()) { String value = (String)e.next(); values.add(value); } } catch (PartialResultException ex) { if (!adCompat) throw ex; } return values; }
/** * Add values of a specified attribute to a list * * @param attrId * Attribute name * @param attrs * Attributes containing the new values * @param values * ArrayList containing values found so far * * @exception NamingException * if a directory server error occurs */ private ArrayList<String> addAttributeValues(String attrId, Attributes attrs, ArrayList<String> values) throws NamingException { if (containerLog.isTraceEnabled()) containerLog.trace(" retrieving values for attribute " + attrId); if (attrId == null || attrs == null) return values; if (values == null) values = new ArrayList<String>(); Attribute attr = attrs.get(attrId); if (attr == null) return values; NamingEnumeration<?> e = attr.getAll(); try { while (e.hasMore()) { String value = (String) e.next(); values.add(value); } } catch (PartialResultException ex) { if (!adCompat) throw ex; } finally { e.close(); } return values; }
/** * Add values of a specified attribute to a list * * @param attrId Attribute name * @param attrs Attributes containing the new values * @param values ArrayList containing values found so far * * @exception NamingException if a directory server error occurs */ private ArrayList<String> addAttributeValues(String attrId, Attributes attrs, ArrayList<String> values) throws NamingException{ if (containerLog.isTraceEnabled()) containerLog.trace(" retrieving values for attribute " + attrId); if (attrId == null || attrs == null) return values; if (values == null) values = new ArrayList<String>(); Attribute attr = attrs.get(attrId); if (attr == null) return (values); NamingEnumeration<?> e = attr.getAll(); try { while(e.hasMore()) { String value = (String)e.next(); values.add(value); } } catch (PartialResultException ex) { if (!adCompat) throw ex; } 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; }
/** * 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()); } } } finally { closeEnumeration(enSearch); } return names; }
/** * Add roles to a user and search for other roles containing them themselves. * We search recursively with a limited depth. * By default the depth is 0, and we only use direct roles. * The search needs to use the distinguished role names, * but to return the role names. * * @param depth Recursion depth, starting at zero * @param context The directory context we are searching * @param recursiveMap The cumulative result map of role names and DNs. * @param recursiveSet The cumulative result set of role names. * @param groupName The role name to add to the list. * @param groupDName The distinguished name of the role. * * @exception NamingException if a directory server error occurs */ private void getRolesRecursive(int depth, DirContext context, Map<String, String> recursiveMap, Set<String> recursiveSet, String groupName, String groupDName) throws NamingException { if (containerLog.isTraceEnabled()) containerLog.trace("Recursive search depth " + depth + " for group '" + groupDName + " (" + groupName + ")'"); // Adding the given group to the result set if not already found if (!recursiveSet.contains(groupDName)) { recursiveSet.add(groupDName); recursiveMap.put(groupDName, groupName); if (depth >= roleRecursionLimit) { if (roleRecursionLimit > 0) containerLog.warn("Terminating recursive role search because of recursion limit " + roleRecursionLimit + ", results might be incomplete"); return; } // Prepare the parameters for searching groups String filter = roleFormat.format(new String[] { groupDName }); SearchControls controls = new SearchControls(); controls.setSearchScope(roleSubtree ? SearchControls.SUBTREE_SCOPE : SearchControls.ONELEVEL_SCOPE); controls.setReturningAttributes(new String[] { roleName }); if (containerLog.isTraceEnabled()) { containerLog.trace("Recursive search in role base '" + roleBase + "' for attribute '" + roleName + "'" + " with filter expression '" + filter + "'"); } // Searching groups that assign the given group NamingEnumeration results = context.search(roleBase, filter, controls); if (results != null) { // Iterate over the resulting groups try { while (results.hasMore()) { SearchResult result = (SearchResult) results.next(); Attributes attrs = result.getAttributes(); if (attrs == null) continue; String dname = getDistinguishedName(context, roleBase, result); String name = getAttributeValue(roleName, attrs); if (name != null && dname != null) { getRolesRecursive(depth+1, context, recursiveMap, recursiveSet, name, dname); } } } catch (PartialResultException ex) { if (!adCompat) throw ex; } } } }
/** * 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; }