Complete Contents
Preface
Chapter 1 Understanding LDAP
Chapter 2 Using the Netscape Directory SDK for Java
Chapter 3 Quick Start
Chapter 4 Writing an LDAP Client
Chapter 5 Using the LDAP Java Classes
Chapter 6 Searching the Directory
Chapter 7 Using Filter Configuration Files
Chapter 8 Adding, Updating, and Deleting Entries
Chapter 9 Comparing Values in Entries
Chapter 10 Working with LDAP URLs
Chapter 11 Getting Server Information
Chapter 12 Connecting Over SSL
Chapter 13 Working with LDAP Controls
Chapter 14 Using SASL Authentication
Chapter 15 Using Netscape's JNDI Service Provider
Chapter 16 Working with Extended Operations
Chapter 17 Using the Asynchronous Interface
Glossary
Directory SDK for Java 4.0 Programmer's Guide: Using Filter Configuration Files
Previous Next Contents Index


Chapter 7 Using Filter Configuration Files

This chapter explains how to use API function to work with filter configuration files. Filter configuration files can help simplify the process of selecting the appropriate search filter for a search request.

The chapter contains the following sections:

Note that the LDAP filter classes use the OROMatcher regular expression package (from ORO Java Software), which is provided with the SDK. If you want to use the OROMatcher package separately (not through the Netscape Directory SDK for Java classes), you must obtain a license to use the OROMatcher package from ORO Java Software. (You can also obtain the OROMatcher documentation directly from ORO.)

For details, see the ORO home page at http://www.oroinc.com/.


Understanding Filter Configuration Files
Suppose that you are writing a client that allows users to search the directory. You might want to use different search filters tailored for specific types of search criteria. For example, suppose the user wants to search for this:

bjensen@airius.com
You might want to use this search filter:

(mail=bjensen@airius.com)
Similarly, suppose the search term entered by the user contains numbers, like this:

555-1212
In this case, you might want to use this search filter:

