use of com.unboundid.ldap.sdk.unboundidds.jsonfilter.JSONObjectFilter in project ldapsdk by pingidentity.
the class FilterTestCase method testMatchesEntryJSONObjectExtensibleMatch.
/**
* Tests the behavior of the matchesEntry method when the provided filter uses
* the jsonObjectFilterExtensibleMatch matching rule.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testMatchesEntryJSONObjectExtensibleMatch() throws Exception {
final Entry entry = new Entry("dn: uid=test.user,ou=People,dc=example,dc=com", "objectClass: top", "objectClass: ubidPerson", "uid: test.user", "givenName: Test", "sn: User", "cn: Test User", "ubidEmailJSON: not-a-valid-json-object", "ubidEmailJSON: " + new JSONObject(new JSONField("type", "personal"), new JSONField("value", "test.user@example.com"), new JSONField("primary", true)).toSingleLineString());
JSONObjectFilter jsonObjectFilter = new EqualsJSONObjectFilter("value", "test.user@example.com");
Filter filter = jsonObjectFilter.toLDAPFilter("ubidEmailJSON");
assertTrue(filter.matchesEntry(entry));
jsonObjectFilter = new EqualsJSONObjectFilter("value", "different.user@example.com");
filter = jsonObjectFilter.toLDAPFilter("ubidEmailJSON");
assertFalse(filter.matchesEntry(entry));
filter = jsonObjectFilter.toLDAPFilter("nonexistentAttribute");
assertFalse(filter.matchesEntry(entry));
try {
filter = Filter.createExtensibleMatchFilter("ubidEmailJSON", "1.3.6.1.4.1.30221.2.4.13", false, "not-a-valid-json-object");
filter.matchesEntry(entry);
fail("Expected an exception when trying to use a JSON object filter " + "whose assertion value is not a valid JSON object");
} catch (final LDAPException e) {
assertEquals(e.getResultCode(), ResultCode.INAPPROPRIATE_MATCHING);
}
try {
filter = Filter.createExtensibleMatchFilter("ubidEmailJSON", "1.3.6.1.4.1.30221.2.4.13", false, "{}");
filter.matchesEntry(entry);
fail("Expected an exception when trying to use a JSON object filter " + "whose assertion value is a valid JSON object but not a valid " + "JSON object filter");
} catch (final LDAPException e) {
assertEquals(e.getResultCode(), ResultCode.INAPPROPRIATE_MATCHING);
}
}
use of com.unboundid.ldap.sdk.unboundidds.jsonfilter.JSONObjectFilter in project ldapsdk by pingidentity.
the class Filter method extensibleMatchFilterMatchesEntry.
/**
* Indicates whether the provided extensible matching filter component matches
* the provided entry. This method provides very limited support for
* extensible matching It can only be used for filters that contain both an
* attribute type and a matching rule ID, and when the matching rule ID is
* one of the following:
* <OL>
* <LI>jsonObjectFilterExtensibleMatch (or 1.3.6.1.4.1.30221.2.4.13)</LI>
* </OL>
*
* @param entry The entry for which to make the determination. It must not
* be {@code null}.
* @param schema The schema to use when making the determination. If this
* is {@code null}, then all matching will be performed using
* a case-ignore matching rule.
*
* @return {@code true} if this filter appears to match the provided entry,
* or {@code false} if not.
*
* @throws LDAPException If a problem occurs while trying to make the
* determination.
*/
private boolean extensibleMatchFilterMatchesEntry(@NotNull final Entry entry, @Nullable final Schema schema) throws LDAPException {
if ((attrName != null) && (matchingRuleID != null) && (!dnAttributes)) {
if (matchingRuleID.equalsIgnoreCase("jsonObjectFilterExtensibleMatch") || matchingRuleID.equals("1.3.6.1.4.1.30221.2.4.13")) {
final JSONObjectFilter jsonObjectFilter;
try {
final JSONObject jsonObject = new JSONObject(assertionValue.stringValue());
jsonObjectFilter = JSONObjectFilter.decode(jsonObject);
} catch (final Exception e) {
Debug.debugException(e);
throw new LDAPException(ResultCode.INAPPROPRIATE_MATCHING, ERR_FILTER_EXTENSIBLE_MATCH_MALFORMED_JSON_OBJECT_FILTER.get(toString(), entry.getDN(), StaticUtils.getExceptionMessage(e)), e);
}
final Attribute attr = entry.getAttribute(attrName, schema);
if (attr != null) {
for (final ASN1OctetString v : attr.getRawValues()) {
try {
final JSONObject jsonObject = new JSONObject(v.stringValue());
if (jsonObjectFilter.matchesJSONObject(jsonObject)) {
return true;
}
} catch (final Exception e) {
Debug.debugException(e);
}
}
}
return false;
}
}
throw new LDAPException(ResultCode.NOT_SUPPORTED, ERR_FILTER_EXTENSIBLE_MATCHING_NOT_SUPPORTED.get());
}
Aggregations