MembershipSelector implements LdapEntrySelector { @Override public Iterable<LdapEntry> select(Connection connection) { final SearchRequest groupsSearch = new SearchRequest(); groupsSearch.setBaseDn(baseDn); groupsSearch.setSearchFilter(new SearchFilter(groupsFilter)); groupsSearch.setSearchScope(SUBTREE); groupsSearch.setReturnAttributes(membersAttr); groupsSearch.setSearchEntryHandlers(new RangeEntryHandler()); try { final Response<SearchResult> response = new SearchOperation(connection).execute(groupsSearch); if (response.getResultCode() != SUCCESS) { throw new SyncException("Couldn't get groups, result code is " + response.getResultCode()); } return new RequestEachEntryIterable( response .getResult() .getEntries() .stream() .flatMap(entry -> entry.getAttribute(membersAttr).getStringValues().stream()) .collect(toSet()), connection, usersFilter, returnAttrs); } catch (LdapException x) { throw new SyncException(x.getLocalizedMessage(), x); } } MembershipSelector(
      String baseDn,
      String groupsFilter,
      String usersFilter,
      String membersAttr,
      String... returnAttrs); @Override Iterable<LdapEntry> select(Connection connection); @Override String toString();  }
@Test public void testMembershipSelection() throws Exception { final MembershipSelector selector = new MembershipSelector( server.getBaseDn(), "(objectClass=groupOfNames)", "(objectClass=inetOrgPerson)", "member", "uid", "givenName"); try (Connection conn = connFactory.getConnection()) { conn.open(); final Set<LdapEntry> selection = StreamSupport.stream(selector.select(conn).spliterator(), false).collect(toSet()); assertEquals(selection.size(), 200); for (LdapEntry entry : selection) { assertNotNull(entry.getAttribute("givenName")); assertNotNull(entry.getAttribute("uid")); } } }