You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nifi.apache.org by ma...@apache.org on 2016/01/19 17:03:51 UTC

[12/14] nifi git commit: NIFI-1362 Set mime.type attribute on response FlowFile based on InvokeHTTP response Content-Type

NIFI-1362 Set mime.type attribute on response FlowFile based on InvokeHTTP response Content-Type

Signed-off-by: Aldrin Piri <al...@apache.org>


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

Branch: refs/heads/NIFI-259
Commit: ecb81ec113e20f3850fd3f7080da2360035cabd8
Parents: 92062f9
Author: Jeremy Dyer <jd...@gmail.com>
Authored: Thu Jan 14 23:16:55 2016 -0500
Committer: Aldrin Piri <al...@apache.org>
Committed: Fri Jan 15 16:40:36 2016 -0500

----------------------------------------------------------------------
 .../nifi/processors/standard/InvokeHTTP.java    |  8 +++-
 .../standard/util/TestInvokeHttpCommon.java     | 41 ++++++++++++++++++++
 2 files changed, 47 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/nifi/blob/ecb81ec1/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 4e5c22d..44f76e5 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
@@ -556,7 +556,7 @@ public final class InvokeHTTP extends AbstractProcessor {
                 throw new IllegalStateException("Status code unknown, connection hasn't been attempted.");
             }
 
-            // Create a map of the status attributes that are always written to the request and reponse FlowFiles
+            // Create a map of the status attributes that are always written to the request and response FlowFiles
             Map<String, String> statusAttributes = new HashMap<>();
             statusAttributes.put(STATUS_CODE, String.valueOf(statusCode));
             statusAttributes.put(STATUS_MESSAGE, statusMessage);
@@ -602,7 +602,7 @@ public final class InvokeHTTP extends AbstractProcessor {
                         responseFlowFile = session.create();
                     }
 
-                    // write the status attributes
+                    // write attributes to response flowfile
                     responseFlowFile = session.putAllAttributes(responseFlowFile, statusAttributes);
 
                     // write the response headers as attributes
@@ -612,6 +612,10 @@ public final class InvokeHTTP extends AbstractProcessor {
                     // transfer the message body to the payload
                     // can potentially be null in edge cases
                     if (bodyExists) {
+                        // write content type attribute to response flowfile if it is available
+                        if (responseBody.contentType() != null) {
+                             responseFlowFile = session.putAttribute(responseFlowFile, CoreAttributes.MIME_TYPE.key(), responseBody.contentType().toString());
+                        }
                         if (teeInputStream != null) {
                             responseFlowFile = session.importFrom(teeInputStream, responseFlowFile);
                         } else {

http://git-wip-us.apache.org/repos/asf/nifi/blob/ecb81ec1/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/util/TestInvokeHttpCommon.java
----------------------------------------------------------------------
diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/util/TestInvokeHttpCommon.java b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/util/TestInvokeHttpCommon.java
index cf14e08..3464f14 100644
--- a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/util/TestInvokeHttpCommon.java
+++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/util/TestInvokeHttpCommon.java
@@ -226,6 +226,47 @@ public abstract class TestInvokeHttpCommon {
     }
 
     @Test
+    public void testOutputResponseSetMimeTypeToResponseContentType() throws Exception {
+        addHandler(new GetOrHeadHandler());
+
+        String statusUrl = "/status/200";
+        runner.setProperty(InvokeHTTP.PROP_URL, url + statusUrl);
+        runner.setProperty(InvokeHTTP.PROP_METHOD, "GET");
+        runner.setProperty(InvokeHTTP.PROP_OUTPUT_RESPONSE_REGARDLESS,"true");
+        runner.setProperty(InvokeHTTP.PROP_PUT_OUTPUT_IN_ATTRIBUTE,"outputBody");
+
+        createFlowFiles(runner);
+
+        runner.run();
+
+        runner.assertTransferCount(InvokeHTTP.REL_SUCCESS_REQ, 1);
+        runner.assertTransferCount(InvokeHTTP.REL_RESPONSE, 1);
+        runner.assertTransferCount(InvokeHTTP.REL_RETRY, 0);
+        runner.assertTransferCount(InvokeHTTP.REL_NO_RETRY,0);
+        runner.assertTransferCount(InvokeHTTP.REL_FAILURE, 0);
+
+        // expected in request status.code and status.message
+        // original flow file (+attributes)
+        final MockFlowFile bundle = runner.getFlowFilesForRelationship(InvokeHTTP.REL_SUCCESS_REQ).get(0);
+        bundle.assertContentEquals("Hello".getBytes("UTF-8"));
+        bundle.assertAttributeEquals(InvokeHTTP.STATUS_CODE, "200");
+        bundle.assertAttributeEquals(InvokeHTTP.STATUS_MESSAGE, "OK");
+        bundle.assertAttributeEquals("outputBody", statusUrl);
+        bundle.assertAttributeEquals("Foo", "Bar");
+
+        // expected in response
+        // status code, status message, all headers from server response --> ff attributes
+        // server response message body into payload of ff
+        final MockFlowFile bundle1 = runner.getFlowFilesForRelationship(InvokeHTTP.REL_RESPONSE).get(0);
+        bundle1.assertContentEquals(statusUrl.getBytes("UTF-8"));
+        bundle1.assertAttributeEquals(InvokeHTTP.STATUS_CODE, "200");
+        bundle1.assertAttributeEquals(InvokeHTTP.STATUS_MESSAGE, "OK");
+        bundle1.assertAttributeEquals("Foo", "Bar");
+        bundle1.assertAttributeEquals("Content-Type", "text/plain; charset=ISO-8859-1");
+        bundle1.assertAttributeEquals("mime.type", "text/plain; charset=ISO-8859-1");
+    }
+
+    @Test
     public void testOutputResponseRegardlessWithOutputInAttributeLarge() throws Exception {
         addHandler(new GetLargeHandler());