Puakma: Under the hood

I'm Brendon Upson, jack-of-all-trades, master of one or two. I'm talking about life running a small ISV tackling business issues and leaping technology hurdles in a single bound.

webWise Network Consultants is based in Sydney, Australia and develops the groundbreaking Tornado Server technology.

Making pages on the fly

Filed under: by Brendon Upson on 2006-09-05

From time to time web programmers need to make parts of pages, particularly in this ajaxian age we are now in. Today's task was: On a page, allow a user to search through an LDAP directory (Microsoft ActiveDirectory) to select user names. Since AD could hold thousands of users and we would have to troll through their X500 Names in the list, eg "CN=Brendon Upson/CN=Users/DC=wnc/DC=net/DC=au", just providing a combo box of names was not so good.

The solution was a text field to type a partial name in, some ajax code to call some server-side logic to perform the search, then insert the names into a combo box. I really didn't want to code my own combo box manually - it's possible but a pain - I have better things to do. So instead I use some inbuilt Tornado to do the heavy lifting for me:

String sSlashNames[] = doLDAPSearch();
String sList = "<P@List name="UserName" style="width: 600px;" @P>";        
HTMLDocumentItem hdi = new HTMLDocumentItem(ActionDocument, sList);        
ActionDocument.setItemChoices("UserName", sSlashNames);
write(hdi.getHTML(false).toString());

In this way I can use a standard Tornado p-tag and just set the values which are stored as a String array (sList). The last call to write(); sends the html code for the combo box back to the browser. The best part (better than if I had coded it by hand!) is that if the client decides tomorrow they'd rather see a row of checkboxes, I just change "<P@List" to "<P@Checkbox".

"Bollocks!" I hear you say. "I bet querying AD is a killer pile of code". Not any more. The new LDAPQuery object (in the puakma.util package) makes it very easy:

LDAPQuery ldap = new LDAPQuery(sURL, sSearchBase, sBindMethod, sUserName, sPassword);
String sNames[] = ldap.makeChoicesArray(sQuery, new String[]{"distinguishedName"}, null);

2 lines of code :-) Tornado rocks!