use of org.apache.directory.api.ldap.model.filter.OrNode 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;
}
use of org.apache.directory.api.ldap.model.filter.OrNode 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;
}
}
use of org.apache.directory.api.ldap.model.filter.OrNode in project directory-ldap-api by apache.
the class SearchRequestDecorator method transform.
/**
* Transform an ExprNode filter to a Filter
*
* @param exprNode The filter to be transformed
* @return A filter
*/
private static Filter transform(ExprNode exprNode) {
if (exprNode != null) {
Filter filter = null;
// Transform OR, AND or NOT leaves
if (exprNode instanceof BranchNode) {
if (exprNode instanceof AndNode) {
filter = new AndFilter();
} else if (exprNode instanceof OrNode) {
filter = new OrFilter();
} else {
filter = new NotFilter();
}
List<ExprNode> children = ((BranchNode) exprNode).getChildren();
// Loop on all AND/OR children
if (children != null) {
for (ExprNode child : children) {
try {
((ConnectorFilter) filter).addFilter(transform(child));
} catch (DecoderException de) {
return null;
}
}
}
} else {
if (exprNode instanceof PresenceNode) {
// Transform Presence Node
filter = new PresentFilter();
((PresentFilter) filter).setAttributeDescription(((PresenceNode) exprNode).getAttribute());
} else if (exprNode instanceof SimpleNode<?>) {
if (exprNode instanceof EqualityNode<?>) {
filter = new AttributeValueAssertionFilter(LdapCodecConstants.EQUALITY_MATCH_FILTER);
AttributeValueAssertion assertion = new AttributeValueAssertion();
assertion.setAttributeDesc(((EqualityNode<?>) exprNode).getAttribute());
assertion.setAssertion(((EqualityNode<?>) exprNode).getValue().getBytes());
((AttributeValueAssertionFilter) filter).setAssertion(assertion);
} else if (exprNode instanceof GreaterEqNode<?>) {
filter = new AttributeValueAssertionFilter(LdapCodecConstants.GREATER_OR_EQUAL_FILTER);
AttributeValueAssertion assertion = new AttributeValueAssertion();
assertion.setAttributeDesc(((GreaterEqNode<?>) exprNode).getAttribute());
assertion.setAssertion(((GreaterEqNode<?>) exprNode).getValue().getBytes());
((AttributeValueAssertionFilter) filter).setAssertion(assertion);
} else if (exprNode instanceof LessEqNode<?>) {
filter = new AttributeValueAssertionFilter(LdapCodecConstants.LESS_OR_EQUAL_FILTER);
AttributeValueAssertion assertion = new AttributeValueAssertion();
assertion.setAttributeDesc(((LessEqNode<?>) exprNode).getAttribute());
assertion.setAssertion(((LessEqNode<?>) exprNode).getValue().getBytes());
((AttributeValueAssertionFilter) filter).setAssertion(assertion);
} else if (exprNode instanceof ApproximateNode<?>) {
filter = new AttributeValueAssertionFilter(LdapCodecConstants.APPROX_MATCH_FILTER);
AttributeValueAssertion assertion = new AttributeValueAssertion();
assertion.setAttributeDesc(((ApproximateNode<?>) exprNode).getAttribute());
assertion.setAssertion(((ApproximateNode<?>) exprNode).getValue().getBytes());
((AttributeValueAssertionFilter) filter).setAssertion(assertion);
}
} else if (exprNode instanceof SubstringNode) {
// Transform Substring Nodes
filter = new SubstringFilter();
((SubstringFilter) filter).setType(((SubstringNode) exprNode).getAttribute());
String initialString = ((SubstringNode) exprNode).getInitial();
String finalString = ((SubstringNode) exprNode).getFinal();
List<String> anyStrings = ((SubstringNode) exprNode).getAny();
if (initialString != null) {
((SubstringFilter) filter).setInitialSubstrings(initialString);
}
if (finalString != null) {
((SubstringFilter) filter).setFinalSubstrings(finalString);
}
if (anyStrings != null) {
for (String any : anyStrings) {
((SubstringFilter) filter).addAnySubstrings(any);
}
}
} else if (exprNode instanceof ExtensibleNode) {
// Transform Extensible Node
filter = new ExtensibleMatchFilter();
String attribute = ((ExtensibleNode) exprNode).getAttribute();
String matchingRule = ((ExtensibleNode) exprNode).getMatchingRuleId();
boolean dnAttributes = ((ExtensibleNode) exprNode).hasDnAttributes();
Value value = ((ExtensibleNode) exprNode).getValue();
if (attribute != null) {
((ExtensibleMatchFilter) filter).setType(attribute);
}
if (matchingRule != null) {
((ExtensibleMatchFilter) filter).setMatchingRule(matchingRule);
}
((ExtensibleMatchFilter) filter).setMatchValue(value);
((ExtensibleMatchFilter) filter).setDnAttributes(dnAttributes);
}
}
return filter;
} else {
// We have found nothing to transform. Return null then.
return null;
}
}
use of org.apache.directory.api.ldap.model.filter.OrNode 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.OrNode in project directory-ldap-api by apache.
the class SearchRequestTest method testDecodeSearchRequestPresentNoControls.
/**
* Test the decoding of a SearchRequest with no controls. Test the present
* filter : =* The search filter is :
* (&(|(objectclass=*)(ou=*))(!(objectclass>=ttt)))
*/
@Test
public void testDecodeSearchRequestPresentNoControls() {
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate(0x7B);
stream.put(new byte[] { 0x30, // LDAPMessage ::=SEQUENCE {
0x79, 0x02, 0x01, // messageID MessageID
0x01, 0x63, // CHOICE { ..., searchRequest SearchRequest, ...
0x74, // 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) 0xA0, // Filter ::= CHOICE {
0x29, // and [0] SET OF Filter,
(byte) 0xA1, // or [1] SET of Filter,
0x11, (byte) 0x87, // present [7] AttributeDescription,
0x0B, // AttributeDescription ::= LDAPString
'o', 'b', 'j', 'e', 'c', 't', 'c', 'l', 'a', 's', 's', // assertionValue AssertionValue (OCTET STRING) }
(byte) 0x87, 0x02, 'o', // present [7]
'u', // AttributeDescription ::= LDAPString
(byte) 0xA2, // not [2] Filter,
0x14, (byte) 0xA5, // greaterOrEqual [5]
0x12, // attributeDesc AttributeDescription (LDAPString),
0x04, 0x0B, 'o', 'b', 'j', 'e', 'c', 't', 'c', 'l', 'a', 's', 's', // assertionValue AssertionValue (OCTET STRING) }
0x04, 0x03, 't', 't', 't', // attributes AttributeDescriptionList }
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());
}
assertEquals(TLVStateEnum.PDU_DECODED, ldapMessageContainer.getState());
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());
// (& (...
ExprNode filter = searchRequest.getFilter();
AndNode andNode = (AndNode) filter;
assertNotNull(andNode);
List<ExprNode> andNodes = andNode.getChildren();
// (& (| (...
assertEquals(2, andNodes.size());
OrNode orFilter = (OrNode) andNodes.get(0);
assertNotNull(orFilter);
// (& (| (objectclass=*) (...
List<ExprNode> orNodes = orFilter.getChildren();
assertEquals(2, orNodes.size());
PresenceNode presenceNode = (PresenceNode) orNodes.get(0);
assertNotNull(presenceNode);
assertEquals("objectclass", presenceNode.getAttribute());
// (& (| (objectclass=*) (ou=*) ) (...
presenceNode = (PresenceNode) orNodes.get(1);
assertNotNull(presenceNode);
assertEquals("ou", presenceNode.getAttribute());
// (& (| (objectclass=*) (ou=*) ) (! ...
NotNode notNode = (NotNode) andNodes.get(1);
assertNotNull(notNode);
// (& (| (objectclass=*) (ou=*) ) (! (objectclass>=ttt) ) )
GreaterEqNode<?> greaterOrEqual = (GreaterEqNode<?>) notNode.getFirstChild();
assertNotNull(greaterOrEqual);
assertEquals("objectclass", greaterOrEqual.getAttribute());
assertEquals("ttt", greaterOrEqual.getValue().getValue());
// 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(0x7B, bb.limit());
String encodedPdu = Strings.dumpBytes(bb.array());
assertEquals(encodedPdu.substring(0, 0x6C), decodedPdu.substring(0, 0x6C));
} catch (EncoderException ee) {
ee.printStackTrace();
fail(ee.getMessage());
}
}
Aggregations