From 9a638cc865518aef71ee34330c5192786bbfff44 Mon Sep 17 00:00:00 2001 From: Koji Kawamura Date: Tue, 7 Feb 2017 11:08:06 +0900 Subject: [PATCH] NIFI-1125 InvokeHTTP throws NullPointerException Added null check in onPropertyModified to avoid NPE. This closes #1477. --- .../nifi/processors/standard/InvokeHTTP.java | 2 +- .../processors/standard/TestInvokeHTTP.java | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/InvokeHTTP.java b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/InvokeHTTP.java index c3d7fe9bf2..c95b639433 100644 --- a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/InvokeHTTP.java +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/InvokeHTTP.java @@ -466,7 +466,7 @@ public final class InvokeHTTP extends AbstractProcessor { } else { // compile the attributes-to-send filter pattern if (PROP_ATTRIBUTES_TO_SEND.getName().equalsIgnoreCase(descriptor.getName())) { - if (newValue.isEmpty()) { + if (newValue == null || newValue.isEmpty()) { regexAttributesToSend = null; } else { final String trimmedValue = StringUtils.trimToEmpty(newValue); diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestInvokeHTTP.java b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestInvokeHTTP.java index fc0570add9..a6b798b5df 100644 --- a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestInvokeHTTP.java +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestInvokeHTTP.java @@ -34,10 +34,14 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; +import java.lang.reflect.Field; import java.net.URL; import java.util.HashMap; import java.util.Map; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + public class TestInvokeHTTP extends TestInvokeHttpCommon { @@ -232,4 +236,30 @@ public class TestInvokeHTTP extends TestInvokeHttpCommon { } } } + + @Test + public void testOnPropertyModified() throws Exception { + final InvokeHTTP processor = new InvokeHTTP(); + final Field regexAttributesToSendField = InvokeHTTP.class.getDeclaredField("regexAttributesToSend"); + regexAttributesToSendField.setAccessible(true); + + assertNull(regexAttributesToSendField.get(processor)); + + // Set Attributes to Send. + processor.onPropertyModified(InvokeHTTP.PROP_ATTRIBUTES_TO_SEND, null, "uuid"); + assertNotNull(regexAttributesToSendField.get(processor)); + + // Null clear Attributes to Send. NIFI-1125: Throws NullPointerException. + processor.onPropertyModified(InvokeHTTP.PROP_ATTRIBUTES_TO_SEND, "uuid", null); + assertNull(regexAttributesToSendField.get(processor)); + + // Set Attributes to Send. + processor.onPropertyModified(InvokeHTTP.PROP_ATTRIBUTES_TO_SEND, null, "uuid"); + assertNotNull(regexAttributesToSendField.get(processor)); + + // Clear Attributes to Send with empty string. + processor.onPropertyModified(InvokeHTTP.PROP_ATTRIBUTES_TO_SEND, "uuid", ""); + assertNull(regexAttributesToSendField.get(processor)); + + } }