Wednesday, 29 February 2012

PartialResultException

I named the post "PartialResultException" because it was the exception which haunted me the most and made me doubt the benefits of Grails as a framework. For instance, code more configure less.

Recently, I was tasked to write a Grails app which authenticates against an MS Active Directory (LDAP enabled), performs lookups, persists some objects and lastly syncs with Google Apps.

As a newbie on Groovy & Grails I started my journey by going through the online documentation and various tutorials. This video tutorial shows how to build a twitter client in 90 minutes and is highly recommended to any Grails beginner.




It is very useful to start by using the command line so that you can get an idea of the ins and outs of the framework. Since I am used to coding in Eclipse, I ended up installing the SpringSource Tool Suite version of Eclipse.

Authentication was fairly trivial as there are plenty of tutorials and information out there. I used the following two plugins, Spring Security Core Plugin which is accompanied by this tutorial, and Grails Spring-Security-Ldap 

All is good so far, however, the nightmare begins now. Performing queries against an MS Active Directory can prove to be tricky and even reach the level beyond annoyance. More specifically if data are spread around a forest rather than lie in one location where LDAP has to process referrals. 

Thanks to this plugin it is easy to perform LDAP queries and represent data in an object oriented fashion

For example, even if you set the flag to automatically follow referrals to true and you try to perform a query for an (objectclass=group) on the baseDn without providing an OrganizationUnit, basically look through the entire tree structure and get an exception of the like, 

ERROR errors.GrailsExceptionResolver - PartialResultException occurred when processing request: [GET] /myapp/mypackage/myaction
Unprocessed Continuation Reference(s). Stacktrace follows:
javax.naming.PartialResultException: Unprocessed Continuation Reference(s); remaining name xxxx.xxxx.xxxxx

the culprit is Active Directory, apparently it cannot handle referrals automatically and throws a PartialResultException, there is a note in the documentation of LdapTemplate.

To resolve this error, you have to ignore the exception. In your Config.groovy where you set up Grails LDAP plugin add this flag.

ldap {
  directories {
    group {
      url = "ldap://myserver:389"
      base = "dc=xxx,dc=xxxx,dc=xx,dc=xx"
      userDn = "cn=xxxxxxxxxxxxxxxx, cn=xxxx, dc=xxxxxx,dc=xxxxxxx,dc=xx,dc=xx"
      password = "xxxxxxx"
        ignorePartialResultExcepton = true
      searchControls {
        countLimit = 40
        timeLimit = 600
        searchScore = "subtree"
        derefLinkFlag = true
      }
    }
  }
}

I hope this post can be helpful and guide other travellers to the right path.

Total Pageviews