Application 4. Searching for stored data
Start with a simple search on the ApacheDS. Suppose there are many users in the ApacheDS. And you want to find all the details of the user Alice. All known things about Alice are listed below:
Alice is a user, so you should be able to find her data entry in the user's data organization unit. (The concept of an organizational unit or "OU" has been introduced in part 1th.) )
Alice's username is "Alice" (case-insensitive).
Alice is a human being, so the object class used by her data entry must extend the person object class directly or indirectly.
Now, see listing 6, which shows an application named Searchforalice. Searchforalice demonstrates a very simple search scenario, and I'll expand the application to fit the more advanced search scenario later.
Listing 6. Searchforalice
public class Searchforalice {
public Searchforalice () {
Try
{
//------------------------------------------
//step1:setting up JNDI properties for ApacheDS
//------------------------------------------
InputStream InputStream = new FileInputStream ("Apacheds.properties");
Properties Properties = new properties ();
properties.load (InputStream);
properties.setproperty ("Java.naming.security.credentials", "secret");
//------------------------------------------
//Step2:fetching a DirContext object
//------------------------------------------
DirContext ctx = new InitialDirContext (properties);
//---------------------------------------------
//step3:setting Search Context
//---------------------------------------------
String searchcontext = "Ou=users";
//--------------------------------------------
//step4:creating Search attributes for Alice
//--------------------------------------------
Attribute uid = new BasicAttribute ("UID");
Attribute objclass = new BasicAttribute ("objectclass");
//adding Attribute Values
Uid.add ("Alice");
objclass.add ("person");
//instantiate Attributes object and put search Attributes in it.
Attributes attrs = new Basicattributes (true);
Attrs.put (UID);
Attrs.put (objclass);
//------------------------------------------
//step5:executing Search
//------------------------------------------
namingenumeration ne = ctx.search (searchcontext, attrs);
if (NE!= null)
{
//step 6:iterating through SearchResults
while (Ne.hasmore ()) {
//step 7:getting Individual SearchResult object
SearchResult sr = (SearchResult) ne.next ();
//step 8:
String Entryrdn = Sr.getname ();
System.out.println ("RDN of the searched entry:" +entryrdn);
//step 9:
Attributes srattrs = Sr.getattributes ();
if (srattrs!= null) {
//step 10:
for (Enumeration e = Attrs.getall (); e.hasmoreelements ();)
{
attribute attr = (attribute) e.nextelement ();
//step 11:
String Attrid = Attr.getid ();
System.out.println ("Attribute Name:" +attrid);
System.out.println ("Attribute Value (s):");
Namingenumeration e1 = Attr.getall ();
while (E1.hasmore ())
System.out.println ("\t\t" +e1.nextelement ());
}//for ()
}//if (srattrs)
}
}//if (NE!= null)
} catch (Exception e) {
System.out.println ("Operation failed:" + e);
}
}
public static void Main (string[] args) {
Searchforalice searchalice = new Searchforalice ();
}
}