Search in sources :

Example 1 with CaseIgnoreStringMatchingRule

use of com.unboundid.ldap.matchingrules.CaseIgnoreStringMatchingRule in project ldapsdk by pingidentity.

the class Filter method hashCode.

/**
 * Generates a hash code for this search filter.
 *
 * @return  The generated hash code for this search filter.
 */
@Override()
public int hashCode() {
    final CaseIgnoreStringMatchingRule matchingRule = CaseIgnoreStringMatchingRule.getInstance();
    int hashCode = filterType;
    switch(filterType) {
        case FILTER_TYPE_AND:
        case FILTER_TYPE_OR:
            for (final Filter f : filterComps) {
                hashCode += f.hashCode();
            }
            break;
        case FILTER_TYPE_NOT:
            hashCode += notComp.hashCode();
            break;
        case FILTER_TYPE_EQUALITY:
        case FILTER_TYPE_GREATER_OR_EQUAL:
        case FILTER_TYPE_LESS_OR_EQUAL:
        case FILTER_TYPE_APPROXIMATE_MATCH:
            hashCode += StaticUtils.toLowerCase(attrName).hashCode();
            hashCode += matchingRule.normalize(assertionValue).hashCode();
            break;
        case FILTER_TYPE_SUBSTRING:
            hashCode += StaticUtils.toLowerCase(attrName).hashCode();
            if (subInitial != null) {
                hashCode += matchingRule.normalizeSubstring(subInitial, MatchingRule.SUBSTRING_TYPE_SUBINITIAL).hashCode();
            }
            for (final ASN1OctetString s : subAny) {
                hashCode += matchingRule.normalizeSubstring(s, MatchingRule.SUBSTRING_TYPE_SUBANY).hashCode();
            }
            if (subFinal != null) {
                hashCode += matchingRule.normalizeSubstring(subFinal, MatchingRule.SUBSTRING_TYPE_SUBFINAL).hashCode();
            }
            break;
        case FILTER_TYPE_PRESENCE:
            hashCode += StaticUtils.toLowerCase(attrName).hashCode();
            break;
        case FILTER_TYPE_EXTENSIBLE_MATCH:
            if (attrName != null) {
                hashCode += StaticUtils.toLowerCase(attrName).hashCode();
            }
            if (matchingRuleID != null) {
                hashCode += StaticUtils.toLowerCase(matchingRuleID).hashCode();
            }
            if (dnAttributes) {
                hashCode++;
            }
            hashCode += matchingRule.normalize(assertionValue).hashCode();
            break;
    }
    return hashCode;
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) JSONObjectFilter(com.unboundid.ldap.sdk.unboundidds.jsonfilter.JSONObjectFilter) CaseIgnoreStringMatchingRule(com.unboundid.ldap.matchingrules.CaseIgnoreStringMatchingRule)

Example 2 with CaseIgnoreStringMatchingRule

use of com.unboundid.ldap.matchingrules.CaseIgnoreStringMatchingRule in project ldapsdk by pingidentity.

the class AddRequestTestCase method testConstructor1.

