Repository: nifi
Updated Branches:
  refs/heads/master 706edeb01 -> b17be66a1


Making metadata extractor max tags property optional

Signed-off-by: Matt Gilman <[email protected]>


Project: http://git-wip-us.apache.org/repos/asf/nifi/repo
Commit: http://git-wip-us.apache.org/repos/asf/nifi/commit/dedff148
Tree: http://git-wip-us.apache.org/repos/asf/nifi/tree/dedff148
Diff: http://git-wip-us.apache.org/repos/asf/nifi/diff/dedff148

Branch: refs/heads/master
Commit: dedff148caa8536fe1c6c06a4b4aba4d4d6f1736
Parents: d992730
Author: Joe Percivall <[email protected]>
Authored: Thu Aug 20 11:18:46 2015 -0400
Committer: Matt Gilman <[email protected]>
Committed: Fri Aug 21 08:56:41 2015 -0400

----------------------------------------------------------------------
 .../processors/image/ExtractImageMetadata.java  | 25 ++++++++++++++++----
 .../image/ExtractImageMetadataTest.java         | 14 ++++++-----
 2 files changed, 28 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/nifi/blob/dedff148/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/src/main/java/org/apache/nifi/processors/image/ExtractImageMetadata.java
----------------------------------------------------------------------
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 67a55f5..10eb892 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<Metadata> value = new ObjectHolder(null);
-        final int max = 
Integer.parseInt(context.getProperty(MaxAttributes).getValue());
+
         if (flowfile == null) {
             return;
         }
 
+        final ObjectHolder<Metadata> 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<String, String> results = getTags(max,metadata);
+            Map<String, String> 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<String, String> getTags(Metadata metadata) {
+        Map<String, String> 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<String, String> getTags(int max, Metadata metadata) {
         Map<String, String> results = new HashMap<>();
         int i =0;

http://git-wip-us.apache.org/repos/asf/nifi/blob/dedff148/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/src/test/java/org/apache/nifi/processors/image/ExtractImageMetadataTest.java
----------------------------------------------------------------------
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 9e68301..a4dde01 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<String, String> 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<String, String> 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<String, String> 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<String, String> 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);

Reply via email to