You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ts...@apache.org on 2019/08/21 06:09:17 UTC

[camel] branch master updated: CAMEL-13886: camel-servlet + camel-http4 with null body causes "Stream closed" IOException

This is an automated email from the ASF dual-hosted git repository.

tsato pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new 4b9b619  CAMEL-13886: camel-servlet + camel-http4 with null body causes "Stream closed" IOException
4b9b619 is described below

commit 4b9b619138d779e7f55743f20bfad68f394d1d15
Author: Tadayoshi Sato <sa...@gmail.com>
AuthorDate: Wed Aug 21 15:07:34 2019 +0900

    CAMEL-13886: camel-servlet + camel-http4 with null body causes "Stream closed" IOException
---
 components/camel-http-common/pom.xml               |  5 +++
 .../org/apache/camel/http/common/HttpHelper.java   |  4 +-
 .../apache/camel/http/common/HttpHelperTest.java   | 50 ++++++++++++++++++++++
 3 files changed, 57 insertions(+), 2 deletions(-)

diff --git a/components/camel-http-common/pom.xml b/components/camel-http-common/pom.xml
index 242b1be..d92f43e 100644
--- a/components/camel-http-common/pom.xml
+++ b/components/camel-http-common/pom.xml
@@ -95,5 +95,10 @@
             <version>${assertj-version}</version>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-core</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>
diff --git a/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpHelper.java b/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpHelper.java
index d647ea4..31d8c8d 100644
--- a/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpHelper.java
+++ b/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpHelper.java
@@ -200,7 +200,7 @@ public final class HttpHelper {
      * @throws IOException is thrown if error reading response body
      */
     public static Object readRequestBodyFromInputStream(InputStream is, Exchange exchange) throws IOException {
-        if (is == null) {
+        if (is == null || is.available() <= 0) {
             return null;
         }
         boolean disableStreamCaching = !exchange.getContext().isStreamCaching();
@@ -224,7 +224,7 @@ public final class HttpHelper {
      * @throws IOException is thrown if error reading response body
      */
     public static Object readResponseBodyFromInputStream(InputStream is, Exchange exchange) throws IOException {
-        if (is == null) {
+        if (is == null || is.available() <= 0) {
             return null;
         }
         // convert the input stream to StreamCache if the stream cache is not disabled
diff --git a/components/camel-http-common/src/test/java/org/apache/camel/http/common/HttpHelperTest.java b/components/camel-http-common/src/test/java/org/apache/camel/http/common/HttpHelperTest.java
new file mode 100644
index 0000000..f2d518c
--- /dev/null
+++ b/components/camel-http-common/src/test/java/org/apache/camel/http/common/HttpHelperTest.java
@@ -0,0 +1,50 @@
+/*
+ * 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.camel.http.common;
+
+import java.io.IOException;
+import javax.servlet.ServletInputStream;
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.support.DefaultExchange;
+import org.junit.Test;
+
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class HttpHelperTest {
+
+    @Test
+    public void readRequestBodyFromServletRequest() throws IOException {
+        HttpServletRequest request = mock(HttpServletRequest.class);
+        ServletInputStream is = mock(ServletInputStream.class);
+        when(request.getInputStream()).thenReturn(is);
+        when(is.available()).thenReturn(0);
+
+        CamelContext context = new DefaultCamelContext();
+        Exchange exchange = new DefaultExchange(context);
+
+        assertNull(HttpHelper.readRequestBodyFromServletRequest(null, exchange));
+        assertNull(HttpHelper.readRequestBodyFromServletRequest(request, exchange));
+    }
+
+}