Search in sources :

Example 1 with JSONObjectFilter

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);
    }
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) EqualsJSONObjectFilter(com.unboundid.ldap.sdk.unboundidds.jsonfilter.EqualsJSONObjectFilter) JSONObjectFilter(com.unboundid.ldap.sdk.unboundidds.jsonfilter.JSONObjectFilter) EqualsJSONObjectFilter(com.unboundid.ldap.sdk.unboundidds.jsonfilter.EqualsJSONObjectFilter) JSONField(com.unboundid.util.json.JSONField) EqualsJSONObjectFilter(com.unboundid.ldap.sdk.unboundidds.jsonfilter.EqualsJSONObjectFilter) JSONObjectFilter(com.unboundid.ldap.sdk.unboundidds.jsonfilter.JSONObjectFilter) Test(org.testng.annotations.Test)

Example 2 with JSONObjectFilter

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());
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) JSONObject(com.unboundid.util.json.JSONObject) JSONObjectFilter(com.unboundid.ldap.sdk.unboundidds.jsonfilter.JSONObjectFilter) ASN1Exception(com.unboundid.asn1.ASN1Exception)

Aggregations

JSONObjectFilter (com.unboundid.ldap.sdk.unboundidds.jsonfilter.JSONObjectFilter)2 JSONObject (com.unboundid.util.json.JSONObject)2 ASN1Exception (com.unboundid.asn1.ASN1Exception)1 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)1 EqualsJSONObjectFilter (com.unboundid.ldap.sdk.unboundidds.jsonfilter.EqualsJSONObjectFilter)1 JSONField (com.unboundid.util.json.JSONField)1 Test (org.testng.annotations.Test)1