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 67a55f5ba8..10eb892518 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,6 +27,7 @@ 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; @@ -64,8 +65,7 @@ public class ExtractImageMetadata extends AbstractProcessor { .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(true) - .defaultValue(Integer.toString(Integer.MAX_VALUE)) + .required(false) .addValidator(StandardValidators.NON_NEGATIVE_INTEGER_VALIDATOR) .build(); @@ -109,12 +109,15 @@ public class ExtractImageMetadata extends AbstractProcessor { public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException { final ProcessorLog logger = this.getLogger(); FlowFile flowfile = session.get(); - final ObjectHolder value = new ObjectHolder(null); - final int max = Integer.parseInt(context.getProperty(MaxAttributes).getValue()); + if (flowfile == null) { return; } + final ObjectHolder value = new ObjectHolder<>(null); + String propertyValue = context.getProperty(MaxAttributes).getValue(); + final int max = propertyValue!=null ? Integer.parseInt(propertyValue) : -1; + try { session.read(flowfile, new InputStreamCallback() { @Override @@ -129,7 +132,7 @@ public class ExtractImageMetadata extends AbstractProcessor { }); Metadata metadata = value.get(); - Map results = getTags(max,metadata); + Map results = max == -1 ? getTags(metadata) : getTags(max, metadata); // Write the results to an attribute if (!results.isEmpty()) { @@ -143,6 +146,18 @@ 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) { Map results = new HashMap<>(); int i =0; 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 9e68301e6f..a4dde01c8b 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 @@ -48,12 +48,12 @@ public class ExtractImageMetadataTest { @Test public void testFailedExtraction() throws IOException { - MockFlowFile flowFile = verifyTestRunnerFlow("src/test/resources/notImage.txt", ExtractImageMetadata.FAILURE,"1000"); + MockFlowFile flowFile = verifyTestRunnerFlow("src/test/resources/notImage.txt", ExtractImageMetadata.FAILURE,null); } @Test public void testExtractJPG() throws IOException { - MockFlowFile flowFile = verifyTestRunnerFlow("src/test/resources/simple.jpg", ExtractImageMetadata.SUCCESS,"1000"); + MockFlowFile flowFile = verifyTestRunnerFlow("src/test/resources/simple.jpg", ExtractImageMetadata.SUCCESS,null); Map attributes = flowFile.getAttributes(); assertEquals("800 pixels", attributes.get(JPEG_HEADER + "Image Width")); @@ -72,7 +72,7 @@ public class ExtractImageMetadataTest { @Test public void testExtractGIF() throws IOException { MockFlowFile flowFile = verifyTestRunnerFlow( - "src/test/resources/photoshop-8x12-32colors-alpha.gif", ExtractImageMetadata.SUCCESS,"1000"); + "src/test/resources/photoshop-8x12-32colors-alpha.gif", ExtractImageMetadata.SUCCESS,null); Map attributes = flowFile.getAttributes(); assertEquals("8", attributes.get(GIF_HEADER + "Image Width")); @@ -87,7 +87,7 @@ public class ExtractImageMetadataTest { @Test public void testExtractPNG() throws IOException { - MockFlowFile flowFile = verifyTestRunnerFlow("src/test/resources/mspaint-8x10.png", ExtractImageMetadata.SUCCESS, "1000"); + MockFlowFile flowFile = verifyTestRunnerFlow("src/test/resources/mspaint-8x10.png", ExtractImageMetadata.SUCCESS, null); Map attributes = flowFile.getAttributes(); assertEquals("8", attributes.get(PNG_HEADER + "Image Width")); @@ -102,7 +102,7 @@ public class ExtractImageMetadataTest { } @Test public void testExtractBMP() throws IOException { - MockFlowFile flowFile = verifyTestRunnerFlow("src/test/resources/16color-10x10.bmp", ExtractImageMetadata.SUCCESS, "1000"); + MockFlowFile flowFile = verifyTestRunnerFlow("src/test/resources/16color-10x10.bmp", ExtractImageMetadata.SUCCESS, null); Map attributes = flowFile.getAttributes(); assertEquals("10", attributes.get(BMP_HEADER+"Image Width")); @@ -138,7 +138,9 @@ public class ExtractImageMetadataTest { public MockFlowFile verifyTestRunnerFlow(String pathStr,Relationship rel, String max) throws IOException { Path path = Paths.get(pathStr); testRunner.enqueue(path); - testRunner.setProperty(ExtractImageMetadata.MaxAttributes, max); + if(max != null) { + testRunner.setProperty(ExtractImageMetadata.MaxAttributes, max); + } testRunner.run(); testRunner.assertAllFlowFilesTransferred(rel, 1);