diff --git a/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/src/main/java/org/apache/nifi/processors/image/ExtractImageMetadata.java b/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/src/main/java/org/apache/nifi/processors/image/ExtractImageMetadata.java index 10eb892518..7fe6195e4d 100644 --- a/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/src/main/java/org/apache/nifi/processors/image/ExtractImageMetadata.java +++ b/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/src/main/java/org/apache/nifi/processors/image/ExtractImageMetadata.java @@ -27,7 +27,6 @@ import org.apache.nifi.annotation.behavior.WritesAttributes; import org.apache.nifi.annotation.documentation.CapabilityDescription; import org.apache.nifi.annotation.documentation.Tags; import org.apache.nifi.components.PropertyDescriptor; -import org.apache.nifi.components.PropertyValue; import org.apache.nifi.flowfile.FlowFile; import org.apache.nifi.logging.ProcessorLog; import org.apache.nifi.processor.AbstractProcessor; @@ -61,8 +60,8 @@ import java.util.HashMap; @SupportsBatching public class ExtractImageMetadata extends AbstractProcessor { - public static final PropertyDescriptor MaxAttributes = new PropertyDescriptor.Builder() - .name("Max number of attributes") + public static final PropertyDescriptor MAX_NUMBER_OF_ATTRIBUTES = new PropertyDescriptor.Builder() + .name("Max Number of Attributes") .description("Specify the max number of attributes to add to the flowfile. There is no guarantee in what order" + " the tags will be processed. By default it will process all of them.") .required(false) @@ -86,7 +85,7 @@ public class ExtractImageMetadata extends AbstractProcessor { protected void init(final ProcessorInitializationContext context) { final List properties = new ArrayList<>(); - properties.add(MaxAttributes); + properties.add(MAX_NUMBER_OF_ATTRIBUTES); this.properties = Collections.unmodifiableList(properties); final Set relationships = new HashSet<>(); @@ -107,16 +106,14 @@ public class ExtractImageMetadata extends AbstractProcessor { @Override public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException { - final ProcessorLog logger = this.getLogger(); FlowFile flowfile = session.get(); - if (flowfile == null) { return; } + final ProcessorLog logger = this.getLogger(); final ObjectHolder value = new ObjectHolder<>(null); - String propertyValue = context.getProperty(MaxAttributes).getValue(); - final int max = propertyValue!=null ? Integer.parseInt(propertyValue) : -1; + final Integer max = context.getProperty(MAX_NUMBER_OF_ATTRIBUTES).asInteger(); try { session.read(flowfile, new InputStreamCallback() { @@ -132,7 +129,7 @@ public class ExtractImageMetadata extends AbstractProcessor { }); Metadata metadata = value.get(); - Map results = max == -1 ? getTags(metadata) : getTags(max, metadata); + Map results = getTags(max, metadata); // Write the results to an attribute if (!results.isEmpty()) { @@ -146,19 +143,7 @@ public class ExtractImageMetadata extends AbstractProcessor { } } - private Map getTags(Metadata metadata) { - Map results = new HashMap<>(); - - for (Directory directory : metadata.getDirectories()) { - for (Tag tag : directory.getTags()) { - results.put(directory.getName() + "." + tag.getTagName(), tag.getDescription()); - } - } - - return results; - } - - private Map getTags(int max, Metadata metadata) { + private Map getTags(Integer max, Metadata metadata) { Map results = new HashMap<>(); int i =0; @@ -166,9 +151,11 @@ public class ExtractImageMetadata extends AbstractProcessor { for (Tag tag : directory.getTags()) { results.put(directory.getName() + "." + tag.getTagName(), tag.getDescription()); - i++; - if(i>=max) { - return results; + if(max!=null) { + i++; + if (i >= max) { + return results; + } } } } diff --git a/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/src/test/java/org/apache/nifi/processors/image/ExtractImageMetadataTest.java b/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/src/test/java/org/apache/nifi/processors/image/ExtractImageMetadataTest.java index a4dde01c8b..cf31e15e54 100644 --- a/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/src/test/java/org/apache/nifi/processors/image/ExtractImageMetadataTest.java +++ b/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/src/test/java/org/apache/nifi/processors/image/ExtractImageMetadataTest.java @@ -139,7 +139,7 @@ public class ExtractImageMetadataTest { Path path = Paths.get(pathStr); testRunner.enqueue(path); if(max != null) { - testRunner.setProperty(ExtractImageMetadata.MaxAttributes, max); + testRunner.setProperty(ExtractImageMetadata.MAX_NUMBER_OF_ATTRIBUTES, max); } testRunner.run();