In this post under Java LDAP, I will show with example two approaches to remove a existing attribute from an existing ldap entry.
For our example I will remove an attribute by name “telephoneNumber” from an ldap entry identified by dn.
Below is the complete main class for your reference
Main class
1 package package7;
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 LDAPDemo7 {
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");
23 ModificationItem modificationItem = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, telephoneNumberBasicAttribute);
24 ModificationItem[] modificationItems = {modificationItem};
25 String dn = "cn=user1,ou=tester,dc=example,dc=org";
26 ctx.modifyAttributes(dn, modificationItems);
27
28 //second approach
29 telephoneNumberBasicAttribute = new BasicAttribute("telephoneNumber");
30 modificationItem = new ModificationItem(DirContext.REMOVE_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 20 to 26, I will show the first approach.
At line 21, I create an instance of “InitialDirContext” pointing the top of the ldap tree.
At line 22, I created an instance of “BasicAttribute” class passing the attribute name that we are planning to remove as an constructor argument.
At line 23, I created an instance of “ModificationItem” class passing the modification type “DirContext.REMOVE_ATTRIBUTE”, and pass the attribute instance (created at line 22) as constructor argument.
At line 24, I created an array of “ModificationItem” and placed the “ModificationItem” instance (created at line 23) as an element in the array.
At line 25, I set the dn of the ldap entry from which I want to remove the attribute.
At line 26, I call “modifyAttributes” method available on “DirContext” instance and pass the dn and “ModificationItem” array as method arguments.
This will remove the attribute “telephoneNumber” from the ldap entry identified by dn.
From line 28 to 33, I 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 whose attribute I have to remove. 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 attribute value is removed.
In this way we can remove an attribute from an ldap entry.