search
The search method performs an LDAP search operation and returns the result as a list of entries. In Groovy LDAP, an entry is simply a map.
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 class 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 very descriptive, and therefore recommended to use.
Using filter arguments
JNDI supports filter expressions with placeholders like {0}, {1} etc., and Groovy LDAP offers their use as well.
Here is a simple example:
...
params = new Search()
params.filter='(&(objectClass={0})(cn={1}))'
params.filterArgs=['person', 'Heather Nova']
results = ldap.search(params)
...
It is possible to use this in map style as well.
Specifying the attributes returned by a search
tbd.