Search in sources :

Example 1 with ParseException

use of datawave.query.language.parser.ParseException in project datawave by NationalSecurityAgency.

the class EvaluationOnly method toString.

@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("((_Eval_ = true) && (");
    List<String> params = getParameterList();
    if (params.size() != 1) {
        BadRequestQueryException qe = new BadRequestQueryException(DatawaveErrorCode.INVALID_FUNCTION_ARGUMENTS, MessageFormat.format("{0}", this.name));
        throw new IllegalArgumentException(qe);
    } else {
        try {
            sb.append(parser.parse(params.get(0)).getOriginalQuery());
        } catch (ParseException e) {
            BadRequestQueryException qe = new BadRequestQueryException(DatawaveErrorCode.INVALID_FUNCTION_ARGUMENTS, MessageFormat.format("{0}", this.name));
            throw new IllegalArgumentException(qe);
        }
    }
    sb.append("))");
    return sb.toString();
}
Also used : BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) ParseException(datawave.query.language.parser.ParseException)

Example 2 with ParseException

use of datawave.query.language.parser.ParseException in project datawave by NationalSecurityAgency.

the class LuceneToJexlQueryParser method convertToJexlNode.

public JexlNode convertToJexlNode(String query) throws ParseException {
    // replace open smart quote 147
    query = query.replaceAll("\\u0093", "\"");
    // replace close smart quote 148
    query = query.replaceAll("\\u0094", "\"");
    // replace open left double quote
    query = query.replaceAll("\\u201c", "\"");
    // replace close right double quote
    query = query.replaceAll("\\u201d", "\"");
    JexlNode parsedQuery = null;
    try {
        Locale.setDefault(Locale.US);
        AccumuloSyntaxParser syntaxParser = new AccumuloSyntaxParser();
        syntaxParser.enable_tracing();
        QueryNodeProcessor processor = getQueryNodeProcessor();
        QueryBuilder builder = new JexlTreeBuilder(allowedFunctions);
        org.apache.lucene.queryparser.flexible.core.nodes.QueryNode queryTree = syntaxParser.parse(query, "");
        queryTree = processor.process(queryTree);
        parsedQuery = (JexlNode) builder.build(queryTree);
    } catch (Exception e) {
        throw new ParseException(e);
    }
    return parsedQuery;
}
Also used : JexlTreeBuilder(datawave.query.language.builder.jexl.JexlTreeBuilder) AccumuloSyntaxParser(datawave.query.language.parser.lucene.AccumuloSyntaxParser) QueryBuilder(org.apache.lucene.queryparser.flexible.core.builders.QueryBuilder) ParseException(datawave.query.language.parser.ParseException) QueryNodeProcessor(org.apache.lucene.queryparser.flexible.core.processors.QueryNodeProcessor) ParseException(datawave.query.language.parser.ParseException)

Example 3 with ParseException

use of datawave.query.language.parser.ParseException in project datawave by NationalSecurityAgency.

the class LuceneToJexlUUIDQueryParser method parse.

@Override
public QueryNode parse(String query) throws ParseException {
    // replace open smart quote 147
    query = query.replaceAll("\\u0093", "\"");
    // replace close smart quote 148
    query = query.replaceAll("\\u0094", "\"");
    QueryNode parsedQuery = null;
    parsedQuery = luceneParser.parse(query);
    if (!validUUIDQuery(parsedQuery))
        throw new ParseException("Query: " + query + " not supported with the LuceneToJexlUUIDQueryParser");
    return super.parse(query);
}
Also used : QueryNode(datawave.query.language.tree.QueryNode) ParseException(datawave.query.language.parser.ParseException)

Example 4 with ParseException

use of datawave.query.language.parser.ParseException in project datawave by NationalSecurityAgency.

the class JexlControlledQueryParser method checkIfQueryAllowed.

private void checkIfQueryAllowed(String query) throws ParseException {
    JexlNode node;
    try {
        node = JexlASTHelper.parseJexlQuery(query);
    } catch (Throwable e) {
        throw new ParseException(e.getMessage());
    }
    Set<String> fields = new TreeSet<>();
    List<ASTIdentifier> idList = JexlASTHelper.getIdentifiers(node);
    for (ASTIdentifier id : idList) {
        String fieldName = id.image;
        if (!StringUtils.isEmpty(fieldName)) {
            fieldName = fieldName.trim().toUpperCase();
            if (fieldName.charAt(0) == '$') {
                fields.add(fieldName.substring(1));
            } else {
                fields.add(fieldName);
            }
        }
    }
    fields.removeAll(allowedFields);
    if (!fields.isEmpty()) {
        throw new ParseException("Unallowed field(s) '" + fields + "' for this type of query");
    }
}
Also used : TreeSet(java.util.TreeSet) JexlNode(org.apache.commons.jexl2.parser.JexlNode) ASTIdentifier(org.apache.commons.jexl2.parser.ASTIdentifier) ParseException(datawave.query.language.parser.ParseException)

