This code snippet will do the work for unknown attribute names and single or multiple values (such as multiple object classes):
Using spring-ldap 2.3.1 and AttributesMapper implementation:
<!-- https://mvnrepository.com/artifact/org.springframework.ldap/spring-ldap-core -->
<dependency>
<groupId>org.springframework.ldap</groupId>
<artifactId>spring-ldap-core</artifactId>
<version>2.3.1.RELEASE</version>
</dependency>
In this sample code the searchResultList contains all entries, each one is represented as a map of attributes (with one or more values):
List<Map<String, List<String>>> searchResultList = sourceLdapTemplate.search(searchBase, filter.encode(), SearchControls.ONELEVEL_SCOPE, new AttributesMapper<Map<String, List<String>>>() {
@Override
public Map<String, List<String>> mapFromAttributes(Attributes attributes) throws NamingException {
Map<String, List<String>> attrsMap = new HashMap<>();
NamingEnumeration<String> attrIdEnum = attributes.getIDs();
while (attrIdEnum.hasMoreElements()) {
// Get attribute id:
String attrId = attrIdEnum.next();
// Get all attribute values:
Attribute attr = attributes.get(attrId);
NamingEnumeration<?> attrValuesEnum = attr.getAll();
while (attrValuesEnum.hasMore()) {
if (!attrsMap.containsKey(attrId))
attrsMap.put(attrId, new ArrayList<String>());
attrsMap.get(attrId).add(attrValuesEnum.next().toString());
}
}
return attrsMap;
}
});
Now, working with the searchResultList looks like this:
for (Map<String, List<String>> attrsMap : searchResultList) {
for (String objectClass : m.get("objectClass")) {
// do something with this objectClass...
}
}