Search in sources :

Example 6 with LessEqNode

use of org.apache.directory.api.ldap.model.filter.LessEqNode in project directory-ldap-api by apache.

the class SearchRequestDsml method transform.

/**
 * Transform the Filter part of a SearchRequest to an ExprNode
 *
 * @param filter The filter to be transformed
 * @return An ExprNode
 */
@SuppressWarnings({ "rawtypes" })
private ExprNode transform(Filter filter) throws LdapSchemaException {
    if (filter != null) {
        // Transform OR, AND or NOT leaves
        if (filter instanceof ConnectorFilter) {
            BranchNode branch;
            if (filter instanceof AndFilter) {
                branch = new AndNode();
            } else if (filter instanceof OrFilter) {
                branch = new OrNode();
            } else {
                branch = new NotNode();
            }
            List<Filter> filtersSet = ((ConnectorFilter) filter).getFilterSet();
            // Loop on all AND/OR children
            if (filtersSet != null) {
                for (Filter node : filtersSet) {
                    branch.addNode(transform(node));
                }
            }
            return branch;
        } else {
            // Transform PRESENT or ATTRIBUTE_VALUE_ASSERTION
            LeafNode branch = null;
            if (filter instanceof PresentFilter) {
                branch = new PresenceNode(((PresentFilter) filter).getAttributeDescription());
            } else if (filter instanceof AttributeValueAssertionFilter) {
                AttributeValueAssertionFilter avaFilter = (AttributeValueAssertionFilter) filter;
                AttributeValueAssertion ava = avaFilter.getAssertion();
                // Transform =, >=, <=, ~= filters
                int filterType = avaFilter.getFilterType();
                byte[] value = null;
                if (ava.getAssertionValue() != null) {
                    value = ava.getAssertionValue().getBytes();
                }
                switch(filterType) {
                    case LdapCodecConstants.EQUALITY_MATCH_FILTER:
                        branch = new EqualityNode(ava.getAttributeDesc(), value);
                        break;
                    case LdapCodecConstants.GREATER_OR_EQUAL_FILTER:
                        branch = new GreaterEqNode(ava.getAttributeDesc(), value);
                        break;
                    case LdapCodecConstants.LESS_OR_EQUAL_FILTER:
                        branch = new LessEqNode(ava.getAttributeDesc(), value);
                        break;
                    case LdapCodecConstants.APPROX_MATCH_FILTER:
                        branch = new ApproximateNode(ava.getAttributeDesc(), value);
                        break;
                    default:
                        throw new IllegalStateException("Unexpected filter type " + filterType);
                }
            } else if (filter instanceof SubstringFilter) {
                // Transform Substring filters
                SubstringFilter substrFilter = (SubstringFilter) filter;
                String initialString = null;
                String finalString = null;
                List<String> anyString = null;
                if (substrFilter.getInitialSubstrings() != null) {
                    initialString = substrFilter.getInitialSubstrings();
                }
                if (substrFilter.getFinalSubstrings() != null) {
                    finalString = substrFilter.getFinalSubstrings();
                }
                if (substrFilter.getAnySubstrings() != null) {
                    anyString = new ArrayList<>();
                    for (String any : substrFilter.getAnySubstrings()) {
                        anyString.add(any);
                    }
                }
                branch = new SubstringNode(anyString, substrFilter.getType(), initialString, finalString);
            } else if (filter instanceof ExtensibleMatchFilter) {
                // Transform Extensible Match Filter
                ExtensibleMatchFilter extFilter = (ExtensibleMatchFilter) filter;
                String matchingRule = null;
                Value value = extFilter.getMatchValue();
                if (extFilter.getMatchingRule() != null) {
                    matchingRule = extFilter.getMatchingRule();
                }
                branch = new ExtensibleNode(extFilter.getType(), value, matchingRule, extFilter.isDnAttributes());
            }
            return branch;
        }
    } else {
        // We have found nothing to transform. Return null then.
        return null;
    }
}
Also used : ApproximateNode(org.apache.directory.api.ldap.model.filter.ApproximateNode) NotNode(org.apache.directory.api.ldap.model.filter.NotNode) PresenceNode(org.apache.directory.api.ldap.model.filter.PresenceNode) AndNode(org.apache.directory.api.ldap.model.filter.AndNode) ExtensibleNode(org.apache.directory.api.ldap.model.filter.ExtensibleNode) BranchNode(org.apache.directory.api.ldap.model.filter.BranchNode) LeafNode(org.apache.directory.api.ldap.model.filter.LeafNode) LessEqNode(org.apache.directory.api.ldap.model.filter.LessEqNode) OrNode(org.apache.directory.api.ldap.model.filter.OrNode) SubstringNode(org.apache.directory.api.ldap.model.filter.SubstringNode) GreaterEqNode(org.apache.directory.api.ldap.model.filter.GreaterEqNode) Value(org.apache.directory.api.ldap.model.entry.Value) EqualityNode(org.apache.directory.api.ldap.model.filter.EqualityNode)