Example 5 with ParseException

use of datawave.query.language.parser.ParseException in project datawave by NationalSecurityAgency.

the class LuceneQueryParser method parse.

@Override
public datawave.query.language.tree.QueryNode parse(String query) throws ParseException {
    // replace open smart quote 147
    query = query.replaceAll("\\u0093", "\"");
    // replace close smart quote 148
    query = query.replaceAll("\\u0094", "\"");
    datawave.query.language.tree.QueryNode parsedQuery = null;
    try {
        Locale.setDefault(Locale.US);
        AccumuloSyntaxParser syntaxParser = new AccumuloSyntaxParser();
        syntaxParser.enable_tracing();
        org.apache.lucene.queryparser.flexible.core.config.QueryConfigHandler queryConfigHandler = new QueryConfigHandler();
        QueryNodeProcessor processor = new CustomQueryNodeProcessorPipeline(queryConfigHandler);
        QueryBuilder builder = null;
        if (allowedFunctions == null) {
            builder = new AccumuloQueryTreeBuilder();
        } else {
            builder = new AccumuloQueryTreeBuilder(allowedFunctions);
        }
        QueryNode queryTree = syntaxParser.parse(query, "");
        queryTree = processor.process(queryTree);
        parsedQuery = (datawave.query.language.tree.QueryNode) builder.build(queryTree);
        Set<FieldedTerm> positiveFilters = new TreeSet<>();
        if (log.isTraceEnabled()) {
            log.trace("Query before filters extracted: " + parsedQuery.getContents());
        }
        extractFilters(parsedQuery, positiveFilters);
        parsedQuery.setPositiveFilters(positiveFilters);
        if (log.isTraceEnabled()) {
            log.trace("Query after filters extracted: " + parsedQuery.getContents());
        }
    } catch (QueryNodeException | RuntimeException e) {
        throw new ParseException(e);
    }
    return parsedQuery;
}
Also used : QueryNodeException(org.apache.lucene.queryparser.flexible.core.QueryNodeException) CustomQueryNodeProcessorPipeline(datawave.query.language.processor.lucene.CustomQueryNodeProcessorPipeline) AccumuloQueryTreeBuilder(datawave.query.language.builder.lucene.AccumuloQueryTreeBuilder) QueryBuilder(org.apache.lucene.queryparser.flexible.core.builders.QueryBuilder) QueryNode(org.apache.lucene.queryparser.flexible.core.nodes.QueryNode) TreeSet(java.util.TreeSet) ParseException(datawave.query.language.parser.ParseException) QueryNodeProcessor(org.apache.lucene.queryparser.flexible.core.processors.QueryNodeProcessor) RangeFieldedTerm(datawave.query.search.RangeFieldedTerm) FieldedTerm(datawave.query.search.FieldedTerm)

Aggregations

ParseException (datawave.query.language.parser.ParseException)7 FieldedTerm (datawave.query.search.FieldedTerm)2 RangeFieldedTerm (datawave.query.search.RangeFieldedTerm)2 BadRequestQueryException (datawave.webservice.query.exception.BadRequestQueryException)2 TreeSet (java.util.TreeSet)2 QueryBuilder (org.apache.lucene.queryparser.flexible.core.builders.QueryBuilder)2 QueryNodeProcessor (org.apache.lucene.queryparser.flexible.core.processors.QueryNodeProcessor)2 UUIDType (datawave.query.data.UUIDType)1 JexlTreeBuilder (datawave.query.language.builder.jexl.JexlTreeBuilder)1 AccumuloQueryTreeBuilder (datawave.query.language.builder.lucene.AccumuloQueryTreeBuilder)1 AccumuloSyntaxParser (datawave.query.language.parser.lucene.AccumuloSyntaxParser)1 CustomQueryNodeProcessorPipeline (datawave.query.language.processor.lucene.CustomQueryNodeProcessorPipeline)1 QueryNode (datawave.query.language.tree.QueryNode)1 SelectorNode (datawave.query.language.tree.SelectorNode)1 WildcardFieldedTerm (datawave.query.search.WildcardFieldedTerm)1 ASTIdentifier (org.apache.commons.jexl2.parser.ASTIdentifier)1 JexlNode (org.apache.commons.jexl2.parser.JexlNode)1 QueryNodeException (org.apache.lucene.queryparser.flexible.core.QueryNodeException)1 QueryNode (org.apache.lucene.queryparser.flexible.core.nodes.QueryNode)1