Moving the JsonPath Validator to the JsonPathUtil class so that it can be reused by other processors.

This commit is contained in:
Aldrin Piri 2015-02-17 09:44:30 -05:00
parent 0a19ada0a3
commit 5a81f19b25
2 changed files with 50 additions and 15 deletions

View File

@ -16,7 +16,10 @@
*/ */
package org.apache.nifi.processors.standard; package org.apache.nifi.processors.standard;
import com.jayway.jsonpath.*; import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.PathNotFoundException;
import com.jayway.jsonpath.spi.json.JsonProvider; import com.jayway.jsonpath.spi.json.JsonProvider;
import net.minidev.json.JSONValue; import net.minidev.json.JSONValue;
import org.apache.nifi.annotation.behavior.EventDriven; import org.apache.nifi.annotation.behavior.EventDriven;
@ -27,13 +30,13 @@ import org.apache.nifi.annotation.documentation.Tags;
import org.apache.nifi.components.PropertyDescriptor; import org.apache.nifi.components.PropertyDescriptor;
import org.apache.nifi.components.ValidationContext; import org.apache.nifi.components.ValidationContext;
import org.apache.nifi.components.ValidationResult; import org.apache.nifi.components.ValidationResult;
import org.apache.nifi.components.Validator;
import org.apache.nifi.flowfile.FlowFile; import org.apache.nifi.flowfile.FlowFile;
import org.apache.nifi.logging.ProcessorLog; import org.apache.nifi.logging.ProcessorLog;
import org.apache.nifi.processor.*; import org.apache.nifi.processor.*;
import org.apache.nifi.processor.exception.ProcessException; import org.apache.nifi.processor.exception.ProcessException;
import org.apache.nifi.processor.io.InputStreamCallback; import org.apache.nifi.processor.io.InputStreamCallback;
import org.apache.nifi.processor.io.OutputStreamCallback; import org.apache.nifi.processor.io.OutputStreamCallback;
import org.apache.nifi.processors.standard.util.JsonPathUtils;
import org.apache.nifi.stream.io.BufferedInputStream; import org.apache.nifi.stream.io.BufferedInputStream;
import org.apache.nifi.stream.io.BufferedOutputStream; import org.apache.nifi.stream.io.BufferedOutputStream;
import org.apache.nifi.util.BooleanHolder; import org.apache.nifi.util.BooleanHolder;
@ -147,7 +150,7 @@ public class EvaluateJsonPath extends AbstractProcessor {
return new PropertyDescriptor.Builder() return new PropertyDescriptor.Builder()
.name(propertyDescriptorName) .name(propertyDescriptorName)
.expressionLanguageSupported(false) .expressionLanguageSupported(false)
.addValidator(new JsonPathValidator()) .addValidator(JsonPathUtils.JSON_PATH_VALIDATOR)
.required(false) .required(false)
.dynamic(true) .dynamic(true)
.build(); .build();
@ -278,17 +281,5 @@ public class EvaluateJsonPath extends AbstractProcessor {
return (obj instanceof String); return (obj instanceof String);
} }
private static class JsonPathValidator implements Validator {
@Override
public ValidationResult validate(String subject, String input, ValidationContext context) {
String error = null;
try {
JsonPath compile = JsonPath.compile(input);
} catch (InvalidPathException ipe) {
error = ipe.toString();
}
return new ValidationResult.Builder().valid(error == null).explanation(error).build();
}
}
} }

View File

@ -0,0 +1,44 @@
/*
* 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.processors.standard.util;
import com.jayway.jsonpath.InvalidPathException;
import com.jayway.jsonpath.JsonPath;
import org.apache.nifi.components.ValidationContext;
import org.apache.nifi.components.ValidationResult;
import org.apache.nifi.components.Validator;
/**
* Provides utilities for interacting with JsonPath expressions and results
*
* @see <a href="https://github.com/jayway/JsonPath">https://github.com/jayway/JsonPath</a>
*/
public class JsonPathUtils {
public static final Validator JSON_PATH_VALIDATOR = new Validator() {
@Override
public ValidationResult validate(final String subject, final String input, final ValidationContext context) {
String error = null;
try {
JsonPath compile = JsonPath.compile(input);
} catch (InvalidPathException ipe) {
error = ipe.toString();
}
return new ValidationResult.Builder().valid(error == null).explanation(error).build();
}
};
}