Java Reference
In-Depth Information
To actually bind to the LDAP server you need to create a
Context
based on these criteria.
Because you are dealing with some sort of directory server, more specifically an LDAP server,
you use a more specific
Context
called a
DirContext
. The
DirContext
includes some methods
and functionality specifically suited to binding to and manipulating a directory server. Here is
how you create a new
Context
to bind to your LDAP server:
DirContext ctx = new InitialDirContext(env);
The
Hashtable
that was generated in the
getEnvironment()
method is what is passed into the
constructor for the
InitialDirContext
.
You now have a
Context
that is properly bound to the LDAP server. It is ready to perform
many different functions on the data stored in the server.
Searching the LDAP Server
Searching for information in an LDAP server is probably the most important function because
this is what LDAP servers are made for—storing a lot of information that is searched often.
To search an LDAP server you need to know where in the directory tree you would like to
begin the search. You need to know how far down the tree you are going to search from there.
Finally you need to know the criteria you are searching on.
The search base can be any valid point in the directory tree, and you specify this location using
its DN. You can begin your search at the base of the tree by specifying
o=virtuas.com
as your
search base. You can also begin the search in the
People
branch by specifying
ou=People,o=virtuas.com
instead.
From this beginning point, there are three different depths for the search:
•
OBJECT_SCOPE
—The shallowest search; searches just the named object.
•
ONELEVEL_SCOPE
—Searches only objects that exist one level directly below the named
point in the tree.
•
SUBTREE_SCOPE
—Searches all objects below the starting point. This is the default that
you specify in your
LDAPManager
.
After you have decided how the search will be accomplished, you must specify the criteria you
want to use to select the objects. Naming the criteria is done by using a string to filter the
objects. For example, you could search for all objects where
uid=jgoodwill
.
The necessary attributes and method used to search your LDAP server look like this:
protected String searchBase = null;
protected int searchScope = SearchControls.SUBTREE_SCOPE;