5.1 search has been edited by Stefan Zoerner (Feb 16, 2009).

(View changes)

Content:

search

Three options to call a search method

Short cut methods

For the most common types of search requests, the LDAP class contains two direct methods.

public List<Object> search( String filter ) throws NamingException
public List<Object> search( String filter, String base, SearchScope scope ) throws NamingException

This example uses the second variant:

...
ldap = LDAP.newInstance('ldap://zanzibar:10389/')

results = ldap.search('(objectClass=person)', 'dc=example,dc=com', SearchScope.ONE)
println " ${results.size} entries found ".center(40,'-')
for (entry in results) {
  println entry.dn
}
...

Using a Search object

The class org.apache.directory.groovyldap.Search is a JavaBean which contains parameters for a search.

Property name Type Default value Description
base java.lang.String "" Search base
scope org.apache.directory.groovyldap.SearchScope SearchScope.SUB (whole sub tree) Search scope, one of BASE, ONE, SUB
filter java.lang.String "(objectClass=*)" (matches all entries) filter expression
filterArgs java.lang.Object[] null filter arguments
attrs java.lang.String[] null (all attributes) returning attributes

You can simply create an object of this class, adjust the attributes to your needs and call an appropriate search method from the LDAP with it. Here is an example:

...
params = new Search()
params.filter='(objectClass=person)'
params.base='dc=example,dc=com'
params.scope=SearchScope.ONE

results = ldap.search(params)
...

Using map style syntax

A very handy way to call search operations is to use the expressive map style arguments. The same properties as described for the Search class are supported. Here is an example.

...
results = ldap.search(filter: '(objectClass=person)', 
    base: 'dc=example,dc=com', scope: 'ONE')
...

The order of arguments does not matter, and this variant is ver descriptive, and therefore recommended to use.

Using filter arguments

JNDI supports filter expression with placeholders like '

Unknown macro: {0}

', '

Unknown macro: {1}
etc., and Groovy LDAP offers their use as well. Here is a simple example:


...
params = new Search()
params.filter='(&(objectClass=

)(cn=
Unknown macro: {1}
))'
params.base='dc=example,dc=com'
params.filterArgs='person', 'Heather Nova'

results = ldap.search(params)
...


Specifying the attributes returned by a search

Powered by Atlassian Confluence (Version: 2.2.9 Build:#527 Sep 07, 2006) - Bug/feature request

Unsubscribe or edit your notifications preferences