This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 50b81a4a598 CAMEL-20889: camel-core: Stream is not reset when
Message.getBody(class) is invoked ans stream caching is enabled (#14574)
50b81a4a598 is described below
commit 50b81a4a5983e38648121806d9ee4f3c8046dd25
Author: Luigi De Masi <[email protected]>
AuthorDate: Fri Jun 21 17:03:39 2024 +0200
CAMEL-20889: camel-core: Stream is not reset when Message.getBody(class) is
invoked ans stream caching is enabled (#14574)
---
.../org/apache/camel/util/ExchangeHelperTest.java | 19 +++++++++++++++++++
.../java/org/apache/camel/support/ExchangeHelper.java | 18 ++++++++++++++++++
2 files changed, 37 insertions(+)
diff --git
a/core/camel-core/src/test/java/org/apache/camel/util/ExchangeHelperTest.java
b/core/camel-core/src/test/java/org/apache/camel/util/ExchangeHelperTest.java
index d9400f810cf..0c69c952c0f 100644
---
a/core/camel-core/src/test/java/org/apache/camel/util/ExchangeHelperTest.java
+++
b/core/camel-core/src/test/java/org/apache/camel/util/ExchangeHelperTest.java
@@ -26,12 +26,14 @@ import org.apache.camel.ExchangePattern;
import org.apache.camel.NoSuchBeanException;
import org.apache.camel.NoSuchHeaderException;
import org.apache.camel.NoSuchPropertyException;
+import org.apache.camel.converter.stream.InputStreamCache;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.support.DefaultExchange;
import org.apache.camel.support.ExchangeHelper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.jupiter.api.Assertions.*;
public class ExchangeHelperTest extends ContextTestSupport {
@@ -213,6 +215,23 @@ public class ExchangeHelperTest extends ContextTestSupport
{
assertTrue(ExchangeHelper.isStreamCachingEnabled(exchange));
}
+ @Test
+ public void testGetBodyAndResetStreamCache() {
+ InputStreamCache body = new InputStreamCache("Hello Camel
Rider!".getBytes(UTF_8));
+ exchange.getMessage().setBody(body);
+
+ String first = ExchangeHelper.getBodyAndResetStreamCache(exchange,
String.class);
+ String second = ExchangeHelper.getBodyAndResetStreamCache(exchange,
String.class);
+
+ assertFalse(ObjectHelper.isEmpty(second), "second should not be null
or empty");
+ assertEquals(first, second);
+
+ // Null checks..
+ exchange.getMessage().setBody(null);
+ String third = ExchangeHelper.getBodyAndResetStreamCache(exchange,
String.class);
+ assertNull(third);
+ }
+
@Override
@BeforeEach
public void setUp() throws Exception {
diff --git
a/core/camel-support/src/main/java/org/apache/camel/support/ExchangeHelper.java
b/core/camel-support/src/main/java/org/apache/camel/support/ExchangeHelper.java
index 9f087318d0a..f34bf2662a0 100644
---
a/core/camel-support/src/main/java/org/apache/camel/support/ExchangeHelper.java
+++
b/core/camel-support/src/main/java/org/apache/camel/support/ExchangeHelper.java
@@ -46,6 +46,7 @@ import org.apache.camel.NoSuchPropertyException;
import org.apache.camel.NoTypeConversionAvailableException;
import org.apache.camel.Route;
import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.StreamCache;
import org.apache.camel.TypeConversionException;
import org.apache.camel.VariableAware;
import org.apache.camel.WrappedFile;
@@ -1248,4 +1249,21 @@ public final class ExchangeHelper {
return null;
}
+ /**
+ * Returns the body as the specified type. If <a
href="http://camel.apache.org/stream-caching.html">stream
+ * caching</a>. is enabled and the body is an instance of {@link
StreamCache}, the stream is reset before converting
+ * and returning the body.
+ *
+ * @param exchange the message exchange being processed
+ * @param type the type to convert to
+ * @return the body of the message as the specified type, or
<tt>null</tt> if body does not exist
+ */
+ public static <T> T getBodyAndResetStreamCache(Exchange exchange, Class<T>
type) {
+ Object body = exchange.getMessage().getBody();
+ if (body instanceof StreamCache) {
+ ((StreamCache) body).reset();
+ }
+ return exchange.getMessage().getBody(type);
+ }
+
}