Search in sources :

Example 1 with SimpleNode

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

the class SearchRequestDsml method toDsml.

/**
 * Recursively converts the filter of the Search Request into a DSML representation and adds
 * it to the XML Element corresponding to the Search Request
 *
 * @param element
 *      the parent Element
 * @param filter
 *      the filter to convert
 */
private void toDsml(Element element, ExprNode filter) {
    // AND FILTER
    if (filter instanceof AndNode) {
        Element newElement = element.addElement("and");
        List<ExprNode> filterList = ((AndNode) filter).getChildren();
        for (int i = 0; i < filterList.size(); i++) {
            toDsml(newElement, filterList.get(i));
        }
    } else // OR FILTER
    if (filter instanceof OrNode) {
        Element newElement = element.addElement("or");
        List<ExprNode> filterList = ((OrNode) filter).getChildren();
        for (int i = 0; i < filterList.size(); i++) {
            toDsml(newElement, filterList.get(i));
        }
    } else // NOT FILTER
    if (filter instanceof NotNode) {
        Element newElement = element.addElement("not");
        toDsml(newElement, ((NotNode) filter).getFirstChild());
    } else // SUBSTRING FILTER
    if (filter instanceof SubstringNode) {
        Element newElement = element.addElement("substrings");
        SubstringNode substringFilter = (SubstringNode) filter;
        newElement.addAttribute(NAME, substringFilter.getAttribute());
        String initial = substringFilter.getInitial();
        if ((initial != null) && (!"".equals(initial))) {
            newElement.addElement("initial").setText(initial);
        }
        List<String> anyList = substringFilter.getAny();
        for (int i = 0; i < anyList.size(); i++) {
            newElement.addElement("any").setText(anyList.get(i));
        }
        String finalString = substringFilter.getFinal();
        if ((finalString != null) && (!"".equals(finalString))) {
            newElement.addElement("final").setText(finalString);
        }
    } else // APPROXMATCH, EQUALITYMATCH, GREATEROREQUALS & LESSOREQUAL FILTERS
    if (filter instanceof SimpleNode) {
        Element newElement;
        if (filter instanceof ApproximateNode) {
            newElement = element.addElement("approxMatch");
        } else if (filter instanceof EqualityNode) {
            newElement = element.addElement("equalityMatch");
        } else if (filter instanceof GreaterEqNode) {
            newElement = element.addElement("greaterOrEqual");
        } else // it is a LessEqNode )
        {
            newElement = element.addElement("lessOrEqual");
        }
        String attributeName = ((SimpleNode<?>) filter).getAttribute();
        newElement.addAttribute(NAME, attributeName);
        Value value = ((SimpleNode<?>) filter).getValue();
        if (value != null) {
            if (ParserUtils.needsBase64Encoding(value)) {
                Namespace xsdNamespace = new Namespace("xsd", ParserUtils.XML_SCHEMA_URI);
                Namespace xsiNamespace = new Namespace("xsi", ParserUtils.XML_SCHEMA_INSTANCE_URI);
                element.getDocument().getRootElement().add(xsdNamespace);
                element.getDocument().getRootElement().add(xsiNamespace);
                Element valueElement = newElement.addElement(VALUE).addText(ParserUtils.base64Encode(value));
                valueElement.addAttribute(new QName("type", xsiNamespace), "xsd:" + ParserUtils.BASE64BINARY);
            } else {
                newElement.addElement(VALUE).setText(value.getValue());
            }
        }
    } else // PRESENT FILTER
    if (filter instanceof PresenceNode) {
        Element newElement = element.addElement("present");
        newElement.addAttribute(NAME, ((PresenceNode) filter).getAttribute());
    } else // EXTENSIBLEMATCH
    if (filter instanceof ExtensibleNode) {
        Element newElement = element.addElement("extensibleMatch");
        Value value = ((ExtensibleNode) filter).getValue();
        if (value != null) {
            if (ParserUtils.needsBase64Encoding(value)) {
                Namespace xsdNamespace = new Namespace("xsd", ParserUtils.XML_SCHEMA_URI);
                Namespace xsiNamespace = new Namespace("xsi", ParserUtils.XML_SCHEMA_INSTANCE_URI);
                element.getDocument().getRootElement().add(xsdNamespace);
                element.getDocument().getRootElement().add(xsiNamespace);
                Element valueElement = newElement.addElement(VALUE).addText(ParserUtils.base64Encode(value.getValue()));
                valueElement.addAttribute(new QName("type", xsiNamespace), "xsd:" + ParserUtils.BASE64BINARY);
            } else {
                newElement.addElement(VALUE).setText(value.getValue());
            }
        }
        if (((ExtensibleNode) filter).hasDnAttributes()) {
            newElement.addAttribute("dnAttributes", "true");
        }
        String matchingRule = ((ExtensibleNode) filter).getMatchingRuleId();
        if ((matchingRule != null) && ("".equals(matchingRule))) {
            newElement.addAttribute("matchingRule", matchingRule);
        }
    }
}
Also used : SubstringNode(org.apache.directory.api.ldap.model.filter.SubstringNode) ApproximateNode(org.apache.directory.api.ldap.model.filter.ApproximateNode) NotNode(org.apache.directory.api.ldap.model.filter.NotNode) QName(org.dom4j.QName) PresenceNode(org.apache.directory.api.ldap.model.filter.PresenceNode) Element(org.dom4j.Element) AndNode(org.apache.directory.api.ldap.model.filter.AndNode) ExtensibleNode(org.apache.directory.api.ldap.model.filter.ExtensibleNode) Namespace(org.dom4j.Namespace) SimpleNode(org.apache.directory.api.ldap.model.filter.SimpleNode) ExprNode(org.apache.directory.api.ldap.model.filter.ExprNode) GreaterEqNode(org.apache.directory.api.ldap.model.filter.GreaterEqNode) Value(org.apache.directory.api.ldap.model.entry.Value) ArrayList(java.util.ArrayList) List(java.util.List) EqualityNode(org.apache.directory.api.ldap.model.filter.EqualityNode) OrNode(org.apache.directory.api.ldap.model.filter.OrNode)

