Search Operation

The primary operation of LDAP servers. Provides the ability to retrieve multiple entries containing variable attribute sets using a defined query syntax. The syntax for LDAP filters is defined in RFC 2254.

Searches for entries matching: (&(givenName=daniel)(sn=fisher)) over the dc=ldaptive,dc=org DIT and returns entries containing just the mail and displayName attributes.

SearchOperation search = new SearchOperation(new DefaultConnectionFactory("ldap://directory.ldaptive.org"));
SearchResponse res = search.execute(SearchRequest.builder()
  .dn("dc=ldaptive,dc=org")
  .filter("(&(givenName=daniel)(sn=fisher))")
  .returnAttributes("mail", "displayName")
  .build());
LdapEntry entry = res.getEntry(); // if you're expecting a single entry
for (LdapEntry le : res.getEntries()) { // if you're expecting multiple entries
  // do something useful with the entry
}

SearchRequest Properties

The following properties can be configured on a per request basis:

Name Default Value Description
baseDn ”” DN to search; An empty value searches the rootDSE;
filter null LDAP filter to execute
returnAttributes ALL_USER names of attributes to include in the search result
searchScope SUBTREE scope of the search; Valid values include: OBJECT, ONELEVEL, SUBTREE
timeLimit 0 length of time that a search operation should execute; A value of 0 means execute indefinitely; When the time limit arrives result will contain any result returned up to that point
sizeLimit 0 maximum number of entries to include in the search result; A value of 0 means includes all entries
derefAliases NEVER how aliases are dereferences; Valid values include: NEVER, SEARCHING, FINDING, ALWAYS
typesOnly false whether to return attribute types only, no values
binaryAttributes null attribute names that should be considered binary regardless of how they are stored

Search Filters

The SearchFilter object provides support for both positional and named parameters. Values provided as parameters are escaped according to RFC 2254.

Positional

FilterTemplate template = FilterTemplate.builder()
  .filter("(|(uid={0})(mail={1}))")
  .parameters("1234", "dfisher*@ldaptive.org")
  .build();

Named

FilterTemplate template = FilterTemplate.builder()
  .filter("(|(uid={uid})(mail={mail}))")
  .parameter("uid", "1234")
  .parameter("mail", "dfisher*@ldaptive.org")
  .build();

In this manner applications can define custom, readable filters for their users and then set the parameters accordingly.

Search Result Order

The result data stored in the SearchResponse, LdapEntry, and LdapAttribute objects are ordered as they are returned in the LDAP response. If you need to sort this data, static methods are available which sort elements naturally:

SearchOperation search = new SearchOperation(new DefaultConnectionFactory("ldap://directory.ldaptive.org"));
  SearchResponse res = search.execute(SearchRequest.builder()
  .dn("dc=ldaptive,dc=org")
  .filter("(&(givenName=daniel)(sn=fisher))")
  .returnAttributes("mail", "displayName")
  .build());
SearchResponse sortedRes = SearchResponse.sort(res);

Search results can be sorted automatically by setting the following JVM switch:

-Dorg.ldaptive.sortSearchResults=true