Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to import attribute change in directory through LDIF File by java program

shivam chauhan
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys

New to ranch

Please help me out, i have a code which connects to LDAP Directory ,and performs an attribute change for a single user Dn.
I am unable to do so ,for a ldif which contains bulk user accounts modify ldif.


dn: uid=DummyTest,ou=people,dc=Org,dc=com
changetype: modify
replace: userPassword
userPassword: ******

dn: uid=DummyTest1,ou=people,dc=Org,dc=com
changetype: modify
replace: userPassword
userPassword: *******

dn: uid=DummyTest2,ou=people,dc=Org,dc=com
changetype: modify
replace: userPassword
userPassword: ********

Java Program:

LDAPConnection ldapConnection = new LDAPConnection() ;
try {

ldapConnection.connect("ldap://hostname", port,"uid=admin", "secret");

} catch (LDAPException e) {
// TODO Auto-generated catch block
return ldapConnection!=null ? ldapConnection:null;


InputStream file2 = new FileInputStream(new File(

LdifReader ldifReader = new LdifReader(file2);
for (LdifEntry entry : ldifReader) //here is the problem i guess

switch (entry.getChangeType()) {
case Modify:

String C = LDIFseter.generatePassword();

String EntryDN = entry.getDn().toString();
LDAPAttribute password = new LDAPAttribute( "userPassword",C);
LDAPModification singleChange = new LDAPModification( LDAPModification.REPLACE, password);
ldapConnection.modify( EntryDN, singleChange );
LDAPAttribute cmpThisAttr = new LDAPAttribute( "userPassword",C );

boolean hasValue = EntryDN, cmpThisAttr );
if ( hasValue ) {
System.out.println( "Attribute and value found in entry." );
} else {

LDAPAttribute attr1 = new LDAPAttribute( "userPassword",C );
LDAPAttributeSet myAttrs = new LDAPAttributeSet();
myAttrs.add( attr1 );
/*Can add many more attributes to the Object like email ,address,ph no */


throw new IllegalArgumentException(
"Unknown change type in LdifEntry: "
+ entry.getChangeType().toString());

  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic