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 099569db5cfd CAMEL-26521: camel-support - count a
ByteArrayOutputStream as a payload in BodyFacts (#30773)
099569db5cfd is described below
commit 099569db5cfd7e6ecf2afce1aec7555f44577288
Author: Karol Krawczyk <[email protected]>
AuthorDate: Thu Nov 26 17:45:51 2026 +0100
CAMEL-26521: camel-support - count a ByteArrayOutputStream as a payload in
BodyFacts (#30773)
A ByteArrayOutputStream, the body camel-tika parse produces, failed as a
value Camel reads only as its toString() in
the watsonx-ai operations that read the body as text (CAMEL-26521) and in
the plaintext header of alibaba-kms
(CAMEL-27035), and as a body that is not text, bytes, a stream or a file in
google-vertexai rawPredict. BodyFacts
counted it as data. It holds bytes, and IOConverter converts it to bytes, a
stream or text, so it is now a payload,
which fixes the three components at once. The message history and the body
type registry record it as a payload too.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../camel/component/alibaba/kms/KMSInputTest.java | 32 ++++++++++++++++++++++
.../vertexai/GoogleVertexAIProducerBodyTest.java | 13 +++++++++
.../ibm/watsonx/ai/WatsonxAiMessageBodyTest.java | 29 ++++++++++++++++++++
.../org/apache/camel/support/BodyFactsTest.java | 3 ++
.../java/org/apache/camel/support/BodyFacts.java | 9 ++++--
5 files changed, 84 insertions(+), 2 deletions(-)
diff --git
a/components/camel-alibaba/camel-alibaba-kms/src/test/java/org/apache/camel/component/alibaba/kms/KMSInputTest.java
b/components/camel-alibaba/camel-alibaba-kms/src/test/java/org/apache/camel/component/alibaba/kms/KMSInputTest.java
index 4398acc30153..7f12e130895f 100644
---
a/components/camel-alibaba/camel-alibaba-kms/src/test/java/org/apache/camel/component/alibaba/kms/KMSInputTest.java
+++
b/components/camel-alibaba/camel-alibaba-kms/src/test/java/org/apache/camel/component/alibaba/kms/KMSInputTest.java
@@ -16,6 +16,7 @@
*/
package org.apache.camel.component.alibaba.kms;
+import java.io.ByteArrayOutputStream;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
@@ -123,6 +124,37 @@ class KMSInputTest extends CamelTestSupport {
.isEqualTo("https://camel.apache.org".getBytes(StandardCharsets.UTF_8));
}
+ /** The body camel-tika parse produces, a payload like bytes. */
+ @Test
+ void aByteArrayOutputStreamPlaintextHeaderIsEncryptedAsItsText() throws
Exception {
+ stubEncrypt();
+ ByteArrayOutputStream plaintext = new ByteArrayOutputStream();
+ plaintext.writeBytes("my secret".getBytes(StandardCharsets.UTF_8));
+
+ Exchange result = template.send("direct:kms",
+ exchange -> exchange.getIn().setHeader(KMSHeaders.PLAINTEXT,
plaintext));
+
+ assertThat(result.getException()).isNull();
+ assertThat(encryptedPlaintext()).isEqualTo("my
secret".getBytes(StandardCharsets.UTF_8));
+ }
+
+ /** Decoded with the charset of the exchange, not with the platform
default its toString() uses. */
+ @Test
+ void
aByteArrayOutputStreamPlaintextHeaderIsDecodedWithTheCharsetOfTheExchange()
throws Exception {
+ stubEncrypt();
+ String text = "Z\u00fcrich";
+ ByteArrayOutputStream plaintext = new ByteArrayOutputStream();
+ plaintext.writeBytes(text.getBytes(StandardCharsets.ISO_8859_1));
+
+ Exchange result = template.send("direct:kms", exchange -> {
+ exchange.setProperty(Exchange.CHARSET_NAME,
StandardCharsets.ISO_8859_1.name());
+ exchange.getIn().setHeader(KMSHeaders.PLAINTEXT, plaintext);
+ });
+
+ assertThat(result.getException()).isNull();
+
assertThat(encryptedPlaintext()).isEqualTo(text.getBytes(StandardCharsets.UTF_8));
+ }
+
@Test
void aMapInThePlaintextHeaderFailsInsteadOfBeingEncryptedAsItsToString()
throws Exception {
stubEncrypt();
diff --git
a/components/camel-google/camel-google-vertexai/src/test/java/org/apache/camel/component/google/vertexai/GoogleVertexAIProducerBodyTest.java
b/components/camel-google/camel-google-vertexai/src/test/java/org/apache/camel/component/google/vertexai/GoogleVertexAIProducerBodyTest.java
index 07026ac5e98d..f75943cc861b 100644
---
a/components/camel-google/camel-google-vertexai/src/test/java/org/apache/camel/component/google/vertexai/GoogleVertexAIProducerBodyTest.java
+++
b/components/camel-google/camel-google-vertexai/src/test/java/org/apache/camel/component/google/vertexai/GoogleVertexAIProducerBodyTest.java
@@ -17,6 +17,7 @@
package org.apache.camel.component.google.vertexai;
import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
@@ -409,6 +410,18 @@ class GoogleVertexAIProducerBodyTest extends
CamelTestSupport {
assertEquals(JSON_REQUEST, predictionService.lastRequest());
}
+ @Test
+ void aJsonRequestInAByteArrayOutputStreamIsSent() {
+ // the body camel-tika parse produces
+ ByteArrayOutputStream body = new ByteArrayOutputStream();
+ body.writeBytes(JSON_REQUEST.getBytes(StandardCharsets.UTF_8));
+
+ Exchange exchange = template.request(META, e ->
e.getIn().setBody(body));
+
+ assertNull(exchange.getException());
+ assertEquals(JSON_REQUEST, predictionService.lastRequest());
+ }
+
@Test
void aJsonRequestInAFileIsSent() throws IOException {
File request = Files.writeString(tempDir.resolve("request.json"),
JSON_REQUEST).toFile();
diff --git
a/components/camel-ibm/camel-ibm-watsonx-ai/src/test/java/org/apache/camel/component/ibm/watsonx/ai/WatsonxAiMessageBodyTest.java
b/components/camel-ibm/camel-ibm-watsonx-ai/src/test/java/org/apache/camel/component/ibm/watsonx/ai/WatsonxAiMessageBodyTest.java
index db2509bad62d..5d962d3c8019 100644
---
a/components/camel-ibm/camel-ibm-watsonx-ai/src/test/java/org/apache/camel/component/ibm/watsonx/ai/WatsonxAiMessageBodyTest.java
+++
b/components/camel-ibm/camel-ibm-watsonx-ai/src/test/java/org/apache/camel/component/ibm/watsonx/ai/WatsonxAiMessageBodyTest.java
@@ -17,6 +17,7 @@
package org.apache.camel.component.ibm.watsonx.ai;
import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
@@ -399,6 +400,34 @@ class WatsonxAiMessageBodyTest extends CamelTestSupport {
assertUserMessage(instant.toString(),
request("/ml/v1/text/chat").get("messages").get(0));
}
+ @Test
+ void chatReadsAByteArrayOutputStreamBodyAsTheUserMessage() throws
Exception {
+ // the body camel-tika parse produces, a payload like bytes
+ ByteArrayOutputStream body = new ByteArrayOutputStream();
+ body.writeBytes("What is Apache
Camel?".getBytes(StandardCharsets.UTF_8));
+
+ call(WatsonxAiOperations.chat, body);
+
+ assertUserMessage("What is Apache Camel?",
request("/ml/v1/text/chat").get("messages").get(0));
+ }
+
+ @Test
+ void chatDecodesAByteArrayOutputStreamBodyWithTheCharsetOfTheExchange()
throws Exception {
+ // camel-tika parse sets the charset of the text it writes; toString()
would decode with the platform default
+ String text = "Z\u00fcrich";
+ ByteArrayOutputStream body = new ByteArrayOutputStream();
+ body.writeBytes(text.getBytes(StandardCharsets.ISO_8859_1));
+
+ Exchange result = template.send("direct:watsonx", exchange -> {
+ exchange.getIn().setHeader(WatsonxAiConstants.OPERATION,
WatsonxAiOperations.chat);
+ exchange.setProperty(Exchange.CHARSET_NAME,
StandardCharsets.ISO_8859_1.name());
+ exchange.getIn().setBody(body);
+ });
+
+ assertNoException(result);
+ assertUserMessage(text,
request("/ml/v1/text/chat").get("messages").get(0));
+ }
+
@Test
void chatFailsWithoutABody() {
Exchange result = send(WatsonxAiOperations.chat, null);
diff --git
a/core/camel-core/src/test/java/org/apache/camel/support/BodyFactsTest.java
b/core/camel-core/src/test/java/org/apache/camel/support/BodyFactsTest.java
index 13e2baa40288..ed4a9e34834b 100644
--- a/core/camel-core/src/test/java/org/apache/camel/support/BodyFactsTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/support/BodyFactsTest.java
@@ -17,6 +17,7 @@
package org.apache.camel.support;
import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
@@ -262,6 +263,8 @@ public class BodyFactsTest extends ContextTestSupport {
assertEquals(Kind.PAYLOAD, BodyFacts.kindOf("text"));
assertEquals(Kind.PAYLOAD, BodyFacts.kindOf(new byte[] { 1 }));
assertEquals(Kind.PAYLOAD, BodyFacts.kindOf(new
ByteArrayInputStream(new byte[0])));
+ // the bytes camel-tika parse writes
+ assertEquals(Kind.PAYLOAD, BodyFacts.kindOf(new
ByteArrayOutputStream()));
assertEquals(Kind.PAYLOAD, BodyFacts.kindOf(42));
assertEquals(Kind.PAYLOAD, BodyFacts.kindOf(Boolean.TRUE));
// parsed data: fields to read, and a marshal to write it
diff --git
a/core/camel-support/src/main/java/org/apache/camel/support/BodyFacts.java
b/core/camel-support/src/main/java/org/apache/camel/support/BodyFacts.java
index 12a63d420e70..144cf3b5f87d 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/BodyFacts.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/BodyFacts.java
@@ -16,6 +16,7 @@
*/
package org.apache.camel.support;
+import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.Reader;
@@ -631,9 +632,13 @@ public final class BodyFacts {
return BodyTypeRegistry.Kind.DATA;
}
- /** The payload in its serialized form: text, bytes, a stream or a file. */
+ /**
+ * The payload in its serialized form: text, bytes, a stream or a file. A
ByteArrayOutputStream, which camel-tika
+ * parse puts in the body, holds the bytes written to it, and converts to
bytes, a stream or text as they do.
+ */
private static boolean isPayload(Object body) {
- return body instanceof String || body instanceof byte[] || body
instanceof InputStream || body instanceof Reader
+ return body instanceof String || body instanceof byte[] || body
instanceof ByteArrayOutputStream
+ || body instanceof InputStream || body instanceof Reader
|| body instanceof StreamCache || body instanceof
WrappedFile<?> || body instanceof File
|| body instanceof Path;
}