Extract data from NamingEnumeration
Asked Answered
I

4

6

My application searches 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 list of NamingEnumeration object, which contains vectors in it. Each vector may contain one or more values. I can print person names by this code

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

As my ldap search can contain mutiple values so that comes in vector inside NamingEnumeration object. How can I get multiple values out of it.

Inconvenient answered 13/12, 2011 at 10:23 Comment(0)
P
4

As you are using a java.util.List of javax.naming.NamingEnumeration<java.util.Vector> such as this,

List<NamingEnumeration<Vector>> list

You should be able to iterate over the 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 is considered by many to be obsolescent, although not deprecated. Also, the enclosed collection could use a type parameter. If you have a choice, consider one of these alternatives:

List<NamingEnumeration<Vector<T>>>
List<NamingEnumeration<List<T>>>
Personify answered 16/12, 2011 at 11:44 Comment(0)
S
2

While iterating a list using the forsyntax introduced with Java5

You shouldn't call hasMore()

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

In case 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 );
}
Subdebutante answered 13/12, 2011 at 10:36 Comment(3)
I was asking that; "How can I get multiple values out of it.". That means How can I know vector contain one or more elements. With counter?Inconvenient
@imrantariq: Are you examining a java.util.List of javax.naming.NamingEnumeration<java.util.Vector>?Personify
IIUC, you can combine @stacker's helpful suggestions, as shown here,Personify
D
2

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...
    }
}
Digestive answered 7/9, 2017 at 14:41 Comment(0)
D
0

NamingEnumeration extends Enumeration therefore you can simply use Collections::list to convert an Enumeration into a List:

interface NamingEnumeration<T> extends Enumeration<T>
static <T> ArrayList<T> Collections::list(Enumeration<T> e)


NamingEnumeration nameEnum = attributes.get("attributeName").getAll()
list = Collections.list(nameEnum)
Dissent answered 28/9, 2023 at 3:7 Comment(2)
While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.Capitation
Hope that clarifiesDissent

© 2022 - 2024 — McMap. All rights reserved.