From 8254b75437a5b5a168259c7e46460ee3a7bc52f0 Mon Sep 17 00:00:00 2001 From: Mark Payne Date: Wed, 10 Dec 2014 11:47:00 -0500 Subject: [PATCH] NIFI-52: Created new implementation of PreparedQuery so that we can still prepare a query even if the expression is invalid --- .../language/InvalidPreparedQuery.java | 71 +++++++++++++++++++ .../attribute/expression/language/Query.java | 44 ++++++------ .../exception/IllegalAttributeException.java | 1 + 3 files changed, 96 insertions(+), 20 deletions(-) create mode 100644 commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/InvalidPreparedQuery.java diff --git a/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/InvalidPreparedQuery.java b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/InvalidPreparedQuery.java new file mode 100644 index 0000000000..e23bcc0972 --- /dev/null +++ b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/InvalidPreparedQuery.java @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.attribute.expression.language; + +import java.util.Map; + +import org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageException; +import org.apache.nifi.expression.AttributeValueDecorator; +import org.apache.nifi.flowfile.FlowFile; +import org.apache.nifi.processor.exception.ProcessException; + + +/** + * An implementation of PreparedQuery that throws an {@link AttributeExpressionLanguageException} when attempting + * to evaluate the query. This allows a PreparedQuery to be created, even though it can't + * be evaluated. + */ +public class InvalidPreparedQuery implements PreparedQuery { + private final String query; + private final String explanation; + + public InvalidPreparedQuery(final String query, final String explanation) { + this.query = query; + this.explanation = explanation; + } + + @Override + public String evaluateExpressions(final FlowFile flowFile, final AttributeValueDecorator decorator) throws ProcessException { + throw new AttributeExpressionLanguageException("Invalid Expression: " + query + " due to " + explanation); + } + + @Override + public String evaluateExpressions() throws ProcessException { + throw new AttributeExpressionLanguageException("Invalid Expression: " + query + " due to " + explanation); + } + + @Override + public String evaluateExpressions(final AttributeValueDecorator decorator) throws ProcessException { + throw new AttributeExpressionLanguageException("Invalid Expression: " + query + " due to " + explanation); + } + + @Override + public String evaluateExpressions(final FlowFile flowFile) throws ProcessException { + throw new AttributeExpressionLanguageException("Invalid Expression: " + query + " due to " + explanation); + } + + @Override + public String evaluateExpressions(final Map attributes) throws ProcessException { + throw new AttributeExpressionLanguageException("Invalid Expression: " + query + " due to " + explanation); + } + + @Override + public String evaluateExpressions(final Map attributes, final AttributeValueDecorator decorator) throws ProcessException { + throw new AttributeExpressionLanguageException("Invalid Expression: " + query + " due to " + explanation); + } + +} diff --git a/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/Query.java b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/Query.java index 6d9cc91ee2..f4c1c0dfb5 100644 --- a/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/Query.java +++ b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/Query.java @@ -569,29 +569,33 @@ public class Query { return new EmptyPreparedQuery(query.replace("$$", "$")); } - final List substrings = new ArrayList<>(); - final Map trees = new HashMap<>(); - - int lastIndex = 0; - for (final Range range : ranges) { - if (range.getStart() > lastIndex) { - substrings.add(query.substring(lastIndex, range.getStart()).replace("$$", "$")); + try { + final List substrings = new ArrayList<>(); + final Map trees = new HashMap<>(); + + int lastIndex = 0; + for (final Range range : ranges) { + if (range.getStart() > lastIndex) { + substrings.add(query.substring(lastIndex, range.getStart()).replace("$$", "$")); + lastIndex = range.getEnd() + 1; + } + + final String treeText = query.substring(range.getStart(), range.getEnd() + 1).replace("$$", "$"); + substrings.add(treeText); + trees.put(treeText, Query.compileTree(treeText)); lastIndex = range.getEnd() + 1; } - - final String treeText = query.substring(range.getStart(), range.getEnd() + 1).replace("$$", "$"); - substrings.add(treeText); - trees.put(treeText, Query.compileTree(treeText)); - lastIndex = range.getEnd() + 1; + + final Range lastRange = ranges.get(ranges.size() - 1); + if (lastRange.getEnd() + 1 < query.length()) { + final String treeText = query.substring(lastRange.getEnd() + 1).replace("$$", "$"); + substrings.add(treeText); + } + + return new StandardPreparedQuery(substrings, trees); + } catch (final AttributeExpressionLanguageParsingException e) { + return new InvalidPreparedQuery(query, e.getMessage()); } - - final Range lastRange = ranges.get(ranges.size() - 1); - if (lastRange.getEnd() + 1 < query.length()) { - final String treeText = query.substring(lastRange.getEnd() + 1).replace("$$", "$"); - substrings.add(treeText); - } - - return new StandardPreparedQuery(substrings, trees); } public static Query compile(final String query) throws AttributeExpressionLanguageParsingException { diff --git a/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/exception/IllegalAttributeException.java b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/exception/IllegalAttributeException.java index 4a9a9c50b9..f6f32cac7d 100644 --- a/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/exception/IllegalAttributeException.java +++ b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/exception/IllegalAttributeException.java @@ -17,6 +17,7 @@ package org.apache.nifi.attribute.expression.language.exception; public class IllegalAttributeException extends RuntimeException { + private static final long serialVersionUID = 12348721897342L; public IllegalAttributeException() { super();