Example 7 with LessEqNode

use of org.apache.directory.api.ldap.model.filter.LessEqNode in project directory-ldap-api by apache.

the class SearchRequestTest method testDecodeSearchRequestCompareFiltersNoControls.

/**
 * Test the decoding of a SearchRequest with no controls. Test the various
 * types of filter : >=, <=, ~= The search filter is :
 * (&(|(objectclass~=top)(ou<=contacts))(!(objectclass>=ttt)))
 */
@Test
public void testDecodeSearchRequestCompareFiltersNoControls() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x90);
    stream.put(new byte[] { 0x30, (byte) 0x81, // LDAPMessage ::=SEQUENCE {
    (byte) 0x8D, 0x02, 0x01, // messageID MessageID
    0x01, 0x63, (byte) 0x81, // CHOICE { ...,
    (byte) 0x87, // SearchRequest ::= APPLICATION[3] SEQUENCE {
    0x04, // baseObject LDAPDN,
    0x1F, 'u', 'i', 'd', '=', 'a', 'k', 'a', 'r', 'a', 's', 'u', 'l', 'u', ',', 'd', 'c', '=', 'e', 'x', 'a', 'm', 'p', 'l', 'e', ',', 'd', 'c', '=', 'c', 'o', 'm', 0x0A, 0x01, // scope ENUMERATED {
    0x01, // wholeSubtree (2) },
    0x0A, 0x01, // derefAliases ENUMERATED {
    0x03, // derefAlways (3) },
    0x02, 0x02, 0x03, // sizeLimit INTEGER (0 .. maxInt), (1000)
    (byte) 0xE8, 0x02, 0x02, 0x03, // timeLimit INTEGER (0 .. maxInt), (1000)
    (byte) 0xE8, 0x01, 0x01, // typesOnly BOOLEAN, (TRUE)
    (byte) 0xFF, // filter Filter,
    (byte) 0xA0, // Filter ::= CHOICE {
    0x3C, // and [0] SET OF Filter,
    (byte) 0xA1, // or [1] SET of Filter,
    0x24, (byte) 0xA8, // approxMatch [8]
    0x12, // Assertion ::= SEQUENCE {
    0x04, // attributeDesc AttributeDescription (LDAPString),
    0x0B, 'o', 'b', 'j', 'e', 'c', 't', 'c', 'l', 'a', 's', 's', 0x04, // attributeDesc AttributeDescription (LDAPString),
    0x03, 't', 'o', 'p', (byte) 0xA6, // lessOrEqual [3] Assertion,
    0x0E, 0x04, // Assertion ::= SEQUENCE {
    0x02, 'o', // attributeDesc AttributeDescription (LDAPString),
    'u', 0x04, // assertionValue AssertionValue (OCTET STRING) }
    0x08, 'c', 'o', 'n', 't', 'a', 'c', 't', 's', (byte) 0xA2, // not [2] Filter,
    0x14, (byte) 0xA5, // greaterOrEqual [5] Assertion,
    0x12, // Assertion ::= SEQUENCE {
    0x04, // attributeDesc AttributeDescription (LDAPString),
    0x0B, 'o', 'b', 'j', 'e', 'c', 't', 'c', 'l', 'a', 's', 's', 0x04, 0x03, 't', 't', // assertionValue AssertionValue (OCTET STRING) }
    't', // attributes AttributeDescriptionList }
    0x30, // AttributeDescriptionList ::= SEQUENCE OF
    0x15, // AttributeDescription
    0x04, 0x05, 'a', 't', 't', 'r', // AttributeDescription ::= LDAPString
    '0', 0x04, 0x05, 'a', 't', 't', 'r', // AttributeDescription ::= LDAPString
    '1', 0x04, 0x05, 'a', 't', 't', 'r', // AttributeDescription ::= LDAPString
    '2' });
    String decodedPdu = Strings.dumpBytes(stream.array());
    stream.flip();
    // Allocate a BindRequest Container
    LdapMessageContainer<SearchRequestDecorator> ldapMessageContainer = new LdapMessageContainer<SearchRequestDecorator>(codec);
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    assertEquals(TLVStateEnum.PDU_DECODED, ldapMessageContainer.getState());
    SearchRequest searchRequest = ldapMessageContainer.getMessage();
    assertEquals(1, searchRequest.getMessageId());
    assertEquals("uid=akarasulu,dc=example,dc=com", searchRequest.getBase().toString());
    assertEquals(SearchScope.ONELEVEL, searchRequest.getScope());
    assertEquals(AliasDerefMode.DEREF_ALWAYS, searchRequest.getDerefAliases());
    assertEquals(1000, searchRequest.getSizeLimit());
    assertEquals(1000, searchRequest.getTimeLimit());
    assertEquals(true, searchRequest.getTypesOnly());
    // (& (...
    ExprNode filter = searchRequest.getFilter();
    AndNode andNode = (AndNode) filter;
    assertNotNull(andNode);
    List<ExprNode> andNodes = andNode.getChildren();
    // (& (| (...
    assertEquals(2, andNodes.size());
    OrNode orFilter = (OrNode) andNodes.get(0);
    assertNotNull(orFilter);
    // (& (| (objectclass~=top) (...
    List<ExprNode> orNodes = orFilter.getChildren();
    assertEquals(2, orNodes.size());
    ApproximateNode<?> approxNode = (ApproximateNode<?>) orNodes.get(0);
    assertNotNull(approxNode);
    assertEquals("objectclass", approxNode.getAttribute());
    assertEquals("top", approxNode.getValue().getValue());
    // (& (| (objectclass~=top) (ou<=contacts) ) (...
    LessEqNode<?> lessOrEqualNode = (LessEqNode<?>) orNodes.get(1);
    assertNotNull(lessOrEqualNode);
    assertEquals("ou", lessOrEqualNode.getAttribute());
    assertEquals("contacts", lessOrEqualNode.getValue().getValue());
    // (& (| (objectclass~=top) (ou<=contacts) ) (! ...
    NotNode notNode = (NotNode) andNodes.get(1);
    assertNotNull(notNode);
    // (& (| (objectclass~=top) (ou<=contacts) ) (! (objectclass>=ttt) ) )
    GreaterEqNode<?> greaterOrEqual = (GreaterEqNode<?>) notNode.getFirstChild();
    assertNotNull(greaterOrEqual);
    assertEquals("objectclass", greaterOrEqual.getAttribute());
    assertEquals("ttt", greaterOrEqual.getValue().getValue());
    // The attributes
    List<String> attributes = searchRequest.getAttributes();
    for (String attribute : attributes) {
        assertNotNull(attribute);
    }
    // attributes may have been reordered
    try {
        ByteBuffer bb = encoder.encodeMessage(searchRequest);
        // Check the length
        assertEquals(0x0090, bb.limit());
        String encodedPdu = Strings.dumpBytes(bb.array());
        assertEquals(encodedPdu.substring(0, 0x81), decodedPdu.substring(0, 0x81));
    } catch (EncoderException ee) {
        ee.printStackTrace();
        fail(ee.getMessage());
    }
}
Also used : LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) SearchRequest(org.apache.directory.api.ldap.model.message.SearchRequest) ApproximateNode(org.apache.directory.api.ldap.model.filter.ApproximateNode) NotNode(org.apache.directory.api.ldap.model.filter.NotNode) AndNode(org.apache.directory.api.ldap.model.filter.AndNode) ByteBuffer(java.nio.ByteBuffer) ExprNode(org.apache.directory.api.ldap.model.filter.ExprNode) DecoderException(org.apache.directory.api.asn1.DecoderException) EncoderException(org.apache.directory.api.asn1.EncoderException) GreaterEqNode(org.apache.directory.api.ldap.model.filter.GreaterEqNode) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) LessEqNode(org.apache.directory.api.ldap.model.filter.LessEqNode) SearchRequestDecorator(org.apache.directory.api.ldap.codec.decorators.SearchRequestDecorator) OrNode(org.apache.directory.api.ldap.model.filter.OrNode) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Aggregations