(telephoneNumber=555-1212)
Rather than write code to explicitly create the appropriate filter (based on the user's search criteria), you can include the filters in a filter configuration file.

A filter configuration file contains a list of filters that you can load and use in your searches.


Understanding the Configuration File Syntax
A filter configuration file has the following format:

tag
     pattern1     delimiters     filter1-1     desc1-1     [scope1]
                                 filter1-2     desc1-2     [scope2]

     pattern2     delimiters     filter2-1     desc2-1     [scope3]
     ...
This format is explained below:

For example, the following section of a filter configuration file specifies a filter for telephone numbers and two filters for email addresses. The telephone number filter is used if the search criteria contains one or more numbers. The email filters are used if the search criteria contains an at sign (@).

"people"

     "^[0-9][0-9-]*$"     " "     "(telephoneNumber=*%v))"     "phone number ends with"

     "@"                  " "     "(mail=%v)"                  "email address is"

                                  "(mail=%v*)"                 "email address starts with"

You should specify the filters in the order that you want them to be used. For example, if you want to apply the (mail=%v) filter before the (mail=%v*) filter, make sure that the filters appear in that order.


Understanding Filter Parameters
Within a filter, you can use the following parameters:


Loading a Filter Configuration File
To use a filter configuration file, you need to create an LDAPFilterDescriptor object. The LDAPFilterDescriptor constructor allows you to read in the filter configuration file from:

The following section of code reads in a filter configuration file named ldapfilter.conf in the current directory.

...
import netscape.ldap.util.*;
...
LDAPFilterDescriptor filtdesc = null;
try {
   /* Read in the filter configuration file. */
   filtdesc = new LDAPFilterDescriptor( "ldapfilter.conf" );
} catch ( Exception e ) {
   System.out.println( "Error: " + e.toString() );
}
...

Retrieving Filters
After loading a filter configuration file into memory, you can retrieve filters based on the search criteria. For example, if the search criteria is an e-mail address (bjensen@airius.com), you can have your client automatically search for this value in the mail attribute.

To use the filter configuration file, do the following:

  1. Invoke the LDAPFilterDescriptor constructor to read in the filter configuration file.
  2. Invoke the getFilters method of the LDAPFilterDescriptor object.

  1. Invoke the next method of the LDAPFilterList object to iterate through the LDAPFilter objects.
  2. For each LDAPFilter object, get the filter by invoking the getFilter method, passing no arguments.
Note that you do not need to invoke the setupFilter of the LDAPFilter object to generate the filter. The getFilters method of the LDAPFilterDescriptor object does this already. You just need to invoke the getFilter method of the LDAPFilter object to get the generated filter.

To get the total number of filter configuration lines that match the specified search term, invoke the numFilters method of the LDAPFilterList object. Note that this number decrements each time you invoke the next or nextElement method.

The following section of code uses a filter configuration file containing the following filters:

"people"

     "^[0-9][0-9-]*$"     " "     "(telephoneNumber=*%v))"     "phone number ends with"

     "@"                  " "     "(mail=%v)"                  "email address is"

                                  "(mail=%v*)"                 "email address starts with"

The following section of code retrieves, generates, and prints filters that match the search criteria.

...
import netscape.ldap.util.*;
import java.util.*;

...
String searchTerm = "bjensen@airius.com";
LDAPFilterDescriptor filtdesc = null;
try {
   /* Read in the filter configuration file. */
   filtdesc = new LDAPFilterDescriptor( "ldapfilt.conf" );

   /* Get filters from the section "people" for the search term. */
   LDAPFilterList filtlist = null;
   try {
      filtlist = filtdesc.getFilters( "people", searchTerm );
   } catch ( Exception e ) {
      System.out.println( "No matching tag section or filter found." );
      System.exit(0);
   }

   int totalFilters = filtlist.numFilters();
   System.out.println( "Found " + totalFilters +
      " applicable filters.\n" );

   /* Iterate through the lines in the list. */
   while ( filtlist.hasMoreElements() ) {
      LDAPFilter filtline = filtlist.next();
      System.out.println( "Filter #" + ( totalFilters -
         filtlist.numFilters() ) );

      /* Get and print information about the selected line
         of filter configuration information. */
      System.out.println( " Description: " +
         filtline.getDescription() );
      System.out.println( " Line #: " + filtline.getLineNumber() );
      System.out.println( " Matches pattern: " +
         filtline.getMatchPattern() );
      System.out.println( " Filter template: " +
         filtline.getFilterTemplate() );
      System.out.println( " Delimiter: " + filtline.getDelimeter() );
      System.out.println( " Scope: " + filtline.getScope() );

      /* Get the generated filter. */
      String filterString = filtline.getFilter();
      System.out.println( " Generated filter string: " + filterString +
         "\n" );
   }
} catch ( Exception e ) {
   System.out.println( "Error: " + e.toString() );
}
System.exit( 0 );
...
Suppose that the search criteria is bjensen@airius.com. In this case, the code prints the following output:

java GetFilter bjensen@airius.com
Found 2 applicable filters.

Filter #1
Description: email address is
Line #: 3
Matches pattern: @
Filter template: (mail=%v)
Delimiter:
Scope: subtree
Generated filter string: (mail=bjensen@airius.com)

Filter #2
Description: email address starts with
Line #: 4
Matches pattern: @
Filter template: (mail=%v*)
Delimiter:
Scope: subtree
Generated filter string: (mail=bjensen@airius.com*)

Adding Filter Prefixes and Suffixes
If you need to apply a filter to all searches, you can add a filter prefix and suffix to all filters (rather than add the criteria to all filters). For example, if your client searches only for person entries, you can add the following filter prefix to restrict the search:

   (&(l=Sunnyvale)
Note that this filter now requires the following suffix to balance the number of parentheses:

   )
For example, given the following filter:

   (sn=Jensen)
you can use the filter prefix "(&(l=Sunnyvale" and the filter suffix ")" to narrow down the search to only the entries with l=Sunnyvale:

(&(l=Sunnyvale)(sn=Jensen))
You can set up the filter prefix and suffix in several ways:

The rest of this section contains examples that illustrate these different approaches to setting prefixes and suffixes.

Adding Affixes for All Filters
The following section of code loads the filter configuration file named myfilters.conf into memory and adds the prefix "(&(l=Sunnyvale)" and the suffix ")" to each filter that will be retrieved from that file:

...
import netscape.ldap.util.*;
...
LDAPFilterDescriptor filtdesc = null;
try {
   /* Read in the filter configuration file. */
   filtdesc = new LDAPFilterDescriptor( "myfilter.conf" );

   /* Add the specified prefix and suffix to all filters. */
   String prefix = "(&(l=Sunnyvale)";
   String suffix = ")";
   filtdesc.setFilterAffixes( prefix, suffix );

   /* Get filters from the section "people" for the search term. */
   LDAPFilterList filtlist = null;
   try {
      filtlist = filtdesc.getFilters( "people", searchTerm );
   } catch ( Exception e ) {
      System.out.println( "No matching tag section or filter found." );
      System.exit(0);
   }

   /* Iterate through the lines in the list. */
   while ( filtlist.hasMoreElements() ) {
      LDAPFilter filtline = filtlist.next();

      /* Get and print each filter. */
      String filterString = filtline.getFilter();
      System.out.println( " Generated filter string: " + filterString +
         "\n" );
   }
} catch ( Exception e ) {
   System.out.println( "Error: " + e.toString() );
}
...
For example, if the following search term is passed to the LDAPFilterDescriptor.getFilters method:

   bjensen@airius.com
and the corresponding filter (without a prefix or suffix) is:

   (mail=bjensen@airius.com)
the entire generated filter string (retrieved by the LDAPFilter.getFilter method) will be:

   (&(l=Sunnyvale)(mail=bjensen@airius.com))
Adding Affixes By Using setFilterAffixes
The following section of code loads the filter configuration file named myfilters.conf into memory and uses the LDAPFilter.setFilterAffixes method to add a prefix and suffix to a generated filter:

...
import netscape.ldap.util.*;
...
LDAPFilterDescriptor filtdesc = null;
try {
   /* Read in the filter configuration file. */
   filtdesc = new LDAPFilterDescriptor( "myfilter.conf" );

   /* Get filters from the section "people" for the search term. */
   LDAPFilterList filtlist = null;
   try {
      filtlist = filtdesc.getFilters( "people", searchTerm );
   } catch ( Exception e ) {
      System.out.println( "No matching tag section or filter found." );
      System.exit(0);
   }

   /* Iterate through the lines in the list. */
   while ( filtlist.hasMoreElements() ) {
      LDAPFilter filtline = filtlist.next();

      /* Add the prefix and suffix, and generate the filter. */
      String prefix = "(&(l=Sunnyvale)";
      String suffix = ")";
      filtline.setFilterAffixes( prefix, suffix );
      String filterString = filtline.getFilter( searchTerm );
      System.out.println( " Generated filter string: " + filterString +
         "\n" );
   }
} catch ( Exception e ) {
   System.out.println( "Error: " + e.toString() );
}
...
For example, if the following search term is passed to the LDAPFilterDescriptor.getFilters method:

   bjensen@airius.com
and the corresponding filter (without a prefix or suffix) is:

   (mail=bjensen@airius.com)
the entire generated filter string (retrieved by the LDAPFilter.getFilter method) will be:

   (&(l=Sunnyvale)(mail=bjensen@airius.com))
Adding Affixes By Using getFilter
The following section of code loads the filter configuration file named myfilters.conf into memory and uses the LDAPFilter.getFilter method to add a prefix and suffix to a generated filter:

...
import netscape.ldap.util.*;
...
LDAPFilterDescriptor filtdesc = null;
try {
   /* Read in the filter configuration file. */
   filtdesc = new LDAPFilterDescriptor( "myfilter.conf" );

   /* Get filters from the section "people" for the search term. */
   LDAPFilterList filtlist = null;
   try {
      filtlist = filtdesc.getFilters( "people", searchTerm );
   } catch ( Exception e ) {
      System.out.println( "No matching tag section or filter found." );
      System.exit(0);
   }

   /* Iterate through the lines in the list. */
   while ( filtlist.hasMoreElements() ) {
      LDAPFilter filtline = filtlist.next();

      /* Add the prefix and suffix, and generate the filter. */
      String prefix = "(&(l=Sunnyvale)";
      String suffix = ")";
      String filterString = filtline.getFilter( searchTerm, prefix,
         suffix );
      System.out.println( " Generated filter string: " + filterString +
         "\n" );
   }
} catch ( Exception e ) {
   System.out.println( "Error: " + e.toString() );
}
...
For example, if the following search term is passed to the LDAPFilterDescriptor.getFilters method:

   bjensen@airius.com
and the corresponding filter (without a prefix or suffix) is:

   (mail=bjensen@airius.com)
the entire generated filter string (retrieved by the LDAPFilter.getFilter method) will be:

   (&(l=Sunnyvale)(mail=bjensen@airius.com))
Adding Affixes By Using setupFilter
The following section of code loads the filter configuration file named myfilters.conf into memory and uses the LDAPFilter.setupFilter method to add a prefix and suffix to a generated filter:

...
import netscape.ldap.util.*;
...
LDAPFilterDescriptor filtdesc = null;
try {
   /* Read in the filter configuration file. */
   filtdesc = new LDAPFilterDescriptor( "myfilter.conf" );

   /* Get filters from the section "people" for the search term. */
   LDAPFilterList filtlist = null;
   try {
      filtlist = filtdesc.getFilters( "people", searchTerm );
   } catch ( Exception e ) {
      System.out.println( "No matching tag section or filter found." );
      System.exit(0);
   }

   /* Iterate through the lines in the list. */
   while ( filtlist.hasMoreElements() ) {
      LDAPFilter filtline = filtlist.next();

      /* Add the prefix and suffix, and generate the filter. */
      String prefix = "(&(l=Sunnyvale)";
      String suffix = ")";
      filtline.setupFilter( searchTerm, prefix, suffix );
      String filterString = filtline.getFilter();      System.out.println( "  
Generated filter string: " + filterString + 
         "\n" );
   }
} catch ( Exception e ) {
   System.out.println( "Error: " + e.toString() );
}
...
For example, if the following search term is passed to the LDAPFilterDescriptor.getFilters method:

   bjensen@airius.com
and the corresponding filter (without a prefix or suffix) is:

   (mail=bjensen@airius.com)
the entire generated filter string (retrieved by the LDAPFilter.getFilter method) will be:

   (&(l=Sunnyvale)(mail=bjensen@airius.com))

 

© Copyright 1999 Netscape Communications Corporation. All rights reserved.