Class Filter
The RFC 4515 string representation of a filter can be generated using the
toString() methods and parsed using the valueOf(String)
factory method.
Filters can be constructed using the various factory methods. For example,
the following code illustrates how to create a filter having the string
representation "(&(cn=bjensen)(age>=21))":
import static org.forgerock.opendj.Filter.*;
Filter filter = and(equality("cn", "bjensen"), greaterOrEqual("age", 21));
// Alternatively use a filter template:
Filter filter = Filter.format("(&(cn=%s)(age>=%s))", "bjensen", 21);
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumThis enumeration defines the set of possible filter types that may be used for search filters. -
Method Summary
Modifier and TypeMethodDescriptionstatic FilterReturns theabsolute falsefilter as defined in RFC 4526 which is comprised of anorfilter containing zero components.static FilterReturns theabsolute truefilter as defined in RFC 4526 which is comprised of anandfilter containing zero components.static Filterand(Collection<Filter> subFilters) Creates an optimized newandfilter using the provided list of sub-filters.static Filterand(Collection<Filter> subFilters, boolean optimize) Creates a newandfilter using the provided list of sub-filters.static FilterCreates a newandfilter using the provided list of sub-filters.static Filterapproximate(String attributeDescription, Object assertionValue) Creates a newapproximate matchfilter using the provided attribute description and assertion value.static FilterCreates a newequality matchfilter using the provided attribute description and assertion value.booleanThis method does not perform any normalization, other than lowercasing attribute descriptions, and will only returntruewhen two filters are identical.static StringescapeAssertionValue(Object assertionValue) Returns the LDAP string representation of the provided filter assertion value in a form suitable for substitution directly into a filter string.static Filterextensible(String matchingRule, String attributeDescription, Object assertionValue, boolean dnAttributes) Creates a newextensible matchfilter.static FilterCreates a new filter using the provided filter template and unescaped assertion values.Returns the assertion value for this comparison filter.Returns the attribute description for this comparison filter, which may benullif the filter is an extensible match filter and theextensible matching ruleis specified.Returns the name of the extensible matching rule which should be used for comparing values, which may benullif theattribute descriptionis provided (in which case equality matching will be performed).Returns the type of this filter.Returns the sub-filter for this NOT filter.Returns an unmodifiable list containing the sub-filters for this AND or OR filter.Returns an unmodifiable list containing any intermediate substring assertion values for this substrings filter, which may be an empty if the substrings filter did not include an intermediate substrings.Returns the final substring assertion value for this substrings filter, ornullif the substrings filter did not include a final substring.Returns the initial substring assertion value for this substrings filter, ornullif the substrings filter did not include an initial substring.Returns the ASN.1 content associated with this unrecognized filter.byteReturns the ASN.1 tag associated with this unrecognized filter.static FiltergreaterOrEqual(String attributeDescription, Object assertionValue) Creates a newgreater or equalfilter using the provided attribute description and assertion value.static FiltergreaterThan(String attributeDescription, Object assertionValue) Creates a newgreater thanfilter using the provided attribute description and assertion value.inthashCode()This method produces a hash-code of the literal filter representation without performing any normalization other than lowercasing attribute descriptions.booleanReturns whether extensible matching should be performed against attributes in an entry's DN.static FilterlessOrEqual(String attributeDescription, Object assertionValue) Creates a newless or equalfilter using the provided attribute description and assertion value.static FilterCreates a newless thanfilter using the provided attribute description and assertion value.matcher()Returns aMatcherwhich can be used to compare thisFilteragainst entries using the default schema.Returns aMatcherwhich can be used to compare thisFilteragainst entries using the providedSchema.Indicates whether thisFiltermatches the providedEntryusing the default schema.static FilterCreates an optimized newnotfilter using the provided sub-filter.static FilterCreates a newnotfilter using the provided sub-filter.static FilterReturns theobjectClasspresence filter(objectClass=*).static Filteror(Collection<Filter> subFilters) Creates an optimized neworfilter using the provided list of sub-filters.static Filteror(Collection<Filter> subFilters, boolean optimize) Creates a neworfilter using the provided list of sub-filters.static FilterCreates a neworfilter using the provided list of sub-filters.static FilterCreates a newpresentfilter using the provided attribute description.static Filtersubstrings(String attributeDescription, Object initialSubstring, Collection<?> anySubstrings, Object finalSubstring) Creates a newsubstringsfilter using the provided attribute description,initial,final, andanysub-strings.toString()Returns aStringwhose contents is the LDAP string representation of thisFilter.static Filterunrecognized(byte filterTag, ByteString filterBytes) Creates a newunrecognizedfilter using the provided ASN1 filter tag and content.static FiltervalueOf(boolean bool) static FilterParses the provided LDAP string representation of a filter as aFilter.
-
Method Details
-
alwaysFalse
Returns theabsolute falsefilter as defined in RFC 4526 which is comprised of anorfilter containing zero components.- Returns:
- The absolute false filter.
- See Also:
-
alwaysTrue
Returns theabsolute truefilter as defined in RFC 4526 which is comprised of anandfilter containing zero components.- Returns:
- The absolute true filter.
- See Also:
-
and
Creates a newandfilter using the provided list of sub-filters.Creating a new
andfilter with anullor empty list of sub-filters is equivalent to callingalwaysTrue().- Parameters:
subFilters- The list of sub-filters, may be empty ornull.optimize- Whether to optimize the filter.- Returns:
- The newly created
andfilter.
-
and
Creates an optimized newandfilter using the provided list of sub-filters. This method is equivalent to callingand(subFilters, true).- Parameters:
subFilters- The list of sub-filters, may be empty ornull.- Returns:
- The newly created
andfilter.
-
and
Creates a newandfilter using the provided list of sub-filters.Creating a new
andfilter with anullor empty list of sub-filters is equivalent to callingalwaysTrue().- Parameters:
subFilters- The list of sub-filters, may be empty ornull.- Returns:
- The newly created
andfilter.
-
approximate
Creates a newapproximate matchfilter using the provided attribute description and assertion value.If
assertionValueis not an instance ofByteStringthen it will be converted using theByteString.valueOfObject(Object)method.- Parameters:
attributeDescription- The attribute description.assertionValue- The assertion value.- Returns:
- The newly created
approximate matchfilter.
-
equality
Creates a newequality matchfilter using the provided attribute description and assertion value.If
assertionValueis not an instance ofByteStringthen it will be converted using theByteString.valueOfObject(Object)method.- Parameters:
attributeDescription- The attribute description.assertionValue- The assertion value.- Returns:
- The newly created
equality matchfilter.
-
escapeAssertionValue
Returns the LDAP string representation of the provided filter assertion value in a form suitable for substitution directly into a filter string. This method may be useful in cases where a filter is to be constructed from a filter template usingString#format(String, Object...). The following example illustrates two approaches to constructing an equality filter:// This may contain user input. String assertionValue = ...; // Using the equality filter constructor: Filter filter = Filter.equality("cn", assertionValue); // Using a String template: String filterTemplate = "(cn=%s)"; String filterString = String.format(filterTemplate, Filter.escapeAssertionValue(assertionValue)); Filter filter = Filter.valueOf(filterString);IfassertionValueis not an instance ofByteStringthen it will be converted using theByteString.valueOfObject(Object)method.Note: assertion values do not and should not be escaped before passing them to constructors like
equality(String, Object). Escaping is only required when creating filter strings.- Parameters:
assertionValue- The assertion value.- Returns:
- The LDAP string representation of the provided filter assertion value in a form suitable for substitution directly into a filter string.
- See Also:
-
extensible
public static Filter extensible(String matchingRule, String attributeDescription, Object assertionValue, boolean dnAttributes) Creates a newextensible matchfilter.If
assertionValueis not an instance ofByteStringthen it will be converted using theByteString.valueOfObject(Object)method.- Parameters:
matchingRule- The matching rule name, may benullifattributeDescriptionis specified.attributeDescription- The attribute description, may benullifmatchingRuleis specified.assertionValue- The assertion value.dnAttributes- Indicates whether DN matching should be performed.- Returns:
- The newly created
extensible matchfilter.
-
greaterOrEqual
Creates a newgreater or equalfilter using the provided attribute description and assertion value.If
assertionValueis not an instance ofByteStringthen it will be converted using theByteString.valueOfObject(Object)method.- Parameters:
attributeDescription- The attribute description.assertionValue- The assertion value.- Returns:
- The newly created
greater or equalfilter.
-
greaterThan
Creates a newgreater thanfilter using the provided attribute description and assertion value.If
assertionValueis not an instance ofByteStringthen it will be converted using theByteString.valueOfObject(Object)method.NOTE: since LDAP does not support
greater thancomparisons, this method returns a filter of the form(!(type<=value)).- Parameters:
attributeDescription- The attribute description.assertionValue- The assertion value.- Returns:
- The newly created
greater thanfilter.
-
lessOrEqual
Creates a newless or equalfilter using the provided attribute description and assertion value.If
assertionValueis not an instance ofByteStringthen it will be converted using theByteString.valueOfObject(Object)method.- Parameters:
attributeDescription- The attribute description.assertionValue- The assertion value.- Returns:
- The newly created
less or equalfilter.
-
lessThan
Creates a newless thanfilter using the provided attribute description and assertion value.If
assertionValueis not an instance ofByteStringthen it will be converted using theByteString.valueOfObject(Object)method.NOTE: since LDAP does not support
less thancomparisons, this method returns a filter of the form(!(type>=value)).- Parameters:
attributeDescription- The attribute description.assertionValue- The assertion value.- Returns:
- The newly created
less thanfilter.
-
not
Creates an optimized newnotfilter using the provided sub-filter. This method is equivalent to callingnot(subFilter, true).- Parameters:
subFilter- The sub-filter.- Returns:
- The newly created
notfilter.
-
not
Creates a newnotfilter using the provided sub-filter.- Parameters:
subFilter- The sub-filter.optimize- Whether to optimize the filter.- Returns:
- The newly created
notfilter.
-
objectClassPresent
Returns theobjectClasspresence filter(objectClass=*).A call to this method is equivalent to but more efficient than the following code:
Filter.present("objectClass");- Returns:
- The
objectClasspresence filter(objectClass=*).
-
or
Creates a neworfilter using the provided list of sub-filters.Creating a new
orfilter with anullor empty list of sub-filters is equivalent to callingalwaysFalse().- Parameters:
subFilters- The list of sub-filters, may be empty ornull.optimize- Whether to optimize the filter.- Returns:
- The newly created
orfilter.
-
or
Creates an optimized neworfilter using the provided list of sub-filters. This method is equivalent to callingor(subFilters, true).Creating a new
orfilter with anullor empty list of sub-filters is equivalent to callingalwaysFalse().- Parameters:
subFilters- The list of sub-filters, may be empty ornull.- Returns:
- The newly created
orfilter.
-
or
Creates a neworfilter using the provided list of sub-filters.Creating a new
orfilter with anullor empty list of sub-filters is equivalent to callingalwaysFalse().- Parameters:
subFilters- The list of sub-filters, may be empty ornull.- Returns:
- The newly created
orfilter.
-
present
Creates a newpresentfilter using the provided attribute description.- Parameters:
attributeDescription- The attribute description.- Returns:
- The newly created
presentfilter.
-
substrings
public static Filter substrings(String attributeDescription, Object initialSubstring, Collection<?> anySubstrings, Object finalSubstring) Creates a newsubstringsfilter using the provided attribute description,initial,final, andanysub-strings.Any substrings which are not instances of
ByteStringwill be converted using theByteString.valueOfObject(Object)method.- Parameters:
attributeDescription- The attribute description.initialSubstring- The initial sub-string, may benullif eitherfinalSubstringoranySubstringsare specified.anySubstrings- The intermediate sub-strings, may benullor empty if eitherfinalSubstringorinitialSubstringare specified.finalSubstring- The final sub-string, may benullif eitherinitialSubstringoranySubstringsare specified.- Returns:
- The newly created
substringsfilter.
-
unrecognized
Creates a newunrecognizedfilter using the provided ASN1 filter tag and content. This type of filter should be used for filters which are not part of the standard filter definition.- Parameters:
filterTag- The ASN.1 tag.filterBytes- The filter content.- Returns:
- The newly created
unrecognizedfilter.
-
valueOf
- Parameters:
bool- The boolean representation of a filter.- Returns:
alwaysTrue()ifboolistrue. Otherwise, returnsalwaysFalse().
-
valueOf
Parses the provided LDAP string representation of a filter as aFilter.- Parameters:
string- The LDAP string representation of a filter.- Returns:
- The parsed
Filter. - Throws:
LocalizedIllegalArgumentException- Ifstringis not a valid LDAP string representation of a filter.- See Also:
-
format
Creates a new filter using the provided filter template and unescaped assertion values. This method first escapes each of the assertion values and then substitutes them into the template usingString.format(String, Object...). Finally, the formatted string is parsed as an LDAP filter usingvalueOf(String).This method may be useful in cases where the structure of a filter is not known at compile time, for example, it may be obtained from a configuration file. Example usage:
String template = "(|(cn=%s)(uid=user.%s))"; Filter filter = Filter.format(template, "alice", 123);
Any assertion values which are not instances ofByteStringwill be converted using theByteString.valueOfObject(Object)method.- Parameters:
template- The filter template.assertionValues- The assertion values to be substituted into the template.- Returns:
- The formatted template parsed as a
Filter. - Throws:
LocalizedIllegalArgumentException- If the formatted template is not a valid LDAP string representation of a filter.- See Also:
-
getFilterType
Returns the type of this filter.- Returns:
- The type of this filter.
-
getSubFilters
Returns an unmodifiable list containing the sub-filters for this AND or OR filter.- Returns:
- An unmodifiable list containing the sub-filters for this AND or OR filter.
- Throws:
UnsupportedOperationException- If this filter is not an AND or OR filter.
-
getNotSubFilter
Returns the sub-filter for this NOT filter.- Returns:
- The sub-filter for this NOT filter.
- Throws:
UnsupportedOperationException- If this filter is not a NOT filter.
-
getAttributeDescription
Returns the attribute description for this comparison filter, which may benullif the filter is an extensible match filter and theextensible matching ruleis specified.- Returns:
- The attribute description for this comparison filter, which may be
nullif the filter is an extensible match filter. - Throws:
UnsupportedOperationException- If this filter is not an equality, greater than, less than, approximate, presence, substrings, or extensible match filter.
-
getAssertionValue
Returns the assertion value for this comparison filter.- Returns:
- The assertion value for this comparison filter.
- Throws:
UnsupportedOperationException- If this filter is not an equality, greater than, less than, approximate, or extensible match filter.
-
getSubstringsInitial
Returns the initial substring assertion value for this substrings filter, ornullif the substrings filter did not include an initial substring.- Returns:
- The initial substring assertion value for this substrings filter, or
nullif the substrings filter did not include an initial substring. - Throws:
UnsupportedOperationException- If this filter is not a substrings filter.
-
getSubstringsAny
Returns an unmodifiable list containing any intermediate substring assertion values for this substrings filter, which may be an empty if the substrings filter did not include an intermediate substrings.- Returns:
- An unmodifiable list containing any intermediate substring assertion values for this substrings filter
- Throws:
UnsupportedOperationException- If this filter is not a substrings filter.
-
getSubstringsFinal
Returns the final substring assertion value for this substrings filter, ornullif the substrings filter did not include a final substring.- Returns:
- The final substring assertion value for this substrings filter, or
nullif the substrings filter did not include a final substring. - Throws:
UnsupportedOperationException- If this filter is not a substrings filter.
-
getExtensibleMatchingRule
Returns the name of the extensible matching rule which should be used for comparing values, which may benullif theattribute descriptionis provided (in which case equality matching will be performed).- Returns:
- The name of the extensible matching rule which should be used for comparing values, which may be
null. - Throws:
UnsupportedOperationException- If this filter is not an extensible match filter.
-
isDnAttributes
public boolean isDnAttributes()Returns whether extensible matching should be performed against attributes in an entry's DN.- Returns:
- Whether extensible matching should be performed against attributes in an entry's DN.
- Throws:
UnsupportedOperationException- If this filter is not an extensible match filter.
-
getUnrecognizedFilterTag
public byte getUnrecognizedFilterTag()Returns the ASN.1 tag associated with this unrecognized filter.- Returns:
- The ASN.1 tag associated with this unrecognized filter.
- Throws:
UnsupportedOperationException- If this filter is not an unrecognized filter.
-
getUnrecognizedFilterBytes
Returns the ASN.1 content associated with this unrecognized filter.- Returns:
- The ASN.1 content associated with this unrecognized filter.
- Throws:
UnsupportedOperationException- If this filter is not an unrecognized filter.
-
equals
This method does not perform any normalization, other than lowercasing attribute descriptions, and will only returntruewhen two filters are identical. In particular, the attribute descriptions "cn" and "commonName" will be treated as different, as will AND/OR filters containing the same sub-filters in a different order. In order to compare filters for equivalence useMatcher.equals(Object). -
hashCode
public int hashCode()This method produces a hash-code of the literal filter representation without performing any normalization other than lowercasing attribute descriptions. In particular, the attribute descriptions "cn" and "commonName" will be treated as different, as will AND/OR filters containing the same sub-filters in a different order. In order to compare filters for equivalence useMatcher.hashCode(). -
matcher
Returns aMatcherwhich can be used to compare thisFilteragainst entries using the default schema.- Returns:
- The
Matcher.
-
matcher
Returns aMatcherwhich can be used to compare thisFilteragainst entries using the providedSchema.- Parameters:
schema- The schema which theMatchershould use for comparisons.- Returns:
- The
Matcher.
-
matches
Indicates whether thisFiltermatches the providedEntryusing the default schema.Calling this method is equivalent to the following:
matcher().matches(entry);
- Parameters:
entry- The entry to be matched.- Returns:
- The result of matching the provided
Entryagainst thisFilterusing the default schema.
-
toString
Returns aStringwhose contents is the LDAP string representation of thisFilter.
-