Templates
The templates project provides a framework for accepting some number of words (or terms) and producing search results from an LDAP. The most common use case being free form search input, like one would expect for a search engine.
A SearchTemplatesOperation
is configured with an ordered number of SearchTemplates
. The first SearchTemplates
object is expected to format search filters for one term queries. The second SearchTemplates
object is expected to format search filters for two term queries, and so forth. The appropriate SearchTemplates
is selected based on the Query
that is provided to the operation and a search is executed for each of the filters in that templates. A single SearchResponse
containing all results is returned. In this fashion a set of search terms can be transformed into LDAP search results.
Some named parameters are defined by the templates in order to write search filters:
- {term1} is replaced with the first query term
- {initial1} is replaced with the first letter of the first query term
- {term2} is replaced with the second query term
- {initial2} is replaced with the first letter of the second query term
- …
- {termN} is replaced with the Nth query term
- {initialN} is replaced with the first letter of the Nth query term
SearchTemplates oneTermTemplate = new SearchTemplates(
"(|(telephoneNumber={term1})(localPhone={term1}))",
"(|(telephoneNumber=*{term1})(localPhone=*{term1}))",
"(|(givenName={term1})(sn={term1}))",
"(|(givenName={term1}*)(sn={term1}*))",
"(|(givenName=*{term1}*)(sn=*{term1}*))");
SearchTemplates twoTermTemplate = new SearchTemplates(
"(&(givenName={term1})(sn={term2}))",
"(cn={term1} {term2})",
"(&(givenName={term1}*)(sn={term2}*))",
"(cn={term1}* {term2}*)",
"(&(givenName=*{term1}*)(sn=*{term2}*))",
"(cn=*{term1}* *{term2}*)");
SearchTemplates threeTermTemplate = new SearchTemplates(
"(|(&(givenName={term1})(sn={term3}))(&(givenName={term2})(sn={term3})))",
"(|(cn={term1} {term2} {term3})(cn={term2} {term1} {term3}))",
"(|(&(givenName={term1}*)(sn={term3}*))(&(givenName={term2}*)(sn={term3}*)))",
"(|(cn={term1}* {term2}* {term3}*)(cn={term2}* {term1}* {term3}*))",
"(|(&(givenName=*{term1}*)(sn=*{term3}*))(&(givenName=*{term2}*)(sn=*{term3}*)))",
"(|(cn=*{term1}* *{term2}* *{term3}*)(cn=*{term2}* *{term1}* *{term3}*))",
"(|(&(givenName={term1})(middleName={initial2}*)(sn={term3}))(&(givenName={term2})(middleName={initial1}*)(sn={term3})))",
"(|(&(givenName={initial1}*)(middlename={initial2}*)(sn={term3}))(&(givenName={initial2}*)(middleName={initial1}*)(sn={term3})))",
"(sn={term3})");
// create a pooled connection factory for searching
PooledConnectionFactory cf = new PooledConnectionFactory("ldap://directory.ldaptive.org");
cf.initialize();
SearchTemplatesOperation search = new SearchTemplatesOperation(
new SearchOperationWorker(new SearchOperation(cf)),
oneTermTemplate,
twoTermTemplate,
threeTermTemplate);
// get results for a one term query
Query oneTermQuery = new Query("fisher");
SearchResponse oneTermResult = search.execute(oneTermQuery);
// get results for a two term query
Query twoTermQuery = new Query("daniel fisher");
SearchResponse twoTermResult = search.execute(twoTermQuery);
// get results for a three term query
Query threeTermQuery = new Query("daniel william fisher");
SearchResponse threeTermResult = search.execute(threeTermQuery);
Templates support is provided in a separate library that is available in the jars directory of the latest download.
Or included as a maven dependency:
<dependencies>
<dependency>
<groupId>org.ldaptive</groupId>
<artifactId>ldaptive-templates</artifactId>
<version>2.4.0</version>
</dependency>
</dependencies>