Stig Rohde Døssing created CAMEL-14028:
------------------------------------------
Summary: Allow DataFormats to unmarshal known data formats without
first converting to bytes
Key: CAMEL-14028
URL: https://issues.apache.org/jira/browse/CAMEL-14028
Project: Camel
Issue Type: Improvement
Affects Versions: 3.0.0
Reporter: Stig Rohde Døssing
The motivating example here is unmarshalling JAXB from a String message body,
but the issue exists for other data formats as well, e.g. JSON. In a route like
{quote}
from("direct:receive-a-string")
.unmarshal().jaxb()
{quote}
Camel will unnecessarily serialize the String to bytes, then deserialize the
bytes to JAXB Java objects. In addition to being inefficient, this can cause
encoding issues if the encoding used by Camel to serialize the String doesn't
match the encoding set in the XML declaration.
The reason Camel needs to serialize to bytes is that the
[DataFormat|https://github.com/apache/camel/blob/7518aa587c0b887d7e071d6c64c14d91bd0d0e67/core/camel-api/src/main/java/org/apache/camel/spi/DataFormat.java]
interface takes an InputStream parameter in the unmarshal method. Because of
this parameter, the
[UnmarshalProcessor|https://github.com/apache/camel/blob/3312243b32af03ac39c3af170e318f03e01d64f0/core/camel-support/src/main/java/org/apache/camel/support/processor/UnmarshalProcessor.java#L56]
needs to convert the message body to bytes before calling the data format.
We should be able to introduce a new method with the signature
{{unmarshal(Exchange)}}, which would let data formats examine the body's type,
and decide whether it is necessary to serialize to bytes before unmarshalling.
For instance, the JAXB data format could choose only to serialize to bytes if
the input body is not one of the types accepted by the [JAXB
Unmarshaller|https://docs.oracle.com/javase/8/docs/api/javax/xml/bind/Unmarshaller.html],
or a String.
We should be able to implement this in a backwards compatible way, by
introducing the new method as a default method on the DataFormat interface. The
default implementation should extract the exchange message body as an
InputStream, and call the existing unmarshal method. We can then update
UnmarshalProcessor to only call the new method.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)