Package org.springframework.ldap.query
Class LdapQueryBuilder
- java.lang.Object
-
- org.springframework.ldap.query.LdapQueryBuilder
-
- All Implemented Interfaces:
LdapQuery
public final class LdapQueryBuilder extends java.lang.Object implements LdapQuery
Builder of LdapQueries. Start with a call toquery()
, proceed with specifying the basic search configuration (e.g. search base, time limit, etc.), finally specify the actual query. Example:import static org.springframework.ldap.query.LdapQueryBuilder.query; ... LdapQuery query = query() .base("dc=261consulting, dc=com") .searchScope(SearchScope.ONELEVEL) .timeLimit(200) .countLimit(221) .where("objectclass").is("person").and("cn").is("John Doe");
Default configuration is that base path is
LdapUtils.emptyLdapName()
. All other parameters are undefined, meaning that (in the case of base search parameters), the LdapTemplate defaults will be used. Filter conditions must always be specified.- Since:
- 2.0
- See Also:
SearchControls
,LdapOperations.search(LdapQuery, org.springframework.ldap.core.AttributesMapper)
,LdapOperations.search(LdapQuery, org.springframework.ldap.core.ContextMapper)
,LdapOperations.searchForObject(LdapQuery, org.springframework.ldap.core.ContextMapper)
,LdapOperations.searchForContext(LdapQuery)
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.String[]
attributes()
Get the attributes to return.LdapQueryBuilder
attributes(java.lang.String... attributesToReturn)
javax.naming.Name
base()
Get the search base.LdapQueryBuilder
base(java.lang.String baseDn)
Set the base search path for the query.LdapQueryBuilder
base(javax.naming.Name baseDn)
Set the base search path for the query.java.lang.Integer
countLimit()
Get the count limit.LdapQueryBuilder
countLimit(int countLimit)
Set the count limit for the query.Filter
filter()
Get the filter.LdapQuery
filter(java.lang.String hardcodedFilter)
Specify a hardcoded filter.LdapQuery
filter(java.lang.String filterFormat, java.lang.Object... params)
Specify a hardcoded filter using the specified parameters.LdapQuery
filter(Filter filter)
Specify the filter to use.static LdapQueryBuilder
query()
Construct a new LdapQueryBuilder.SearchScope
searchScope()
Get the search scope.LdapQueryBuilder
searchScope(SearchScope searchScope)
Set the search scope for the query.java.lang.Integer
timeLimit()
Get the time limit.LdapQueryBuilder
timeLimit(int timeLimit)
Set the time limit for the query.ConditionCriteria
where(java.lang.String attribute)
Start specifying the filter conditions in this query.
-
-
-
Method Detail
-
query
public static LdapQueryBuilder query()
Construct a new LdapQueryBuilder.- Returns:
- a new instance.
-
base
public LdapQueryBuilder base(java.lang.String baseDn)
Set the base search path for the query. Default isLdapUtils.emptyLdapName()
.- Parameters:
baseDn
- the base search path.- Returns:
- this instance.
-
base
public LdapQueryBuilder base(javax.naming.Name baseDn)
Set the base search path for the query. Default isLdapUtils.emptyLdapName()
.- Parameters:
baseDn
- the base search path.- Returns:
- this instance.
-
searchScope
public LdapQueryBuilder searchScope(SearchScope searchScope)
Set the search scope for the query. Default isSearchScope.SUBTREE
.- Parameters:
searchScope
- the search scope.- Returns:
- this instance.
-
countLimit
public LdapQueryBuilder countLimit(int countLimit)
Set the count limit for the query. Default is 0 (no limit).- Parameters:
countLimit
- the count limit.- Returns:
- this instance.
-
attributes
public LdapQueryBuilder attributes(java.lang.String... attributesToReturn)
-
timeLimit
public LdapQueryBuilder timeLimit(int timeLimit)
Set the time limit for the query. Default is 0 (no limit).- Parameters:
timeLimit
- the time limit.- Returns:
- this instance.
-
where
public ConditionCriteria where(java.lang.String attribute)
Start specifying the filter conditions in this query.- Parameters:
attribute
- The attribute that the first part of the filter should test against.- Returns:
- A ConditionCriteria instance for specifying the compare operation.
- Throws:
java.lang.IllegalStateException
- if a filter has already been specified.
-
filter
public LdapQuery filter(java.lang.String hardcodedFilter)
Specify a hardcoded filter. Please note that using this method, the filter string will not be validated or escaped in any way. Never use direct user input and use it concatenating strings to use as LDAP filters. Doing so opens up for "LDAP injection", where malicious user may inject specifically constructed data to form filters at their convenience. When user input is used consider usingwhere(String)
,filter(String, Object...)
, orfilter(Filter)
instead.- Parameters:
hardcodedFilter
- The hardcoded filter string to use in the search.- Returns:
- this instance.
- Throws:
java.lang.IllegalStateException
- if a filter has already been specified.
-
filter
public LdapQuery filter(Filter filter)
Specify the filter to use.- Parameters:
filter
- The filter to use in the search.- Returns:
- this instance.
- Throws:
java.lang.IllegalStateException
- if a filter has already been specified.
-
filter
public LdapQuery filter(java.lang.String filterFormat, java.lang.Object... params)
Specify a hardcoded filter using the specified parameters. The parameters will be properly encoded usingLdapEncoder.filterEncode(String)
to make sure no malicious data gets through. ThefilterFormat
String should be formatted for input toMessageFormat.format(String, Object...)
.- Parameters:
filterFormat
- the filter format string, formatted for input toMessageFormat.format(String, Object...)
.params
- the parameters that will be used for building the final filter. All parameters will be properly encoded.- Returns:
- this instance.
- Throws:
java.lang.IllegalStateException
- if a filter has already been specified.
-
base
public javax.naming.Name base()
Description copied from interface:LdapQuery
Get the search base. Default isLdapUtils.emptyLdapName()
.
-
searchScope
public SearchScope searchScope()
Description copied from interface:LdapQuery
Get the search scope. Default isnull
, indicating that the LdapTemplate default should be used.- Specified by:
searchScope
in interfaceLdapQuery
- Returns:
- the search scope.
-
countLimit
public java.lang.Integer countLimit()
Description copied from interface:LdapQuery
Get the count limit. Default isnull
, indicating that the LdapTemplate default should be used.- Specified by:
countLimit
in interfaceLdapQuery
- Returns:
- the count limit.
-
timeLimit
public java.lang.Integer timeLimit()
Description copied from interface:LdapQuery
Get the time limit. Default isnull
, indicating that the LdapTemplate default should be used.
-
attributes
public java.lang.String[] attributes()
Description copied from interface:LdapQuery
Get the attributes to return. Default isnull
, indicating that all attributes should be returned.- Specified by:
attributes
in interfaceLdapQuery
- Returns:
- the attributes to return.
-
-