Example 2 with SimpleNode

use of org.apache.directory.api.ldap.model.filter.SimpleNode in project structr by structr.

the class StructrLDAPWrapper method matches.

private boolean matches(final LDAPNode node, final ExprNode filter) throws FrameworkException, LdapInvalidAttributeValueException {
    if (filter instanceof SimpleNode) {
        return evaluateSimpleNode(node, (SimpleNode) filter);
    } else if (filter instanceof SubstringNode) {
        return evaluateSubstringNode(node, (SubstringNode) filter);
    } else if (filter instanceof PresenceNode) {
        final PresenceNode presence = (PresenceNode) filter;
        final Attribute attribute = new DefaultAttribute(presence.getAttributeType());
        return findAttribute(node, attribute.getId()) != null;
    } else if (filter instanceof OrNode) {
        final OrNode orNode = (OrNode) filter;
        for (final ExprNode child : orNode.getChildren()) {
            if (matches(node, child)) {
                return true;
            }
        }
        return false;
    } else if (filter instanceof AndNode) {
        final AndNode andNode = (AndNode) filter;
        boolean result = true;
        for (final ExprNode child : andNode.getChildren()) {
            result &= matches(node, child);
        }
        return result;
    } else {
        System.out.println("Unsupported filter type " + filter.getClass());
    }
    return false;
}
Also used : ExprNode(org.apache.directory.api.ldap.model.filter.ExprNode) SubstringNode(org.apache.directory.api.ldap.model.filter.SubstringNode) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) NodeAttribute(org.structr.core.graph.NodeAttribute) LDAPAttribute(org.structr.ldap.entity.LDAPAttribute) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) PresenceNode(org.apache.directory.api.ldap.model.filter.PresenceNode) AndNode(org.apache.directory.api.ldap.model.filter.AndNode) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) SimpleNode(org.apache.directory.api.ldap.model.filter.SimpleNode) OrNode(org.apache.directory.api.ldap.model.filter.OrNode)

Aggregations

AndNode (org.apache.directory.api.ldap.model.filter.AndNode)2 ExprNode (org.apache.directory.api.ldap.model.filter.ExprNode)2 OrNode (org.apache.directory.api.ldap.model.filter.OrNode)2 PresenceNode (org.apache.directory.api.ldap.model.filter.PresenceNode)2 SimpleNode (org.apache.directory.api.ldap.model.filter.SimpleNode)2 SubstringNode (org.apache.directory.api.ldap.model.filter.SubstringNode)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Attribute (org.apache.directory.api.ldap.model.entry.Attribute)1 DefaultAttribute (org.apache.directory.api.ldap.model.entry.DefaultAttribute)1 Value (org.apache.directory.api.ldap.model.entry.Value)1 ApproximateNode (org.apache.directory.api.ldap.model.filter.ApproximateNode)1 EqualityNode (org.apache.directory.api.ldap.model.filter.EqualityNode)1 ExtensibleNode (org.apache.directory.api.ldap.model.filter.ExtensibleNode)1 GreaterEqNode (org.apache.directory.api.ldap.model.filter.GreaterEqNode)1 NotNode (org.apache.directory.api.ldap.model.filter.NotNode)1 Element (org.dom4j.Element)1 Namespace (org.dom4j.Namespace)1 QName (org.dom4j.QName)1 NodeAttribute (org.structr.core.graph.NodeAttribute)1