LessEqNode (org.apache.directory.api.ldap.model.filter.LessEqNode)7 ExprNode (org.apache.directory.api.ldap.model.filter.ExprNode)5 AndNode (org.apache.directory.api.ldap.model.filter.AndNode)4 ApproximateNode (org.apache.directory.api.ldap.model.filter.ApproximateNode)4 GreaterEqNode (org.apache.directory.api.ldap.model.filter.GreaterEqNode)4 OrNode (org.apache.directory.api.ldap.model.filter.OrNode)4 SearchRequest (org.apache.directory.api.ldap.model.message.SearchRequest)4 Test (org.junit.Test)4 AbstractTest (org.apache.directory.api.dsmlv2.AbstractTest)3 Dsmlv2Parser (org.apache.directory.api.dsmlv2.Dsmlv2Parser)3 Value (org.apache.directory.api.ldap.model.entry.Value)3 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)3 BranchNode (org.apache.directory.api.ldap.model.filter.BranchNode)3 EqualityNode (org.apache.directory.api.ldap.model.filter.EqualityNode)3 ExtensibleNode (org.apache.directory.api.ldap.model.filter.ExtensibleNode)3 NotNode (org.apache.directory.api.ldap.model.filter.NotNode)3 PresenceNode (org.apache.directory.api.ldap.model.filter.PresenceNode)3 SubstringNode (org.apache.directory.api.ldap.model.filter.SubstringNode)3 DecoderException (org.apache.directory.api.asn1.DecoderException)2 BerValue (org.apache.directory.api.asn1.ber.tlv.BerValue)2