use of org.apache.directory.api.ldap.model.filter.EqualityNode 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.EqualityNode in project directory-ldap-api by apache.
the class SearchRequestTest method testDecodeSearchRequestAndEqEq.
/**
* Test the decoding of a SearchRequest
* (&(a=b)(c=d))
*/
@Test
public void testDecodeSearchRequestAndEqEq() {
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate(0x2F);
stream.put(new byte[] { // LDAPMessage ::=SEQUENCE {
0x30, // LDAPMessage ::=SEQUENCE {
0x2D, 0x02, 0x01, // messageID MessageID
0x01, 0x63, // CHOICE { ...,
0x28, // SearchRequest ::= APPLICATION[3] SEQUENCE {
0x04, // baseObject LDAPDN,
0x03, 'a', '=', 'b', 0x0A, 0x01, // scope ENUMERATED {
0x01, // wholeSubtree (2) },
0x0A, 0x01, // derefAliases ENUMERATED {
0x03, // derefAlways (3) },
0x02, 0x01, // sizeLimit INTEGER (0 .. maxInt), (0)
0x00, 0x02, 0x01, // timeLimit INTEGER (0 .. maxInt), (1000)
0x00, 0x01, 0x01, // typesOnly BOOLEAN, (TRUE)
(byte) 0xFF, // filter Filter,
(byte) 0xA0, // Filter ::= CHOICE {
0x10, (byte) 0xA3, 0x06, // Assertion ::= SEQUENCE {
0x04, 0x01, // attributeDesc AttributeDescription (LDAPString),
'a', 0x04, 0x01, // assertionValue AssertionValue (OCTET STRING) }
'b', (byte) 0xA3, 0x06, // Assertion ::= SEQUENCE {
0x04, 0x01, // attributeDesc AttributeDescription (LDAPString),
'c', 0x04, 0x01, // assertionValue AssertionValue (OCTET STRING) }
'd', // attributes AttributeDescriptionList }
0x30, // AttributeDescriptionList ::= SEQUENCE OF AttributeDescription
0x00 });
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("a=b", 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());
// (&(...
ExprNode filter = searchRequest.getFilter();
AndNode andNode = (AndNode) filter;
assertNotNull(andNode);
List<ExprNode> andNodes = andNode.getChildren();
assertEquals(2, andNodes.size());
// (&(a=b)...
EqualityNode<?> equalityNode = (EqualityNode<?>) andNodes.get(0);
assertNotNull(equalityNode);
assertEquals("a", equalityNode.getAttribute());
assertEquals("b", equalityNode.getValue().getValue());
// (&(a=b)(c=d))
equalityNode = (EqualityNode<?>) andNodes.get(1);
assertNotNull(equalityNode);
assertEquals("c", equalityNode.getAttribute());
assertEquals("d", equalityNode.getValue().getValue());
List<String> attributes = searchRequest.getAttributes();
assertEquals(0, attributes.size());
// attributes may have been reordered
try {
ByteBuffer bb = encoder.encodeMessage(searchRequest);
// Check the length
assertEquals(0x2F, bb.limit());
String encodedPdu = Strings.dumpBytes(bb.array());
assertEquals(encodedPdu.substring(0, 0x2F), decodedPdu.substring(0, 0x2F));
} catch (EncoderException ee) {
ee.printStackTrace();
fail(ee.getMessage());
}
}
use of org.apache.directory.api.ldap.model.filter.EqualityNode in project directory-ldap-api by apache.
the class SearchRequestTest method testDecodeSearchRequestComplexFilterWithControl.
/**
* Test the decoding of a SearchRequest with a complex filter :
* (&(objectClass=person)(|(cn=Tori*)(sn=Jagger)))
*/
@Test
public void testDecodeSearchRequestComplexFilterWithControl() {
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate(0x77);
stream.put(new byte[] { // LdapMessage
0x30, // LdapMessage
0x75, 0x02, 0x01, // message Id = 6
0x06, 0x63, // SearchRequest
0x53, 0x04, // BaseDN 'ou=system'
0x09, 0x6F, 0x75, 0x3D, 0x73, 0x79, 0x73, 0x74, 0x65, 0x6D, 0x0A, 0x01, // scope = SUBTREE
0x02, 0x0A, 0x01, // derefAlias = 3
0x03, 0x02, 0x01, // sizeLimit = none
0x00, 0x02, 0x01, // timeLimit = none
0x00, 0x01, 0x01, // types only = false
0x00, (byte) 0xA0, // AND
0x35, (byte) 0xA3, // equals
0x15, 0x04, // 'objectclass'
0x0B, 0x6F, 0x62, 0x6A, 0x65, 0x63, 0x74, 0x43, 0x6C, 0x61, 0x73, 0x73, 0x04, // 'person'
0x06, 0x70, 0x65, 0x72, 0x73, 0x6F, 0x6E, (byte) 0xA1, // OR
0x1C, (byte) 0xA4, // substrings : 'cn=Tori*'
0x0C, 0x04, // 'cn'
0x02, 0x63, 0x6E, 0x30, // initial = 'Tori'
0x06, (byte) 0x80, 0x04, 0x54, 0x6F, 0x72, 0x69, (byte) 0xA3, // equals
0x0C, 0x04, // 'sn'
0x02, 0x73, 0x6E, 0x04, // 'Jagger'
0x06, 0x4A, 0x61, 0x67, 0x67, 0x65, 0x72, 0x30, // Control
0x00, (byte) 0xA0, 0x1B, 0x30, 0x19, 0x04, 0x17, '2', '.', '1', '6', '.', '8', '4', '0', '.', '1', '.', '1', '1', '3', '7', '3', '0', '.', '3', '.', '4', '.', '2' });
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(6, searchRequest.getMessageId());
assertEquals("ou=system", searchRequest.getBase().toString());
assertEquals(SearchScope.SUBTREE, searchRequest.getScope());
assertEquals(AliasDerefMode.DEREF_ALWAYS, searchRequest.getDerefAliases());
assertEquals(0, searchRequest.getSizeLimit());
assertEquals(0, searchRequest.getTimeLimit());
assertEquals(false, searchRequest.getTypesOnly());
// (&(...
ExprNode filter = searchRequest.getFilter();
AndNode andNode = (AndNode) filter;
assertNotNull(andNode);
List<ExprNode> andNodes = andNode.getChildren();
assertEquals(2, andNodes.size());
// (&(objectClass=person)..
EqualityNode<?> equalityNode = (EqualityNode<?>) andNodes.get(0);
assertNotNull(equalityNode);
assertEquals("objectClass", equalityNode.getAttribute());
assertEquals("person", equalityNode.getValue().getValue());
// (&(a=b)(|
OrNode orNode = (OrNode) andNodes.get(1);
assertNotNull(orNode);
List<ExprNode> orNodes = orNode.getChildren();
assertEquals(2, orNodes.size());
// (&(a=b)(|(cn=Tori*
SubstringNode substringNode = (SubstringNode) orNodes.get(0);
assertNotNull(substringNode);
assertEquals("cn", substringNode.getAttribute());
assertEquals("Tori", substringNode.getInitial());
assertEquals(0, substringNode.getAny().size());
assertEquals(null, substringNode.getFinal());
// (&(a=b)(|(cn=Tori*)(sn=Jagger)))
equalityNode = (EqualityNode<?>) orNodes.get(1);
assertNotNull(equalityNode);
assertEquals("sn", equalityNode.getAttribute());
assertEquals("Jagger", equalityNode.getValue().getValue());
}
use of org.apache.directory.api.ldap.model.filter.EqualityNode in project directory-ldap-api by apache.
the class SearchRequestTest method testDecodeSearchRequestDIRSERVER_810.
/**
* Test the decoding of a SearchRequest with special length (long form)
* for rootDSE
*/
@Test
public void testDecodeSearchRequestDIRSERVER_810() {
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate(0x6B);
stream.put(new byte[] { 0x30, (byte) 0x84, 0x00, 0x00, 0x00, 0x65, 0x02, 0x01, 0x03, 0x63, (byte) 0x84, 0x00, 0x00, 0x00, 0x5c, 0x04, 0x12, 0x6f, 0x75, 0x3d, 0x75, 0x73, 0x65, 0x72, 0x73, 0x2c, 0x6f, 0x75, 0x3d, 0x73, 0x79, 0x73, 0x74, 0x65, // 'ou=users,ou=system'
0x6d, 0x0a, 0x01, 0x01, 0x0a, 0x01, 0x00, 0x02, 0x01, 0x00, 0x02, 0x01, 0x1e, 0x01, 0x01, (byte) 0xff, (byte) 0xa0, (byte) 0x84, 0x00, 0x00, 0x00, 0x2d, (byte) 0xa3, (byte) 0x84, 0x00, 0x00, 0x00, 0x0e, 0x04, 0x03, 0x75, 0x69, 0x64, 0x04, 0x07, 0x62, 0x75, 0x73, 0x74, 0x65, 0x72, // 'buster ' (with a space at the end)
0x20, (byte) 0xa3, (byte) 0x84, 0x00, 0x00, 0x00, 0x13, 0x04, 0x0b, 0x73, 0x62, 0x41, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, // sbAttribute
0x65, 0x04, 0x04, 0x42, 0x75, 0x79, // 'Buy ' (with a space at the end)
0x20, 0x30, (byte) 0x84, 0x00, 0x00, 0x00, 0x00 });
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(3, searchRequest.getMessageId());
assertEquals("ou=users,ou=system", searchRequest.getBase().toString());
assertEquals(SearchScope.ONELEVEL, searchRequest.getScope());
assertEquals(AliasDerefMode.NEVER_DEREF_ALIASES, searchRequest.getDerefAliases());
assertEquals(0, searchRequest.getSizeLimit());
assertEquals(30, 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());
// (&(uid=buster)...
EqualityNode<?> equalityNode = (EqualityNode<?>) andNodes.get(0);
assertNotNull(equalityNode);
assertEquals("uid", equalityNode.getAttribute());
assertEquals("buster ", equalityNode.getValue().getValue());
// (&(uid=buster)(sbAttribute=Buy))
equalityNode = (EqualityNode<?>) andNodes.get(1);
assertNotNull(equalityNode);
assertEquals("sbAttribute", equalityNode.getAttribute());
assertEquals("Buy ", equalityNode.getValue().getValue());
List<String> attributes = searchRequest.getAttributes();
assertEquals(0, attributes.size());
}
use of org.apache.directory.api.ldap.model.filter.EqualityNode in project directory-ldap-api by apache.
the class SearchRequestTest method testDecodeSearchRequestEq.
/**
* Test the decoding of a SearchRequest
* (a=b)
*/
@Test
public void testDecodeSearchRequestEq() {
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate(0x25);
stream.put(new byte[] { // LDAPMessage ::=SEQUENCE {
0x30, // LDAPMessage ::=SEQUENCE {
0x23, 0x02, 0x01, // messageID MessageID
0x01, 0x63, // CHOICE { ...,
0x1E, // SearchRequest ::= APPLICATION[3] SEQUENCE {
0x04, // baseObject LDAPDN,
0x03, 'a', '=', 'b', 0x0A, 0x01, // scope ENUMERATED {
0x01, // wholeSubtree (2) },
0x0A, 0x01, // derefAliases ENUMERATED {
0x03, // derefAlways (3) },
0x02, 0x01, // sizeLimit INTEGER (0 .. maxInt), (0)
0x00, 0x02, 0x01, // timeLimit INTEGER (0 .. maxInt), (1000)
0x00, 0x01, 0x01, // typesOnly BOOLEAN, (TRUE)
(byte) 0xFF, // filter Filter,
(byte) 0xA3, // Filter ::= CHOICE {
0x06, // Assertion ::= SEQUENCE {
0x04, 0x01, // attributeDesc AttributeDescription (LDAPString),
'a', 0x04, 0x01, // assertionValue AssertionValue (OCTET STRING) }
'b', // attributes AttributeDescriptionList }
0x30, // AttributeDescriptionList ::= SEQUENCE OF AttributeDescription
0x00 });
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("a=b", 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());
// (a=b)
EqualityNode<?> equalityNode = (EqualityNode<?>) searchRequest.getFilter();
assertNotNull(equalityNode);
assertEquals("a", equalityNode.getAttribute());
assertEquals("b", equalityNode.getValue().getValue());
List<String> attributes = searchRequest.getAttributes();
assertEquals(0, attributes.size());
// attributes may have been reordered
try {
ByteBuffer bb = encoder.encodeMessage(searchRequest);
// Check the length
assertEquals(0x25, bb.limit());
String encodedPdu = Strings.dumpBytes(bb.array());
assertEquals(encodedPdu.substring(0, 0x25), decodedPdu.substring(0, 0x25));
} catch (EncoderException ee) {
ee.printStackTrace();
fail(ee.getMessage());
}
}
Aggregations