You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by "mattyb149 (via GitHub)" <gi...@apache.org> on 2023/06/08 19:24:50 UTC

[GitHub] [nifi] mattyb149 commented on a diff in pull request #4044: NIFI-4957: Add ability to get JOLT transform from file

mattyb149 commented on code in PR #4044:
URL: https://github.com/apache/nifi/pull/4044#discussion_r1223458724


##########
nifi-nar-bundles/nifi-jolt-record-bundle/nifi-jolt-record-processors/src/main/java/org/apache/nifi/processors/jolt/record/JoltTransformRecord.java:
##########
@@ -238,48 +254,65 @@ protected Collection<ValidationResult> customValidate(ValidationContext validati
         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();
-        if (!validationContext.getProperty(JOLT_SPEC).isSet() || StringUtils.isEmpty(validationContext.getProperty(JOLT_SPEC).getValue())) {
-            if (!SORTR.getValue().equals(transform)) {
-                final String message = "A specification is required for this transformation";
-                results.add(new ValidationResult.Builder().valid(false)
-                        .explanation(message)
-                        .build());
+        final String modulePath = validationContext.getProperty(MODULES).isSet()? validationContext.getProperty(MODULES).getValue() : null;
+        final String joltSpecBody = validationContext.getProperty(JOLT_SPEC).getValue();
+        final String joltSpecFile = validationContext.getProperty(JOLT_SPEC_FILE).getValue();
+
+        if (StringUtils.isEmpty(joltSpecBody) == StringUtils.isEmpty(joltSpecFile)) {
+            if(!SORTR.getValue().equals(transform)) {
+                results.add(new ValidationResult.Builder().subject("Spec Body or Spec File").valid(false).explanation(
+                        "exactly one of 'Jolt Specification' or 'Path To Jolt Specification' must be set, or the Transformation must be 'Sort'").build());
             }
         } else {
+            final ClassLoader customClassLoader;
+
             try {
-                final String specValue = validationContext.getProperty(JOLT_SPEC).getValue();
+                if (modulePath != null) {
+                    customClassLoader = ClassLoaderUtils.getCustomClassLoader(modulePath, this.getClass().getClassLoader(), getJarFilenameFilter());
+                } else {
+                    customClassLoader =  this.getClass().getClassLoader();
+                }
 
-                if (validationContext.isExpressionLanguagePresent(specValue) ) {
+                String specValue = validationContext.getProperty(JOLT_SPEC).getValue();
+                final boolean useBody = !StringUtils.isEmpty(specValue);
+                if (!useBody) {
+                    specValue = validationContext.getProperty(JOLT_SPEC_FILE).getValue();
+                }
+                final PropertyDescriptor pd = useBody ? JOLT_SPEC : JOLT_SPEC_FILE;
+                final boolean elPresent = validationContext.isExpressionLanguagePresent(specValue);
+
+                if (elPresent) {
                     final String invalidExpressionMsg = validationContext.newExpressionLanguageCompiler().validateExpression(specValue, true);
                     if (!StringUtils.isEmpty(invalidExpressionMsg)) {
                         results.add(new ValidationResult.Builder().valid(false)
-                                .subject(JOLT_SPEC.getDisplayName())
+                                .subject(pd.getDisplayName())
                                 .explanation("Invalid Expression Language: " + invalidExpressionMsg)
                                 .build());
                     }
                 } else {
-                    //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
-                    Object specJson = SORTR.getValue().equals(transform) ? null : JsonUtils.jsonToObject(specValue.replaceAll("\\$\\{", "\\\\\\\\\\$\\{"), DEFAULT_CHARSET);
-
-                    if (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 if (validationContext.isExpressionLanguagePresent(customTransform)) {
-                            final String invalidExpressionMsg = validationContext.newExpressionLanguageCompiler().validateExpression(customTransform, true);
-                            if (!StringUtils.isEmpty(invalidExpressionMsg)) {
+                    if (!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;
+                        if(useBody) {
+                            content = specValue;
+                        } else {
+                            content = new String(Files.readAllBytes(Paths.get(specValue)), DEFAULT_CHARSET);
+                        }
+                        final Object specJson = JsonUtils.jsonToObject(content.replaceAll("\\$\\{", "\\\\\\\\\\$\\{"), DEFAULT_CHARSET);
+
+                        if (CUSTOMR.getValue().equals(transform)) {

Review Comment:
   Line 293 checks for `!SORTR`, so it could be any of the others including CUSTOM



-- 
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: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org