use of org.apache.directory.api.ldap.model.filter.SubstringNode in project directory-ldap-api by apache.
the class SearchRequestTest method testRequestWithSubstrings1EmptyInitial.
/**
* Test parsing of a request with a Substrings Filter with 1 emptyInitial element
*/
@Test
public void testRequestWithSubstrings1EmptyInitial() {
Dsmlv2Parser parser = null;
try {
parser = newParser();
parser.setInput(SearchRequestTest.class.getResource("filters/request_with_substrings_1_empty_initial.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 SubstringNode);
SubstringNode substringFilter = (SubstringNode) filter;
assertNull(substringFilter.getInitial());
}
use of org.apache.directory.api.ldap.model.filter.SubstringNode 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;
}
}
use of org.apache.directory.api.ldap.model.filter.SubstringNode in project directory-ldap-api by apache.
the class SearchRequestSubstringTest method testDecodeSearchRequestSubstringAnyFinal.
/**
* Test the decoding of a SearchRequest with a substring filter. Test the
* initial filter : (objectclass=*t*t)
*/
@Test
public void testDecodeSearchRequestSubstringAnyFinal() {
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate(0x67);
stream.put(new byte[] { 0x30, // LDAPMessage ::=SEQUENCE {
0x65, 0x02, 0x01, // messageID MessageID
0x01, 0x63, // CHOICE { ..., searchRequest SearchRequest, ...
0x60, // 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
0x01, // wholeSubtree (2) },
0x0A, 0x01, // derefAliases ENUMERATED {
0x03, // sizeLimit INTEGER (0 .. maxInt), (1000)
0x02, 0x02, 0x03, (byte) 0xE8, // timeLimit INTEGER (0 .. maxInt), (1000)
0x02, 0x02, 0x03, (byte) 0xE8, 0x01, 0x01, // typesOnly
(byte) 0xFF, // filter Filter,
(byte) 0xA4, // Filter ::= CHOICE {
0x15, // SubstringFilter ::= SEQUENCE {
0x04, // type AttributeDescription,
0x0B, 'o', 'b', 'j', 'e', 'c', 't', 'c', 'l', 'a', 's', 's', 0x30, 0x06, (byte) 0x81, 0x01, //
't', (byte) 0x82, 0x01, //
't', 0x30, // AttributeDescriptionList ::= SEQUENCE OF
0x15, // AttributeDescription
0x04, 0x05, 'a', 't', 't', 'r', // AttributeDescription
'0', // ::= LDAPString
0x04, 0x05, 'a', 't', 't', 'r', // AttributeDescription
'1', // ::= LDAPString
0x04, 0x05, 'a', 't', 't', 'r', // AttributeDescription ::=
'2' // LDAPString
});
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());
}
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());
// (objectclass=t*)
ExprNode node = searchRequest.getFilter();
SubstringNode substringNode = (SubstringNode) node;
assertNotNull(substringNode);
assertEquals("objectclass", substringNode.getAttribute());
assertEquals(null, substringNode.getInitial());
assertEquals("t", substringNode.getAny().get(0));
assertEquals("t", substringNode.getFinal());
// 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(0x67, bb.limit());
String encodedPdu = Strings.dumpBytes(bb.array());
assertEquals(encodedPdu.substring(0, 0x58), decodedPdu.substring(0, 0x58));
} catch (EncoderException ee) {
ee.printStackTrace();
fail(ee.getMessage());
}
}
use of org.apache.directory.api.ldap.model.filter.SubstringNode in project directory-ldap-api by apache.
the class SearchRequestSubstringTest method testDecodeSearchRequestSubstringAny.
/**
* Test the decoding of a SearchRequest with a substring filter. Test the
* any filter : (objectclass=*t*)
*/
@Test
public void testDecodeSearchRequestSubstringAny() {
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate(0x64);
stream.put(new byte[] { 0x30, // LDAPMessage ::=SEQUENCE {
0x62, 0x02, 0x01, // messageID MessageID
0x01, 0x63, // CHOICE { ..., searchRequest SearchRequest, ...
0x5D, // 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
0x01, // wholeSubtree (2) },
0x0A, 0x01, // derefAliases ENUMERATED {
0x03, // sizeLimit INTEGER (0 .. maxInt), (1000)
0x02, 0x02, 0x03, (byte) 0xE8, // timeLimit INTEGER (0 .. maxInt), (1000)
0x02, 0x02, 0x03, (byte) 0xE8, 0x01, 0x01, // typesOnly
(byte) 0xFF, // filter Filter,
(byte) 0xA4, // Filter ::= CHOICE {
0x12, // SubstringFilter ::= SEQUENCE {
0x04, // type AttributeDescription,
0x0B, 'o', 'b', 'j', 'e', 'c', 't', 'c', 'l', 'a', 's', 's', 0x30, 0x03, (byte) 0x81, 0x01, //
't', 0x30, // AttributeDescriptionList ::= SEQUENCE OF
0x15, // AttributeDescription
0x04, 0x05, 'a', 't', 't', 'r', // AttributeDescription
'0', // ::= LDAPString
0x04, 0x05, 'a', 't', 't', 'r', // AttributeDescription
'1', // ::= LDAPString
0x04, 0x05, 'a', 't', 't', 'r', // AttributeDescription ::=
'2' // LDAPString
});
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());
}
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());
// (objectclass=t*)
ExprNode node = searchRequest.getFilter();
SubstringNode substringNode = (SubstringNode) node;
assertNotNull(substringNode);
assertEquals("objectclass", substringNode.getAttribute());
assertEquals(null, substringNode.getInitial());
assertEquals("t", substringNode.getAny().get(0));
assertEquals(null, substringNode.getFinal());
// 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(0x64, bb.limit());
String encodedPdu = Strings.dumpBytes(bb.array());
assertEquals(encodedPdu.substring(0, 0x53), decodedPdu.substring(0, 0x53));
} catch (EncoderException ee) {
ee.printStackTrace();
fail(ee.getMessage());
}
}
use of org.apache.directory.api.ldap.model.filter.SubstringNode in project directory-ldap-api by apache.
the class SearchRequestSubstringTest method testDecodeSearchRequestSubstringAnyAnyFinal.
/**
* Test the decoding of a SearchRequest with a substring filter. Test the
* initial filter : (objectclass=t*t*t)
*/
@Test
public void testDecodeSearchRequestSubstringAnyAnyFinal() {
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate(0x6A);
stream.put(new byte[] { 0x30, // LDAPMessage ::=SEQUENCE {
0x68, 0x02, 0x01, // messageID MessageID
0x01, 0x63, // CHOICE { ..., searchRequest SearchRequest, ...
0x63, // 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) 0xA4, // Filter ::= CHOICE {
0x18, // SubstringFilter ::= SEQUENCE {
0x04, // type AttributeDescription,
0x0B, 'o', 'b', 'j', 'e', 'c', 't', 'c', 'l', 'a', 's', 's', 0x30, 0x09, (byte) 0x81, 0x01, 't', (byte) 0x81, 0x01, 't', (byte) 0x82, 0x01, 't', 0x30, // AttributeDescriptionList ::= SEQUENCE OF AttributeDescription
0x15, 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());
}
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());
// (objectclass=t*)
ExprNode node = searchRequest.getFilter();
SubstringNode substringNode = (SubstringNode) node;
assertNotNull(substringNode);
assertEquals("objectclass", substringNode.getAttribute());
assertEquals(null, substringNode.getInitial());
assertEquals("t", substringNode.getAny().get(0));
assertEquals("t", substringNode.getAny().get(1));
assertEquals("t", substringNode.getFinal());
// 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(0x6A, bb.limit());
String encodedPdu = Strings.dumpBytes(bb.array());
assertEquals(encodedPdu.substring(0, 0x5B), decodedPdu.substring(0, 0x5B));
} catch (EncoderException ee) {
ee.printStackTrace();
fail(ee.getMessage());
}
}
Aggregations