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:09 UTC

(camel) 04/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 e8a4a0285a31778c168488817ea39a09fa5e4e92
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sun Dec 24 12:14:52 2023 +0100

    CAMEL-14028: Allow DataFormats to unmarshal known data formats without first converting to bytes
---
 .../apache/camel/dataformat/csv/CsvDataFormat.java |  7 ++++-
 .../camel/dataformat/csv/CsvUnmarshaller.java      | 30 ++++++++++++++--------
 2 files changed, 26 insertions(+), 11 deletions(-)

diff --git a/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvDataFormat.java b/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvDataFormat.java
index 3de23c32eca..9d400e7e6b1 100644
--- a/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvDataFormat.java
+++ b/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvDataFormat.java
@@ -92,7 +92,12 @@ public class CsvDataFormat extends ServiceSupport implements DataFormat, DataFor
 
     @Override
     public Object unmarshal(Exchange exchange, InputStream inputStream) throws Exception {
-        return unmarshaller.unmarshal(exchange, inputStream);
+        return unmarshal(exchange, (Object) inputStream);
+    }
+
+    @Override
+    public Object unmarshal(Exchange exchange, Object body) throws Exception {
+        return unmarshaller.unmarshal(exchange, body);
     }
 
     @Override
diff --git a/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvUnmarshaller.java b/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvUnmarshaller.java
index 060537730ca..6811ef71166 100644
--- a/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvUnmarshaller.java
+++ b/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvUnmarshaller.java
@@ -69,12 +69,12 @@ abstract class CsvUnmarshaller {
     /**
      * Unmarshal the CSV
      *
-     * @param  exchange    Exchange (used for accessing type converter)
-     * @param  inputStream Input CSV stream
-     * @return             Unmarshalled CSV
-     * @throws IOException if the stream cannot be read properly
+     * @param  exchange  Exchange (used for accessing type converter)
+     * @param  body      the input
+     * @return           Unmarshalled CSV
+     * @throws Exception if error during unmarshalling
      */
-    public abstract Object unmarshal(Exchange exchange, InputStream inputStream) throws IOException;
+    public abstract Object unmarshal(Exchange exchange, Object body) throws Exception;
 
     private static CsvRecordConverter<?> extractConverter(CsvDataFormat dataFormat) {
         if (dataFormat.getRecordConverter() != null) {
@@ -99,9 +99,15 @@ abstract class CsvUnmarshaller {
         }
 
         @Override
-        public Object unmarshal(Exchange exchange, InputStream inputStream) throws IOException {
+        public Object unmarshal(Exchange exchange, Object body) throws Exception {
+            Reader reader = exchange.getContext().getTypeConverter().tryConvertTo(Reader.class, exchange, body);
+            if (reader == null) {
+                // fallback to input stream
+                InputStream is = exchange.getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, exchange, body);
+                reader = new InputStreamReader(is, ExchangeHelper.getCharsetName(exchange));
+            }
             CSVParser parser
-                    = new CSVParser(new InputStreamReader(inputStream, ExchangeHelper.getCharsetName(exchange)), format);
+                    = new CSVParser(reader, format);
             try {
                 if (dataFormat.isCaptureHeaderRecord()) {
                     exchange.getMessage().setHeader(CsvConstants.HEADER_RECORD, parser.getHeaderNames());
@@ -132,10 +138,14 @@ abstract class CsvUnmarshaller {
         }
 
         @Override
-        public Object unmarshal(Exchange exchange, InputStream inputStream) throws IOException {
-            Reader reader = null;
+        public Object unmarshal(Exchange exchange, Object body) throws Exception {
+            Reader reader = exchange.getContext().getTypeConverter().tryConvertTo(Reader.class, exchange, body);
+            if (reader == null) {
+                // fallback to input stream
+                InputStream is = exchange.getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, exchange, body);
+                reader = new InputStreamReader(is, ExchangeHelper.getCharsetName(exchange));
+            }
             try {
-                reader = new InputStreamReader(inputStream, ExchangeHelper.getCharsetName(exchange));
                 CSVParser parser = new CSVParser(reader, format);
                 CsvIterator<?> answer = new CsvIterator<>(parser, converter);
                 // add to UoW, so we can close the iterator, so it can release any resources