Search in sources :

Example 1 with ExtensibleNode

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

the class SearchRequestMatchingRuleAssertionTest method testDecodeSearchRequestExtensibleMatchMatchValueAlone.

/**
 * Test the decoding of a SearchRequest with an extensible match and a match
 * Value and nothing else
 */
@Test
public void testDecodeSearchRequestExtensibleMatchMatchValueAlone() {
    byte[] asn1BER = new byte[] { 0x30, 0x43, 0x02, 0x01, // messageID
    0x04, 0x63, 0x3E, 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, 0x01, 0x0A, 0x01, 0x03, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x01, 0x01, (byte) 0xFF, (byte) 0xA9, 0x06, (byte) 0x83, 0x04, 't', 'e', 's', 't', 0x30, // AttributeDescriptionList ::= SEQUENCE OF AttributeDescription
    0x02, 0x04, 0x00 };
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(asn1BER.length);
    stream.put(asn1BER);
    stream.flip();
    // Allocate a LdapMessage Container
    LdapMessageContainer<SearchRequestDecorator> ldapMessageContainer = new LdapMessageContainer<SearchRequestDecorator>(codec);
    // Decode a SearchRequest message
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        fail(de.getMessage());
    }
    SearchRequest searchRequest = ldapMessageContainer.getMessage();
    assertEquals(4, searchRequest.getMessageId());
    assertEquals("uid=akarasulu,dc=example,dc=com", searchRequest.getBase().toString());
    assertEquals(SearchScope.ONELEVEL, searchRequest.getScope());
    assertEquals(AliasDerefMode.DEREF_ALWAYS, searchRequest.getDerefAliases());
    assertEquals(0, searchRequest.getSizeLimit());
    assertEquals(0, searchRequest.getTimeLimit());
    assertEquals(true, searchRequest.getTypesOnly());
    // Extended
    ExprNode filter = searchRequest.getFilter();
    ExtensibleNode extensibleNode = (ExtensibleNode) filter;
    assertNotNull(extensibleNode);
    assertNull(extensibleNode.getMatchingRuleId());
    assertNull(extensibleNode.getAttribute());
    assertEquals("test", extensibleNode.getValue().getValue());
    assertFalse(extensibleNode.hasDnAttributes());
    List<String> attributes = searchRequest.getAttributes();
    assertEquals(0, attributes.size());
}
Also used : ExprNode(org.apache.directory.api.ldap.model.filter.ExprNode) LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) DecoderException(org.apache.directory.api.asn1.DecoderException) SearchRequest(org.apache.directory.api.ldap.model.message.SearchRequest) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) ExtensibleNode(org.apache.directory.api.ldap.model.filter.ExtensibleNode) ByteBuffer(java.nio.ByteBuffer) SearchRequestDecorator(org.apache.directory.api.ldap.codec.decorators.SearchRequestDecorator) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 2 with ExtensibleNode

use of org.apache.directory.api.ldap.model.filter.ExtensibleNode 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 3 with ExtensibleNode

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

the class SearchRequestTest method testRequestWithExtensibleMatchWithEmptyValue.

/**
 * Test parsing of a request with an ExtensibleMatch Filter with empty value
 */
@Test
public void testRequestWithExtensibleMatchWithEmptyValue() {
    Dsmlv2Parser parser = null;
    try {
        parser = newParser();
        parser.setInput(SearchRequestTest.class.getResource("filters/request_with_extensibleMatch_with_empty_value.xml").openStream(), "UTF-8");
        parser.parse();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    SearchRequest searchRequest = (SearchRequest) parser.getBatchRequest().getCurrentRequest();
    ExprNode filter = searchRequest.getFilter();
    assertTrue(filter instanceof ExtensibleNode);
    ExtensibleNode extensibleMatchFilter = (ExtensibleNode) filter;
    assertNull(extensibleMatchFilter.getValue());
    assertEquals(false, extensibleMatchFilter.hasDnAttributes());
}
Also used : ExprNode(org.apache.directory.api.ldap.model.filter.ExprNode) SearchRequest(org.apache.directory.api.ldap.model.message.SearchRequest) Dsmlv2Parser(org.apache.directory.api.dsmlv2.Dsmlv2Parser) ExtensibleNode(org.apache.directory.api.ldap.model.filter.ExtensibleNode) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) Test(org.junit.Test) AbstractTest(org.apache.directory.api.dsmlv2.AbstractTest)

Example 4 with ExtensibleNode

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

