Thursday, February 09, 2006

Spring's LDAPTemplate

The Spring Framework is my new favourite thing! I toyed with LDAP and Spring recently. There is a Sourceforge project called LDAPTemplate maintained by Mattias Arthursson and Ulrik Sandberg of Jayway which looks to be very impressive. It is built on the same principles as Spring JDBCTemplate and other DAO templates. Unfortunately since I am working with LDAP version 2 (rather than version 3) I couldn't get it to work but the guys are working on fixing this for a future release. I can see this project becoming part of the core Spring framework in the not too distant future.

I did however manage to get an earlier version of the code from LDAPTemplate written by Olivier Jolly working with the following example.

Spring Config file (applicationContext.xml):


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
<bean id="rootContextSource"
class="org.springframework.ldap.support.UrlContextSource">
<property name="ldapUrl" value="ldap://hostname:389/o=Blah, c=GB"/>
</bean>
<bean id="ldapTemplate"
class="org.springframework.ldap.core.LdapTemplate">
<property name="contextSource">
<ref bean="rootContextSource" />
</property>
</bean>
</beans>

LDAPTest.java


import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.naming.NamingEnumeration;
import javax.naming.directory.Attributes;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.SearchResultCallbackHandler;

public class LDAPTest {

public static void main(String[] args) throws Exception {

// Manually create the Spring Bean Factory
// Note: The factory is driven by the Bean definitions held in ApplicationContext
// Note: There are also other implementations of Resource and Factory, but
// the following combination work well for standalone apps.

ClassPathResource res = new ClassPathResource("applicationContext.xml");
XmlBeanFactory factory = new XmlBeanFactory(res);
{
SearchControls ctls = new SearchControls();
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
// Specify the ids of the attributes to return
String[] attrIDs = {"sn", "mail", "uid", "cn", "ou", "postalAddress", "postalCode"};
ctls.setReturningAttributes(attrIDs);

LdapTemplate ldapTemplate = (LdapTemplate)factory.getBean("ldapTemplate");
List l = (List) ldapTemplate.search(
"",
"(sn=McLaren)",
ctls, new SearchResultCallbackHandler() {
LinkedList list = new LinkedList();

public void processSearchResult(SearchResult searchResult) throws javax.naming.NamingException {
Attributes attrs = searchResult.getAttributes();
list.add(attrs);
}

public Object getResult() {
return list;
}
});
Iterator i = l.iterator();
while(i.hasNext()) {
Attributes o = (Attributes) i.next();
NamingEnumeration ids = o.getIDs();
while(ids.hasMore()){
String att = (String) ids.next();
System.out.println(att + ":");
System.out.println(o.get(att).get());
}
System.out.println();
}
}
System.out.println("Done");
}

}

What is it about Spring that makes me use anonymous classes all the time?

3 comments:

Mark McLaren said...

By the way, LdapV2 should be working in the current version of LdapTemplate (1.0 RC-2). We're currently working on the final 1.0 release that should hopefully be released before the end of April.
Note: Comment imported. Original by Mattias Arthursson website: http://ldaptemplate.sourceforge.net at 2006-04-13 15:34

Mark McLaren said...

Hi..

I am having trouble in successfully implementing Jayway's method of using LDAP TEmplate API with LDAPv2. So thought I can try your method. Where do I get org.springframework.ldap.support.UrlContextSource class file?? which version of Springldap can i find this?
Note: Comment imported. Original by Anonymous at 2009-04-09 16:23

Mark McLaren said...

You can still get UrlContextSource from JASIG and Google Code. I expect the name has probably changed since it became an official Spring project.
Note: Comment imported. Original by markmc website: http://content.mark-mclaren.info/ at 2009-04-27 08:25