avseq1234 closed pull request #3235: Nifi 1893 URL: https://github.com/apache/nifi/pull/3235
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/nifi-nar-bundles/nifi-validator-bundles/nifi-validator-nar/pom.xml b/nifi-nar-bundles/nifi-validator-bundles/nifi-validator-nar/pom.xml new file mode 100644 index 0000000000..50b1ee46ce --- /dev/null +++ b/nifi-nar-bundles/nifi-validator-bundles/nifi-validator-nar/pom.xml @@ -0,0 +1,41 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.nifi</groupId> + <artifactId>nifi-validator-bundles</artifactId> + <version>1.8.0-SNAPSHOT</version> + </parent> + + <artifactId>nifi-validator-nar</artifactId> + <version>1.0-SNAPSHOT</version> + <packaging>nar</packaging> + <properties> + <maven.javadoc.skip>true</maven.javadoc.skip> + <source.skip>true</source.skip> + </properties> + + <dependencies> + <dependency> + <groupId>org.apache.nifi</groupId> + <artifactId>nifi-validator-processors</artifactId> + <version>1.8.0-SNAPSHOT</version> + </dependency> + </dependencies> + +</project> diff --git a/nifi-nar-bundles/nifi-validator-bundles/nifi-validator-processors/pom.xml b/nifi-nar-bundles/nifi-validator-bundles/nifi-validator-processors/pom.xml new file mode 100644 index 0000000000..ddfaf0108c --- /dev/null +++ b/nifi-nar-bundles/nifi-validator-bundles/nifi-validator-processors/pom.xml @@ -0,0 +1,65 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.nifi</groupId> + <artifactId>nifi-validator-bundles</artifactId> + <version>1.8.0-SNAPSHOT</version> + </parent> + + <artifactId>nifi-validator-processors</artifactId> + <packaging>jar</packaging> + + <dependencies> + <dependency> + <groupId>org.apache.nifi</groupId> + <artifactId>nifi-api</artifactId> + </dependency> + <dependency> + <groupId>org.apache.nifi</groupId> + <artifactId>nifi-utils</artifactId> + <version>1.8.0</version> + </dependency> + <dependency> + <groupId>org.apache.nifi</groupId> + <artifactId>nifi-mock</artifactId> + <version>1.8.0</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-simple</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>com.github.fge</groupId> + <artifactId>json-schema-validator</artifactId> + <version>2.2.6</version> + </dependency> + <dependency> + <groupId>commons-io</groupId> + <artifactId>commons-io</artifactId> + <version>2.6</version> + </dependency> + </dependencies> +</project> diff --git a/nifi-nar-bundles/nifi-validator-bundles/nifi-validator-processors/src/main/java/org/apache/nifi/processors/validator/NiFiJsonValidator.java b/nifi-nar-bundles/nifi-validator-bundles/nifi-validator-processors/src/main/java/org/apache/nifi/processors/validator/NiFiJsonValidator.java new file mode 100644 index 0000000000..408862cba7 --- /dev/null +++ b/nifi-nar-bundles/nifi-validator-bundles/nifi-validator-processors/src/main/java/org/apache/nifi/processors/validator/NiFiJsonValidator.java @@ -0,0 +1,132 @@ +/* + * 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.validator; + +import com.github.fge.jackson.JsonLoader; +import com.github.fge.jsonschema.core.report.ProcessingReport; +import com.github.fge.jsonschema.main.JsonSchemaFactory; +import org.apache.commons.io.IOUtils; +import org.apache.nifi.components.PropertyDescriptor; +import org.apache.nifi.expression.ExpressionLanguageScope; +import org.apache.nifi.flowfile.FlowFile; +import org.apache.nifi.annotation.behavior.ReadsAttribute; +import org.apache.nifi.annotation.behavior.ReadsAttributes; +import org.apache.nifi.annotation.behavior.WritesAttribute; +import org.apache.nifi.annotation.behavior.WritesAttributes; +import org.apache.nifi.annotation.lifecycle.OnScheduled; +import org.apache.nifi.annotation.documentation.CapabilityDescription; +import org.apache.nifi.annotation.documentation.SeeAlso; +import org.apache.nifi.annotation.documentation.Tags; +import org.apache.nifi.processor.exception.ProcessException; +import org.apache.nifi.processor.AbstractProcessor; +import org.apache.nifi.processor.ProcessContext; +import org.apache.nifi.processor.ProcessSession; +import org.apache.nifi.processor.ProcessorInitializationContext; +import org.apache.nifi.processor.Relationship; +import org.apache.nifi.processor.io.InputStreamCallback; +import org.apache.nifi.processor.util.StandardValidators; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +@Tags({"json"}) +@CapabilityDescription("Provide a description") +@SeeAlso({}) +@ReadsAttributes({@ReadsAttribute(attribute="", description="")}) +@WritesAttributes({@WritesAttribute(attribute="", description="")}) +public class NiFiJsonValidator extends AbstractProcessor { + + public static final PropertyDescriptor JSON_SCHEMA = new PropertyDescriptor + .Builder().name("JSON Schema") + .description("Input the JSON Schema for JSON validation") + .required(true) + .expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES) + .addValidator(StandardValidators.NON_EMPTY_VALIDATOR) + .build(); + + public static final Relationship REL_SUCCESS = new Relationship.Builder() + .name("success") + .description("A FlowFile is routed to this relationship after JSON validation is successful.") + .build(); + + public static final Relationship REL_FAILURE = new Relationship.Builder() + .name("failure") + .description("A FlowFile is routed to this relationship after JSON validation is failed.") + .build(); + + private List<PropertyDescriptor> descriptors; + + private Set<Relationship> relationships; + + @Override + protected void init(final ProcessorInitializationContext context) { + final List<PropertyDescriptor> descriptors = new ArrayList<PropertyDescriptor>(); + descriptors.add(JSON_SCHEMA); + this.descriptors = Collections.unmodifiableList(descriptors); + + final Set<Relationship> relationships = new HashSet<Relationship>(); + relationships.add(REL_SUCCESS); + relationships.add(REL_FAILURE); + this.relationships = Collections.unmodifiableSet(relationships); + } + + @Override + public Set<Relationship> getRelationships() { + return this.relationships; + } + + @Override + public final List<PropertyDescriptor> getSupportedPropertyDescriptors() { + return descriptors; + } + + @OnScheduled + public void onScheduled(final ProcessContext context) { + + } + + @Override + public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException { + FlowFile flowFile = session.get(); + if ( flowFile == null ) { + return; + } + + String jsonSchema = context.getProperty(JSON_SCHEMA).evaluateAttributeExpressions().getValue(); + com.github.fge.jsonschema.main.JsonValidator jsonValidator = JsonSchemaFactory.byDefault().getValidator(); + + boolean[] isSuccess = new boolean[1]; + session.read(flowFile, new InputStreamCallback() { + @Override + public void process(InputStream in) throws IOException { + String content = IOUtils.toString(in , StandardCharsets.UTF_8); + ProcessingReport report = jsonValidator.validateUnchecked(JsonLoader.fromString(jsonSchema) , JsonLoader.fromString(content)); + isSuccess[0] = report.isSuccess(); + } + } ); + + session.transfer( flowFile , isSuccess[0] ? REL_SUCCESS : REL_FAILURE); + } +} + + diff --git a/nifi-nar-bundles/nifi-validator-bundles/nifi-validator-processors/src/main/resources/META-INF/services/org.apache.nifi.processor.Processor b/nifi-nar-bundles/nifi-validator-bundles/nifi-validator-processors/src/main/resources/META-INF/services/org.apache.nifi.processor.Processor new file mode 100644 index 0000000000..aeeb296dd3 --- /dev/null +++ b/nifi-nar-bundles/nifi-validator-bundles/nifi-validator-processors/src/main/resources/META-INF/services/org.apache.nifi.processor.Processor @@ -0,0 +1,15 @@ +# 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. +org.apache.nifi.processors.validator.NiFiJsonValidator \ No newline at end of file diff --git a/nifi-nar-bundles/nifi-validator-bundles/nifi-validator-processors/src/test/java/org/apache/nifi/processors/validator/NiFiJsonValidatorTest.java b/nifi-nar-bundles/nifi-validator-bundles/nifi-validator-processors/src/test/java/org/apache/nifi/processors/validator/NiFiJsonValidatorTest.java new file mode 100644 index 0000000000..456ad75a18 --- /dev/null +++ b/nifi-nar-bundles/nifi-validator-bundles/nifi-validator-processors/src/test/java/org/apache/nifi/processors/validator/NiFiJsonValidatorTest.java @@ -0,0 +1,141 @@ +/* + * 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.validator; + +import org.apache.nifi.util.TestRunner; +import org.apache.nifi.util.TestRunners; +import org.junit.Before; +import org.junit.Test; + + +public class NiFiJsonValidatorTest { + + private TestRunner testRunner; + + @Before + public void init() { + testRunner = TestRunners.newTestRunner(NiFiJsonValidator.class); + } + + @Test + public void testJsonValidatorSuccess() { + + String jsonSchema = "{\n" + + " \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n" + + " \"type\": \"object\",\n" + + " \"properties\": {\n" + + " \"name\": {\n" + + " \"type\": \"string\"\n" + + " },\n" + + " \"age\": {\n" + + " \"type\": \"integer\"\n" + + " },\n" + + " \"weight\": {\n" + + " \"type\": \"integer\"\n" + + " },\n" + + " \"title\": {\n" + + " \"type\": \"string\"\n" + + " }\n" + + " },\n" + + " \"required\": [\n" + + " \"name\",\n" + + " \"age\",\n" + + " \"weight\",\n" + + " \"title\"\n" + + " ]\n" + + "}"; + + testRunner.setProperty( NiFiJsonValidator.JSON_SCHEMA ,jsonSchema ); + testRunner.setIncomingConnection(true); + + String content1 = "{\n" + + " \"name\": \"Steven\",\n" + + " \"age\": 20,\n" + + " \"weight\": 100,\n" + + " \"title\": \"Engineer\"\n" + + "}"; + System.out.println("flow file content"); + + String content2 = "{\n" + + " \"name\": \"Jason\",\n" + + " \"age\": 22,\n" + + " \"weight\": 110,\n" + + " \"title\": \"Teacher\"\n" + + "}"; + + testRunner.enqueue( content1 ); + testRunner.enqueue( content2 ); + testRunner.run(2); + testRunner.assertAllFlowFilesTransferred( NiFiJsonValidator.REL_SUCCESS , 2); + + } + + @Test + public void testJsonValidatorFailed() { + + String jsonSchema = "{\n" + + " \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n" + + " \"type\": \"object\",\n" + + " \"properties\": {\n" + + " \"name\": {\n" + + " \"type\": \"string\"\n" + + " },\n" + + " \"age\": {\n" + + " \"type\": \"integer\"\n" + + " },\n" + + " \"weight\": {\n" + + " \"type\": \"integer\"\n" + + " },\n" + + " \"title\": {\n" + + " \"type\": \"string\"\n" + + " }\n" + + " },\n" + + " \"required\": [\n" + + " \"name\",\n" + + " \"age\",\n" + + " \"weight\",\n" + + " \"title\"\n" + + " ]\n" + + "}"; + + + testRunner.setProperty( NiFiJsonValidator.JSON_SCHEMA ,jsonSchema ); + testRunner.setIncomingConnection(true); + + String content1 = "{\n" + + " \"name\": \"Steven\",\n" + + " \"age\": 20,\n" + + " \"weight\": 100,\n" + + " \"title\": \"Engineer\"\n" + + "}"; + + String content2 = "{\n" + + " \"name\": \"Jason\",\n" + + " \"weight\": 110,\n" + + " \"title\": \"Teacher\"\n" + + "}"; + + testRunner.enqueue( content1 ); + testRunner.enqueue( content2 ); + testRunner.run(2); + + testRunner.assertTransferCount(NiFiJsonValidator.REL_SUCCESS , 1); + testRunner.assertTransferCount(NiFiJsonValidator.REL_FAILURE , 1); + + } + +} diff --git a/nifi-nar-bundles/nifi-validator-bundles/pom.xml b/nifi-nar-bundles/nifi-validator-bundles/pom.xml new file mode 100644 index 0000000000..afacac0d03 --- /dev/null +++ b/nifi-nar-bundles/nifi-validator-bundles/pom.xml @@ -0,0 +1,35 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.nifi</groupId> + <artifactId>nifi-nar-bundles</artifactId> + <version>1.8.0</version> + </parent> + + <groupId>org.apache.nifi</groupId> + <artifactId>nifi-validator-bundles</artifactId> + <version>1.8.0-SNAPSHOT</version> + <packaging>pom</packaging> + + <modules> + <module>nifi-validator-processors</module> + <module>nifi-validator-nar</module> + </modules> + +</project> ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