/**
 * Tests the first constructor, which takes a string DN and an attribute
 * array.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testConstructor1() throws Exception {
    Attribute[] attrs = { new Attribute("objectClass", "top", "domain"), new Attribute("dc", "example") };
    AddRequest addRequest = new AddRequest("dc=example,dc=com", attrs);
    addRequest = addRequest.duplicate();
    assertNotNull(addRequest.getDN());
    assertEquals(addRequest.getDN(), "dc=example,dc=com");
    assertNotNull(addRequest.getAttributes());
    assertEquals(addRequest.getAttributes().size(), 2);
    assertNotNull(addRequest.getAttribute("objectClass"));
    assertNotNull(addRequest.getAttribute("dc"));
    assertNull(addRequest.getAttribute("description"));
    assertTrue(addRequest.hasAttribute("objectClass"));
    assertTrue(addRequest.hasAttribute("dc"));
    assertFalse(addRequest.hasAttribute("description"));
    assertTrue(addRequest.hasAttribute(new Attribute("objectClass", "top", "domain")));
    assertTrue(addRequest.hasAttribute(new Attribute("objectClass", "domain", "top")));
    assertFalse(addRequest.hasAttribute(new Attribute("objectClass", "top")));
    assertFalse(addRequest.hasAttribute(new Attribute("objectClass", "domain")));
    assertFalse(addRequest.hasAttribute(new Attribute("objectClass", "top", "domain", "dcObject")));
    assertFalse(addRequest.hasAttribute(new Attribute("objectClass")));
    assertTrue(addRequest.hasAttribute(new Attribute("dc", "example")));
    assertFalse(addRequest.hasAttribute(new Attribute("dc", "foo")));
    assertFalse(addRequest.hasAttribute(new Attribute("dc", "example", "foo")));
    assertFalse(addRequest.hasAttribute(new Attribute("dc")));
    assertFalse(addRequest.hasAttribute(new Attribute("description", "foo")));
    assertFalse(addRequest.hasAttribute(new Attribute("description")));
    assertTrue(addRequest.hasAttributeValue("objectClass", "top"));
    assertTrue(addRequest.hasAttributeValue("objectClass", "domain"));
    assertFalse(addRequest.hasAttributeValue("objectClass", "foo"));
    assertTrue(addRequest.hasAttributeValue("dc", "example"));
    assertFalse(addRequest.hasAttributeValue("dc", "bar"));
    assertFalse(addRequest.hasAttributeValue("description", "baz"));
    CaseIgnoreStringMatchingRule mr = CaseIgnoreStringMatchingRule.getInstance();
    assertTrue(addRequest.hasAttributeValue("objectClass", "top", mr));
    assertTrue(addRequest.hasAttributeValue("objectClass", "domain", mr));
    assertFalse(addRequest.hasAttributeValue("objectClass", "foo", mr));
    assertTrue(addRequest.hasAttributeValue("dc", "example", mr));
    assertFalse(addRequest.hasAttributeValue("dc", "bar", mr));
    assertFalse(addRequest.hasAttributeValue("description", "baz", mr));
    assertTrue(addRequest.hasAttributeValue("objectClass", "top".getBytes("UTF-8")));
    assertTrue(addRequest.hasAttributeValue("objectClass", "domain".getBytes("UTF-8")));
    assertFalse(addRequest.hasAttributeValue("objectClass", "foo".getBytes("UTF-8")));
    assertTrue(addRequest.hasAttributeValue("dc", "example".getBytes("UTF-8")));
    assertFalse(addRequest.hasAttributeValue("dc", "bar".getBytes("UTF-8")));
    assertFalse(addRequest.hasAttributeValue("description", "baz".getBytes("UTF-8")));
    assertTrue(addRequest.hasAttributeValue("objectClass", "top".getBytes("UTF-8"), mr));
    assertTrue(addRequest.hasAttributeValue("objectClass", "domain".getBytes("UTF-8"), mr));
    assertFalse(addRequest.hasAttributeValue("objectClass", "foo".getBytes("UTF-8"), mr));
    assertTrue(addRequest.hasAttributeValue("dc", "example".getBytes("UTF-8"), mr));
    assertFalse(addRequest.hasAttributeValue("dc", "bar".getBytes("UTF-8"), mr));
    assertFalse(addRequest.hasAttributeValue("description", "baz".getBytes("UTF-8"), mr));
    assertTrue(addRequest.hasObjectClass("top"));
    assertTrue(addRequest.hasObjectClass("domain"));
    assertFalse(addRequest.hasObjectClass("foo"));
    assertNotNull(addRequest.toEntry());
    assertFalse(addRequest.hasControl());
    assertFalse(addRequest.hasControl("1.2.3.4"));
    assertNull(addRequest.getControl("1.2.3.4"));
    assertNotNull(addRequest.getControls());
    assertEquals(addRequest.getControls().length, 0);
    assertNotNull(addRequest.toLDIFChangeRecord());
    assertNotNull(addRequest.toLDIF());
    assertTrue(addRequest.toLDIF().length > 0);
    assertNotNull(addRequest.toLDIFString());
    assertNotNull(addRequest.toString());
    final ArrayList<String> toCodeLines = new ArrayList<String>(10);
    addRequest.toCode(toCodeLines, "foo", 0, false);
    assertFalse(toCodeLines.isEmpty());
    toCodeLines.clear();
    addRequest.toCode(toCodeLines, "bar", 4, true);
    assertFalse(toCodeLines.isEmpty());
    assertEquals(addRequest.getProtocolOpType(), LDAPMessage.PROTOCOL_OP_TYPE_ADD_REQUEST);
    assertNull(addRequest.getIntermediateResponseListener());
    addRequest.setIntermediateResponseListener(new TestIntermediateResponseListener());
    assertNotNull(addRequest.getIntermediateResponseListener());
    addRequest.setIntermediateResponseListener(null);
    assertNull(addRequest.getIntermediateResponseListener());
    testEncoding(addRequest);
}
Also used : ArrayList(java.util.ArrayList) CaseIgnoreStringMatchingRule(com.unboundid.ldap.matchingrules.CaseIgnoreStringMatchingRule) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) Test(org.testng.annotations.Test)

Example 3 with CaseIgnoreStringMatchingRule

use of com.unboundid.ldap.matchingrules.CaseIgnoreStringMatchingRule in project ldapsdk by pingidentity.

the class Filter method equals.

/**
 * Indicates whether the provided object is equal to this search filter.
 *
 * @param  o  The object for which to make the determination.
 *
 * @return  {@code true} if the provided object can be considered equal to
 *          this search filter, or {@code false} if not.
 */
