Adding LDAP entry using JNDI

In the previous chapter we discussed adding  attribute to existing LDAP entry. In Java application involving LDAP access it is often required to add LDAP entries from Java application itself.This chapter discusses the way of adding LDAP entry using JNDI . Our application is creating an LDAP user entry with all the necessary attributes.

Adding LDAP entry using JNDI

We discussed the way by which we are creating user entries using Apache Directory Studio before.There we created one user. In the coming  example we will see how to add one user from our Java application. The important attributes  of existing users will be displayed before adding the new user. After the addition of new user , the new set of users also will be displayed.

Now see the Java code.

AddLDAPSample.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.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;

public class AddLDAPSample {

public AddLDAPSample() {

}
public void addEntry() {
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 in ou=users , ou=system");
listEntries(context);
System.out.println("Adding new user..");
addUser(context);
System.out.println("New list of users...");
listEntries(context);
context.close();

} catch (NamingException e) {

e.printStackTrace();
}

}
public void addUser(DirContext context) {
Attributes attributes = new BasicAttributes();
Attribute attribute = new BasicAttribute("objectClass");
attribute.add("inetOrgPerson");
attributes.put(attribute);
Attribute sn = new BasicAttribute("sn");
Attribute cn = new BasicAttribute("cn");
sn.add("Karthik");
cn.add("users");
attributes.put(sn);
attributes.put(cn);
attributes.put("telephoneNumber", "777777777");
try {
context.createSubcontext(
"employeeNumber= 333333,ou=users,ou=system", attributes);
} 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) {
AddLDAPSample sample = new AddLDAPSample();
sample.addEntry();
}
}

Compile and run the code.Now lets see the output. Before running the code ,I had one user record created  before . So after running the above application there will be two user records.

Output

Existing users in  ou=users , ou=system

Name = Bijoy

Employee Number = 112233

Phone Number = 99999999999

Adding new user..

New list of users…

Name = Bijoy

Employee Number = 112233

Phone Number = 99999999999

Name = users

Employee Number = 333333

Phone Number = 777777777

So new user has been successfully added.

See also:

JNDI overview

Naming Service example using JBossNS.

Accessing LDAP using JNDI

Update attributes of LDAP entries using JNDI

Add attribute to LDAP  entries using JNDI

Delete attribute from existing LDAP entry using JNDI

Displaying user entries in LDAP using JNDI

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">