You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2023/12/24 12:04:13 UTC

(camel) 08/08: CAMEL-14028: Allow DataFormats to unmarshal known data formats without first converting to bytes

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

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

commit 35051dabe2df12b8a371cbce6556e61efb3aa1d5
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sun Dec 24 12:43:29 2023 +0100

    CAMEL-14028: Allow DataFormats to unmarshal known data formats without first converting to bytes
---
 .../camel/component/grok/GrokDataFormat.java       | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)

diff --git a/components/camel-grok/src/main/java/org/apache/camel/component/grok/GrokDataFormat.java b/components/camel-grok/src/main/java/org/apache/camel/component/grok/GrokDataFormat.java
index fd0a4f8c196..8d47841e820 100644
--- a/components/camel-grok/src/main/java/org/apache/camel/component/grok/GrokDataFormat.java
+++ b/components/camel-grok/src/main/java/org/apache/camel/component/grok/GrokDataFormat.java
@@ -20,6 +20,8 @@ import java.io.BufferedReader;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStream;
+import java.io.Reader;
+import java.io.StringReader;
 import java.nio.CharBuffer;
 import java.util.*;
 import java.util.stream.Stream;
@@ -131,17 +133,31 @@ public class GrokDataFormat extends ServiceSupport implements DataFormat, DataFo
 
     @Override
     public Object unmarshal(Exchange exchange, InputStream stream) throws Exception {
+        return unmarshal(exchange, (Object) stream);
+    }
+
+    @Override
+    public Object unmarshal(Exchange exchange, Object body) throws Exception {
         List<Map<String, Object>> result = new ArrayList<>();
 
-        InputStreamReader in = new InputStreamReader(stream, ExchangeHelper.getCharsetName(exchange));
-        try (Stream<String> lines = new BufferedReader(in).lines()) {
+        Reader reader = null;
+        if (body instanceof String s) {
+            reader = new StringReader(s);
+        } else if (body instanceof Reader r) {
+            reader = r;
+        } else {
+            // fallback to input stream
+            InputStream is = exchange.getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, exchange, body);
+            reader = new InputStreamReader(is, ExchangeHelper.getCharsetName(exchange));
+        }
+
+        try (Stream<String> lines = new BufferedReader(reader).lines()) {
             lines.forEachOrdered(line -> processLine(line, result));
         }
 
         if (result.isEmpty()) {
             return null;
         }
-
         if (result.size() == 1) {
             return result.get(0);
         }