@Override()
public boolean equals(@Nullable final Object o) {
    if (o == null) {
        return false;
    }
    if (o == this) {
        return true;
    }
    if (!(o instanceof Filter)) {
        return false;
    }
    final Filter f = (Filter) o;
    if (filterType != f.filterType) {
        return false;
    }
    final CaseIgnoreStringMatchingRule matchingRule = CaseIgnoreStringMatchingRule.getInstance();
    switch(filterType) {
        case FILTER_TYPE_AND:
        case FILTER_TYPE_OR:
            if (filterComps.length != f.filterComps.length) {
                return false;
            }
            final HashSet<Filter> compSet = new HashSet<>(StaticUtils.computeMapCapacity(10));
            compSet.addAll(Arrays.asList(filterComps));
            for (final Filter filterComp : f.filterComps) {
                if (!compSet.remove(filterComp)) {
                    return false;
                }
            }
            return true;
        case FILTER_TYPE_NOT:
            return notComp.equals(f.notComp);
        case FILTER_TYPE_EQUALITY:
        case FILTER_TYPE_GREATER_OR_EQUAL:
        case FILTER_TYPE_LESS_OR_EQUAL:
        case FILTER_TYPE_APPROXIMATE_MATCH:
            return (attrName.equalsIgnoreCase(f.attrName) && matchingRule.valuesMatch(assertionValue, f.assertionValue));
        case FILTER_TYPE_SUBSTRING:
            if (!attrName.equalsIgnoreCase(f.attrName)) {
                return false;
            }
            if (subAny.length != f.subAny.length) {
                return false;
            }
            if (subInitial == null) {
                if (f.subInitial != null) {
                    return false;
                }
            } else {
                if (f.subInitial == null) {
                    return false;
                }
                final ASN1OctetString si1 = matchingRule.normalizeSubstring(subInitial, MatchingRule.SUBSTRING_TYPE_SUBINITIAL);
                final ASN1OctetString si2 = matchingRule.normalizeSubstring(f.subInitial, MatchingRule.SUBSTRING_TYPE_SUBINITIAL);
                if (!si1.equals(si2)) {
                    return false;
                }
            }
            for (int i = 0; i < subAny.length; i++) {
                final ASN1OctetString sa1 = matchingRule.normalizeSubstring(subAny[i], MatchingRule.SUBSTRING_TYPE_SUBANY);
                final ASN1OctetString sa2 = matchingRule.normalizeSubstring(f.subAny[i], MatchingRule.SUBSTRING_TYPE_SUBANY);
                if (!sa1.equals(sa2)) {
                    return false;
                }
            }
            if (subFinal == null) {
                if (f.subFinal != null) {
                    return false;
                }
            } else {
                if (f.subFinal == null) {
                    return false;
                }
                final ASN1OctetString sf1 = matchingRule.normalizeSubstring(subFinal, MatchingRule.SUBSTRING_TYPE_SUBFINAL);
                final ASN1OctetString sf2 = matchingRule.normalizeSubstring(f.subFinal, MatchingRule.SUBSTRING_TYPE_SUBFINAL);
                if (!sf1.equals(sf2)) {
                    return false;
                }
            }
            return true;
        case FILTER_TYPE_PRESENCE:
            return (attrName.equalsIgnoreCase(f.attrName));
        case FILTER_TYPE_EXTENSIBLE_MATCH:
            if (attrName == null) {
                if (f.attrName != null) {
                    return false;
                }
            } else {
                if (f.attrName == null) {
                    return false;
                } else {
                    if (!attrName.equalsIgnoreCase(f.attrName)) {
                        return false;
                    }
                }
            }
            if (matchingRuleID == null) {
                if (f.matchingRuleID != null) {
                    return false;
                }
            } else {
                if (f.matchingRuleID == null) {
                    return false;
                } else {
                    if (!matchingRuleID.equalsIgnoreCase(f.matchingRuleID)) {
                        return false;
                    }
                }
            }
            if (dnAttributes != f.dnAttributes) {
                return false;
            }
            return matchingRule.valuesMatch(assertionValue, f.assertionValue);
        default:
            return false;
    }
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) JSONObjectFilter(com.unboundid.ldap.sdk.unboundidds.jsonfilter.JSONObjectFilter) CaseIgnoreStringMatchingRule(com.unboundid.ldap.matchingrules.CaseIgnoreStringMatchingRule) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 4 with CaseIgnoreStringMatchingRule

