Using JNDI it is possible to add new attributes to existing LDAP entries.In a previous chapter we have seen how to create LDAP entry and managing it using Apache Directory Studio. In the just previous chapters we have seen how to fetch attributes of LDAP entries and how to update attributes of LDAP entries. In this chapter we are discussing the way by which Java adding new attribute to LDAP entry using JNDI.We should follow the steps till step 15 described in the previous chapter. Now right click on the object explorer and select the New Attribute option.Note the available attribute types.Please don’t proceed further .Now our intention is to add attribute from code. So we are taking the attribute type ‘displayName‘.Now we got a property to add value to it. So please close the new Attribute window . Now let us see the Java code shown below.
Adding new attribute to LDAP entry using JNDI
AddAttributeSample.java
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 AddAttributeSample {
public AddAttributeSample() {
}
public void addEntry() {
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 adding new attribute..");
displayAttributes(attributes);
System.out.println("Inserting new attribute : " + "");
Attribute attribute = new BasicAttribute("displayName", "Bijoy");
ModificationItem[] item = new ModificationItem[1];
item[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE, attribute);
attributes.put(attribute);
context.modifyAttributes(
"employeeNumber=112233,ou=users,ou=system", item);
System.out.println("After addition of new attribute..");
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) {
AddAttributeSample sample = new AddAttributeSample();
sample.addEntry();
}
}
So the code is almost similar to the code of previous discussion.Here we are using the DirContext.ADD_ATTRIBUTE(value = 1) while creating ModificationItem object. There we used DirContext.REPLACE_ATTRIBUTE(value = 2).
Now lets see the output.
Output
Before adding new attribute..
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
Inserting new attribute :
After addition of 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
See also:
Updating attributes of LDAP entry
Deleting attribute from LDAP entry