LdapUsersProvider extends ExternalUsersProvider { public UserDetails getUserDetails(String username) { LOG.debug("Requesting details for user {}", username); if (userMappings.isEmpty()) { String errorMessage = format("Unable to retrieve details for user %s: No user mapping found.", username); LOG.debug(errorMessage); throw new LdapException(errorMessage); } UserDetails details = null; LdapException exception = null; for (String serverKey : userMappings.keySet()) { SearchResult searchResult = null; try { searchResult = userMappings.get(serverKey).createSearch(contextFactories.get(serverKey), username) .returns(userMappings.get(serverKey).getEmailAttribute(), userMappings.get(serverKey).getRealNameAttribute()) .findUnique(); } catch (NamingException e) { LOG.debug(e.getMessage(), e); exception = new LdapException("Unable to retrieve details for user " + username + " in " + serverKey, e); } if (searchResult != null) { try { details = mapUserDetails(serverKey, searchResult); break; } catch (NamingException e) { LOG.debug(e.getMessage(), e); exception = new LdapException("Unable to retrieve details for user " + username + " in " + serverKey, e); } } else { LOG.debug("User {} not found in {}", username, serverKey); continue; } } if (details == null && exception != null) { throw exception; } return details; } LdapUsersProvider(Map<String, LdapContextFactory> contextFactories, Map<String, LdapUserMapping> userMappings); @Override UserDetails doGetUserDetails(Context context); UserDetails getUserDetails(String username);  }
@Test public void test() throws Exception { Settings settings = LdapSettingsFactory.generateSimpleAnonymousAccessSettings(exampleServer, infosupportServer); LdapSettingsManager settingsManager = new LdapSettingsManager(settings, new LdapAutodiscovery()); LdapUsersProvider usersProvider = new LdapUsersProvider(settingsManager.getContextFactories(), settingsManager.getUserMappings()); UserDetails details; details = usersProvider.getUserDetails("godin"); assertThat(details.getName()).isEqualTo("Evgeny Mandrikov"); assertThat(details.getEmail()).isEqualTo("godin@example.org"); details = usersProvider.getUserDetails("tester"); assertThat(details.getName()).isEqualTo("Tester Testerovich"); assertThat(details.getEmail()).isEqualTo("tester@example.org"); details = usersProvider.getUserDetails("without_email"); assertThat(details.getName()).isEqualTo("Without Email"); assertThat(details.getEmail()).isEqualTo(""); details = usersProvider.getUserDetails("notfound"); assertThat(details).isNull(); details = usersProvider.getUserDetails("robby"); assertThat(details.getName()).isEqualTo("Robby Developer"); assertThat(details.getEmail()).isEqualTo("rd@infosupport.com"); details = usersProvider.getUserDetails("testerInfo"); assertThat(details.getName()).isEqualTo("Tester Testerovich"); assertThat(details.getEmail()).isEqualTo("tester@infosupport.com"); }