Wednesday, 25 May 2016

Add new entry to LDAP using Java


In this post, I am going to add new user "Sandesh" under the context "ou=people,dc=example,dc=com". By using createSubcontext method, we can add new entry to directory. As you see following figure, currently there are only four people. After adding new person “Sandesh”, it should have 5 persons.
Following step-by-step procedure explains, how to add new person.

Step 1: Get DirContext instance.
DirContext context = DirectoryUtil.getContext();

Step 2: Make the string representation of the entry.
String distinguishedName = "cn=Sandesh,ou=people,dc=example,dc=com";

Step 3: Define attributes that are specific to new entry.
Attribute objectClass = new BasicAttribute("objectClass", "person");
Attribute userSn = new BasicAttribute("sn", "Nair");
Attribute description = new BasicAttribute("description","Senior Software Engineer, work for XYZ");
Attribute telephoneNumber = new BasicAttribute("telephoneNumber", "1234567890");

Step 4: Define new entry.
Attributes entry = new BasicAttributes();
entry.put(userSn);
entry.put(description);
entry.put(telephoneNumber);
entry.put(objectClass);

Step 5: Call createSubcontext method by passing distinguished name and entry instance defined in step 4.
context.createSubcontext(distinguishedName, entry);

Following is the complete working Application.
import java.util.Objects;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

public class DirectoryUtil {
    private final static String FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";
    private final static String PROVIDER_URL = "ldap://localhost:10389";
    private static DirContext context = null;

    public static DirContext getContext() throws NamingException {
        if (!Objects.isNull(context)) {
            return context;
        }

        Properties properties = new Properties();
        properties.put(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
        properties.put(Context.PROVIDER_URL, PROVIDER_URL);
        DirContext context = new InitialDirContext(properties);
        return context;
    }
}

import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;

public class Test {

    public static void main(String[] args) throws NamingException {
        DirContext context = DirectoryUtil.getContext();

        String distinguishedName = "cn=Sandesh,ou=people,dc=example,dc=com";

        Attribute objectClass = new BasicAttribute("objectClass", "person");
        Attribute userSn = new BasicAttribute("sn", "Nair");
        Attribute description = new BasicAttribute("description",
                "Senior Software Engineer, work for XYZ");
        Attribute telephoneNumber = new BasicAttribute("telephoneNumber",
                "1234567890");

        Attributes entry = new BasicAttributes();
        entry.put(userSn);
        entry.put(description);
        entry.put(telephoneNumber);
        entry.put(objectClass);

        context.createSubcontext(distinguishedName, entry);

    }

}

Run above program, You can able to see the entry “Sandesh” in DIT.


Previous                                                 Next                                                 Home

No comments:

Post a Comment