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

davsclaus pushed a commit to branch camel-3.11.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-3.11.x by this push:
     new 0cecd84  CAMEL-14823: Fixed so camel-servlet honours when stream 
caching is disabled.
0cecd84 is described below

commit 0cecd84946c5be323a11d2fd110884716725797d
Author: Claus Ibsen <[email protected]>
AuthorDate: Sun Sep 26 17:22:11 2021 +0200

    CAMEL-14823: Fixed so camel-servlet honours when stream caching is disabled.
---
 .../org/apache/camel/http/common/HttpHelper.java   | 48 +++++++++++++---------
 .../camel/component/http/HttpPollingConsumer.java  | 11 +++--
 2 files changed, 34 insertions(+), 25 deletions(-)

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 f477d41..dc116cc 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
@@ -139,46 +139,56 @@ public final class HttpHelper {
      */
     public static Object readRequestBodyFromServletRequest(HttpServletRequest 
request, Exchange exchange) throws IOException {
         InputStream is = HttpConverter.toInputStream(request, exchange);
-        return readRequestBodyFromInputStream(is, exchange);
+        // when using servlet (camel-servlet and camel-jetty) then they should 
always use stream caching
+        // as the message body is parsed for url-form and other things, so we 
need to be able to re-read the message body
+        // however there is an option to turn this off, which is set as 
exchange property
+        boolean streamCaching = 
!exchange.getProperty(Exchange.DISABLE_HTTP_STREAM_CACHE, false, boolean.class);
+        if (streamCaching) {
+            return cacheResponseBodyFromInputStream(is, exchange);
+        } else {
+            return is;
+        }
     }
 
     /**
-     * Reads the request body from the given input stream.
+     * Caches the response body from the given input stream, which is needed by
+     * {@link org.apache.camel.PollingConsumer}.
      *
      * @param  is          the input stream
      * @param  exchange    the exchange
-     * @return             the request body, can be <tt>null</tt> if no body
-     * @throws IOException is thrown if error reading request body
+     * @return             the cached response body
+     * @throws IOException is thrown if error reading response body
      */
-    public static Object readRequestBodyFromInputStream(InputStream is, 
Exchange exchange) throws IOException {
+    public static Object cacheResponseBodyFromInputStream(InputStream is, 
Exchange exchange) throws IOException {
         if (is == null) {
             return null;
         }
-        boolean disableStreamCaching = 
!exchange.getContext().isStreamCaching();
-        // convert the input stream to StreamCache if the stream cache is not 
disabled
-        if (exchange.getProperty(Exchange.DISABLE_HTTP_STREAM_CACHE, 
disableStreamCaching, Boolean.class)) {
-            return is;
-        } else {
-            CachedOutputStream cos = new CachedOutputStream(exchange);
-            IOHelper.copyAndCloseInput(is, cos);
-            return cos.newStreamCache();
-        }
+        CachedOutputStream cos = new CachedOutputStream(exchange);
+        IOHelper.copyAndCloseInput(is, cos);
+        return cos.newStreamCache();
+    }
+
+    @Deprecated
+    public static Object readResponseBodyFromInputStream(InputStream is, 
Exchange exchange) throws IOException {
+        return cacheResponseBodyFromInputStream(is, exchange);
     }
 
     /**
-     * Reads the response body from the given input stream.
+     * Reads the request body from the given input stream.
      *
      * @param  is          the input stream
      * @param  exchange    the exchange
-     * @return             the response body, can be <tt>null</tt> if no body
-     * @throws IOException is thrown if error reading response body
+     * @return             the request body, can be <tt>null</tt> if no body
+     * @throws IOException is thrown if error reading request body
      */
-    public static Object readResponseBodyFromInputStream(InputStream is, 
Exchange exchange) throws IOException {
+    @Deprecated
+    public static Object readRequestBodyFromInputStream(InputStream is, 
Exchange exchange) throws IOException {
         if (is == null) {
             return null;
         }
+        boolean disableStreamCaching = 
!exchange.getContext().isStreamCaching();
         // convert the input stream to StreamCache if the stream cache is not 
disabled
-        if (exchange.getProperty(Exchange.DISABLE_HTTP_STREAM_CACHE, 
Boolean.FALSE, Boolean.class)) {
+        if (exchange.getProperty(Exchange.DISABLE_HTTP_STREAM_CACHE, 
disableStreamCaching, Boolean.class)) {
             return is;
         } else {
             CachedOutputStream cos = new CachedOutputStream(exchange);
diff --git 
a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpPollingConsumer.java
 
b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpPollingConsumer.java
index 437f701..4359d8d 100644
--- 
a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpPollingConsumer.java
+++ 
b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpPollingConsumer.java
@@ -49,7 +49,6 @@ public class HttpPollingConsumer extends 
PollingConsumerSupport {
         this.endpoint = endpoint;
         this.httpContext = endpoint.getHttpContext();
         this.httpClient = endpoint.getHttpClient();
-
     }
 
     @Override
@@ -83,13 +82,13 @@ public class HttpPollingConsumer extends 
PollingConsumerSupport {
             httpClientContext.setRequestConfig(requestConfig);
         }
 
-        HttpEntity responeEntity = null;
+        HttpEntity responseEntity = null;
         try {
             // execute request
             HttpResponse response = executeMethod(method, httpClientContext);
             int responseCode = response.getStatusLine().getStatusCode();
-            responeEntity = response.getEntity();
-            Object body = 
HttpHelper.readResponseBodyFromInputStream(responeEntity.getContent(), 
exchange);
+            responseEntity = response.getEntity();
+            Object body = 
HttpHelper.cacheResponseBodyFromInputStream(responseEntity.getContent(), 
exchange);
 
             // lets store the result in the output message.
             Message message = exchange.getOut();
@@ -118,9 +117,9 @@ public class HttpPollingConsumer extends 
PollingConsumerSupport {
         } catch (IOException e) {
             throw new RuntimeCamelException(e);
         } finally {
-            if (responeEntity != null) {
+            if (responseEntity != null) {
                 try {
-                    EntityUtils.consume(responeEntity);
+                    EntityUtils.consume(responseEntity);
                 } catch (IOException e) {
                     // nothing what we can do
                 }

Reply via email to