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:27:23 UTC

[camel] 01/02: 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 camel-2.x
in repository https://gitbox.apache.org/repos/asf/camel.git

commit eb33736ab1ddc84b83147ebc19fefd8ce104d75b
Author: Tadayoshi Sato <sa...@gmail.com>
AuthorDate: Wed Aug 21 15:19:23 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 16e9d5e..6f690f4 100644
--- a/components/camel-http-common/pom.xml
+++ b/components/camel-http-common/pom.xml
@@ -84,6 +84,11 @@
       <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 c2d271c..6d7dae4 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
@@ -203,7 +203,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 = false;
@@ -232,7 +232,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..0cfca20
--- /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.impl.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));
+    }
+
+}