FeaturesPluginsDocs & SupportCommunityPartners

Using JavaServer Faces Component Library to Access a LDAP Server

Contributed by Alexandre Iline

This tutorial shows you how to create a Visual Web JSF project that accesses a LDAP server. Using a LDAP JavaServer Faces (JSF) component library, which contains a required set of nonvisual components, makes it possible to access a LDAP server. It provides instructions for downloading the LDAP component library and shows you how to create and run a sample application that relies on these LDAP components. You'll also see how everything works.

Note: When you see the term "LDAP component library" it is used as a synonym for a "JSF component library."

Contents

Content on this page applies to NetBeans IDE 6.0

External code used in this tutorial.

To follow this tutorial, you need the following software and resources.

Software or Resource Version Required
NetBeans IDE Web & Java EE version 6.0
Java Developer Kit (JDK) Version 6 or
version 5
JavaServer Faces Components/
Java EE Platform
1.2 with Java EE 5* or
1.1 with J2EE 1.4
GlassFish Application Server V2
Travel Database Not required

* To take advantage of NetBeans IDE's Java EE 5 capabilities, use an application server that is fully compliant with the Java EE 5 specification, such as the GlassFish Application Server V2 UR2. If you are using a different server, consult the Release Notes and FAQs for known problems and workarounds. For detailed information about the supported servers and Java EE platform, see the Release Notes.

Before you use this tutorial, you must have installed NetBeans IDE 6.0 with Web functionality (either the Web & Java EE or Full installation) on your system, available at the NetBeans IDE 6.0 Download page.

Introduction

This tutorial illustrates how to implement a web application for requesting and diplaying data from a LDAP server. It relies on two Visual Web JSF application projects created in NetBeans 6.0. The projects use the Visual Web JSF data binding functions to connect visual components to data that data providers deliver from the LDAP server.

The LDAP browser project is a web application that holds an implementation of a data provider (LDAPTableDataProvider) as an internal Java class. In addition to data binding, the application demonstrates how to pass a request to the server and how to specify the set of attributes to be recieved from the server. The application uses JavaServer Faces components to set and display the results.

The LDAP component library project is a JavaServer Faces component library that provides a set of components for use in a NetBeans 6.0 web project. The library provides four nonvisual components—LDAPConnection, LDAPQuery, LDAPTableDataProvider, and LDAPDataProvider—which together give a web application developer flexible access to data on the LDAP server.

The LDAP component library can be installed into Netbeans and used for any Visual Web JSF application. When added to a project, the components appear in the LDAP section of the Palette window in the IDE. The user can drop LDAP components from the Palette onto pages in the Design window, define properties of these components, and bind a JavaServer Faces Table component to the LDAP data provider component LDAPTableDataPropvider.

References

For further information, see the following:

Obtaining a LDAP Component Library and Source Files

You can get a LDAP component library and the source files for the component library and the browser via the following links.

  • Use the link LDAP component library to download this prepared .complib-file: LDAP_DataProviders_jdk1.5.complib.
  • Get the source files by downloading the zip file Project_LDAPComponentLibrary_sources.zip.
  • Get the browser source files from the zip file Project_LDAPBrowser_sources.zip.

Setting Up a Sample Application to Use LDAP Components

Start NetBeans IDE 6.0. Select main menu item Tools > Component Libraries and, in the dialog that opens, click the button Import...
In the Import Component Library dialog, click the button Browse... Then, in the file chooser select the stored file LDAP_DataProviders_jdk1.5.complib.

Import Component Library Dialog Box

After you select the LDAP_DataProviders_jdk1.5.complib, the palette category LDAP Data Providers appears in the Import Component Library dialog. This palette category indicates the category within the Palette window where all LDAP components are placed. Click OK.

Component Library Palette Categories

The Component Libraries dialog displays the structure of the LDAP component library. The Component List pane displays the components in the library. Click Close to continue. Component List

Now, create a new Visual Web JSF project and call it LDAPSampleApp. Then, from the Projects window, right click the tree node Component Libraries and select Add Component Library... from the context menu.

Add Component Library

In the Add Component Library dialog, select the list item LDAP Data Providers (1.0) and click the Add Component Library button.

Add the LDAP Library

The LDAP component library is added. In the Projects window, you should see a subnode LDAP Data Providers (1.0) within the Component Libraries node. Also, the Palette now includes the LDAP Data Providers category and its four LDAP components.

LDAP Component Library and LDAP Components

Now, drag and drop the component LDAP Connection on Page1 in the Design window. Although the Page1 display in the Design window remains empty, because all LDAP components are nonvisual, you can see a new subnode ldapConnection1 appears in the window Navigator under the node Page1.

