IBM®
Skip to main content
    Country/region select      Terms of use
 
 
   
     Home      Products      Services & solutions      Support & downloads      My account     
 
developerWorks
AIX and UNIX
Information Mgmt
Lotus
New to Lotus
Products
How to buy
Downloads
Live demos
Technical library
Training
Support
Forums & community
Events
Rational
Tivoli
WebSphere
Java™ technology
Linux
Open source
SOA and Web services
Web development
XML
My developerWorks
About dW
Submit content
Feedback



developerWorks  >  Lotus  >  Technical Library
developerWorks



[Back to "Building real-time access to an LDAP directory server from your Notes application."]

The LDAPSearchWithFilter agent code
The following is the complete code example for the LDAPSearchWithFilter agent.

import lotus.domino.*;
import javax.naming.*;
import javax.naming.directory.*;
import java.util.Hashtable;
import java.util.Vector;

public class LDAPSearchWithFilter extends AgentBase {
    public void NotesMain() {
      try {
      Database _db;
      Document _doc;
      Session session = getSession();
      AgentContext agentContext = session.getAgentContext();
      _db = agentContext.getCurrentDatabase();

      // Used for running agent on server with docID being passed from the calling action
      Agent ag1 = agentContext.getCurrentAgent();
      String paramid = ag1.getParameterDocID();
      Document doc = _db.getDocumentByID(paramid);

      String searchCN = doc.getItemValueString("SearchCN");
      String searchAttr = doc.getItemValueString("SearchAttr");

      // Set up the environment for creating the initial context
      String ldapCF = "com.sun.jndi.ldap.LdapCtxFactory";
      String ldapURL = "ldap://myldap.mycompany.com:389/";
      String ldapBaseDN = "ou=myou, o=myorg, c=us";
      String ldapUserID = "cn=Administrator";
      String ldapPassword = "thepassword";

      Hashtable env = new Hashtable(4);
      env.put(Context.INITIAL_CONTEXT_FACTORY, ldapCF);
      env.put(Context.PROVIDER_URL, ldapURL + ldapBaseDN);
      env.put(Context.SECURITY_PRINCIPAL, ldapUserID);
      env.put(Context.SECURITY_CREDENTIALS, ldapPassword);
      try {
      // Create initial context
      DirContext ctx = new InitialDirContext(env);

      String[] attrIDs = {searchAttr};
      SearchControls ctls = new SearchControls();
      ctls.setReturningAttributes(attrIDs);

// Specify the search filter
String filter = "(|(cn="+searchCN+")(uid="+searchCN+"))";

// Search for objects using the above filter
NamingEnumeration answer = ctx.search("", filter, ctls);

findCN(answer, doc);
      // Close the context when we're done
      ctx.close();
      } catch(NamingException e) {
      e.printStackTrace();
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
} // end of NotesMain

public static void findCN(NamingEnumeration enum, Document doc) {
    try {
      if (enum.hasMore()) {
      while (enum.hasMore()) {
        SearchResult sr = (SearchResult)enum.next();
        System.out.println(">>>" + sr.getName());
        findAttrs(sr.getAttributes(), doc);
      }
} else {
      Item item = doc.getFirstItem("HiddenSearchResults");
      doc.replaceItemValue("HiddenSearchResults", "CN not found");
      doc.save(true);
}
    } catch (NamingException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    }
} // end of findCN

public static void findAttrs(Attributes attrs, Document doc) {
    try {
      Item item = doc.getFirstItem("HiddenSearchResults");
      Vector v = new Vector();
      String result;
      if (attrs.size() == 0) {
      v.addElement("Name found but attribute is blank or not found. Try again ...");
      } else {
      /* Get each attribute */
      try {
        for (NamingEnumeration ae = attrs.getAll(); ae.hasMore();) {
          Attribute attr = (Attribute)ae.next();
          /* Get each value */
          for (NamingEnumeration e = attr.getAll(); e.hasMore();) {
            result = (String)e.next();
            v.addElement(result);
          }
        }
      } catch (NamingException e) {
        e.printStackTrace();
      }
    } // end if
      doc.replaceItemValue("HiddenSearchResults", v);
      doc.save(true);
    } catch (Exception e) {
      e.printStackTrace();
}
    }
} // end of findAttrs

The Search LDAP button code
The following is the complete code example for the Search LDAP button.

Sub Click(Source As Button)
    Dim s As New NotesSession
    Dim ws As New NotesUIWorkspace
    Dim db As NotesDatabase
    Dim agent As NotesAgent
    Dim doc As NotesDocument
    Dim uidoc As NotesUIDocument
    Dim searchResultItem As NotesItem
    Dim paramid As String

    Set db = s.CurrentDatabase
    Set uidoc = ws.CurrentDocument
    Set doc = uidoc.Document
    Set agent = db.GetAgent("LDAPSearchWithFilter")

    Call doc.save(True, False)
    paramid = doc.NoteID

    Call agent.RunOnServer(paramid)

    Delete doc

    Set doc = db.GetDocumentByID(paramid) ‘ retrieve the back-end document

    'Get the updated field
    Set searchResultItem = doc.getFirstItem("HiddenSearchResults")
    Forall values In searchResultItem.Values
      Call uidoc.FieldAppendText("SearchResults", values)
      ‘ Add new line, for results that have multi values
      Call uidoc.FieldAppendText("SearchResults", Chr(10))
    End Forall
    Call uidoc.Refresh
    doc.Remove(True) ‘you do not want to keep the back-end doc
End Sub

The Entering event code for the SearchCN field
The following is the code example for the Entering event of the SearchCN field.
    Dim ws As New NotesUIWorkspace
    Dim uidoc As NotesUIDocument
    Set uidoc = ws.CurrentDocument

    Call uidoc.FieldSetText("SearchCN", "")
    Call uidoc.FieldSetText("SearchAttr", "")
    Call uidoc.FieldSetText("SearchResults", "")
    Call uidoc.FieldSetText("SearchResultsGroup", "")
    Call uidoc.FieldSetText("HiddenSearchResults", "")
    If uidoc.FieldGetText("SearchType") = "Group" Then
      Call uidoc.FieldSetText("SearchAttr", "member")
    End If
    Call uidoc.Refresh

The Exiting event code for the SearchCN field
The following is the code example for the Exiting event of the SearchCN field. The code is similar to the code for the Search LDAP button, but with additional string processing as follows:

'Get the updated field
    Set searchResultItem = doc.getFirstItem("HiddenSearchResults")
    Forall values In searchResultItem.Values
      pos1 = Instr(Cstr(values), "=")
      pos2 = Instr(Cstr(values), ",")
      stringValue =Mid(Cstr(values), pos1+1, pos2-pos1)
      Call uidoc.FieldAppendText("HiddenSearchResults", stringValue)
    End Forall




    About IBM Privacy Contact