exceptionfactory commented on code in PR #8249:
URL: https://github.com/apache/nifi/pull/8249#discussion_r1454379609


##########
nifi-nar-bundles/nifi-jolt-bundle/nifi-jolt-processors/src/main/java/org/apache/nifi/processors/jolt/AbstractJoltTransform.java:
##########
@@ -0,0 +1,255 @@
+/*
+ * 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.jolt;
+
+import com.bazaarvoice.jolt.JoltTransform;
+import com.bazaarvoice.jolt.JsonUtils;
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.nifi.annotation.lifecycle.OnScheduled;
+import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.components.PropertyValue;
+import org.apache.nifi.components.ValidationContext;
+import org.apache.nifi.components.ValidationResult;
+import org.apache.nifi.components.resource.ResourceCardinality;
+import org.apache.nifi.components.resource.ResourceReference;
+import org.apache.nifi.components.resource.ResourceType;
+import org.apache.nifi.expression.ExpressionLanguageScope;
+import org.apache.nifi.flowfile.FlowFile;
+import org.apache.nifi.jolt.util.JoltTransformStrategy;
+import org.apache.nifi.jolt.util.TransformFactory;
+import org.apache.nifi.processor.AbstractProcessor;
+import org.apache.nifi.processor.ProcessContext;
+import org.apache.nifi.processor.util.StandardValidators;
+import org.apache.nifi.util.StringUtils;
+import org.apache.nifi.util.file.classloader.ClassLoaderUtils;
+
+import java.io.BufferedReader;
+import java.io.FilenameFilter;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.UncheckedIOException;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
+public abstract class AbstractJoltTransform extends AbstractProcessor {
+    public static final PropertyDescriptor JOLT_TRANSFORM = new 
PropertyDescriptor.Builder()
+            .name("Jolt Transform")
+            .description("Specifies the Jolt Transformation that should be 
used with the provided specification.")
+            .required(true)
+            .allowableValues(JoltTransformStrategy.class)
+            .defaultValue(JoltTransformStrategy.CHAINR.getValue())
+            .build();
+
+    public static final PropertyDescriptor JOLT_SPEC = new 
PropertyDescriptor.Builder()
+            .name("Jolt Specification")
+            .description("Jolt Specification for transformation of JSON data. 
The value for this property may be the text of a Jolt specification "
+                    + "or the path to a file containing a Jolt specification. 
'Jolt Specification' must be set, or "
+                    + "the value is ignored if the Jolt Sort Transformation is 
selected.")
+            
.expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES)
+            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
+            .identifiesExternalResource(ResourceCardinality.SINGLE, 
ResourceType.FILE, ResourceType.TEXT)
+            .required(false)
+            .build();
+
+    public static final PropertyDescriptor CUSTOM_CLASS = new 
PropertyDescriptor.Builder()
+            .name("Custom Transformation Class Name")
+            .description("Fully Qualified Class Name for Custom 
Transformation")
+            .required(false)
+            
.expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES)
+            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
+            .dependsOn(JOLT_TRANSFORM, JoltTransformStrategy.CUSTOMR)
+            .build();
+
+    public static final PropertyDescriptor MODULES = new 
PropertyDescriptor.Builder()
+            .name("Custom Module Directory")
+            .description("Comma-separated list of paths to files and/or 
directories which contain modules containing custom transformations (that are 
not included on NiFi's classpath).")
+            .required(false)
+            .identifiesExternalResource(ResourceCardinality.MULTIPLE, 
ResourceType.FILE, ResourceType.DIRECTORY)
+            .expressionLanguageSupported(ExpressionLanguageScope.ENVIRONMENT)
+            .dynamicallyModifiesClasspath(true)
+            .dependsOn(JOLT_TRANSFORM, JoltTransformStrategy.CUSTOMR)
+            .build();
+
+    static final PropertyDescriptor TRANSFORM_CACHE_SIZE = new 
PropertyDescriptor.Builder()
+            .name("Transform Cache Size")
+            .description("Compiling a Jolt Transform can be fairly expensive. 
Ideally, this will be done only once. However, if the Expression Language is 
used in the transform, we may need "
+                    + "a new Transform for each FlowFile. This value controls 
how many of those Transforms we cache in memory in order to avoid having to 
compile the Transform each time.")
+            .expressionLanguageSupported(ExpressionLanguageScope.NONE)
+            .addValidator(StandardValidators.POSITIVE_INTEGER_VALIDATOR)
+            .defaultValue("1")
+            .required(true)
+            .build();
+
+    static final List<PropertyDescriptor> PROPERTIES = List.of(JOLT_TRANSFORM, 
JOLT_SPEC, CUSTOM_CLASS,
+            MODULES, TRANSFORM_CACHE_SIZE);
+
+    /**
+     * It is a cache for transform objects. It keeps values indexed by jolt 
specification string.
+     * For some cases the key could be empty. It means that it represents 
default transform (e.g. for custom transform
+     * when there is no jolt-record-spec specified).
+     */
+    private Cache<Optional<String>, JoltTransform> transformCache;
+
+    @OnScheduled
+    public void setup(final ProcessContext context) {
+        int maxTransformsToCache = 
context.getProperty(TRANSFORM_CACHE_SIZE).asInteger();
+        transformCache = Caffeine.newBuilder()
+                .maximumSize(maxTransformsToCache)
+                .build();
+    }
+
+    @Override
+    protected Collection<ValidationResult> customValidate(ValidationContext 
validationContext) {
+        final List<ValidationResult> results = new 
ArrayList<>(super.customValidate(validationContext));
+        final String transform = 
validationContext.getProperty(JOLT_TRANSFORM).getValue();
+        final String customTransform = 
validationContext.getProperty(CUSTOM_CLASS).getValue();
+        final String modulePath = 
validationContext.getProperty(MODULES).isSet() ? 
validationContext.getProperty(MODULES).getValue() : null;
+        final String joltSpecValue = 
validationContext.getProperty(JOLT_SPEC).getValue();
+
+        if (StringUtils.isEmpty(joltSpecValue) && 
!JoltTransformStrategy.SORTR.getValue().equals(transform)) {
+            results.add(new 
ValidationResult.Builder().subject(JOLT_SPEC.getDisplayName()).valid(false).explanation(
+                    "'Jolt Specification' must be set, or the Transformation 
must be 'Sort'").build());
+        } else {
+            final ClassLoader customClassLoader;
+
+            try {
+                if (modulePath != null && 
!validationContext.isExpressionLanguagePresent(modulePath)) {
+                    customClassLoader = 
ClassLoaderUtils.getCustomClassLoader(modulePath, 
this.getClass().getClassLoader(), getJarFilenameFilter());
+                } else {
+                    customClassLoader = this.getClass().getClassLoader();
+                }
+
+                final boolean elPresent = 
validationContext.isExpressionLanguagePresent(joltSpecValue);
+
+                if (elPresent) {
+                    final String invalidExpressionMsg = 
validationContext.newExpressionLanguageCompiler().validateExpression(joltSpecValue,
 true);
+                    if (!StringUtils.isEmpty(invalidExpressionMsg)) {
+                        results.add(new ValidationResult.Builder().valid(false)
+                                .subject(JOLT_SPEC.getDisplayName())
+                                .explanation("Invalid Expression Language: " + 
invalidExpressionMsg)
+                                .build());
+                    }
+                } else if 
(validationContext.isExpressionLanguagePresent(customTransform)) {
+                    final String invalidExpressionMsg = 
validationContext.newExpressionLanguageCompiler().validateExpression(customTransform,
 true);
+                    if (!StringUtils.isEmpty(invalidExpressionMsg)) {
+                        results.add(new ValidationResult.Builder().valid(false)
+                                .subject(CUSTOM_CLASS.getDisplayName())
+                                .explanation("Invalid Expression Language: " + 
invalidExpressionMsg)
+                                .build());
+                    }
+                } else {
+                    if 
(!JoltTransformStrategy.SORTR.getValue().equals(transform)) {
+
+                        //for validation we want to be able to ensure the spec 
is syntactically correct and not try to resolve variables since they may not 
exist yet
+                        final String content = 
readTransform(validationContext.getProperty(JOLT_SPEC));
+                        final Object specJson = 
JsonUtils.jsonToObject(content.replaceAll("\\$\\{", "\\\\\\\\\\$\\{"), 
StandardCharsets.UTF_8.toString());
+
+                        if 
(JoltTransformStrategy.CUSTOMR.getValue().equals(transform)) {
+                            if (StringUtils.isEmpty(customTransform)) {
+                                final String customMessage = "A custom 
transformation class should be provided. ";
+                                results.add(new 
ValidationResult.Builder().valid(false)
+                                        .explanation(customMessage)
+                                        .build());
+                            } else {
+                                
TransformFactory.getCustomTransform(customClassLoader, customTransform, 
specJson);
+                            }
+                        } else {
+                            TransformFactory.getTransform(customClassLoader, 
transform, specJson);
+                        }
+                    }
+                }
+            } catch (final Exception e) {
+                getLogger().info("Processor is not valid - ", e);

Review Comment:
   This log should be removed.
   ```suggestion
   ```



##########
nifi-nar-bundles/nifi-jolt-bundle/nifi-jolt-processors/src/main/java/org/apache/nifi/processors/jolt/JoltTransformJSON.java:
##########
@@ -0,0 +1,196 @@
+/*
+ * 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.jolt;
+
+import com.bazaarvoice.jolt.JoltTransform;
+import com.bazaarvoice.jolt.JsonUtil;
+import com.bazaarvoice.jolt.JsonUtils;
+import com.fasterxml.jackson.core.StreamReadConstraints;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.nifi.annotation.behavior.InputRequirement;
+import org.apache.nifi.annotation.behavior.RequiresInstanceClassLoading;
+import org.apache.nifi.annotation.behavior.SideEffectFree;
+import org.apache.nifi.annotation.behavior.SupportsBatching;
+import org.apache.nifi.annotation.behavior.WritesAttribute;
+import org.apache.nifi.annotation.documentation.CapabilityDescription;
+import org.apache.nifi.annotation.documentation.Tags;
+import org.apache.nifi.annotation.lifecycle.OnScheduled;
+import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.flowfile.FlowFile;
+import org.apache.nifi.flowfile.attributes.CoreAttributes;
+import org.apache.nifi.jolt.util.TransformUtils;
+import org.apache.nifi.logging.ComponentLog;
+import org.apache.nifi.migration.PropertyConfiguration;
+import org.apache.nifi.processor.DataUnit;
+import org.apache.nifi.processor.ProcessContext;
+import org.apache.nifi.processor.ProcessSession;
+import org.apache.nifi.processor.Relationship;
+import org.apache.nifi.processor.exception.ProcessException;
+import org.apache.nifi.processor.util.StandardValidators;
+import org.apache.nifi.util.StopWatch;
+import org.apache.nifi.util.file.classloader.ClassLoaderUtils;
+
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+
+@SideEffectFree
+@SupportsBatching
+@Tags({"json", "jolt", "transform", "shiftr", "chainr", "defaultr", "removr", 
"cardinality", "sort"})
+@InputRequirement(InputRequirement.Requirement.INPUT_REQUIRED)
+@WritesAttribute(attribute = "mime.type", description = "Always set to 
application/json")
+@CapabilityDescription("Applies a list of Jolt specifications to the flowfile 
JSON payload. A new FlowFile is created "
+        + "with transformed content and is routed to the 'success' 
relationship. If the JSON transform "
+        + "fails, the original FlowFile is routed to the 'failure' 
relationship.")
+@RequiresInstanceClassLoading
+public class JoltTransformJSON extends AbstractJoltTransform {
+    public static final PropertyDescriptor PRETTY_PRINT = new 
PropertyDescriptor.Builder()
+            .name("pretty_print")
+            .displayName("Pretty Print")
+            .description("Apply pretty print formatting to the output of the 
Jolt transform")
+            .required(true)
+            .allowableValues("true", "false")
+            .defaultValue("false")
+            .build();
+
+    public static final PropertyDescriptor MAX_STRING_LENGTH = new 
PropertyDescriptor.Builder()
+            .name("Max String Length")
+            .displayName("Max String Length")
+            .description("The maximum allowed length of a string value when 
parsing the JSON document")
+            .required(true)
+            .defaultValue("20 MB")
+            .addValidator(StandardValidators.DATA_SIZE_VALIDATOR)
+            .build();
+
+    public static final Relationship REL_SUCCESS = new Relationship.Builder()
+            .name("success")
+            .description("The FlowFile with transformed content will be routed 
to this relationship")
+            .build();
+    public static final Relationship REL_FAILURE = new Relationship.Builder()
+            .name("failure")
+            .description("If a FlowFile fails processing for any reason (for 
example, the FlowFile is not valid JSON), it will be routed to this 
relationship")
+            .build();
+
+    private static final List<PropertyDescriptor> PROPERTIES;
+    private static final Set<Relationship> RELATIONSHIPS;
+    private volatile ClassLoader customClassLoader;
+    private volatile JsonUtil jsonUtil;
+
+    static {
+        final List<PropertyDescriptor> tmp = new 
ArrayList<>(AbstractJoltTransform.PROPERTIES);
+        tmp.add(PRETTY_PRINT);
+        tmp.add(MAX_STRING_LENGTH);
+        PROPERTIES = Collections.unmodifiableList(tmp);
+
+        RELATIONSHIPS = Set.of(REL_SUCCESS, REL_FAILURE);

Review Comment:
   The declaration and assignment of `RELATIONSHIPS` can be merged into a 
single line, with the need for a static initializer.



##########
nifi-nar-bundles/nifi-jolt-bundle/nifi-jolt-processors/src/main/java/org/apache/nifi/processors/jolt/JoltTransformJSON.java:
##########
@@ -0,0 +1,196 @@
+/*
+ * 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.jolt;
+
+import com.bazaarvoice.jolt.JoltTransform;
+import com.bazaarvoice.jolt.JsonUtil;
+import com.bazaarvoice.jolt.JsonUtils;
+import com.fasterxml.jackson.core.StreamReadConstraints;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.nifi.annotation.behavior.InputRequirement;
+import org.apache.nifi.annotation.behavior.RequiresInstanceClassLoading;
+import org.apache.nifi.annotation.behavior.SideEffectFree;
+import org.apache.nifi.annotation.behavior.SupportsBatching;
+import org.apache.nifi.annotation.behavior.WritesAttribute;
+import org.apache.nifi.annotation.documentation.CapabilityDescription;
+import org.apache.nifi.annotation.documentation.Tags;
+import org.apache.nifi.annotation.lifecycle.OnScheduled;
+import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.flowfile.FlowFile;
+import org.apache.nifi.flowfile.attributes.CoreAttributes;
+import org.apache.nifi.jolt.util.TransformUtils;
+import org.apache.nifi.logging.ComponentLog;
+import org.apache.nifi.migration.PropertyConfiguration;
+import org.apache.nifi.processor.DataUnit;
+import org.apache.nifi.processor.ProcessContext;
+import org.apache.nifi.processor.ProcessSession;
+import org.apache.nifi.processor.Relationship;
+import org.apache.nifi.processor.exception.ProcessException;
+import org.apache.nifi.processor.util.StandardValidators;
+import org.apache.nifi.util.StopWatch;
+import org.apache.nifi.util.file.classloader.ClassLoaderUtils;
+
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+
+@SideEffectFree
+@SupportsBatching
+@Tags({"json", "jolt", "transform", "shiftr", "chainr", "defaultr", "removr", 
"cardinality", "sort"})
+@InputRequirement(InputRequirement.Requirement.INPUT_REQUIRED)
+@WritesAttribute(attribute = "mime.type", description = "Always set to 
application/json")
+@CapabilityDescription("Applies a list of Jolt specifications to the flowfile 
JSON payload. A new FlowFile is created "
+        + "with transformed content and is routed to the 'success' 
relationship. If the JSON transform "
+        + "fails, the original FlowFile is routed to the 'failure' 
relationship.")
+@RequiresInstanceClassLoading
+public class JoltTransformJSON extends AbstractJoltTransform {
+    public static final PropertyDescriptor PRETTY_PRINT = new 
PropertyDescriptor.Builder()
+            .name("pretty_print")
+            .displayName("Pretty Print")

Review Comment:
   This is a good opportunity to change the name to `Pretty Print`:
   ```suggestion
               .name("Pretty Print")
               .displayName("Pretty Print")
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to