Announcement Announcement Module
Collapse
No announcement yet.
Accessing LDAP attr using Spring LDAP API Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Accessing LDAP attr using Spring LDAP API

    Hi
    I am using Spring LDAP 1.3.0 for LDAP connection .

    ApplicationContext.xml is as
    Code:
     	<bean id="ldapPropertyConfigurer"
    		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    		<property name="location">
    			<value>classpath:ldap.properties</value>
    		</property>
    	</bean>
    	       
       <bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource">
          <property name="url" value="myhost.ldap.com" />
          <property name="base" value="ou=people, dc=st, dc=com" />
          <property name="userDn" value="st-eduid=ed123123,ou=people,dc=st,dc=com" />
          <property name="password" value="password" />
       </bean>
    
       <bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">
          <constructor-arg ref="contextSource" />
       </bean>
    
       <bean id="ldapUtils" class="com.st.ict.qs.rc.ldap.LDAPUtilsImpl">
          <property name="ldapTemplate" ref="ldapTemplate" />
       </bean>
    
    Java class for code is as :
    
    public List getUserInfoForCN(String cn) {
      LikeFilter filter = new LikeFilter("cn",cn+"*");
     return ldapTemplate.search("", filter.encode(), 2, new UserAttributeMapper());
    }
    
    UserAttributeMapper is as :
    
    public class UserAttributeMapper implements AttributesMapper{
    
    	public Object mapFromAttributes(Attributes attrs) throws NamingException {
    		Person person = new Person();
            person.setFname((String)attrs.get("cn").get());
            //person.setLname((String)attrs.get("sn").get());
            
            System.out.println(attrs.get("st-eduid").get());
            System.out.println(attrs.get("givenName").get());
            System.out.println(attrs.get("mail").get());
            System.out.println(attrs.get("o").get());
            System.out.println(attrs.get("st-locationdescription").get());
            System.out.println(attrs.get("postOfficeBox").get());
            System.out.println(attrs.get("l").get());
            System.out.println(attrs.get("st-orgdescription").get());
            return person;
    
    	}
    }
    I am able to get st-eduid , cn from this code but when I try to fetch other attribute mail , givenName , following error encountered :

    Exception in thread "main" java.lang.NullPointerException
    at com.st.ict.qs.rc.ldap.UserAttributeMapper.mapFromA ttributes(UserAttributeMapper.java:21)
    at org.springframework.ldap.core.AttributesMapperCall backHandler.getObjectFromNameClassPair(AttributesM apperCallbackHandler.java:62)
    at org.springframework.ldap.core.CollectingNameClassP airCallbackHandler.handleNameClassPair(CollectingN ameClassPairCallbackHandler.java:50)
    at org.springframework.ldap.core.LdapTemplate.search( LdapTemplate.java:297)
    at org.springframework.ldap.core.LdapTemplate.search( LdapTemplate.java:259)
    at org.springframework.ldap.core.LdapTemplate.search( LdapTemplate.java:571)
    at org.springframework.ldap.core.LdapTemplate.search( LdapTemplate.java:556)
    at org.springframework.ldap.core.LdapTemplate.search( LdapTemplate.java:411)
    at org.springframework.ldap.core.LdapTemplate.search( LdapTemplate.java:431)


    => Same userDN and password, when I use old way of accessing LDAP provide me all above attributes .

    The old code that I am using is as :
    Code:
    public class LDAPUtils {
    	
    		
        
         public static HashMap<String,String> getUserInfoForFilter(String filter)  {
             HashMap<String,String> userMap= new HashMap<String,String>();
              LDAPConnection ldapConnection = null;
                 
    
                 try {
                     String host="myhost.ldap.com";
                     int port=123;
                 
                     ldapConnection = new LDAPConnection();
                     ldapConnection.connect(host, port); // throws exception if cannot connect
                     String genericUserEDUid = "st-eduid=ed123123,ou=people,dc=st,dc=com";
                     String genericUserPassword = "password";
                     
                     // Generic authentication
                     if (genericUserEDUid != null) {
                         ldapConnection.authenticate(genericUserEDUid, genericUserPassword);
                     }
                     String subSearchBase="ou=people, dc=st, dc=com";
                                    
                    
                     String[] ldapAttrNames = { "st-eduid", "givenName", "sn","cn","mail","o","st-orgdescription",
                     		               		"st-locationdescription","st-workingforcompany", "l"};   
    
                     LDAPSearchResults res = ldapConnection.search(subSearchBase, LDAPConnection.SCOPE_SUB,
                             filter, ldapAttrNames, false);
                     while(res.hasMoreElements()){
                         LDAPEntry findEntry = null;
                         findEntry = res.next();
                         LDAPAttributeSet findAttrs=findEntry.getAttributeSet();
                         Enumeration enumAttrs = findAttrs.getAttributes();
                         while ( enumAttrs.hasMoreElements() ) {
                             LDAPAttribute anAttr =(LDAPAttribute)enumAttrs.nextElement();
                             //logger.debug("::::::::Attributes::::::::::::"+anAttr.getBaseName());
                             Enumeration enumVals = anAttr.getStringValues();
                             while ( enumVals.hasMoreElements() ) {
    //                            eduid=(String)enumVals.nextElement();
                                 userMap.put(anAttr.getBaseName(),(String)enumVals.nextElement());
                                // logger.debug(":::::"+anAttr.getBaseName()+":::::::::"+eduid);
                             }
                         }
                     }
    
                     
                 }
                 catch(Exception ex)
                 {
                    
                 }
                 finally{
                     try{
                         if(ldapConnection!=null){
                             ldapConnection.disconnect();    
                         }
                     }
                     catch(LDAPException exc){
                        
                     }
                     
                 }
                 return userMap;
         }
    }
    URL , base , userDN & password in applicationContext & old code is same but still I have the error .

    Any help is greatly appreciated.

  • #2
    I have resolved the issue .
    Problem was because of objectclass , i was using person which is not the case in my Company it is xy-person .

    Comment

    Working...
    X