Search in sources :

Example 1 with SearchParseException

use of org.apache.cxf.jaxrs.ext.search.SearchParseException in project syncope by apache.

the class SyncopeFiqlParser method parseComparison.

@Override
protected ASTNode<T> parseComparison(final String expr) throws SearchParseException {
    Matcher m = comparatorsPattern.matcher(expr);
    if (m.find()) {
        String propertyName = expr.substring(0, m.start(1));
        String operator = m.group(1);
        String value = expr.substring(m.end(1));
        if ("".equals(value)) {
            throw new SearchParseException("Not a comparison expression: " + expr);
        }
        String name = unwrapSetter(propertyName);
        name = getActualSetterName(name);
        TypeInfoObject castedValue = parseType(propertyName, name, value);
        if (castedValue != null) {
            return new SyncopeComparison(name, operator, castedValue);
        } else {
            return null;
        }
    } else {
        throw new SearchParseException("Not a comparison expression: " + expr);
    }
}
Also used : SearchParseException(org.apache.cxf.jaxrs.ext.search.SearchParseException) Matcher(java.util.regex.Matcher)

Example 2 with SearchParseException

use of org.apache.cxf.jaxrs.ext.search.SearchParseException in project cxf by apache.

the class FiqlParser method parseAndsOrsBrackets.

private ASTNode<T> parseAndsOrsBrackets(String expr) throws SearchParseException {
    List<String> subexpressions = new ArrayList<>();
    List<String> operators = new ArrayList<>();
    int level = 0;
    int lastIdx = 0;
    for (int idx = 0; idx < expr.length(); idx++) {
        char c = expr.charAt(idx);
        if (c == '(') {
            level++;
        } else if (c == ')') {
            level--;
            if (level < 0) {
                throw new SearchParseException(String.format("Unexpected closing bracket at position %d", idx));
            }
        }
        String cs = Character.toString(c);
        boolean isOperator = AND.equals(cs) || OR.equals(cs);
        if (level == 0 && isOperator) {
            String s1 = expr.substring(lastIdx, idx);
            String s2 = expr.substring(idx, idx + 1);
            subexpressions.add(s1);
            operators.add(s2);
            lastIdx = idx + 1;
        }
        boolean isEnd = idx == expr.length() - 1;
        if (isEnd) {
            String s1 = expr.substring(lastIdx, idx + 1);
            subexpressions.add(s1);
            operators.add(null);
            lastIdx = idx + 1;
        }
    }
    if (level != 0) {
        throw new SearchParseException(String.format("Unmatched opening and closing brackets in expression: %s", expr));
    }
    if (operators.get(operators.size() - 1) != null) {
        String op = operators.get(operators.size() - 1);
        String ex = subexpressions.get(subexpressions.size() - 1);
        throw new SearchParseException("Dangling operator at the end of expression: ..." + ex + op);
    }
    // looking for adjacent ANDs then group them into ORs
    // Note: in case not ANDs is found (e.g only ORs) every single subexpression is
    // treated as "single item group of ANDs"
    int from = 0;
    int to = 0;
    SubExpression ors = new SubExpression(OR);
    while (to < operators.size()) {
        while (to < operators.size() && AND.equals(operators.get(to))) {
            to++;
        }
        SubExpression ands = new SubExpression(AND);
        for (; from <= to; from++) {
            String subex = subexpressions.get(from);
            ASTNode<T> node;
            if (subex.startsWith("(")) {
                node = parseAndsOrsBrackets(subex.substring(1, subex.length() - 1));
            } else {
                node = parseComparison(subex);
            }
            if (node != null) {
                ands.add(node);
            }
        }
        to = from;
        if (ands.getSubnodes().size() == 1) {
            ors.add(ands.getSubnodes().get(0));
        } else {
            ors.add(ands);
        }
    }
    if (ors.getSubnodes().size() == 1) {
        return ors.getSubnodes().get(0);
    }
    return ors;
}
Also used : SearchParseException(org.apache.cxf.jaxrs.ext.search.SearchParseException) ArrayList(java.util.ArrayList)

Example 3 with SearchParseException

use of org.apache.cxf.jaxrs.ext.search.SearchParseException in project cxf by apache.

the class FiqlParser method parseComparison.

protected ASTNode<T> parseComparison(String expr) throws SearchParseException {
    Matcher m = comparatorsPattern.matcher(expr);
    if (m.find()) {
        String propertyName = expr.substring(0, m.start(1));
        String operator = m.group(1);
        String value = expr.substring(m.end(1));
        if ("".equals(value)) {
            throw new SearchParseException("Not a comparison expression: " + expr);
        }
        String name = unwrapSetter(propertyName);
        name = getActualSetterName(name);
        TypeInfoObject castedValue = parseType(propertyName, name, value);
        if (castedValue != null) {
            return new Comparison(name, operator, castedValue);
        }
        return null;
    }
    throw new SearchParseException("Not a comparison expression: " + expr);
}
Also used : SearchParseException(org.apache.cxf.jaxrs.ext.search.SearchParseException) Matcher(java.util.regex.Matcher)

