In this post under Java LDAP, I will show with example how to modify an existing attribute of an ldap entry.
For our example I will modify the “description” attribute of an ldap entry identified by dn.
I will show two approaches through which we can modify the attribute of an ldap entry.
Below is the complete main class code for your reference.
Main class
1 package package5;
2
3 import java.util.Hashtable;
4
5 import javax.naming.Context;
6 import javax.naming.NamingException;
7 import javax.naming.directory.BasicAttribute;
8 import javax.naming.directory.DirContext;
9 import javax.naming.directory.InitialDirContext;
10 import javax.naming.directory.ModificationItem;
11
12 public class LDAPDemo5 {
13 public static void main(String[] args) {
14 Hashtable<String, Object> env = new Hashtable<String, Object>();
15 env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
16 env.put(Context.PROVIDER_URL, "ldap://localhost:1389");
17 env.put(Context.SECURITY_PRINCIPAL, "cn=admin,dc=example,dc=org");
18 env.put(Context.SECURITY_CREDENTIALS, "adminpassword");
19 DirContext ctx = null;
20 try {
21 //first approach
22 ctx = new InitialDirContext(env);
23 BasicAttribute descriptionBasicAttribute = new BasicAttribute("description", "user1 testing description");
24 ModificationItem modificationItem = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, descriptionBasicAttribute);
25 ModificationItem[] modificationItems = {modificationItem};
26 String dn = "cn=user1,ou=testing,dc=example,dc=org";
27 ctx.modifyAttributes(dn, modificationItems);
28
29 //second approach
30 descriptionBasicAttribute = new BasicAttribute("description", "user2 testing description");
31 modificationItem = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, descriptionBasicAttribute);
32 modificationItems = new ModificationItem[]{modificationItem};
33 ctx = (DirContext) ctx.lookup("ou=testing,dc=example,dc=org");
34 ctx.modifyAttributes("cn=user2", modificationItems);
35 } catch(NamingException excep) {
36 excep.printStackTrace();
37 } finally {
38 if(ctx != null) {
39 try {
40 ctx.close();
41 } catch(NamingException excep) {
42 excep.printStackTrace();
43 }
44 }
45 }
46 }
47 }
In the above code, from line 21 to 27 I will show the first approach
At line 22 I create an instance of “InitialDirContext” class.
At line 23, I create an instance of “BasicAttribute” class for “description” attribute. Set its new value also.
At line 24, I create an instance of “ModificationItem” class. I pass the below two arguments to the constructor
1) the modification type “REPLACE_ATTRIBUTE” which in this case is to replace the value of existing attribute as first argument
2) the attribute instance that I created at line 22.
At line 25, I create an array of “ModificationItem” class named “modificationItems” and store the “modificationItem” instance I created at line 24.
Now at line 27, I call “modifyAttributes” method and pass the fully qualified dn and “modificationItems” array as an argument.
This is one way.
From line 30 to 34, I will show the second approach.
Similar to first approach we create an instance of BasicAttribute, ModificationItem, and an array of ModificationItem. Refer to line 30 to 32.
Now at line 33, I lookup for the parent ldap entry of the ldap entry whose attribute I have to modify. The reference to the parent ldap entry is stored in “ctx” object.
Then at line 34, I call “modifyAttributes” method on “ctx” object and pass only the “cn” of the ldap entry that I have to modify and modificationItems as an argument.
The attribute value is modified.
In this way we can modify the value of an existing attribute in an ldap entry.
In this way I can modify the value of existing attribute of an ldap entry.