Adding a new attribute for an ldap entry

In this post under Java LDAP, I will show with example how to add a new attribute to an existing ldap entry.

For our example I will add a “telephoneNumber” attribute to an ldap entry identified by dn.

I will show two approaches through which we can add a new attribute to an existing ldap entry.

Below is the complete main class code for your reference.

Main class

1  package package6;
2  
3  import javax.naming.Context;
4  import javax.naming.NamingException;
5  import javax.naming.directory.BasicAttribute;
6  import javax.naming.directory.DirContext;
7  import javax.naming.directory.InitialDirContext;
8  import javax.naming.directory.ModificationItem;
9  import java.util.Hashtable;
10 
11 public class LDAPDemo6 {
12     public static void main(String[] args) {
13         Hashtable<String, Object> env = new Hashtable<String, Object>();
14         env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
15         env.put(Context.PROVIDER_URL, "ldap://localhost:1389");
16         env.put(Context.SECURITY_PRINCIPAL, "cn=admin,dc=example,dc=org");
17         env.put(Context.SECURITY_CREDENTIALS, "adminpassword");
18         DirContext ctx = null;
19         try {
20             //first approach
21             ctx = new InitialDirContext(env);
22             BasicAttribute telephoneNumberBasicAttribute = new BasicAttribute("telephoneNumber", "Value1");
23             ModificationItem modificationItem = new ModificationItem(DirContext.ADD_ATTRIBUTE, telephoneNumberBasicAttribute);
24             ModificationItem[] modificationItems = {modificationItem};
25             String dn = "cn=user1,ou=testing,dc=example,dc=org";
26             ctx.modifyAttributes(dn, modificationItems);
27 
28             //second approach
29             telephoneNumberBasicAttribute = new BasicAttribute("telephoneNumber", "Value1");
30             modificationItem = new ModificationItem(DirContext.ADD_ATTRIBUTE, telephoneNumberBasicAttribute);
31             modificationItems = new ModificationItem[]{modificationItem};
32             ctx = (DirContext) ctx.lookup("ou=testing,dc=example,dc=org");
33             ctx.modifyAttributes("cn=user2", modificationItems);
34         } catch(NamingException excep) {
35             excep.printStackTrace();
36         } finally {
37             if(ctx != null) {
38                 try {
39                     ctx.close();
40                 } catch(NamingException excep) {
41                     excep.printStackTrace();
42                 }
43             }
44         }
45     }
46 }

In the above code, from line 21 to 27 I will show the first approach

At line 21 I create an instance of “InitialDirContext” class.

At line 22, I create an instance of “BasicAttribute” class for “telephoneNumber” attribute. Set its new value also.

At line 23, I create an instance of “ModificationItem” class. I pass the below two arguments to the constructor
1) the modification type “ADD_ATTRIBUTE” as first argument
2) the attribute instance that I created at line 22.

At line 24, I create an array of “ModificationItem” class named “modificationItems” and store the “modificationItem” instance I created at line 23.

Now at line 26, I call “modifyAttributes” method and pass the fully qualified dn and “modificationItems” array as an argument.

This is one way.

From line 28 to 33, 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 29 to 31.

Now at line 32, I lookup for the parent ldap entry of the ldap entry where I need to add a new attribute. The reference to the parent ldap entry is stored in “ctx” object.

Then at line 33, 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 new attribute is added.

In this way we can add a new attribute in an ldap entry.

Leave a comment