the class SearchRequestTest method testRequestWithExtensibleMatchWithDnAttributesFalse.

/**
 * Test parsing of a request with typesOnly to false
 */
@Test
public void testRequestWithExtensibleMatchWithDnAttributesFalse() {
    Dsmlv2Parser parser = null;
    try {
        parser = newParser();
        parser.setInput(SearchRequestTest.class.getResource("filters/request_with_extensibleMatch_with_dnAttributes_false.xml").openStream(), "UTF-8");
        parser.parse();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    SearchRequest searchRequest = (SearchRequest) parser.getBatchRequest().getCurrentRequest();
    ExprNode filter = searchRequest.getFilter();
    assertTrue(filter instanceof ExtensibleNode);
    ExtensibleNode extensibleMatchFilter = (ExtensibleNode) filter;
    assertFalse(extensibleMatchFilter.hasDnAttributes());
}
Also used : ExprNode(org.apache.directory.api.ldap.model.filter.ExprNode) SearchRequest(org.apache.directory.api.ldap.model.message.SearchRequest) Dsmlv2Parser(org.apache.directory.api.dsmlv2.Dsmlv2Parser) ExtensibleNode(org.apache.directory.api.ldap.model.filter.ExtensibleNode) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) Test(org.junit.Test) AbstractTest(org.apache.directory.api.dsmlv2.AbstractTest)

Example 5 with ExtensibleNode

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

the class SearchRequestTest method testRequestWithExtensibleMatchFilterBase64Value.

/**
 * Test parsing of a request with an ExtensibleMatch Filter
 */
@Test
public void testRequestWithExtensibleMatchFilterBase64Value() {
    Dsmlv2Parser parser = null;
    try {
        parser = newParser();
        parser.setInput(SearchRequestTest.class.getResource("filters/request_with_extensibleMatch_base64_value.xml").openStream(), "UTF-8");
        parser.parse();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    SearchRequest searchRequest = (SearchRequest) parser.getBatchRequest().getCurrentRequest();
    ExprNode filter = searchRequest.getFilter();
    assertTrue(filter instanceof ExtensibleNode);
    ExtensibleNode extensibleMatchFilter = (ExtensibleNode) filter;
    assertEquals("DSMLv2.0 rocks!!", extensibleMatchFilter.getValue().getValue());
    assertEquals(false, extensibleMatchFilter.hasDnAttributes());
}
Also used : ExprNode(org.apache.directory.api.ldap.model.filter.ExprNode) SearchRequest(org.apache.directory.api.ldap.model.message.SearchRequest) Dsmlv2Parser(org.apache.directory.api.dsmlv2.Dsmlv2Parser) ExtensibleNode(org.apache.directory.api.ldap.model.filter.ExtensibleNode) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) Test(org.junit.Test) AbstractTest(org.apache.directory.api.dsmlv2.AbstractTest)

Aggregations

ExtensibleNode (org.apache.directory.api.ldap.model.filter.ExtensibleNode)15 ExprNode (org.apache.directory.api.ldap.model.filter.ExprNode)13 SearchRequest (org.apache.directory.api.ldap.model.message.SearchRequest)11 Test (org.junit.Test)11 AbstractTest (org.apache.directory.api.dsmlv2.AbstractTest)9 Dsmlv2Parser (org.apache.directory.api.dsmlv2.Dsmlv2Parser)9 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)9 Value (org.apache.directory.api.ldap.model.entry.Value)4 AndNode (org.apache.directory.api.ldap.model.filter.AndNode)4 ApproximateNode (org.apache.directory.api.ldap.model.filter.ApproximateNode)4 EqualityNode (org.apache.directory.api.ldap.model.filter.EqualityNode)4 GreaterEqNode (org.apache.directory.api.ldap.model.filter.GreaterEqNode)4 OrNode (org.apache.directory.api.ldap.model.filter.OrNode)4 PresenceNode (org.apache.directory.api.ldap.model.filter.PresenceNode)4 SubstringNode (org.apache.directory.api.ldap.model.filter.SubstringNode)4 DecoderException (org.apache.directory.api.asn1.DecoderException)3 BranchNode (org.apache.directory.api.ldap.model.filter.BranchNode)3 LessEqNode (org.apache.directory.api.ldap.model.filter.LessEqNode)3 NotNode (org.apache.directory.api.ldap.model.filter.NotNode)3 ByteBuffer (java.nio.ByteBuffer)2