Search in sources :

Example 1 with LeafNode

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

the class SearchRequestDecorator method transform.

/**
 * Transform the Filter part of a SearchRequest to an ExprNode
 *
 * @param filter The filter to be transformed
 * @return An ExprNode
 * @throws LdapSchemaException If teh filter is invalid
 */
@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) {
                AttributeValueAssertion ava = ((AttributeValueAssertionFilter) filter).getAssertion();
                // Transform =, >=, <=, ~= filters
                int filterType = ((AttributeValueAssertionFilter) filter).getFilterType();
                switch(filterType) {
                    case LdapCodecConstants.EQUALITY_MATCH_FILTER:
                        branch = new EqualityNode(ava.getAttributeDesc(), ava.getAssertion());
                        break;
                    case LdapCodecConstants.GREATER_OR_EQUAL_FILTER:
                        branch = new GreaterEqNode(ava.getAttributeDesc(), ava.getAssertion());
                        break;
                    case LdapCodecConstants.LESS_OR_EQUAL_FILTER:
                        branch = new LessEqNode(ava.getAttributeDesc(), ava.getAssertion());
                        break;
                    case LdapCodecConstants.APPROX_MATCH_FILTER:
                        branch = new ApproximateNode(ava.getAttributeDesc(), ava.getAssertion());
                        break;
                    default:
                        throw new IllegalArgumentException("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) ExtensibleMatchFilter(org.apache.directory.api.ldap.codec.search.ExtensibleMatchFilter) 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) AttributeValueAssertion(org.apache.directory.api.ldap.codec.AttributeValueAssertion) 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) AttributeValueAssertionFilter(org.apache.directory.api.ldap.codec.search.AttributeValueAssertionFilter) SubstringFilter(org.apache.directory.api.ldap.codec.search.SubstringFilter) OrFilter(org.apache.directory.api.ldap.codec.search.OrFilter) AndFilter(org.apache.directory.api.ldap.codec.search.AndFilter) PresentFilter(org.apache.directory.api.ldap.codec.search.PresentFilter) GreaterEqNode(org.apache.directory.api.ldap.model.filter.GreaterEqNode) AttributeValueAssertionFilter(org.apache.directory.api.ldap.codec.search.AttributeValueAssertionFilter) AndFilter(org.apache.directory.api.ldap.codec.search.AndFilter) ConnectorFilter(org.apache.directory.api.ldap.codec.search.ConnectorFilter) SubstringFilter(org.apache.directory.api.ldap.codec.search.SubstringFilter) NotFilter(org.apache.directory.api.ldap.codec.search.NotFilter) OrFilter(org.apache.directory.api.ldap.codec.search.OrFilter) PresentFilter(org.apache.directory.api.ldap.codec.search.PresentFilter) ExtensibleMatchFilter(org.apache.directory.api.ldap.codec.search.ExtensibleMatchFilter) Filter(org.apache.directory.api.ldap.codec.search.Filter) ConnectorFilter(org.apache.directory.api.ldap.codec.search.ConnectorFilter) Value(org.apache.directory.api.ldap.model.entry.Value) BerValue(org.apache.directory.api.asn1.ber.tlv.BerValue) EqualityNode(org.apache.directory.api.ldap.model.filter.EqualityNode)

Example 2 with LeafNode

use of org.apache.directory.api.ldap.model.filter.LeafNode 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)

Aggregations

Value (org.apache.directory.api.ldap.model.entry.Value)2 AndNode (org.apache.directory.api.ldap.model.filter.AndNode)2 ApproximateNode (org.apache.directory.api.ldap.model.filter.ApproximateNode)2 BranchNode (org.apache.directory.api.ldap.model.filter.BranchNode)2 EqualityNode (org.apache.directory.api.ldap.model.filter.EqualityNode)2 ExtensibleNode (org.apache.directory.api.ldap.model.filter.ExtensibleNode)2 GreaterEqNode (org.apache.directory.api.ldap.model.filter.GreaterEqNode)2 LeafNode (org.apache.directory.api.ldap.model.filter.LeafNode)2 LessEqNode (org.apache.directory.api.ldap.model.filter.LessEqNode)2 NotNode (org.apache.directory.api.ldap.model.filter.NotNode)2 OrNode (org.apache.directory.api.ldap.model.filter.OrNode)2 PresenceNode (org.apache.directory.api.ldap.model.filter.PresenceNode)2 SubstringNode (org.apache.directory.api.ldap.model.filter.SubstringNode)2 BerValue (org.apache.directory.api.asn1.ber.tlv.BerValue)1 AttributeValueAssertion (org.apache.directory.api.ldap.codec.AttributeValueAssertion)1 AndFilter (org.apache.directory.api.ldap.codec.search.AndFilter)1 AttributeValueAssertionFilter (org.apache.directory.api.ldap.codec.search.AttributeValueAssertionFilter)1 ConnectorFilter (org.apache.directory.api.ldap.codec.search.ConnectorFilter)1 ExtensibleMatchFilter (org.apache.directory.api.ldap.codec.search.ExtensibleMatchFilter)1 Filter (org.apache.directory.api.ldap.codec.search.Filter)1