Announcement Announcement Module
Collapse
No announcement yet.
Reading multi-value attribute from LDAP Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Reading multi-value attribute from LDAP

    From LDAP, I am reading a number of attributes and mapping attribute names to variables using an internal class that implements AttributesMapper. (I am following Example 2.2 at http://static.springframework.org/sp...tml/basic.html)

    Code:
    	private class CompanyAttributesMapper implements AttributesMapper {
    		@SuppressWarnings("unchecked")
    		public Object mapFromAttributes(Attributes attrs)
    				throws NamingException {
    			Company co1 = new Company();
    
    			// Read a single valued attribute from LDAP and set it
    			co1.setCoName((String) attrs.get("cn").get());
    		
    			// Read a multi-valued attribute from LDAP and set it
    			int NumExecutives = 0;
    			while (((List<String>) attrs.get("execName").get()).iterator().hasNext()) {
    				NumExecutives++;
    			}
    			co1.setNumExecutives(NumExecutives);
    			
    			return co1;
    		}
    	}
    I see that I get an Exception at:

    Code:
    while (((List<String>) attrs.get("execName").get()).iterator().hasNext()) {
    Perhaps, my question is too basic; but it's my first day with Spring so I would very much appreciate tips, references, etc.

    Thanks in advance,

    Chintan Zaveri.

  • #2
    This has nothing to do with Spring LDAP - Attributes and Attribute are Java classes. Attributes.get() will return an Attribute object - you will have to iterate through its values manually (it's a NamingEnumeration).

    Handling Attributes manually is verbose and tedious work - you should use a ContextMapper instead. This will give you a DirContextAdapter to work with, which will greatly simplify working Attributes, particularly multi-value ones. Please refer to the the Spring LDAP reference docs for further information on how to use a ContextMapper.

    Comment


    • #3
      Originally posted by rasky View Post
      This has nothing to do with Spring LDAP - Attributes and Attribute are Java classes. Attributes.get() will return an Attribute object - you will have to iterate through its values manually (it's a NamingEnumeration).

      Handling Attributes manually is verbose and tedious work - you should use a ContextMapper instead. This will give you a DirContextAdapter to work with, which will greatly simplify working Attributes, particularly multi-value ones. Please refer to the the Spring LDAP reference docs for further information on how to use a ContextMapper.
      Thank-you very much! This is very useful.

      Kind regards,

      Chintan Zaveri.

      Comment

      Working...
      X