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

   [CAMEL-27366](https://issues.apache.org/jira/browse/CAMEL-27366)
   
   ## What was wrong
   
   1. **The producer ignored MSH-18.** It encoded a `String` body in the 
charset of the `CamelCharsetName` header, then the exchange property, then 
`charsetName`, while the consumer decodes by MSH-18. A message marked `UNICODE 
UTF-8` and sent with the default charset arrived broken (`S?rensen`, `?ucja`).
   2. **The `CamelCharsetName` exchange property won over `charsetName`.** The 
consumer always set it (ISO-8859-1 by default) and the producer set it after 
each send. In a route from an mllp consumer to an mllp producer the producer's 
`charsetName` was ignored, and later steps that turn text into bytes used 
ISO-8859-1 too.
   3. **`CamelMllpCharset` did not hold MSH-18,** as the catalog says. It held 
the consumer's `charsetName` (`ISO_8859_1`) whenever the message had an MSH-18.
   
   ## Why it was like that (git history)
   
   - Camel 2.21 (CAMEL-12106 / CAMEL-12265): `charsetName` was unset by default 
and, when set, overrode MSH-18. The consumer set the property only for an 
explicit `charsetName`, and the `CamelMllpCharset` header showed the override.
   - CAMEL-17122 (2021) set `charsetName` from the component's `defaultCharset` 
on every endpoint, so `hasCharsetName()` became always true. After that the 
property was always set and the header always showed `charsetName`. This looks 
like a side effect, not a decision.
   - CAMEL-17128 (2021) made MSH-18 win for decoding in the consumer, but did 
not change the producer or the header.
   - CAMEL-26183 (#29008, 4.24, not released yet) made the producer set the 
property to "the charset the message was sent in". This PR takes that back (see 
below), so the paragraph it added to the 4.24 upgrade guide is replaced.
   
   So for the header I chose to make it hold MSH-18, not to change its 
description. This matches the docs, matches the CAMEL-17128 direction (MSH-18 
wins), and the header is only set when the message has an MSH-18 anyway.
   
   ## The fix
   
   - `MllpCharsetHelper.getMsh18Charset(...)` and `encode(...)`: the charset of 
MSH-18, else the endpoint charset. The exchange charset is not used.
   - **Producer:** encodes a `String`/`CharSequence` in the MSH-18 charset, 
else `charsetName`, and no longer sets `CamelCharsetName`. A converted body 
(such as a `Reader`) is converted in `charsetName`; the exchange's charset 
property and header are restored afterwards. The acknowledgement string is 
decoded by its MSH-18, else `charsetName`.
   - **Consumer:** with a `String` body (the default) it no longer sets 
`CamelCharsetName`. With `stringPayload=false` it sets it to the charset of the 
`byte[]` body (MSH-18, else `charsetName`). The headers are decoded in that 
charset. `CamelMllpCharset` is the raw MSH-18 value. An acknowledgement given 
as a `String` in `CamelMllpAcknowledgement`/`CamelMllpAcknowledgementString` is 
encoded by its MSH-18, else the exchange charset, else `charsetName`. Before, 
the type converter did this, using the property the consumer had set.
   - `MllpEndpoint.createExchange` no longer sets the property.
   - Javadoc of `charsetName` and `stringPayload` fixed: `stringPayload` said 
`charsetName` wins over MSH-18. Added a "Character set" section to the 
component docs. Regenerated `mllp.json`, the catalog copies (json and adoc), 
and the endpoint and component DSL.
   - Upgrade guide 4.24: a new section, which replaces the CAMEL-26183 
paragraph about the property after sending.
   
   ### A judgment call worth checking
   
   The consumer still sets `CamelCharsetName` with `stringPayload=false`. A 
`byte[]` body really is in that charset, and camel-hl7 needs it: 
`HL7Converter.toMessage(byte[], Exchange)` and `HL7DataFormat.guessCharsetName` 
decode bytes by the exchange charset. Dropping it would make those fall back to 
the JVM default (UTF-8) for messages without MSH-18. The value is now the 
MSH-18 charset rather than always `charsetName`. The mllp producer ignores the 
property in any case.
   
   The producer also no longer uses the `CamelCharsetName` **header** as an 
override. camel-hl7's `unmarshal` sets that header to a guessed charset, so it 
can leak just like the property. To send in a charset other than MSH-18, send a 
`byte[]`.
   
   I did not change `Hl7Util.findMsh18`: MSH-18 as the last field already 
works, because the segment delimiter counts as a separator.
   
   ## Tests
   
   - New `MllpCharsetTest` (8 tests) with `Sørensen^Łucja`. It covers: producer 
encoding by MSH-18 `UNICODE UTF-8` with the default charset; MSH-18 `8859/2` 
winning over `charsetName=UTF-8`; `charsetName` winning over an exchange 
property when there is no MSH-18 (String and `StringBuilder`); consumer 
decoding plus `CamelMllpCharset` = `UNICODE UTF-8` and no property for a String 
body; relay mllp -> mllp keeping MSH-18 UTF-8; relay using the producer's 
`charsetName`; consumer acknowledgement String encoded by its MSH-18.
   - Without the fix all 8 fail. With the fix all pass.
   - `MllpTcpClientProducerBodyConversionTest`: the CAMEL-26183 test that 
asserted "property wins" now asserts that `charsetName` wins. Added a test that 
a converted `Reader` body uses `charsetName` and the exchange's charset 
property and header are kept. Both fail without the fix.
   - Full `camel-mllp` module: `mvn -o test`: 381 tests, 0 failures, 3 skipped 
(the skips were there before).
   
   Nothing in the ticket failed to reproduce.
   
   _Claude Code on behalf of davsclaus_
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
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