Archives for 

JNDI

This category is for JNDI tutorial

Displaying user entries in LDAP using JNDI

In Java applications it is often needed to  fetch the LDAP entries. In this chapter we are discussing the way by which a Java application is displaying user entries in LDAP using JNDI . In previous chapters we have seen how to add a user to LDAP using Apache Directory Studio.Also we have seen how to add a user  from Java application.So we have few user entries in our directory. The Java code shown below  fetches the user entries  from LDAP and displays them in console.

Displaying user entries in LDAP using JNDI

ListEntries.java

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 ListEntries {
public ListEntries() {

}

public void getEntries() {
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);
String searchFilter = "(objectClass=inetOrgPerson)";
String[] requiredAttributes = { "employeeNumber", "cn",
"telephoneNumber" };

SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
controls.setReturningAttributes(requiredAttributes);

NamingEnumeration 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) {
ListEntries sample = new ListEntries();
sample.getEntries();
}

}

The application fetches users from ‘ou=users,ou=system’.Now let us see the output.

Output

Name = Bijoy

Employee Number = 112233

Phone Number = 99999999999

Name = Karthik

Employee Number = 333333

Phone Number = 777777777

See also:

JNDI overview

Naming service  example using JBossNS service provider

Accessing LDAP using JNDI

Updating attribute of LDAP entry using JNDI

Deleting attribute of an LDAP entry

Adding attribute to an LDAP entry

Adding LDAP entry using JNDI

Deleting LDAP entry using JNDI