Notice that the Properties windows displays two properties of ldapConnection1. The property url, which is the URL to an LDAP directory server, has to be defined correctly. By default, the value x500.bund.de is assigned to this property.

LDAP Component Properties

You will probably need to change the URL to the LDAP server. To change the LDAP server URL or port values, click the ellipsis button next to the property. This invokes the custom editor.

LDAP URL Custom Editor

From the Palette, drag the component LDAP Search Query and drop it on Page1 in the Design window. Notice that a new subnode ldapQuery1 appears in the Navigator window beneath Page1. Click the subnode ldapQuery1 and examine its properties in the Properties window. The property connection must be set correctly. By default, it is assigned to an existing ldapConnection1. For the connection property, use the drop-down list to change its value.

You may need to set other properties for ldapQuery1. Click the ellipsis button to invoke the custom editor and define a value for the searchContext property. You can define the required search context by selecting an appropriate tree node. For example, if you choose the root tree node o=Bund,c=DE (the base Naming context always shows as o=Bund,c=DE), the search context will be o=Bund,c=DE.

Define searchContext Property

Similarly, invoke the custom editor to define a value of the property resultAttributes. You can define the required set of result attributes by moving selected items from the Available attributes list to the Selected attributes list.

Define the resultAttributes Property

If you need to restrict data that is delivered by the query, you should assign an appropriate value to the property searchFilterExpression. To do so, you merely type an expression (for example, cn=B*) in the values column for this property. When you finish with these changes, the ldapQuery1 properties are as follows:

ldapQuery1 Properties

Now, drop the component LDAP Table DataProvider on Page1 in the Design window. The Navigator window displays a new subnode ldapTableDataProvider1 beneath Page1. Click the subnode ldapTableDataProvider1 and check its properties in the Properties window. The query property must be set to a correct value, which you choose from a drop-down list: At this point, the only value available is ldapQuery1.

Set the query Property

Drop a Table component (found in the Palette window Basic category) on Page1. Right click the Table component and select Bind to Data... from the context menu. Using the drop-down list for the Get Data From field, change the value from defaultTableDataProvider to ldapTableDataProvider1.

Figure 13: Bind Table Component to ldapTableDataProvider1

The Selected list changes to display the names of LDAP query result attributes as names of table columns.

LDAP Query Result Attributes

Running the Project

The NetBeans IDE 6.0 Standard and Full installers include the Sun Java System Application Server 9.1 technology, which is a version of the Glassfish application server. You can deploy the project to this server in NetBeans IDE 6.0.
Click the Run > Run Main Project menu option or press the F6 key. When deployment of the application LDAPSampleApp finishes, the appropriate web page displays in a browser:

Page1 Displayed in the Browser

How It Works

You can see the library source code, or rebuild it, using the link Project_LDAPComponentLibrary_sources.zip">LDAP component library sources to download an appropriate .zip file. The zip file contains the LDAP component library project. Extract the project from the downloaded zip fileand open it using NetBeans IDE 6.0.

Reference Problem Warning

Most likely, you will see a message that this project has reference problems. (The project contains a reference to a designtime.jar file, but at this point the file cannot be found.) Close the warning dialog, and in the Projects window, right click the node LDAP Data Providers and select the Resolve Reference Problems... from the context menu.

In the Resolve Reference Problems dialog, click Resolve... and, in the file chooser, select the file <NetBeans_IDE-6.0_installation_dir>/visualweb1/modules/ext/designtime.jar.

Resolve Reference Problems Dialog Box

After resolving the reference, build the project. The LDAP component library will be placed in the directory ldap_complib/complib/LDAP_DataProviders.complib.

Summary

Creating a NetBeans 6.0 Visual Web JSF project, which uses a JavaServer Faces component library for accessing a LDAP server, consists of the following steps:

  1. Create a new project in the NetBeans 6.0 IDE.
  2. Add a required JavaServer Faces component library to the project.
  3. Add three nonvisual LDAP components to the project and properly customize their property values.
  4. Put a visual JSF component Table on the page in the Design window, then bind the Table to an instance of LDAPTableDataProvider.
  5. Deploy the project.

See Also


This page was last modified: April 15, 2008


Bookmark this page

del.icio.us furl simpy slashdot technorati digg
Companion
Projects:
MySQL Database Server   Open JDK: an Open SourceJDK   GlassFish Community: an Open Source Application Server    Mobile & Embedded Community    Open Solaris   java.net - The Source for Java Technology Collaboration   Open ESB - The Open Enterprise Service Bus Powered by