mayurbm opened a new pull request, #25555:
URL: https://github.com/apache/camel/pull/25555

   ## Summary
   
   Fixes [CAMEL-24406](https://issues.apache.org/jira/browse/CAMEL-24406).
   
   `XmlConverter.toDOMDocument(byte[], Exchange)` and 
`toDOMDocument(StreamCache, Exchange)` passed content directly to 
`DocumentBuilder.parse()` without checking whether it could plausibly be XML. 
Non-XML content (empty body, JSON error response, plain-text HTTP response, 
BOM-only) triggered:
   
   ```
   org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is 
not allowed in prolog.
   ```
   
   Surfaced as:
   ```
   Cannot read from xml stream. Reason: Error during type conversion from type:
   org.apache.camel.converter.stream.ByteArrayInputStreamCache to the required 
type:
   org.w3c.dom.Document ... due to org.xml.sax.SAXParseException: Content is 
not allowed in prolog.
   ```
   
   ## JIRA
   
   **[CAMEL-24406](https://issues.apache.org/jira/browse/CAMEL-24406)** — 
camel-xml-jaxp: XmlConverter.toDOMDocument throws SAXParseException for non-XML 
content
   
   ## Stack Trace (from CAMEL-24406)
   
   ```
   org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is 
not allowed in prolog.
       at 
java.xml/javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:122)
       at 
org.apache.camel.converter.jaxp.XmlConverter.toDOMDocument(XmlConverter.java:661)
       at 
org.apache.camel.converter.jaxp.CamelXmlJaxpBulkConverterLoader.doConvertTo(CamelXmlJaxpBulkConverterLoader.java:317)
       at 
org.apache.camel.component.cxf.converter.CxfPayloadConverter.convertTo(CxfPayloadConverter.java:165)
       at 
org.apache.camel.impl.converter.CoreTypeConverterRegistry.convertTo(CoreTypeConverterRegistry.java:203)
       at 
org.apache.camel.support.MessageSupport.getBody(MessageSupport.java:96)
       at 
org.apache.camel.component.cxf.DefaultCxfBinding.getBodyFromCamel(DefaultCxfBinding.java:1019)
       at 
org.apache.camel.component.cxf.DefaultCxfBinding.populateCxfResponseFromExchange(DefaultCxfBinding.java:461)
       at 
org.apache.camel.component.cxf.CxfConsumer.CxfConsumerInvoker.setResponseBack(CxfConsumer.java:324)
   ```
   
   ## Fix
   
   Add a cheap static `looksLikeXml(byte[])` helper (handles UTF-8/UTF-16 BOMs 
and leading whitespace) and guard the two `toDOMDocument` overloads that take 
`byte[]` and `StreamCache` to return `null` when content is definitely not XML. 
Mark both with `@Converter(allowNull = true)` so the Camel type-converter 
framework falls through gracefully without an error.
   
   ## Changes
   
   - 
`core/camel-xml-jaxp/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java`
     - `toDOMDocument(byte[], Exchange)`: add `looksLikeXml` guard, `allowNull 
= true`
     - `toDOMDocument(StreamCache, Exchange)`: add `looksLikeXml` guard, 
`allowNull = true`; convert to `byte[]` first to avoid consuming stream twice
     - Add `looksLikeXml(byte[])` static package-private helper
   
   - 
`core/camel-xml-jaxp/src/test/java/org/apache/camel/converter/jaxp/XmlConverterPrologTest.java`
 (new) — 13 tests
   
   ## Test Results
   
   ```
   Tests run: 13, Failures: 0, Errors: 0, Skipped: 0  [JDK 21 / Maven 3.9]
   BUILD SUCCESS
   ```
   
   ## AI Attribution
   
   This contribution was developed with AI assistance using [Claude 
Code](https://github.com/anthropics/claude-code).
   
   ```
   Co-authored-by: Claude <[email protected]>
   ```
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to