Making metadata extractor max tags property optional

Signed-off-by: Matt Gilman <matt.c.gilman@gmail.com>
This commit is contained in:
Joe Percivall 2015-08-20 11:18:46 -04:00 committed by Matt Gilman
parent d992730fb7
commit dedff148ca
2 changed files with 28 additions and 11 deletions

View File

@ -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;

View File

@ -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);