Announcement Announcement Module
Collapse
No announcement yet.
how to insert multi value attribute in AD Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • how to insert multi value attribute in AD

    hello i want to create a person object in AD which having multi value attribute like roomNumber

    below is my code.

    Code:
    my test class:
                             String[] roll={"23","45"};
    			
    			ContactDTO newContactDTO = new ContactDTO();
    			newContactDTO.setCommonName("Martin");
    			newContactDTO.setLastName("missi");
    			newContactDTO.setDescription("Former leader");
    			newContactDTO.setRoomNumber(roll);
    			ldapContact.insertContact(newContactDTO);

    Code:
    my DAO method
    
    public void insertContact(ContactDTO contactDTO) {
    
    		Attributes personAttributes = new BasicAttributes();
    		BasicAttribute personBasicAttribute = new BasicAttribute("objectclass");
    		personBasicAttribute.add("person");
    		personAttributes.put(personBasicAttribute);
    		personAttributes.put("cn", contactDTO.getCommonName());
    		personAttributes.put("sn", contactDTO.getLastName());
    		personAttributes.put("description", contactDTO.getDescription());
    		personAttributes.put("roomNumber", contactDTO.getRoomNumber());
    
    		DistinguishedName newContactDN = new DistinguishedName();
    		newContactDN.add("cn", contactDTO.getCommonName());
    		
    		ldapTemplate.bind(newContactDN, null, personAttributes);
    	}

    Code:
    Person DTO class
    
    String[] roomNumber;
    public String[] getRoomNumber() {
    			return roomNumber;
    		}
    		public void setRoomNumber(String[] roomNumber) {
    			this.roomNumber = roomNumber;
    		}
    ....

    Code:
    Spring configuration file
    
    <beans>
    	 <bean id="contextSource"
    		class="org.springframework.ldap.support.LdapContextSource">
    		<property name="url" value="ldap://XXXXXXX:389" />
    		<property name="base" value="ou=Students,DC=lti-student,DC=test" />
    		<property name="userName" value="XXXXXXX" />
    		<property name="password" value="XXXXXXXXX" />
    	</bean>

    when i run my test class i find the below exception

    Code:
    
    org.springframework.ldap.UncategorizedLdapException: Operation failed; nested exception is javax.naming.directory.InvalidAttributeValueException: Malformed 'roomNumber' attribute value; remaining name 'cn=Martin'
    	at org.springframework.ldap.DefaultNamingExceptionTranslator.translate(DefaultNamingExceptionTranslator.java:93)
    	at org.springframework.ldap.LdapTemplate.executeWithContext(LdapTemplate.java:764)
    	at org.springframework.ldap.LdapTemplate.executeReadWrite(LdapTemplate.java:757)
    	at org.springframework.ldap.LdapTemplate.bind(LdapTemplate.java:963)
    	at com.javaworld.sample.LDAPContactDAO.insertContact(LDAPContactDAO.java:80)
    	at com.javaworld.sample.SpringFrameworkLDAPClient.main(SpringFrameworkLDAPClient.java:54)
    Caused by: javax.naming.directory.InvalidAttributeValueException: Malformed 'roomNumber' attribute value; remaining name 'cn=Martin'
    	at com.sun.jndi.ldap.LdapClient.encodeAttribute(LdapClient.java:951)
    	at com.sun.jndi.ldap.LdapClient.add(LdapClient.java:999)
    	at com.sun.jndi.ldap.LdapCtx.c_bind(LdapCtx.java:393)
    	at com.sun.jndi.toolkit.ctx.ComponentDirContext.p_bind(ComponentDirContext.java:277)
    	at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.bind(PartialCompositeDirContext.java:197)
    	at javax.naming.directory.InitialDirContext.bind(InitialDirContext.java:163)
    	at org.springframework.ldap.LdapTemplate$21.executeWithContext(LdapTemplate.java:966)
    	at org.springframework.ldap.LdapTemplate.executeWithContext(LdapTemplate.java:762)
    	... 4 more
    Error occured javax.naming.directory.InvalidAttributeValueException: Malformed 'roomNumber' attribute value; remaining name 'cn=Martin'

    can any one tell me whats going wrong

  • #2
    To get this working, replace the following line in your insertContact() method

    Code:
     personAttributes.put("roomNumber", contactDTO.getRoomNumber());
    with this:

    Code:
    BasicAttribute roomAttribute = new BasicAttribute("roomNumber");
    for(String r : contactDTO.getRoomNumber())
    {
      roomAttribute.add(r);
    }
    personAttributes.put(roomAttribute);

    Comment


    • #3
      thanks for replay..
      i still facing a problem, below is my insert method as you suggested

      Code:
      public void insertContact(ContactDTO contactDTO) {
      
      		Attributes personAttributes = new BasicAttributes();
      		BasicAttribute personBasicAttribute = new BasicAttribute("objectclass");
      		personBasicAttribute.add("person");
      		personAttributes.put(personBasicAttribute);
      		personAttributes.put("cn", contactDTO.getCommonName());
      		personAttributes.put("sn", contactDTO.getLastName());
      		personAttributes.put("description", contactDTO.getDescription());
      	
      		BasicAttribute roomAttribute = new BasicAttribute("roomNumber");
      		for(String r : contactDTO.getRoomNumber())
      		{
      		  roomAttribute.add(r);
      		}
      		personAttributes.put(roomAttribute);
      
      				
      		DistinguishedName newContactDN = new DistinguishedName();
      		newContactDN.add("cn", contactDTO.getCommonName());
      		
      		ldapTemplate.bind(newContactDN, null, personAttributes);
      	}
      but again exception while run the test.. now error code 65 occur
      Code:
      org.springframework.ldap.UncategorizedLdapException: Operation failed; nested exception is javax.naming.directory.SchemaViolationException: [LDAP: error code 65 - 0000207D: UpdErr: DSID-031514DE, problem 6002 (OBJ_CLASS_VIOLATION), data 1376262
      ]; remaining name 'cn=Martin101'
      	at org.springframework.ldap.DefaultNamingExceptionTranslator.translate(DefaultNamingExceptionTranslator.java:93)
      	at org.springframework.ldap.LdapTemplate.executeWithContext(LdapTemplate.java:764)
      	at org.springframework.ldap.LdapTemplate.executeReadWrite(LdapTemplate.java:757)
      	at org.springframework.ldap.LdapTemplate.bind(LdapTemplate.java:963)
      	at com.javaworld.sample.LDAPContactDAO.insertContact(LDAPContactDAO.java:98)
      	at com.javaworld.sample.SpringFrameworkLDAPClient.main(SpringFrameworkLDAPClient.java:55)
      Caused by: javax.naming.directory.SchemaViolationException: [LDAP: error code 65 - 0000207D: UpdErr: DSID-031514DE, problem 6002 (OBJ_CLASS_VIOLATION), data 1376262
      why this exception is occur i fully check my schema of LDAP where roomNumber is multivalued attribute and i insert multiple values from ldap client interface.
      can you please suggest me more to overcome this problem..

      waiting for response

      Comment


      • #4
        thanks....

        i solve it now...

        Code:
        
        		Attributes personAttributes = new BasicAttributes();
        		BasicAttribute personBasicAttribute = new BasicAttribute("objectclass");
                           // this is the object class which i missed
                           personBasicAttribute.add("room");	     
        		personBasicAttribute.add("person");
        
        		personAttributes.put(personBasicAttribute);
        		personAttributes.put("cn", contactDTO.getCommonName());
        		personAttributes.put("sn", contactDTO.getLastName());
        		personAttributes.put("description", contactDTO.getDescription());
        	
        		BasicAttribute roomAttribute = new BasicAttribute("roomNumber");
        		for(String r : contactDTO.getRoomNumber())
        		{
        		  roomAttribute.add(r);
        		}
        		personAttributes.put(roomAttribute);
        
        				
        		DistinguishedName newContactDN = new DistinguishedName();
        		newContactDN.add("cn", contactDTO.getCommonName());
        		
        		ldapTemplate.bind(newContactDN, null, personAttributes);
        	}

        thanks you bvar

        Comment

        Working...
        X