use of org.apache.directory.api.ldap.model.filter.EqualityNode 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.EqualityNode 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.EqualityNode in project directory-ldap-api by apache.
the class ApiLdapModelOsgiTest method useBundleClasses.
@Override
protected void useBundleClasses() throws Exception {
// uses FastDnParser
new Dn("dc=example,dc=com");
// uses ComplexDnparser (antlr based)
new Dn("cn=a+sn=b,dc=example,dc=com");
new Value("foo");
new DefaultAttribute("cn");
new DefaultEntry();
AttributeUtils.toJndiAttribute(new DefaultAttribute("cn"));
new BindRequestImpl();
new EqualityNode<String>("cn", "foo");
new LdapUrl("ldap://ldap.example.com:10389/dc=example,dc=com?objectclass");
new ObjectClassDescriptionSchemaParser().parse("( 2.5.6.0 NAME 'top' DESC 'top of the superclass chain' ABSTRACT MUST objectClass )");
SchemaObject schemaObject = new LdapSyntax("1.2.3");
new Registries().getGlobalOidRegistry().register(schemaObject);
new Registries().getLoadedSchemas();
}
use of org.apache.directory.api.ldap.model.filter.EqualityNode in project directory-ldap-api by apache.
the class SearchRequestTest method testRequestWithEqualityMatchFilterWithEmptyValue.
/**
* Test parsing of a request with an Equality Filter with an empty value
*/
@Test
public void testRequestWithEqualityMatchFilterWithEmptyValue() {
Dsmlv2Parser parser = null;
try {
parser = newParser();
parser.setInput(SearchRequestTest.class.getResource("filters/request_with_equalityMatch_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 EqualityNode);
EqualityNode<?> equalityFilter = (EqualityNode<?>) filter;
assertEquals("sn", equalityFilter.getAttribute());
assertEquals("", equalityFilter.getValue().getValue());
}
use of org.apache.directory.api.ldap.model.filter.EqualityNode in project directory-ldap-api by apache.
the class SearchRequestTest method testRequestWithEqualityMatchFilter.
/**
* Test parsing of a request with an Equality Filter
*/
@Test
public void testRequestWithEqualityMatchFilter() {
Dsmlv2Parser parser = null;
try {
parser = newParser();
parser.setInput(SearchRequestTest.class.getResource("filters/request_with_equalityMatch.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 EqualityNode);
EqualityNode<?> equalityFilter = (EqualityNode<?>) filter;
assertEquals("sn", equalityFilter.getAttribute());
assertEquals("foobar", equalityFilter.getValue().getValue());
}
Aggregations