Move conversion of roles to Strings into LdapTemplate

This commit is contained in:
Luke Taylor 2006-05-19 22:29:17 +00:00
parent 3239cd139e
commit d3e42c6f3f
2 changed files with 21 additions and 16 deletions

View File

@ -53,6 +53,8 @@ public class LdapTemplate {
/** Default search controls */ /** Default search controls */
private SearchControls searchControls = new SearchControls(); private SearchControls searchControls = new SearchControls();
private NamingExceptionTranslator exceptionTranslator = new LdapExceptionTranslator();
public LdapTemplate(InitialDirContextFactory dirContextFactory) { public LdapTemplate(InitialDirContextFactory dirContextFactory) {
Assert.notNull(dirContextFactory, "An InitialDirContextFactory is required"); Assert.notNull(dirContextFactory, "An InitialDirContextFactory is required");
this.dirContextFactory = dirContextFactory; this.dirContextFactory = dirContextFactory;
@ -112,8 +114,7 @@ public class LdapTemplate {
return callback.execute(ctx); return callback.execute(ctx);
} catch (NamingException exception) { } catch (NamingException exception) {
// TODO: Write a static method in separate NamingExceptionExceptionTranslator class called public DataAccessException convert(NamingException); throw exceptionTranslator.translate("LdapCallback", exception);
throw new LdapDataAccessException("xxxx", exception);
} finally { } finally {
LdapUtils.closeContext(ctx); LdapUtils.closeContext(ctx);
} }
@ -148,13 +149,13 @@ public class LdapTemplate {
/** /**
* Performs a search using the supplied filter and returns the union of the values of the named * Performs a search using the supplied filter and returns the union of the values of the named
* attribute found in all entries matched by the search. Note that one directory entry may have several * attribute found in all entries matched by the search. Note that one directory entry may have several
* values for the attribute. * values for the attribute. Intended for role searches and similar scenarios.
* *
* @param base the DN to search in * @param base the DN to search in
* @param filter search filter to use * @param filter search filter to use
* @param params the parameters to substitute in the search filter * @param params the parameters to substitute in the search filter
* @param attributeName the attribute who's values are to be retrieved. * @param attributeName the attribute who's values are to be retrieved.
* @return the set of values for the attribute as a union of the values found in all the matching entries. * @return the set of String values for the attribute as a union of the values found in all the matching entries.
*/ */
public Set searchForSingleAttributeValues(final String base, final String filter, final Object[] params, final String attributeName) { public Set searchForSingleAttributeValues(final String base, final String filter, final Object[] params, final String attributeName) {
@ -187,7 +188,8 @@ public class LdapTemplate {
while(attributeValues.hasMore()) { while(attributeValues.hasMore()) {
Object value = attributeValues.next(); Object value = attributeValues.next();
unionOfValues.add(value);
unionOfValues.add(value.toString());
} }
} }
@ -288,4 +290,12 @@ public class LdapTemplate {
); );
} }
private static class LdapExceptionTranslator implements NamingExceptionTranslator {
public DataAccessException translate(String task, NamingException e) {
return new LdapDataAccessException(task + ";" + e.getMessage(), e);
}
}
} }

View File

@ -229,18 +229,13 @@ public class DefaultLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator
Iterator it = userRoles.iterator(); Iterator it = userRoles.iterator();
while(it.hasNext()) { while(it.hasNext()) {
Object role = it.next(); String role = (String) it.next();
// We only handle Strings for the time being
if(role instanceof String) {
if(convertToUpperCase) { if(convertToUpperCase) {
role = ((String)role).toUpperCase(); role = role.toUpperCase();
} }
authorities.add(new GrantedAuthorityImpl(rolePrefix + role)); authorities.add(new GrantedAuthorityImpl(rolePrefix + role));
} else {
logger.warn("Non-String value found for role: " + role);
}
} }
return authorities; return authorities;