From e7a254f78ee5846e38487b02ffa6857ff9f41ea6 Mon Sep 17 00:00:00 2001 From: Pierre Villard Date: Mon, 7 Mar 2016 15:40:49 +0100 Subject: [PATCH] NIFI-1332 Fixed HTTP-204 handling + unit test This closes #259. Signed-off-by: Aldrin Piri --- .../nifi/processors/standard/GetHTTP.java | 2 +- .../processors/standard/HttpErrorServlet.java | 51 +++++++++++++++++++ .../nifi/processors/standard/TestGetHTTP.java | 48 +++++++++++++++++ 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/HttpErrorServlet.java diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/GetHTTP.java b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/GetHTTP.java index f2ed529047..6558b2543f 100644 --- a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/GetHTTP.java +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/GetHTTP.java @@ -439,7 +439,7 @@ public class GetHTTP extends AbstractSessionFactoryProcessor { } final String statusExplanation = response.getStatusLine().getReasonPhrase(); - if (statusCode >= 300) { + if ((statusCode >= 300) || (statusCode == 204)) { logger.error("received status code {}:{} from {}", new Object[]{statusCode, statusExplanation, url}); // doing a commit in case there were flow files in the input queue session.commit(); diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/HttpErrorServlet.java b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/HttpErrorServlet.java new file mode 100644 index 0000000000..505917b96e --- /dev/null +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/HttpErrorServlet.java @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.processors.standard; + +import java.io.IOException; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * Class to mock HTTP errors responses + */ +public class HttpErrorServlet extends HttpServlet { + /** serial UID */ + private static final long serialVersionUID = 7428077098486751916L; + + /** error to return */ + private int errorToReturn = HttpServletResponse.SC_NOT_FOUND; + + /** + * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) + */ + @Override + protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { + resp.sendError(this.errorToReturn); + } + + /** + * Set the HTTP error to return + * @param errorToReturn error to return + */ + public void setErrorToReturn(int errorToReturn) { + this.errorToReturn = errorToReturn; + } +} diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestGetHTTP.java b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestGetHTTP.java index f8e4122d37..6402392380 100644 --- a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestGetHTTP.java +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestGetHTTP.java @@ -16,6 +16,8 @@ */ package org.apache.nifi.processors.standard; +import javax.servlet.http.HttpServletResponse; + import org.apache.nifi.components.state.Scope; import org.apache.nifi.flowfile.attributes.CoreAttributes; import org.apache.nifi.reporting.InitializationException; @@ -278,6 +280,52 @@ public class TestGetHTTP { } } + /** + * Test for HTTP errors + * @throws Exception exception + */ + @Test + public final void testHttpErrors() throws Exception { + // set up web service + ServletHandler handler = new ServletHandler(); + handler.addServletWithMapping(HttpErrorServlet.class, "/*"); + HttpErrorServlet servlet = (HttpErrorServlet) handler.getServlets()[0].getServlet(); + + // create the service + TestServer server = new TestServer(); + server.addHandler(handler); + + try { + server.startServer(); + String destination = server.getUrl(); + + this.controller = TestRunners.newTestRunner(GetHTTP.class); + this.controller.setProperty(GetHTTP.CONNECTION_TIMEOUT, "5 secs"); + this.controller.setProperty(GetHTTP.URL, destination+"/test_${literal(1)}.pdf"); + this.controller.setProperty(GetHTTP.FILENAME, "test_${now():format('yyyy/MM/dd_HH:mm:ss')}"); + this.controller.setProperty(GetHTTP.ACCEPT_CONTENT_TYPE, "application/json"); + this.controller.setProperty(GetHTTP.USER_AGENT, "testUserAgent"); + + // 204 - NO CONTENT + servlet.setErrorToReturn(HttpServletResponse.SC_NO_CONTENT); + this.controller.run(); + this.controller.assertTransferCount(GetHTTP.REL_SUCCESS, 0); + + // 404 - NOT FOUND + servlet.setErrorToReturn(HttpServletResponse.SC_NOT_FOUND); + this.controller.run(); + this.controller.assertTransferCount(GetHTTP.REL_SUCCESS, 0); + + // 500 - INTERNAL SERVER ERROR + servlet.setErrorToReturn(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); + this.controller.run(); + this.controller.assertTransferCount(GetHTTP.REL_SUCCESS, 0); + } finally { + // shutdown web service + server.shutdownServer(); + } + } + @Test public final void testSecure_oneWaySsl() throws Exception { // set up web service