/** * Checks whether search result contains exactly one entry. * @return true if the search result contains a single entry. * @throws NamingException */ public boolean hasSingleResult() throws NamingException { List<String> allResults = getAllLdapNames(); return allResults != null && allResults.size() == 1; }
/** * Returns a single entry from the search result. * Throws {@code NamingException} if the search result doesn't contain exactly one entry. * @return name in the namespace * @throws NamingException */ public String getSingleLdapName() throws NamingException { List<String> allLdapNames = getAllLdapNames(); if (allLdapNames.size() == 1) { return allLdapNames.get(0); } throw new NamingException("Single result was expected"); }
private List<String> findDnByPattern(List<String> patterns, String name) throws NamingException { for (String pattern : patterns) { String baseDnFromPattern = LdapUtils.extractBaseDn(pattern); String rdn = LdapUtils.extractFirstRdn(pattern).replaceAll("%s", name); List<String> list = execute(Collections.singletonList(baseDnFromPattern), queries.findDnByPattern(rdn)).getAllLdapNames(); if (!list.isEmpty()) { return list; } } return Collections.emptyList(); }
/** * {@inheritDoc} */ @Override public List<String> findGroupsForUser(String userDn) throws NamingException { String userName = LdapUtils.extractUserName(userDn); return execute(groupBases, queries.findGroupsForUser(userName, userDn)).getAllLdapNames(); }
/** * {@inheritDoc} */ @Override public String findUserDn(String user) throws NamingException { List<String> allLdapNames; if (LdapUtils.isDn(user)) { String userBaseDn = LdapUtils.extractBaseDn(user); String userRdn = LdapUtils.extractFirstRdn(user); allLdapNames = execute(Collections.singletonList(userBaseDn), queries.findUserDnByRdn(userRdn)).getAllLdapNames(); } else { allLdapNames = findDnByPattern(userPatterns, user); if (allLdapNames.isEmpty()) { allLdapNames = execute(userBases, queries.findUserDnByName(user)).getAllLdapNames(); } } if (allLdapNames.size() == 1) { return allLdapNames.get(0); } else { LOG.info("Expected exactly one user result for the user: {}, but got {}. Returning null", user, allLdapNames.size()); LOG.debug("Matched users: {}", allLdapNames); return null; } }
@Test public void testGetAllLdapNamesWithExceptionInNamingEnumerationClose() throws NamingException { MockResultCollection resultCollection = MockResultCollection.create() .addSearchResultWithDns("1") .addSearchResultWithDns("2"); doThrow(NamingException.class).when(resultCollection.iterator().next()).close(); handler = new SearchResultHandler(resultCollection); List<String> actual = handler.getAllLdapNames(); assertEquals("Resultset size", 2, actual.size()); assertAllNamingEnumerationsClosed(resultCollection); }
@Test public void testGetAllLdapNames() throws NamingException { String objectDn1 = "cn=a1,dc=b,dc=c"; String objectDn2 = "cn=a2,dc=b,dc=c"; String objectDn3 = "cn=a3,dc=b,dc=c"; MockResultCollection resultCollection = MockResultCollection.create() .addSearchResultWithDns(objectDn1) .addSearchResultWithDns(objectDn2, objectDn3); handler = new SearchResultHandler(resultCollection); List<String> expected = Arrays.asList(objectDn1, objectDn2, objectDn3); Collections.sort(expected); List<String> actual = handler.getAllLdapNames(); Collections.sort(actual); assertEquals(expected, actual); assertAllNamingEnumerationsClosed(resultCollection); }
@Test public void testGetAllLdapNamesNoRecords() throws NamingException { MockResultCollection resultCollection = MockResultCollection.create() .addEmptySearchResult(); handler = new SearchResultHandler(resultCollection); List<String> actual = handler.getAllLdapNames(); assertEquals("Resultset size", 0, actual.size()); assertAllNamingEnumerationsClosed(resultCollection); }
/** * Checks whether search result contains exactly one entry. * @return true if the search result contains a single entry. * @throws NamingException */ public boolean hasSingleResult() throws NamingException { List<String> allResults = getAllLdapNames(); return allResults != null && allResults.size() == 1; }
/** * Returns a single entry from the search result. * Throws {@code NamingException} if the search result doesn't contain exactly one entry. * @return name in the namespace * @throws NamingException */ public String getSingleLdapName() throws NamingException { List<String> allLdapNames = getAllLdapNames(); if (allLdapNames.size() == 1) { return allLdapNames.get(0); } throw new NamingException("Single result was expected"); }
private List<String> findDnByPattern(List<String> patterns, String name) throws NamingException { for (String pattern : patterns) { String baseDnFromPattern = LdapUtils.extractBaseDn(pattern); String rdn = LdapUtils.extractFirstRdn(pattern).replaceAll("%s", name); List<String> list = execute(Collections.singletonList(baseDnFromPattern), queries.findDnByPattern(rdn)).getAllLdapNames(); if (!list.isEmpty()) { return list; } } return Collections.emptyList(); }
/** * {@inheritDoc} */ @Override public List<String> findGroupsForUser(String userDn) throws NamingException { String userName = LdapUtils.extractUserName(userDn); return execute(groupBases, queries.findGroupsForUser(userName, userDn)).getAllLdapNames(); }
/** * {@inheritDoc} */ @Override public String findUserDn(String user) throws NamingException { List<String> allLdapNames; if (LdapUtils.isDn(user)) { String userBaseDn = LdapUtils.extractBaseDn(user); String userRdn = LdapUtils.extractFirstRdn(user); allLdapNames = execute(Collections.singletonList(userBaseDn), queries.findUserDnByRdn(userRdn)).getAllLdapNames(); } else { allLdapNames = findDnByPattern(userPatterns, user); if (allLdapNames.isEmpty()) { allLdapNames = execute(userBases, queries.findUserDnByName(user)).getAllLdapNames(); } } if (allLdapNames.size() == 1) { return allLdapNames.get(0); } else { LOG.info("Expected exactly one user result for the user: {}, but got {}. Returning null", user, allLdapNames.size()); LOG.debug("Matched users: {}", allLdapNames); return null; } }