mirror of https://github.com/apache/nifi.git
Making metadata extractor max tags property optional
Signed-off-by: Matt Gilman <matt.c.gilman@gmail.com>
This commit is contained in:
parent
d992730fb7
commit
dedff148ca
|
@ -27,6 +27,7 @@ import org.apache.nifi.annotation.behavior.WritesAttributes;
|
||||||
import org.apache.nifi.annotation.documentation.CapabilityDescription;
|
import org.apache.nifi.annotation.documentation.CapabilityDescription;
|
||||||
import org.apache.nifi.annotation.documentation.Tags;
|
import org.apache.nifi.annotation.documentation.Tags;
|
||||||
import org.apache.nifi.components.PropertyDescriptor;
|
import org.apache.nifi.components.PropertyDescriptor;
|
||||||
|
import org.apache.nifi.components.PropertyValue;
|
||||||
import org.apache.nifi.flowfile.FlowFile;
|
import org.apache.nifi.flowfile.FlowFile;
|
||||||
import org.apache.nifi.logging.ProcessorLog;
|
import org.apache.nifi.logging.ProcessorLog;
|
||||||
import org.apache.nifi.processor.AbstractProcessor;
|
import org.apache.nifi.processor.AbstractProcessor;
|
||||||
|
@ -64,8 +65,7 @@ public class ExtractImageMetadata extends AbstractProcessor {
|
||||||
.name("Max number of attributes")
|
.name("Max number of attributes")
|
||||||
.description("Specify the max number of attributes to add to the flowfile. There is no guarantee in what order"
|
.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.")
|
+ " the tags will be processed. By default it will process all of them.")
|
||||||
.required(true)
|
.required(false)
|
||||||
.defaultValue(Integer.toString(Integer.MAX_VALUE))
|
|
||||||
.addValidator(StandardValidators.NON_NEGATIVE_INTEGER_VALIDATOR)
|
.addValidator(StandardValidators.NON_NEGATIVE_INTEGER_VALIDATOR)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
@ -109,12 +109,15 @@ public class ExtractImageMetadata extends AbstractProcessor {
|
||||||
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
|
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
|
||||||
final ProcessorLog logger = this.getLogger();
|
final ProcessorLog logger = this.getLogger();
|
||||||
FlowFile flowfile = session.get();
|
FlowFile flowfile = session.get();
|
||||||
final ObjectHolder<Metadata> value = new ObjectHolder(null);
|
|
||||||
final int max = Integer.parseInt(context.getProperty(MaxAttributes).getValue());
|
|
||||||
if (flowfile == null) {
|
if (flowfile == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final ObjectHolder<Metadata> value = new ObjectHolder<>(null);
|
||||||
|
String propertyValue = context.getProperty(MaxAttributes).getValue();
|
||||||
|
final int max = propertyValue!=null ? Integer.parseInt(propertyValue) : -1;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
session.read(flowfile, new InputStreamCallback() {
|
session.read(flowfile, new InputStreamCallback() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -129,7 +132,7 @@ public class ExtractImageMetadata extends AbstractProcessor {
|
||||||
});
|
});
|
||||||
|
|
||||||
Metadata metadata = value.get();
|
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
|
// Write the results to an attribute
|
||||||
if (!results.isEmpty()) {
|
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) {
|
private Map<String, String> getTags(int max, Metadata metadata) {
|
||||||
Map<String, String> results = new HashMap<>();
|
Map<String, String> results = new HashMap<>();
|
||||||
int i =0;
|
int i =0;
|
||||||
|
|
|
@ -48,12 +48,12 @@ public class ExtractImageMetadataTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFailedExtraction() throws IOException {
|
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
|
@Test
|
||||||
public void testExtractJPG() throws IOException {
|
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();
|
Map<String, String> attributes = flowFile.getAttributes();
|
||||||
|
|
||||||
assertEquals("800 pixels", attributes.get(JPEG_HEADER + "Image Width"));
|
assertEquals("800 pixels", attributes.get(JPEG_HEADER + "Image Width"));
|
||||||
|
@ -72,7 +72,7 @@ public class ExtractImageMetadataTest {
|
||||||
@Test
|
@Test
|
||||||
public void testExtractGIF() throws IOException {
|
public void testExtractGIF() throws IOException {
|
||||||
MockFlowFile flowFile = verifyTestRunnerFlow(
|
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();
|
Map<String, String> attributes = flowFile.getAttributes();
|
||||||
|
|
||||||
assertEquals("8", attributes.get(GIF_HEADER + "Image Width"));
|
assertEquals("8", attributes.get(GIF_HEADER + "Image Width"));
|
||||||
|
@ -87,7 +87,7 @@ public class ExtractImageMetadataTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testExtractPNG() throws IOException {
|
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();
|
Map<String, String> attributes = flowFile.getAttributes();
|
||||||
|
|
||||||
assertEquals("8", attributes.get(PNG_HEADER + "Image Width"));
|
assertEquals("8", attributes.get(PNG_HEADER + "Image Width"));
|
||||||
|
@ -102,7 +102,7 @@ public class ExtractImageMetadataTest {
|
||||||
}
|
}
|
||||||
@Test
|
@Test
|
||||||
public void testExtractBMP() throws IOException {
|
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();
|
Map<String, String> attributes = flowFile.getAttributes();
|
||||||
|
|
||||||
assertEquals("10", attributes.get(BMP_HEADER+"Image Width"));
|
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 {
|
public MockFlowFile verifyTestRunnerFlow(String pathStr,Relationship rel, String max) throws IOException {
|
||||||
Path path = Paths.get(pathStr);
|
Path path = Paths.get(pathStr);
|
||||||
testRunner.enqueue(path);
|
testRunner.enqueue(path);
|
||||||
testRunner.setProperty(ExtractImageMetadata.MaxAttributes, max);
|
if(max != null) {
|
||||||
|
testRunner.setProperty(ExtractImageMetadata.MaxAttributes, max);
|
||||||
|
}
|
||||||
|
|
||||||
testRunner.run();
|
testRunner.run();
|
||||||
testRunner.assertAllFlowFilesTransferred(rel, 1);
|
testRunner.assertAllFlowFilesTransferred(rel, 1);
|
||||||
|
|
Loading…
Reference in New Issue