use of com.unboundid.ldap.matchingrules.CaseIgnoreStringMatchingRule in project ldapsdk by pingidentity.

the class Filter method toNormalizedString.

/**
 * Appends a normalized string representation of this search filter to the
 * provided buffer.
 *
 * @param  buffer  The buffer to which to append a normalized string
 *                 representation of this search filter.
 */
public void toNormalizedString(@NotNull final StringBuilder buffer) {
    final CaseIgnoreStringMatchingRule mr = CaseIgnoreStringMatchingRule.getInstance();
    switch(filterType) {
        case FILTER_TYPE_AND:
            buffer.append("(&");
            for (final Filter f : filterComps) {
                f.toNormalizedString(buffer);
            }
            buffer.append(')');
            break;
        case FILTER_TYPE_OR:
            buffer.append("(|");
            for (final Filter f : filterComps) {
                f.toNormalizedString(buffer);
            }
            buffer.append(')');
            break;
        case FILTER_TYPE_NOT:
            buffer.append("(!");
            notComp.toNormalizedString(buffer);
            buffer.append(')');
            break;
        case FILTER_TYPE_EQUALITY:
            buffer.append('(');
            buffer.append(StaticUtils.toLowerCase(attrName));
            buffer.append('=');
            encodeValue(mr.normalize(assertionValue), buffer);
            buffer.append(')');
            break;
        case FILTER_TYPE_SUBSTRING:
            buffer.append('(');
            buffer.append(StaticUtils.toLowerCase(attrName));
            buffer.append('=');
            if (subInitial != null) {
                encodeValue(mr.normalizeSubstring(subInitial, MatchingRule.SUBSTRING_TYPE_SUBINITIAL), buffer);
            }
            buffer.append('*');
            for (final ASN1OctetString s : subAny) {
                encodeValue(mr.normalizeSubstring(s, MatchingRule.SUBSTRING_TYPE_SUBANY), buffer);
                buffer.append('*');
            }
            if (subFinal != null) {
                encodeValue(mr.normalizeSubstring(subFinal, MatchingRule.SUBSTRING_TYPE_SUBFINAL), buffer);
            }
            buffer.append(')');
            break;
        case FILTER_TYPE_GREATER_OR_EQUAL:
            buffer.append('(');
            buffer.append(StaticUtils.toLowerCase(attrName));
            buffer.append(">=");
            encodeValue(mr.normalize(assertionValue), buffer);
            buffer.append(')');
            break;
        case FILTER_TYPE_LESS_OR_EQUAL:
            buffer.append('(');
            buffer.append(StaticUtils.toLowerCase(attrName));
            buffer.append("<=");
            encodeValue(mr.normalize(assertionValue), buffer);
            buffer.append(')');
            break;
        case FILTER_TYPE_PRESENCE:
            buffer.append('(');
            buffer.append(StaticUtils.toLowerCase(attrName));
            buffer.append("=*)");
            break;
        case FILTER_TYPE_APPROXIMATE_MATCH:
            buffer.append('(');
            buffer.append(StaticUtils.toLowerCase(attrName));
            buffer.append("~=");
            encodeValue(mr.normalize(assertionValue), buffer);
            buffer.append(')');
            break;
        case FILTER_TYPE_EXTENSIBLE_MATCH:
            buffer.append('(');
            if (attrName != null) {
                buffer.append(StaticUtils.toLowerCase(attrName));
            }
            if (dnAttributes) {
                buffer.append(":dn");
            }
            if (matchingRuleID != null) {
                buffer.append(':');
                buffer.append(StaticUtils.toLowerCase(matchingRuleID));
            }
            buffer.append(":=");
            encodeValue(mr.normalize(assertionValue), buffer);
            buffer.append(')');
            break;
    }
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) JSONObjectFilter(com.unboundid.ldap.sdk.unboundidds.jsonfilter.JSONObjectFilter) CaseIgnoreStringMatchingRule(com.unboundid.ldap.matchingrules.CaseIgnoreStringMatchingRule)

Aggregations

ASN1OctetString (com.unboundid.asn1.ASN1OctetString)4 CaseIgnoreStringMatchingRule (com.unboundid.ldap.matchingrules.CaseIgnoreStringMatchingRule)4 JSONObjectFilter (com.unboundid.ldap.sdk.unboundidds.jsonfilter.JSONObjectFilter)3 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Test (org.testng.annotations.Test)1