Example 4 with SearchParseException

use of org.apache.cxf.jaxrs.ext.search.SearchParseException in project cxf by apache.

the class FiqlParserTest method testWildcard.

@Test
public void testWildcard() throws SearchParseException {
    SearchCondition<Condition> filter = parser.parse("name==*");
    try {
        filter.isMet(new Condition("foobaz", 0, null));
        fail("Failure expected on an invalid search condition");
    } catch (SearchParseException ex) {
    // expected
    }
}
Also used : SearchCondition(org.apache.cxf.jaxrs.ext.search.SearchCondition) SearchParseException(org.apache.cxf.jaxrs.ext.search.SearchParseException) Test(org.junit.Test)

Example 5 with SearchParseException

use of org.apache.cxf.jaxrs.ext.search.SearchParseException in project cxf by apache.

the class SQLPrinterVisitor method visit.

public void visit(SearchCondition<T> sc) {
    StringBuilder sb = getStringBuilder();
    PrimitiveStatement statement = sc.getStatement();
    if (statement != null) {
        if (statement.getProperty() != null) {
            String property = statement.getProperty();
            String[] properties = property.split("\\.");
            if (properties.length > 2) {
                throw new SearchParseException("SQL Visitor supports only a single JOIN");
            } else if (properties.length == 2) {
                if (joinDone) {
                    throw new SearchParseException("SQL Visitor has already created JOIN");
                }
                joinDone = true;
                String joinTable = getRealPropertyName(properties[0]);
                // Joining key can be pre-configured
                String joiningKey = primaryTable;
                if (joiningKey.endsWith("s")) {
                    joiningKey = joiningKey.substring(0, joiningKey.length() - 1);
                }
                joiningKey += "_id";
                topBuilder.append(" left join ").append(joinTable);
                topBuilder.append(" on ").append(primaryTable).append(".id").append(" = ").append(joinTable).append('.').append(joiningKey);
                property = joinTable + "." + getRealPropertyName(properties[1]);
            }
            String name = getRealPropertyName(property);
            String originalValue = getPropertyValue(name, statement.getValue());
            validatePropertyValue(name, originalValue);
            String value = SearchUtils.toSqlWildcardString(originalValue, isWildcardStringMatch());
            value = SearchUtils.duplicateSingleQuoteIfNeeded(value);
            if (tableAlias != null) {
                name = tableAlias + "." + name;
            }
            sb.append(name).append(' ').append(SearchUtils.conditionTypeToSqlOperator(sc.getConditionType(), value, originalValue)).append(' ').append("'").append(value).append(// NOPMD
            "'");
        }
    } else {
        boolean first = true;
        for (SearchCondition<T> condition : sc.getSearchConditions()) {
            if (!first) {
                sb.append(' ').append(sc.getConditionType().toString()).append(' ');
            } else {
                first = false;
            }
            sb.append('(');
            saveStringBuilder(sb);
            condition.accept(this);
            sb = getStringBuilder();
            sb.append(')');
        }
    }
    saveStringBuilder(sb);
}
Also used : PrimitiveStatement(org.apache.cxf.jaxrs.ext.search.PrimitiveStatement) SearchParseException(org.apache.cxf.jaxrs.ext.search.SearchParseException)

Aggregations

SearchParseException (org.apache.cxf.jaxrs.ext.search.SearchParseException)6 Matcher (java.util.regex.Matcher)2 SearchCondition (org.apache.cxf.jaxrs.ext.search.SearchCondition)2 ArrayList (java.util.ArrayList)1 AndSearchCondition (org.apache.cxf.jaxrs.ext.search.AndSearchCondition)1 OrSearchCondition (org.apache.cxf.jaxrs.ext.search.OrSearchCondition)1 PrimitiveSearchCondition (org.apache.cxf.jaxrs.ext.search.PrimitiveSearchCondition)1 PrimitiveStatement (org.apache.cxf.jaxrs.ext.search.PrimitiveStatement)1 ODataApplicationException (org.apache.olingo.odata2.api.exception.ODataApplicationException)1 ODataMessageException (org.apache.olingo.odata2.api.exception.ODataMessageException)1 FilterExpression (org.apache.olingo.odata2.api.uri.expression.FilterExpression)1 Test (org.junit.Test)1