Hosted By

Contact Me

Misc Links

OpenNTF BlogSphere LotusGeek CoComment Custom Button

Ads by Google

Welcome to keithstric.com!

I hope you find this site useful in some way or another. I strive to bring you all sorts of geeky information and solutions to your most frustrating of issues with the occasional rant on whatever topic, technical reviews and weblog. You'll also find many products that I've developed and make available for you to use however you like. So, grab a cup of coffee, sit down and visit for a while.

« lsxldap bug.... | Home Page| Comcast High Speed Internet... »

lsxldap bug and Java Agent

08/01/2007 9:23 AM By Keith Strickland

OK, I posted earlier about a bug I've encountered with lsxldap. I never did find a way to get lsxldap to login and I've never gotten around to trying it on my home system. So, I broke down and wrote a Java agent to do my lookup. I gathered code from several sources and modified it and got it working. The biggest problem I encountered was trying to catch an InvalidAttributeValueException within a try/catch block. But instead of me rambling on with this as some of it I have no clue what it really does, here's the code I ended up with, hopefully someone will find a use for this...


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

public class JavaAgent extends AgentBase {

   public void NotesMain() {

      try {
         Database thisDB;
         Document thisDoc, proDoc, nthisDoc;
         View thisView;
         int i, e;
         Session session = getSession();
         AgentContext agentContext = session.getAgentContext();

         thisDB = agentContext.getCurrentDatabase();
         thisView = thisDB.getView("(luUpdateUN)");
         thisView.setAutoUpdate(false);
         proDoc = thisDB.getProfileDocument("SystemProfile","");

         String ldapBaseDN = proDoc.getItemValueString("sysLDAPSearchBaseDN");

         //Setup the LDAP parameters and login credentials
         System.out.println("Establishing LDAP credentials");
         Hashtable env = new Hashtable(4);
         env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
         env.put(Context.PROVIDER_URL, "ldap://" + proDoc.getItemValueString("sysLDAPServer"));
         env.put(Context.SECURITY_AUTHENTICATION, "simple");
         env.put(Context.SECURITY_PRINCIPAL, proDoc.getItemValueString("sysLDAPLoginName"));
         env.put(Context.SECURITY_CREDENTIALS, proDoc.getItemValueString("sysLDAPPassword"));

         //Connect to LDAP
         System.out.println("Connecting to LDAP");
         DirContext ctx = new InitialDirContext(env);
         System.out.println("Setting LDAP Search Parameters");
         SearchControls searchCtls = new SearchControls();
         String[] returnedAtts={"sAMAccountName"};
         searchCtls.setReturningAttributes(returnedAtts);
         searchCtls.setCountLimit(0);
         searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);

         //Loop through all the documents in the (luUpdteUN) view
         thisDoc = thisView.getFirstDocument();
         i = 0;
         e = 0;
         System.out.println("Looping through documents");
         while (thisDoc != null) {
            String InetAdd = thisDoc.getItemValueString("InternetAddress");
            //Error catching, NamingEnumeration returns InvalidAttributeValueException
            //if something is missing from the LDAP record
            try {
               //If InternetAddress is not blank, lookup the InternetAddress in
               //LDAP Active Directory
               if (InetAdd != ""){
                  String filter = "(userPrincipalName=" + InetAdd + ")";
                  NamingEnumeration answer = ctx.search(ldapBaseDN,filter,searchCtls);
                  boolean goodhit;
                  //Error catching, returns NamingException of answer.hasMore()
                  //is false. Set goodhit accordingly
                  try {
                     goodhit = answer.hasMore();
                  } catch(NamingException ne) {
                     goodhit = false;
                     System.out.println(InetAdd + " not found!");
                  }
                  if (goodhit){ //goodhit = true. Update username and UpdateUN flag
                     i = i + 1;
                     SearchResult result = (SearchResult)answer.next();
                     Attributes ra = result.getAttributes();
                     thisDoc.replaceItemValue("UserName",ra.get("sAMAccountName").toString().substring(16));
                     thisDoc.replaceItemValue("UpdateUN","False");
                  }else{  //goodhit = false. Update the UpdateUN flag as an error
                     e = e + 1;
                     thisDoc.replaceItemValue("UpdateUN","Error");
                  } //End If
                  thisDoc.computeWithForm(false,false);
                  thisDoc.save();
               } else {
                  thisDoc.replaceItemValue("UpdateUN","False");
                  thisDoc.computeWithForm(false,false);
                  thisDoc.save();
               } //End If
            } catch(InvalidAttributeValueException ia) {
               System.out.println("InvalidAttributeValueException on '" + thisDoc.getItemValueString("FirstName") + " " + thisDoc.getItemValueString("LastName") + "'. Skipping...");
               e = e + 1;
               thisDoc.replaceItemValue("UpdateUN","Error");
               thisDoc.computeWithForm(false,false);
               thisDoc.save();
            } finally {
               nthisDoc = thisView.getNextDocument(thisDoc);
               thisDoc.recycle();
               thisDoc = nthisDoc;
            }
         } //End While
         System.out.println("Completed document loop. Updated " + Integer.toString(i) + " usernames. Didn't find " + Integer.toString(e) + " users in LDAP. Closing LDAP Connection");
         ctx.close();
      } catch(Exception e) {
         e.printStackTrace();
      }
   }
}


Post A Comment

:-D:-o:-p:-(:-):-\:-|:angry::cool::cry::dontknow::emb::hairout::laugh::rolleyes::whew:;-)

Subscribe to keithstric.com

OpenNTF

Disclaimer

The opinions and ideas posted on keithstric.com are not necessarily the opinions and ideas of my employer. The solutions, techniques and code provided here are not guaranteed or warranted in any way and are free for you to use at your own risk.