NIFI-2819: Added support for Expresssion Language in ModifyBytes

This closes #1130
This commit is contained in:
Matt Burgess 2016-10-13 09:50:56 -04:00 committed by Oleg Zhurakousky
parent 05ea76dd6f
commit 892c74dff2
2 changed files with 39 additions and 2 deletions

View File

@ -66,6 +66,7 @@ public class ModifyBytes extends AbstractProcessor {
.required(true) .required(true)
.addValidator(StandardValidators.DATA_SIZE_VALIDATOR) .addValidator(StandardValidators.DATA_SIZE_VALIDATOR)
.defaultValue("0 B") .defaultValue("0 B")
.expressionLanguageSupported(true)
.build(); .build();
public static final PropertyDescriptor END_OFFSET = new PropertyDescriptor.Builder() public static final PropertyDescriptor END_OFFSET = new PropertyDescriptor.Builder()
.name("End Offset") .name("End Offset")
@ -73,6 +74,7 @@ public class ModifyBytes extends AbstractProcessor {
.required(true) .required(true)
.addValidator(StandardValidators.DATA_SIZE_VALIDATOR) .addValidator(StandardValidators.DATA_SIZE_VALIDATOR)
.defaultValue("0 B") .defaultValue("0 B")
.expressionLanguageSupported(true)
.build(); .build();
public static final PropertyDescriptor REMOVE_ALL = new PropertyDescriptor.Builder() public static final PropertyDescriptor REMOVE_ALL = new PropertyDescriptor.Builder()
.name("Remove All Content") .name("Remove All Content")
@ -114,8 +116,8 @@ public class ModifyBytes extends AbstractProcessor {
final ComponentLog logger = getLogger(); final ComponentLog logger = getLogger();
final long startOffset = context.getProperty(START_OFFSET).asDataSize(DataUnit.B).longValue(); final long startOffset = context.getProperty(START_OFFSET).evaluateAttributeExpressions(ff).asDataSize(DataUnit.B).longValue();
final long endOffset = context.getProperty(END_OFFSET).asDataSize(DataUnit.B).longValue(); final long endOffset = context.getProperty(END_OFFSET).evaluateAttributeExpressions(ff).asDataSize(DataUnit.B).longValue();
final boolean removeAll = context.getProperty(REMOVE_ALL).asBoolean(); final boolean removeAll = context.getProperty(REMOVE_ALL).asBoolean();
final long newFileSize = removeAll ? 0L : ff.getSize() - startOffset - endOffset; final long newFileSize = removeAll ? 0L : ff.getSize() - startOffset - endOffset;

View File

@ -21,6 +21,7 @@ import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.HashMap;
import org.apache.nifi.util.MockFlowFile; import org.apache.nifi.util.MockFlowFile;
import org.apache.nifi.util.TestRunner; import org.apache.nifi.util.TestRunner;
@ -94,6 +95,24 @@ public class TestModifyBytes {
out.assertContentEquals(noHeaderFile); out.assertContentEquals(noHeaderFile);
} }
@Test
public void testRemoveHeaderEL() throws IOException {
final TestRunner runner = TestRunners.newTestRunner(new ModifyBytes());
runner.setProperty(ModifyBytes.START_OFFSET, "${numBytes}"); //REMOVE - '<<<HEADER>>>'
runner.setProperty(ModifyBytes.END_OFFSET, "0 MB");
runner.enqueue(testFilePath, new HashMap<String, String>() {{
put("numBytes", "12 B");
}});
runner.run();
runner.assertAllFlowFilesTransferred(ModifyBytes.REL_SUCCESS, 1);
final MockFlowFile out = runner.getFlowFilesForRelationship(ModifyBytes.REL_SUCCESS).get(0);
final String outContent = new String(out.toByteArray(), StandardCharsets.UTF_8);
System.out.println(outContent);
out.assertContentEquals(noHeaderFile);
}
@Test @Test
public void testKeepFooter() throws IOException { public void testKeepFooter() throws IOException {
final TestRunner runner = TestRunners.newTestRunner(new ModifyBytes()); final TestRunner runner = TestRunners.newTestRunner(new ModifyBytes());
@ -124,6 +143,22 @@ public class TestModifyBytes {
out.assertContentEquals("<<<HEADER>>>".getBytes("UTF-8")); out.assertContentEquals("<<<HEADER>>>".getBytes("UTF-8"));
} }
@Test
public void testKeepHeaderEL() throws IOException {
final TestRunner runner = TestRunners.newTestRunner(new ModifyBytes());
runner.setProperty(ModifyBytes.START_OFFSET, "0 B");
runner.setProperty(ModifyBytes.END_OFFSET, "${numBytes}");
runner.enqueue(testFilePath, new HashMap<String, String>() {{
put("numBytes", "181 B");
}});
runner.run();
runner.assertAllFlowFilesTransferred(ModifyBytes.REL_SUCCESS, 1);
final MockFlowFile out = runner.getFlowFilesForRelationship(ModifyBytes.REL_SUCCESS).get(0);
out.assertContentEquals("<<<HEADER>>>".getBytes("UTF-8"));
}
@Test @Test
public void testRemoveFooter() throws IOException { public void testRemoveFooter() throws IOException {
final TestRunner runner = TestRunners.newTestRunner(new ModifyBytes()); final TestRunner runner = TestRunners.newTestRunner(new ModifyBytes());