Deleting attribute from LDAP entry using JNDI

We were discussing the various operations on LDAP using JNDI in the previous chapters. In the just previous chapter we discussed how to add an attribute to an LDAP entry using JNDI. There we added a value to ‘displayName’ attribute. It is often required in Java application to delete attributes of LDAP entries.This chapter discusses how the  deleting attribute from LDAP entry using JNDI   is implementing.

Deleting LDAP attribute in Java

In this chapter we are discussing  the deletion of the attribute  from an LDAP entry.In many cases , our Java application needs to delete an attribute  from an entry. The example shows how to do it.

Deleting attribute from LDAP entry using JNDI

In this example we are deleting the attribute ‘displayName’ from LDAP entry .

import java.util.Properties;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.ModificationItem;

public class DeleteAttributeSample {

public DeleteAttributeSample() {

}
public void deleteAttribute() {
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
properties.put(Context.PROVIDER_URL, "ldap://localhost:10389");
properties.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
properties.put(Context.SECURITY_CREDENTIALS, "secret");
try {
DirContext context = new InitialDirContext(properties);
Attributes attributes = context
.getAttributes("employeeNumber=112233,ou=users,ou=system");
System.out.println("Before remeving new attribute..");
displayAttributes(attributes);
System.out.println("Removing attribute : " + "");
Attribute attribute = new BasicAttribute("displayName", "Bijoy");
ModificationItem[] item = new ModificationItem[1];
item[0] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE,
attribute);
attributes.put(attribute);

context.modifyAttributes(
"employeeNumber=112233,ou=users,ou=system", item);
System.out.println("Attribute displayName is deleted....Existing attributes are :");
Attributes attributes1 = context
.getAttributes("employeeNumber=112233,ou=users,ou=system");
displayAttributes(attributes1);

} catch (NamingException e) {
e.printStackTrace();
}
}
public void displayAttributes(Attributes attributes) {
if (attributes != null) {
try {
for (NamingEnumeration e = attributes.getAll(); e.hasMore();) {
Attribute attr = (Attribute) e.next();
System.out.println("Attribute name: " + attr.getID());

for (NamingEnumeration n = attr.getAll(); n.hasMore(); System.out
.println("value: " + n.next()))
;
}
} catch (NamingException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
DeleteAttributeSample sample = new DeleteAttributeSample();
sample.deleteAttribute();
}
}

While creating the ModificationItem object the parameter passing as argument is important here. It should be DirContext.REMOVE_ATTRIBUTE (value = 3)

Now lets see the output.

Output

Before remeving new attribute..

Attribute name: telephoneNumber

value: 99999999999

Attribute name: displayName

value: Bijoy

Attribute name: objectClass

value: organizationalPerson

value: person

value: inetOrgPerson

value: top

Attribute name: employeeNumber

value: 112233

Attribute name: sn

value: Bijoy

Attribute name: cn

value: Bijoy

Removing attribute :

Attribute displayName is deleted….Existing attributes are :

Attribute name: telephoneNumber

value: 99999999999

Attribute name: objectClass

value: organizationalPerson

value: person

value: inetOrgPerson

value: top

Attribute name: employeeNumber

value: 112233

Attribute name: sn

value: Bijoy

Attribute name: cn

value: Bijoy

See also:

JNDI fundamentals

Naming service example using JBossNS.

Displaying attributes of an LDAP entry

Adding attribute to an LDAP entry

Updating attribute in LDAP entry

Adding LDAP entry using JNDI

Deleting LDAP entry using JNDI

Displaying user entries in LDAP using JNDI