Repository: nifi
Updated Branches:
  refs/heads/master 229b20f39 -> 31b943d2c


NIFI-3408 Add exception class when InvokeHTTP fails

This closes #1467.


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

Branch: refs/heads/master
Commit: 31b943d2c137f4ded254d4beb7c1d3268ce1bac8
Parents: 229b20f
Author: Giovanni Lanzani <[email protected]>
Authored: Thu Feb 2 16:49:15 2017 +0100
Committer: Pierre Villard <[email protected]>
Committed: Tue Feb 7 08:51:22 2017 +0100

----------------------------------------------------------------------
 .../nifi/processors/standard/InvokeHTTP.java    |  8 ++++++-
 .../processors/standard/TestInvokeHTTP.java     | 25 ++++++++++++++++++++
 2 files changed, 32 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/nifi/blob/31b943d2/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/InvokeHTTP.java
----------------------------------------------------------------------
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 6c3e32e..bb5b3b5 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
@@ -104,6 +104,7 @@ import org.joda.time.format.DateTimeFormatter;
     @WritesAttribute(attribute = "invokehttp.request.url", description = "The 
request URL"),
     @WritesAttribute(attribute = "invokehttp.tx.id", description = "The 
transaction ID that is returned after reading the response"),
     @WritesAttribute(attribute = "invokehttp.remote.dn", description = "The DN 
of the remote server"),
+    @WritesAttribute(attribute = "invokehttp.java.exception", description = 
"The Java exception raised when the processor fails"),
     @WritesAttribute(attribute = "user-defined", description = "If the 'Put 
Response Body In Attribute' property is set then whatever it is set to "
         + "will become the attribute key and the value would be the body of 
the HTTP response.")})
 @DynamicProperty(name = "Header Name", value = "Attribute Expression 
Language", supportsExpressionLanguage = true, description = "Send request 
header "
@@ -118,6 +119,8 @@ public final class InvokeHTTP extends AbstractProcessor {
     public final static String REQUEST_URL = "invokehttp.request.url";
     public final static String TRANSACTION_ID = "invokehttp.tx.id";
     public final static String REMOTE_DN = "invokehttp.remote.dn";
+    public final static String EXCEPTION_CLASS = "invokehttp.java.exception";
+
 
     public static final String DEFAULT_CONTENT_TYPE = 
"application/octet-stream";
 
@@ -126,7 +129,7 @@ public final class InvokeHTTP extends AbstractProcessor {
     // This set includes our strings defined above as well as some standard 
flowfile
     // attributes.
     public static final Set<String> IGNORED_ATTRIBUTES = 
Collections.unmodifiableSet(new HashSet<>(Arrays.asList(
-            STATUS_CODE, STATUS_MESSAGE, RESPONSE_BODY, REQUEST_URL, 
TRANSACTION_ID, REMOTE_DN,
+            STATUS_CODE, STATUS_MESSAGE, RESPONSE_BODY, REQUEST_URL, 
TRANSACTION_ID, REMOTE_DN, EXCEPTION_CLASS,
             "uuid", "filename", "path")));
 
     // properties
@@ -753,6 +756,9 @@ public final class InvokeHTTP extends AbstractProcessor {
             if (requestFlowFile != null) {
                 logger.error("Routing to {} due to exception: {}", new 
Object[]{REL_FAILURE.getName(), e}, e);
                 requestFlowFile = session.penalize(requestFlowFile);
+                String attributeKey = EXCEPTION_CLASS;
+                String attributeValue = e.getClass().getName();
+                requestFlowFile = session.putAttribute(requestFlowFile, 
attributeKey, attributeValue);
                 // transfer original to failure
                 session.transfer(requestFlowFile, REL_FAILURE);
             } else {

http://git-wip-us.apache.org/repos/asf/nifi/blob/31b943d2/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestInvokeHTTP.java
----------------------------------------------------------------------
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 d08f083..fc0570a 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
@@ -184,6 +184,31 @@ public class TestInvokeHTTP extends TestInvokeHttpCommon {
         bundle1.assertAttributeEquals("Content-Type", 
"text/plain;charset=iso-8859-1");
     }
 
+    @Test
+    public void testFailingHttpRequest() throws Exception {
+
+        runner = TestRunners.newTestRunner(InvokeHTTP.class);
+
+        // Remember: we expect that connecting to the following URL should 
raise a Java exception
+        runner.setProperty(InvokeHTTP.PROP_URL, "http://127.0.0.1:0";);
+
+        createFlowFiles(runner);
+
+        runner.run();
+
+        runner.assertTransferCount(InvokeHTTP.REL_SUCCESS_REQ, 0);
+        runner.assertTransferCount(InvokeHTTP.REL_RESPONSE, 0);
+        runner.assertTransferCount(InvokeHTTP.REL_RETRY, 0);
+        runner.assertTransferCount(InvokeHTTP.REL_NO_RETRY, 0);
+        runner.assertTransferCount(InvokeHTTP.REL_FAILURE, 1);
+        runner.assertPenalizeCount(1);
+
+        // expected in request java.exception
+        final MockFlowFile bundle = 
runner.getFlowFilesForRelationship(InvokeHTTP.REL_FAILURE).get(0);
+        bundle.assertAttributeEquals(InvokeHTTP.EXCEPTION_CLASS, 
"java.lang.IllegalArgumentException");
+
+    }
+
     public static class MyProxyHandler extends AbstractHandler {
 
         @Override

Reply via email to