I'm trying to retrieve attributes from an Active Directory server using the Spring LdapTemplate. My code is working fine for String attributes, but I can't get other data types working.
I'm using an AbstractContextMapper to retrieve the values. Using a DirContextOperations object I can retrieve String attributes OK, but when I try to retrieve a Data or Integer value (using the getObjectAttribute method) the value returned is always a String class. How can I get these values as Dates and Integers?
Here's my code:
I noticed another couple of forum entries with similar queries but no resolution:Code:public class LdapDaoImpl implements LdapDao { private static final Logger LOGGER = Logger.getLogger(LdapUserDaoImpl.class); private LdapTemplate ldapTemplate; /** * Perform a search using the filter criteria provided. The SearchEntityAttribute array is * used to determine the result attributes to return e.g. for each record found in LDAP * we return the array of attributes specified in resultAttributes */ @Override public List find(final Filter filter, final SearchEntityAttribute[] resultAttributes) { return ldapTemplate.search("", filter.encode(), new EntityContextMapper(resultAttributes)); } /** * Search by distinguished name (a distinguished name is a unique identifier in LDAP, so we * can perform a direct lookup instead of having to perform a search) */ @Override public Object findByDn(final String dn, final SearchEntityAttribute[] resultAttributes) { return ldapTemplate.lookup(dn, new EntityContextMapper(resultAttributes)); } @Override public void setLdapTemplate(final LdapTemplate ldapTemplate) { this.ldapTemplate = ldapTemplate; } /** * EntityContextMapper is used by the LdapTemplate to map the LDAP search results to objects */ class EntityContextMapper extends AbstractContextMapper { SearchEntityAttribute[] resultAttributes; public EntityContextMapper(final SearchEntityAttribute[] resultAttributes) { this.resultAttributes = resultAttributes; } @Override protected Object doMapFromContext(final DirContextOperations ctx) { //return an object array - this will get converted into a DTO (SearchResultDTO) by the SearchController final Object[] result = new Object[resultAttributes.length]; for (int i = 0; i < resultAttributes.length; i++) { final SearchEntityAttribute resultAttribute = resultAttributes[i]; final IEntityAttributeType entityAttributeType = resultAttribute.getEntityAttributeType(); //The pk should map to the LDAP Distinguished Name - regardless of what the datasource name of the attribute if (entityAttributeType.getPrimaryKeyAttribute()) { //get the distinguished name result[i] = ctx.getDn().toString(); } else { //use the datasource name of the entity attribute type to find the result EntityAttributeDataTypeEnum dataType = entityAttributeType.getDataType(); String dataSourceName = entityAttributeType.getDataSourceName(); if (EntityAttributeDataTypeEnum.String.equals(dataType)) { result[i] = ctx.getStringAttribute(dataSourceName); } else { //there don't seem to be methods to get other data types like Dates, Integers or Boolean's //so just using the getOjectAttribute method Object objectAttribute = ctx.getObjectAttribute(dataSourceName); System.out.println("<<<<< Ojbect attribute " + dataSourceName + " type: " + objectAttribute.getClass().getName()); result[i] = objectAttribute; //This is always a String class - how can I get a Date or Integer ????????? } } } return result; } } }
http://forum.springsource.org/showth...Date+attribute
http://forum.springsource.org/showth...DAP+attributes
Is what I'm trying to do possible, or can Spring LDAP only retrieve String values?
I'm using Spring LDAP 1.3.1.RELEASE.
Thanks.


Reply With Quote