Developing A White Pages Service with LDAP and JNDI

Developing A White Pages Service with LDAP and JNDI

Developing a White Pages Service

A white pages service for locating a person in an LDAP server. As mentioned previously, I use the LDAP server from OpenLDAP. In order to keep the project simple, I use the person object defined in the core.schema file.

For convenience, the person object in the core.schema file is re-presented here.

objectclass ( 2.5.6.6 NAME 'person' SUP top STRUCTURAL
  MUST ( sn $ cn )
  MAY ( userPassword $ telephoneNumber $ seeAlso $ description )
)

The person object has two mandatory attributes: sn and cn, and four optional attributes:

  • userPassword
  • telephoneNumber
  • seeAlso
  • description

Adding Some Entries

To test the code in this project, you need to populate the directory:

ldapadd -x -D "cn=Manager ,dc=sendal,dc=jepit,dc=edu,dc=au" -w 

secret -f example.ldif

This reads the example.ldif file and insert its content as entries to the server. The example.ldif file contains the following.

dn: cn=Bulbul, dc=sedal,dc=usyd,dc=edu,dc=au

objectclass: person

cn: Bulbul Kurniawan

sn: Kurniawan

userPassword: secret

telephoneNumber: +61 98371313



dn: cn=boni, dc=sedal,dc=usyd,dc=edu,dc=au

objectclass: person

cn: Boni Milliken

sn: Milliken

userPassword: dog

telephoneNumber: +61 9555 1212



dn: cn=boy, dc=sedal,dc=usyd,dc=edu,dc=au

objectclass: person

cn: Boy Milliken

sn: Milliken

userPassword: taboo

telephoneNumber: +61 98989898

Make sure that you have installed the correct service provider and your CLASSPATH variable contains the path to the JNDI packages.

The Code

The code for the white pages service is given in Listing 1. The Java code allows you to access the LDAP server and search a person or persons by passing a surname. The code starts by preparing a environment Hashtable object and setting the necessary properties for the environment.

Hashtable env = new Hashtable();



env.put(Context.INITIAL_CONTEXT_FACTORY,

  "com.sun.jndi.ldap.LdapCtxFactory");

env.put(Context.PROVIDER_URL, 

  "ldap://sendal.jepit.edu.au:389");

And then, as explained above, you need a DirContext object as the initial context, which is done by calling the InitialDirContext constructor, passing the environment Hashtable.

DirContext ctx = new InitialDirContext(env);

Once you have a DirContext object, you can use it to access the LDAP service. To start searching, use the search method by passing a SearchControls object.

SearchControls constraints = new SearchControls();

constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);

NamingEnumeration persons =

  ctx.search("dc=sendal,dc=jepit,dc=edu,dc=au",

        "(objectclass=person)", constraints);

Then, display the search result, i.e., the attributes of all the person objects that match the search criteria.

For each person object found, you use the getAttributes method to retrieve the object's attributes. This method returns the Attributes object. You can then use the get method of the Attributes object to obtain the value of an attribute by passing the attribute name.

attributes.get( attributeName );

The part of the code that displays the attribute names of the person objects found is given below.

System.out.println("Distinguished Name \t| " +

  "Common Name \t| Surname \t| Phone");



while (persons != null && persons.hasMore()) {

  SearchResult sr = (SearchResult) persons.next();  

  System.out.print( sr.getName() + "\t| ");  // distinguised name

  Attributes attrs = sr.getAttributes();

  attrs.put(new BasicAttribute("sn", searchedSurname));

  // attrs.put(new BasicAttribute("cn", "boy"));

  System.out.print(attrs.get("cn") + "\t| "); // common name

  System.out.print(attrs.get("sn") + "\t| "); // surname

  System.out.println(attrs.get("telephoneNumber")); // phone



} // end of while

If you run the code in Listing 1, you can see the result that looks something like the following.

Distinguished Name  |  Common Name  |  Surname  |  Phone

cn=Boni Milliken  |cn: boy  |sn: Milliken  | +61 9555 1212
cn=Boy Milliken  |cn: boy  |sn: Milliken  | +61 98989898

Summary

Naming and directory services are important, providing a way to find objects based on their name or other attributes. A directory service is an extension of a naming service in which object has various attributes. So you can you look up an object by its name, and you can get the object's attributes or search for the object based on its attributes. Using a directory service such as an LDAP server, you can create many applications, including the white pages service described above.

Budi Kurniawan is a senior J2EE architect and author.


Return to ONJava.com.

Prev  [1] [2] [3] [4] [5] [6] [7] 

Close    To Top
  • Prev Article-Java:
  • Next Article-Java:
  • Now: Tutorial for Web and Software Design > Java > Java EE > Java Content
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Geek Tutorial
     

    Blogging Tutorial

      RSS Tutorial
      Podcasting Tutorial
    Graphic Design Tutorial
      Coreldraw Tutorial
      Illustrator Tutorial
      3D Tutorials
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial/ Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial/ Articles
     

    XML Style

      AJAX Tutorial
      XML Mobile
    Flash Tutorial/ Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial/ Articles
      Linux Tutorial
      Symbian Tutorial
      MacOS Tutorial
    Personal Tech
      Hardware Tutorial
      Software Tutorial
      Online Auction