Retrieving Data from NamingEnumeration

My application is looking for an LDAP server for people.

return ldapTemplate.search("", "(objectclass=person)", new AttributesMapper() { public Object mapFromAttributes(Attributes attrs) throws NamingException { return attrs.get("cn").getAll(); } }); 

It returns a list of the NamingEnumeration object that contains the vectors in it. Each vector may contain one or more values. I can print the names of people by this code

 for(NamingEnumeration ne : list){ while (ne.hasMore()) { System.out.println("name is : " + ne.next().toString()); } } 

Since my ldap search can contain multiple values, so it falls into the vector inside the NamingEnumeration object. How can I get several values โ€‹โ€‹from it.

+4
source share
3 answers

Since you are using java.util.List of javax.naming.NamingEnumeration<java.util.Vector> for example

 List<NamingEnumeration<Vector>> list 

You should be able to iterate over Vector in each NamingEnumeration :

 for (NamingEnumeration<Vector> ne : list) { while (ne.hasMore()) { Vector vector = ne.next(); for (Object object : vector) { System.out.println(object); } } } 

Note that Vector considered by many to be obsolete, although not obsolete. Alternatively, a nested collection can use a type parameter. If you have a choice, consider one of the following options:

 List<NamingEnumeration<Vector<T>>> List<NamingEnumeration<List<T>>> 
+3
source

Iterate over a list using for syntax presented with Java5

You should not call hasMore()

 for(NamingEnumeration ne : list){ System.out.println("name is : " + ne.toString()); } 

If your list does not support the Iterator interface, you need to use the old form:

 for ( Enumeration e = v.elements() ; e.hasMoreElements() ; ) { String a = (String) e.nextElement(); System.out.println( a ); } 
+1
source

This piece of code will work with unknown attribute names and one or more values โ€‹โ€‹(for example, several classes of objects):

Using spring -ldap 2.3.1 and the Mapper Attributes :

 <!-- 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 code example, searchResultList contains all the records, each of which is represented as an attribute map (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 searchResultList looks like this:

 for (Map<String, List<String>> attrsMap : searchResultList) { for (String objectClass : m.get("objectClass")) { // do something with this objectClass... } } 
0
source

Source: https://habr.com/ru/post/1386073/


All Articles