In the previous chapter we have seen how to add a new entry to an LDAP directory using JNDI. There we added a new user record to existing directory. It is often needed in Java applications to delete an entry from the LDAP directory. By using JNDI we can do the deletion.Here we are discussing how we can do Deleting LDAP entry using JNDI .
Deleting LDAP entry using JNDI
In the previous example we added a new user record to the directory. In the example we are discussing here deletes the same record we added there.
import java.util.Properties;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
public class EntryDeleteSample {
public EntryDeleteSample() {
}
public void deleteEntry() {
Properties initilaProperties = new Properties();
initilaProperties.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
initilaProperties.put(Context.PROVIDER_URL, "ldap://localhost:10389");
initilaProperties
.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
initilaProperties.put(Context.SECURITY_CREDENTIALS, "secret");
try {
DirContext context = new InitialDirContext(initilaProperties);
System.out.println("Existing users before deletion....");
listEntries(context);
System.out
.println("Deletion of user with employeeNumber = 333333 starts");
deleteUser(context);
System.out.println("After deletion");
listEntries(context);
context.close();
} catch (NamingException n) {
n.printStackTrace();
}
}
public void deleteUser(DirContext context) {
try {
context.destroySubcontext("employeeNumber=333333,ou=users,ou=system");
} catch (NamingException e) {
e.printStackTrace();
}
}
public void listEntries(DirContext context) {
String searchFilter = "(objectClass=inetOrgPerson)";
String[] requiredAttributes = { "employeeNumber", "cn",
"telephoneNumber" };
SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
controls.setReturningAttributes(requiredAttributes);
NamingEnumeration users;
try {
users = context
.search("ou=users,ou=system", searchFilter, controls);
SearchResult searchResult = null;
String commonName = null;
String empNumber = null;
String telephoneNumber = null;
while (users.hasMore()) {
searchResult = (SearchResult) users.next();
Attributes attr = searchResult.getAttributes();
commonName = attr.get("cn").get(0).toString();
empNumber = attr.get("employeeNumber").get(0).toString();
telephoneNumber = attr.get("telephoneNumber").get(0).toString();
System.out.println("Name = " + commonName);
System.out.println("Employee Number = " + empNumber);
System.out.println("Phone Number = " + telephoneNumber);
}
} catch (NamingException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
EntryDeleteSample sample = new EntryDeleteSample();
sample.deleteEntry();
}
}
Compile and run the code. I am giving the output I got.
Output
Existing users before deletion….
Name = Bijoy
Employee Number = 112233
Phone Number = 99999999999
Name = users
Employee Number = 333333
Phone Number = 777777777
Deletion of user with employeeNumber = 333333 starts
After deletion
Name = Bijoy
Employee Number = 112233
Phone Number = 99999999999
See Related Posts:
Naming Service Example using JBossNS
Updating attribute of an LDAP entry using JNDI
Deleting attribute of an LDAP entry using JNDI
Adding attribute to an existing LDAP entry