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 a49b254f07bbbf3a7ee5011bd530217f1f63450a Author: Claus Ibsen <[email protected]> AuthorDate: Sun Dec 24 12:08:13 2023 +0100 CAMEL-14028: Allow DataFormats to unmarshal known data formats without first converting to bytes --- .../jackson/AbstractJacksonDataFormat.java | 40 ++++++++++++++++++++-- .../component/jacksonxml/JacksonXMLDataFormat.java | 38 ++++++++++++++++++-- .../camel/converter/jaxb/JaxbDataFormat.java | 2 +- 3 files changed, 75 insertions(+), 5 deletions(-) diff --git a/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/AbstractJacksonDataFormat.java b/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/AbstractJacksonDataFormat.java index 7bf41509fa0..a1d686da2b7 100644 --- a/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/AbstractJacksonDataFormat.java +++ b/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/AbstractJacksonDataFormat.java @@ -16,8 +16,10 @@ */ package org.apache.camel.component.jackson; +import java.io.File; import java.io.InputStream; import java.io.OutputStream; +import java.io.Reader; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -30,9 +32,11 @@ import java.util.TimeZone; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.core.FormatSchema; import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.MapperFeature; import com.fasterxml.jackson.databind.Module; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectReader; import com.fasterxml.jackson.databind.PropertyNamingStrategies; import com.fasterxml.jackson.databind.PropertyNamingStrategy; import com.fasterxml.jackson.databind.SerializationFeature; @@ -40,6 +44,7 @@ import com.fasterxml.jackson.databind.type.CollectionType; import org.apache.camel.CamelContext; import org.apache.camel.CamelContextAware; import org.apache.camel.Exchange; +import org.apache.camel.WrappedFile; import org.apache.camel.spi.DataFormat; import org.apache.camel.spi.DataFormatContentTypeHeader; import org.apache.camel.spi.DataFormatName; @@ -161,6 +166,11 @@ public abstract class AbstractJacksonDataFormat extends ServiceSupport @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 { FormatSchema schema = null; if (this.schemaResolver != null) { schema = this.schemaResolver.resolve(exchange); @@ -177,12 +187,38 @@ public abstract class AbstractJacksonDataFormat extends ServiceSupport if (type != null) { clazz = exchange.getContext().getClassResolver().resolveMandatoryClass(type); } + + ObjectReader reader; if (collectionType != null) { CollectionType collType = objectMapper.getTypeFactory().constructCollectionType(collectionType, clazz); - return this.objectMapper.readerFor(collType).with(schema).readValue(stream); + reader = this.objectMapper.readerFor(collType).with(schema); + } else { + reader = this.objectMapper.reader(schema).forType(clazz); + } + + // unwrap file (such as from camel-file) + if (body instanceof WrappedFile<?>) { + body = ((WrappedFile<?>) body).getBody(); + } + Object answer; + if (body instanceof String b) { + answer = reader.readValue(b); + } else if (body instanceof byte[] arr) { + answer = reader.readValue(arr); + } else if (body instanceof Reader r) { + answer = reader.readValue(r); + } else if (body instanceof File f) { + answer = reader.readValue(f); + } else if (body instanceof JsonNode n) { + answer = reader.readValue(n); } else { - return this.objectMapper.reader(schema).readValue(stream, clazz); + // fallback to input stream + InputStream is = exchange.getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, exchange, body); + answer = reader.readValue(is); } + + return answer; + } // Properties diff --git a/components/camel-jacksonxml/src/main/java/org/apache/camel/component/jacksonxml/JacksonXMLDataFormat.java b/components/camel-jacksonxml/src/main/java/org/apache/camel/component/jacksonxml/JacksonXMLDataFormat.java index 18192f32afb..4ec59b5bf8f 100644 --- a/components/camel-jacksonxml/src/main/java/org/apache/camel/component/jacksonxml/JacksonXMLDataFormat.java +++ b/components/camel-jacksonxml/src/main/java/org/apache/camel/component/jacksonxml/JacksonXMLDataFormat.java @@ -16,8 +16,10 @@ */ package org.apache.camel.component.jacksonxml; +import java.io.File; import java.io.InputStream; import java.io.OutputStream; +import java.io.Reader; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -28,8 +30,10 @@ import java.util.TimeZone; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.MapperFeature; import com.fasterxml.jackson.databind.Module; +import com.fasterxml.jackson.databind.ObjectReader; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.type.CollectionType; import com.fasterxml.jackson.dataformat.xml.XmlMapper; @@ -38,6 +42,7 @@ import com.fasterxml.jackson.module.jakarta.xmlbind.JakartaXmlBindAnnotationModu import org.apache.camel.CamelContext; import org.apache.camel.CamelContextAware; import org.apache.camel.Exchange; +import org.apache.camel.WrappedFile; import org.apache.camel.spi.DataFormat; import org.apache.camel.spi.DataFormatContentTypeHeader; import org.apache.camel.spi.DataFormatName; @@ -173,7 +178,11 @@ public class JacksonXMLDataFormat extends ServiceSupport @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 { // is there a header with the unmarshal type? Class<?> clazz = unmarshalType; String type = null; @@ -186,12 +195,37 @@ public class JacksonXMLDataFormat extends ServiceSupport if (type != null) { clazz = exchange.getContext().getClassResolver().resolveMandatoryClass(type); } + + ObjectReader reader; if (collectionType != null) { CollectionType collType = xmlMapper.getTypeFactory().constructCollectionType(collectionType, clazz); - return this.xmlMapper.readValue(stream, collType); + reader = this.xmlMapper.readerFor(collType); + } else { + reader = this.xmlMapper.reader().forType(clazz); + } + + // unwrap file (such as from camel-file) + if (body instanceof WrappedFile<?>) { + body = ((WrappedFile<?>) body).getBody(); + } + Object answer; + if (body instanceof String b) { + answer = reader.readValue(b); + } else if (body instanceof byte[] arr) { + answer = reader.readValue(arr); + } else if (body instanceof Reader r) { + answer = reader.readValue(r); + } else if (body instanceof File f) { + answer = reader.readValue(f); + } else if (body instanceof JsonNode n) { + answer = reader.readValue(n); } else { - return this.xmlMapper.readValue(stream, clazz); + // fallback to input stream + InputStream is = exchange.getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, exchange, body); + answer = reader.readValue(is); } + + return answer; } // Properties diff --git a/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java b/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java index 7bb3db0c02f..dbd845c19c6 100644 --- a/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java +++ b/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java @@ -272,7 +272,7 @@ public class JaxbDataFormat extends ServiceSupport @Override public Object unmarshal(Exchange exchange, InputStream stream) throws Exception { - throw new UnsupportedOperationException("Not in use"); + return unmarshal(exchange, (Object) stream); } @Override
