This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch CAMEL-27366-mllp-charset in repository https://gitbox.apache.org/repos/asf/camel.git
commit 36b5ccb221627f5e1e549b2c6b2c20ba44c5a080 Author: Claus Ibsen <[email protected]> AuthorDate: Sun Nov 22 10:00:24 2026 +0100 CAMEL-27366: camel-mllp - the charset of a message is MSH-18, then charsetName, not the CamelCharsetName exchange property The producer ignored MSH-18 and encoded a String body in the charset of the CamelCharsetName header, then the exchange property, then charsetName. The consumer always set that property (ISO-8859-1 by default) and the producer set it after each send, so in a route from an mllp consumer to an mllp producer the charsetName of the producer was ignored, and a message marked UNICODE UTF-8 in MSH-18 was sent in ISO-8859-1. The producer now encodes text in the charset of MSH-18, else charsetName, and no longer sets the property. The consumer sets the property only for a byte[] body (stringPayload=false), to the charset of MSH-18 or charsetName. An acknowledgement given as a String is encoded in the charset of its MSH-18. The CamelMllpCharset header now holds the MSH-18 value, as documented, instead of charsetName. The override came from Camel 2.21, when charsetName was unset by default and overrode MSH-18; since CAMEL-17122 it is always set, and since CAMEL-17128 MSH-18 wins for decoding. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Claus Ibsen <[email protected]> --- .../org/apache/camel/catalog/components/mllp.json | 10 +- .../apache/camel/catalog/docs/mllp-component.adoc | 20 +- .../org/apache/camel/component/mllp/mllp.json | 10 +- .../camel-mllp/src/main/docs/mllp-component.adoc | 20 +- .../camel/component/mllp/MllpCharsetHelper.java | 54 ++++-- .../camel/component/mllp/MllpConfiguration.java | 10 +- .../apache/camel/component/mllp/MllpEndpoint.java | 18 +- .../component/mllp/MllpTcpClientProducer.java | 31 ++-- .../component/mllp/MllpTcpServerConsumer.java | 36 ++-- .../camel/component/mllp/MllpCharsetTest.java | 206 +++++++++++++++++++++ .../MllpTcpClientProducerBodyConversionTest.java | 24 ++- .../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc | 23 ++- .../component/dsl/MllpComponentBuilderFactory.java | 16 +- .../endpoint/dsl/MllpEndpointBuilderFactory.java | 75 ++++---- 14 files changed, 436 insertions(+), 117 deletions(-) diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/mllp.json b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/mllp.json index a8f45c565bc1..769a0c17b881 100644 --- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/mllp.json +++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/mllp.json @@ -25,11 +25,11 @@ }, "componentProperties": { "autoAck": { "index": 0, "kind": "property", "displayName": "Auto Ack", "group": "common", "label": "", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "configurationClass": "org.apache.camel.component.mllp.MllpConfiguration", "configurationField": "configuration", "description": "Enable\/Disable the automatic generation of a MLLP Acknowledgement MLLP Consumers only" }, - "charsetName": { "index": 1, "kind": "property", "displayName": "Charset Name", "group": "common", "label": "", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.mllp.MllpConfiguration", "configurationField": "configuration", "description": "Sets the default charset to use" }, + "charsetName": { "index": 1, "kind": "property", "displayName": "Charset Name", "group": "common", "label": "", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.mllp.MllpConfiguration", "configurationField": "configuration", "description": "The charset of an HL7 message that has no MSH-18. The consumer decodes, and the producer encodes, a message in the char [...] "configuration": { "index": 2, "kind": "property", "displayName": "Configuration", "group": "common", "label": "", "required": false, "type": "object", "javaType": "org.apache.camel.component.mllp.MllpConfiguration", "deprecated": false, "autowired": false, "secret": false, "description": "Sets the default configuration to use when creating MLLP endpoints." }, "hl7Headers": { "index": 3, "kind": "property", "displayName": "Hl7 Headers", "group": "common", "label": "", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "configurationClass": "org.apache.camel.component.mllp.MllpConfiguration", "configurationField": "configuration", "description": "Enable\/Disable the automatic generation of message headers from the HL7 Message MLLP Consumers only" }, "requireEndOfData": { "index": 4, "kind": "property", "displayName": "Require End Of Data", "group": "common", "label": "", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "configurationClass": "org.apache.camel.component.mllp.MllpConfiguration", "configurationField": "configuration", "description": "Enable\/Disable strict compliance to the MLLP standard. The MLLP standard specifies [START_OF [...] - "stringPayload": { "index": 5, "kind": "property", "displayName": "String Payload", "group": "common", "label": "", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "configurationClass": "org.apache.camel.component.mllp.MllpConfiguration", "configurationField": "configuration", "description": "Enable\/Disable converting the payload to a String. If enabled, HL7 Payloads received from external s [...] + "stringPayload": { "index": 5, "kind": "property", "displayName": "String Payload", "group": "common", "label": "", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "configurationClass": "org.apache.camel.component.mllp.MllpConfiguration", "configurationField": "configuration", "description": "Enable\/Disable converting the payload to a String. If enabled, HL7 Payloads received from external s [...] "validatePayload": { "index": 6, "kind": "property", "displayName": "Validate Payload", "group": "common", "label": "", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "configurationClass": "org.apache.camel.component.mllp.MllpConfiguration", "configurationField": "configuration", "description": "Enable\/Disable the validation of HL7 Payloads If enabled, HL7 Payloads received from external s [...] "acceptTimeout": { "index": 7, "kind": "property", "displayName": "Accept Timeout", "group": "consumer", "label": "advanced,consumer", "required": false, "type": "integer", "javaType": "int", "deprecated": false, "autowired": false, "secret": false, "defaultValue": 60000, "configurationClass": "org.apache.camel.component.mllp.MllpConfiguration", "configurationField": "configuration", "description": "Timeout (in milliseconds) while waiting for a TCP connection TCP Server Only" }, "backlog": { "index": 8, "kind": "property", "displayName": "Backlog", "group": "consumer", "label": "advanced,consumer", "required": false, "type": "integer", "javaType": "java.lang.Integer", "deprecated": false, "autowired": false, "secret": false, "defaultValue": 5, "configurationClass": "org.apache.camel.component.mllp.MllpConfiguration", "configurationField": "configuration", "description": "The maximum queue length for incoming connection indications (a request to connect) is s [...] @@ -88,17 +88,17 @@ "bodyTypes": { "consumerOut": { "carriers": [ "text" ], "javaType": "java.lang.String", "byOption": [ { "option": "stringPayload", "value": "false", "carriers": [ "bytes" ], "javaType": "byte[]" } ], "description": "The HL7 message without the MLLP envelope, as a String in the charset of MSH-18 or charsetName, or a byte array with stringPayload=false" }, "consumerReply": { "carriers": [ "any" ], "description": "The body is not sent back: the acknowledgement is the CamelMllpAcknowledgement or CamelMllpAcknowledgementString exchange property, or with autoAck an HL7 ACK (AA, or AE when the exchange failed)" }, - "producerIn": { "carriers": [ "text", "bytes", "stream", "file" ], "description": "The HL7 message as a String (in the charset of charsetName) or a byte array. Another body, such as a stream or a file, is converted to a byte array; no body, or a body that cannot be converted, fails" }, + "producerIn": { "carriers": [ "text", "bytes", "stream", "file" ], "description": "The HL7 message as a String (encoded in the charset of MSH-18, or charsetName) or a byte array. Another body, such as a stream or a file, is converted to a byte array; no body, or a body that cannot be converted, fails" }, "producerOut": { "carriers": [ "unchanged" ], "description": "The body is left as it was: with InOut the acknowledgement is in the CamelMllpAcknowledgement (byte array) and CamelMllpAcknowledgementString headers" } }, "properties": { "hostname": { "index": 0, "kind": "path", "displayName": "Hostname", "group": "common", "label": "", "required": true, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "description": "Hostname or IP for connection for the TCP connection. The default value is null, which means any local IP address" }, "port": { "index": 1, "kind": "path", "displayName": "Port", "group": "common", "label": "", "required": true, "type": "integer", "javaType": "int", "deprecated": false, "autowired": false, "secret": false, "description": "Port number for the TCP connection" }, "autoAck": { "index": 2, "kind": "parameter", "displayName": "Auto Ack", "group": "common", "label": "", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "configurationClass": "org.apache.camel.component.mllp.MllpConfiguration", "configurationField": "configuration", "description": "Enable\/Disable the automatic generation of a MLLP Acknowledgement MLLP Consumers only" }, - "charsetName": { "index": 3, "kind": "parameter", "displayName": "Charset Name", "group": "common", "label": "", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.mllp.MllpConfiguration", "configurationField": "configuration", "description": "Sets the default charset to use" }, + "charsetName": { "index": 3, "kind": "parameter", "displayName": "Charset Name", "group": "common", "label": "", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.mllp.MllpConfiguration", "configurationField": "configuration", "description": "The charset of an HL7 message that has no MSH-18. The consumer decodes, and the producer encodes, a message in the cha [...] "hl7Headers": { "index": 4, "kind": "parameter", "displayName": "Hl7 Headers", "group": "common", "label": "", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "configurationClass": "org.apache.camel.component.mllp.MllpConfiguration", "configurationField": "configuration", "description": "Enable\/Disable the automatic generation of message headers from the HL7 Message MLLP Consumers only" }, "requireEndOfData": { "index": 5, "kind": "parameter", "displayName": "Require End Of Data", "group": "common", "label": "", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "configurationClass": "org.apache.camel.component.mllp.MllpConfiguration", "configurationField": "configuration", "description": "Enable\/Disable strict compliance to the MLLP standard. The MLLP standard specifies [START_O [...] - "stringPayload": { "index": 6, "kind": "parameter", "displayName": "String Payload", "group": "common", "label": "", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "configurationClass": "org.apache.camel.component.mllp.MllpConfiguration", "configurationField": "configuration", "description": "Enable\/Disable converting the payload to a String. If enabled, HL7 Payloads received from external [...] + "stringPayload": { "index": 6, "kind": "parameter", "displayName": "String Payload", "group": "common", "label": "", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "configurationClass": "org.apache.camel.component.mllp.MllpConfiguration", "configurationField": "configuration", "description": "Enable\/Disable converting the payload to a String. If enabled, HL7 Payloads received from external [...] "validatePayload": { "index": 7, "kind": "parameter", "displayName": "Validate Payload", "group": "common", "label": "", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "configurationClass": "org.apache.camel.component.mllp.MllpConfiguration", "configurationField": "configuration", "description": "Enable\/Disable the validation of HL7 Payloads If enabled, HL7 Payloads received from external [...] "acceptTimeout": { "index": 8, "kind": "parameter", "displayName": "Accept Timeout", "group": "consumer", "label": "advanced,consumer", "required": false, "type": "integer", "javaType": "int", "deprecated": false, "autowired": false, "secret": false, "defaultValue": 60000, "configurationClass": "org.apache.camel.component.mllp.MllpConfiguration", "configurationField": "configuration", "description": "Timeout (in milliseconds) while waiting for a TCP connection TCP Server Only" }, "backlog": { "index": 9, "kind": "parameter", "displayName": "Backlog", "group": "consumer", "label": "advanced,consumer", "required": false, "type": "integer", "javaType": "java.lang.Integer", "deprecated": false, "autowired": false, "secret": false, "defaultValue": 5, "configurationClass": "org.apache.camel.component.mllp.MllpConfiguration", "configurationField": "configuration", "description": "The maximum queue length for incoming connection indications (a request to connect) is [...] diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/mllp-component.adoc b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/mllp-component.adoc index 02bcd48919e5..66a6cfefd8a8 100644 --- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/mllp-component.adoc +++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/mllp-component.adoc @@ -33,8 +33,8 @@ The component supports the following: The MLLP consumer gives the HL7 payload as a `String` by default (`stringPayload=true`), or as a `byte[]` with `stringPayload=false`. The MLLP producer sends a `String` (or another `CharSequence`) or a `byte[]` body, with text encoded in the -`charsetName` charset. Another body, such as an `InputStream` or a file, is converted to a `byte[]` with the -Camel type converter. +charset of MSH-18, or `charsetName` (see <<_character_set>>). Another body, such as an `InputStream` or a file, is +converted to a `byte[]` with the Camel type converter. A body that cannot be converted fails the exchange with a `MllpInvalidMessageException`. Maven users will need to add the following dependency to their pom.xml @@ -114,6 +114,22 @@ The state of the TCP Socket can be controlled by these properties on the Camel e |`CamelMllpResetConnectionAfterSend` | `Boolean` | If true, the Socket will be reset immediately after sending any data |==================================== +=== Character set + +The character set of an HL7 message is named in MSH-18 (for example `UNICODE UTF-8` or `8859/1`), and the +`charsetName` option is the character set of a message that has no MSH-18, or whose MSH-18 names a character set +that Java does not support. It is `ISO-8859-1` by default, from the `defaultCharset` option of the component. + +* The consumer decodes the message into a `String` body in the character set of MSH-18, else `charsetName`. +The `CamelMllpCharset` header holds the MSH-18 value of the message. +* The producer encodes a `String` body in the character set of MSH-18, else `charsetName`, so a message keeps +the character set it declares when a route relays it from an MLLP consumer to an MLLP producer. +A `byte[]` body is sent as it is. + +The `CamelCharsetName` header and exchange property do not choose the character set of the MLLP producer, as they may +come from another endpoint of the route. The MLLP consumer sets the `CamelCharsetName` exchange property only with +`stringPayload=false`, to the character set of the `byte[]` body, for converting it to text later in the route. + === Example The first route is an MLLP server that acknowledges each HL7 message automatically. The second route sends it an diff --git a/components/camel-mllp/src/generated/resources/META-INF/org/apache/camel/component/mllp/mllp.json b/components/camel-mllp/src/generated/resources/META-INF/org/apache/camel/component/mllp/mllp.json index a8f45c565bc1..769a0c17b881 100644 --- a/components/camel-mllp/src/generated/resources/META-INF/org/apache/camel/component/mllp/mllp.json +++ b/components/camel-mllp/src/generated/resources/META-INF/org/apache/camel/component/mllp/mllp.json @@ -25,11 +25,11 @@ }, "componentProperties": { "autoAck": { "index": 0, "kind": "property", "displayName": "Auto Ack", "group": "common", "label": "", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "configurationClass": "org.apache.camel.component.mllp.MllpConfiguration", "configurationField": "configuration", "description": "Enable\/Disable the automatic generation of a MLLP Acknowledgement MLLP Consumers only" }, - "charsetName": { "index": 1, "kind": "property", "displayName": "Charset Name", "group": "common", "label": "", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.mllp.MllpConfiguration", "configurationField": "configuration", "description": "Sets the default charset to use" }, + "charsetName": { "index": 1, "kind": "property", "displayName": "Charset Name", "group": "common", "label": "", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.mllp.MllpConfiguration", "configurationField": "configuration", "description": "The charset of an HL7 message that has no MSH-18. The consumer decodes, and the producer encodes, a message in the char [...] "configuration": { "index": 2, "kind": "property", "displayName": "Configuration", "group": "common", "label": "", "required": false, "type": "object", "javaType": "org.apache.camel.component.mllp.MllpConfiguration", "deprecated": false, "autowired": false, "secret": false, "description": "Sets the default configuration to use when creating MLLP endpoints." }, "hl7Headers": { "index": 3, "kind": "property", "displayName": "Hl7 Headers", "group": "common", "label": "", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "configurationClass": "org.apache.camel.component.mllp.MllpConfiguration", "configurationField": "configuration", "description": "Enable\/Disable the automatic generation of message headers from the HL7 Message MLLP Consumers only" }, "requireEndOfData": { "index": 4, "kind": "property", "displayName": "Require End Of Data", "group": "common", "label": "", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "configurationClass": "org.apache.camel.component.mllp.MllpConfiguration", "configurationField": "configuration", "description": "Enable\/Disable strict compliance to the MLLP standard. The MLLP standard specifies [START_OF [...] - "stringPayload": { "index": 5, "kind": "property", "displayName": "String Payload", "group": "common", "label": "", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "configurationClass": "org.apache.camel.component.mllp.MllpConfiguration", "configurationField": "configuration", "description": "Enable\/Disable converting the payload to a String. If enabled, HL7 Payloads received from external s [...] + "stringPayload": { "index": 5, "kind": "property", "displayName": "String Payload", "group": "common", "label": "", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "configurationClass": "org.apache.camel.component.mllp.MllpConfiguration", "configurationField": "configuration", "description": "Enable\/Disable converting the payload to a String. If enabled, HL7 Payloads received from external s [...] "validatePayload": { "index": 6, "kind": "property", "displayName": "Validate Payload", "group": "common", "label": "", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "configurationClass": "org.apache.camel.component.mllp.MllpConfiguration", "configurationField": "configuration", "description": "Enable\/Disable the validation of HL7 Payloads If enabled, HL7 Payloads received from external s [...] "acceptTimeout": { "index": 7, "kind": "property", "displayName": "Accept Timeout", "group": "consumer", "label": "advanced,consumer", "required": false, "type": "integer", "javaType": "int", "deprecated": false, "autowired": false, "secret": false, "defaultValue": 60000, "configurationClass": "org.apache.camel.component.mllp.MllpConfiguration", "configurationField": "configuration", "description": "Timeout (in milliseconds) while waiting for a TCP connection TCP Server Only" }, "backlog": { "index": 8, "kind": "property", "displayName": "Backlog", "group": "consumer", "label": "advanced,consumer", "required": false, "type": "integer", "javaType": "java.lang.Integer", "deprecated": false, "autowired": false, "secret": false, "defaultValue": 5, "configurationClass": "org.apache.camel.component.mllp.MllpConfiguration", "configurationField": "configuration", "description": "The maximum queue length for incoming connection indications (a request to connect) is s [...] @@ -88,17 +88,17 @@ "bodyTypes": { "consumerOut": { "carriers": [ "text" ], "javaType": "java.lang.String", "byOption": [ { "option": "stringPayload", "value": "false", "carriers": [ "bytes" ], "javaType": "byte[]" } ], "description": "The HL7 message without the MLLP envelope, as a String in the charset of MSH-18 or charsetName, or a byte array with stringPayload=false" }, "consumerReply": { "carriers": [ "any" ], "description": "The body is not sent back: the acknowledgement is the CamelMllpAcknowledgement or CamelMllpAcknowledgementString exchange property, or with autoAck an HL7 ACK (AA, or AE when the exchange failed)" }, - "producerIn": { "carriers": [ "text", "bytes", "stream", "file" ], "description": "The HL7 message as a String (in the charset of charsetName) or a byte array. Another body, such as a stream or a file, is converted to a byte array; no body, or a body that cannot be converted, fails" }, + "producerIn": { "carriers": [ "text", "bytes", "stream", "file" ], "description": "The HL7 message as a String (encoded in the charset of MSH-18, or charsetName) or a byte array. Another body, such as a stream or a file, is converted to a byte array; no body, or a body that cannot be converted, fails" }, "producerOut": { "carriers": [ "unchanged" ], "description": "The body is left as it was: with InOut the acknowledgement is in the CamelMllpAcknowledgement (byte array) and CamelMllpAcknowledgementString headers" } }, "properties": { "hostname": { "index": 0, "kind": "path", "displayName": "Hostname", "group": "common", "label": "", "required": true, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "description": "Hostname or IP for connection for the TCP connection. The default value is null, which means any local IP address" }, "port": { "index": 1, "kind": "path", "displayName": "Port", "group": "common", "label": "", "required": true, "type": "integer", "javaType": "int", "deprecated": false, "autowired": false, "secret": false, "description": "Port number for the TCP connection" }, "autoAck": { "index": 2, "kind": "parameter", "displayName": "Auto Ack", "group": "common", "label": "", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "configurationClass": "org.apache.camel.component.mllp.MllpConfiguration", "configurationField": "configuration", "description": "Enable\/Disable the automatic generation of a MLLP Acknowledgement MLLP Consumers only" }, - "charsetName": { "index": 3, "kind": "parameter", "displayName": "Charset Name", "group": "common", "label": "", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.mllp.MllpConfiguration", "configurationField": "configuration", "description": "Sets the default charset to use" }, + "charsetName": { "index": 3, "kind": "parameter", "displayName": "Charset Name", "group": "common", "label": "", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.mllp.MllpConfiguration", "configurationField": "configuration", "description": "The charset of an HL7 message that has no MSH-18. The consumer decodes, and the producer encodes, a message in the cha [...] "hl7Headers": { "index": 4, "kind": "parameter", "displayName": "Hl7 Headers", "group": "common", "label": "", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "configurationClass": "org.apache.camel.component.mllp.MllpConfiguration", "configurationField": "configuration", "description": "Enable\/Disable the automatic generation of message headers from the HL7 Message MLLP Consumers only" }, "requireEndOfData": { "index": 5, "kind": "parameter", "displayName": "Require End Of Data", "group": "common", "label": "", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "configurationClass": "org.apache.camel.component.mllp.MllpConfiguration", "configurationField": "configuration", "description": "Enable\/Disable strict compliance to the MLLP standard. The MLLP standard specifies [START_O [...] - "stringPayload": { "index": 6, "kind": "parameter", "displayName": "String Payload", "group": "common", "label": "", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "configurationClass": "org.apache.camel.component.mllp.MllpConfiguration", "configurationField": "configuration", "description": "Enable\/Disable converting the payload to a String. If enabled, HL7 Payloads received from external [...] + "stringPayload": { "index": 6, "kind": "parameter", "displayName": "String Payload", "group": "common", "label": "", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "configurationClass": "org.apache.camel.component.mllp.MllpConfiguration", "configurationField": "configuration", "description": "Enable\/Disable converting the payload to a String. If enabled, HL7 Payloads received from external [...] "validatePayload": { "index": 7, "kind": "parameter", "displayName": "Validate Payload", "group": "common", "label": "", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "configurationClass": "org.apache.camel.component.mllp.MllpConfiguration", "configurationField": "configuration", "description": "Enable\/Disable the validation of HL7 Payloads If enabled, HL7 Payloads received from external [...] "acceptTimeout": { "index": 8, "kind": "parameter", "displayName": "Accept Timeout", "group": "consumer", "label": "advanced,consumer", "required": false, "type": "integer", "javaType": "int", "deprecated": false, "autowired": false, "secret": false, "defaultValue": 60000, "configurationClass": "org.apache.camel.component.mllp.MllpConfiguration", "configurationField": "configuration", "description": "Timeout (in milliseconds) while waiting for a TCP connection TCP Server Only" }, "backlog": { "index": 9, "kind": "parameter", "displayName": "Backlog", "group": "consumer", "label": "advanced,consumer", "required": false, "type": "integer", "javaType": "java.lang.Integer", "deprecated": false, "autowired": false, "secret": false, "defaultValue": 5, "configurationClass": "org.apache.camel.component.mllp.MllpConfiguration", "configurationField": "configuration", "description": "The maximum queue length for incoming connection indications (a request to connect) is [...] diff --git a/components/camel-mllp/src/main/docs/mllp-component.adoc b/components/camel-mllp/src/main/docs/mllp-component.adoc index 02bcd48919e5..66a6cfefd8a8 100644 --- a/components/camel-mllp/src/main/docs/mllp-component.adoc +++ b/components/camel-mllp/src/main/docs/mllp-component.adoc @@ -33,8 +33,8 @@ The component supports the following: The MLLP consumer gives the HL7 payload as a `String` by default (`stringPayload=true`), or as a `byte[]` with `stringPayload=false`. The MLLP producer sends a `String` (or another `CharSequence`) or a `byte[]` body, with text encoded in the -`charsetName` charset. Another body, such as an `InputStream` or a file, is converted to a `byte[]` with the -Camel type converter. +charset of MSH-18, or `charsetName` (see <<_character_set>>). Another body, such as an `InputStream` or a file, is +converted to a `byte[]` with the Camel type converter. A body that cannot be converted fails the exchange with a `MllpInvalidMessageException`. Maven users will need to add the following dependency to their pom.xml @@ -114,6 +114,22 @@ The state of the TCP Socket can be controlled by these properties on the Camel e |`CamelMllpResetConnectionAfterSend` | `Boolean` | If true, the Socket will be reset immediately after sending any data |==================================== +=== Character set + +The character set of an HL7 message is named in MSH-18 (for example `UNICODE UTF-8` or `8859/1`), and the +`charsetName` option is the character set of a message that has no MSH-18, or whose MSH-18 names a character set +that Java does not support. It is `ISO-8859-1` by default, from the `defaultCharset` option of the component. + +* The consumer decodes the message into a `String` body in the character set of MSH-18, else `charsetName`. +The `CamelMllpCharset` header holds the MSH-18 value of the message. +* The producer encodes a `String` body in the character set of MSH-18, else `charsetName`, so a message keeps +the character set it declares when a route relays it from an MLLP consumer to an MLLP producer. +A `byte[]` body is sent as it is. + +The `CamelCharsetName` header and exchange property do not choose the character set of the MLLP producer, as they may +come from another endpoint of the route. The MLLP consumer sets the `CamelCharsetName` exchange property only with +`stringPayload=false`, to the character set of the `byte[]` body, for converting it to text later in the route. + === Example The first route is an MLLP server that acknowledges each HL7 message automatically. The second route sends it an diff --git a/components/camel-mllp/src/main/java/org/apache/camel/component/mllp/MllpCharsetHelper.java b/components/camel-mllp/src/main/java/org/apache/camel/component/mllp/MllpCharsetHelper.java index b5b4faca4265..afe564141937 100644 --- a/components/camel-mllp/src/main/java/org/apache/camel/component/mllp/MllpCharsetHelper.java +++ b/components/camel-mllp/src/main/java/org/apache/camel/component/mllp/MllpCharsetHelper.java @@ -43,7 +43,48 @@ public final class MllpCharsetHelper { } public static Charset getCharset(Exchange exchange, byte[] hl7Bytes, Hl7Util hl7Util, Charset defaultCharset) { + Charset msh18Charset = findMsh18Charset(hl7Bytes, hl7Util, defaultCharset); + if (msh18Charset != null) { + return msh18Charset; + } + + return getCharset(exchange, defaultCharset); + } + + /** + * The charset of an HL7 message: the charset named in MSH-18, or the given charset when the message has no MSH-18 + * or MSH-18 names a charset that is not supported. The CamelCharsetName header and exchange property are not used, + * as they may come from another endpoint. + * + * @param hl7Bytes the HL7 message + * @param hl7Util the HL7 utility to find MSH-18 with + * @param defaultCharset the charset to use when the message has no (supported) MSH-18 + * @return the charset of the message + */ + public static Charset getMsh18Charset(byte[] hl7Bytes, Hl7Util hl7Util, Charset defaultCharset) { + Charset msh18Charset = findMsh18Charset(hl7Bytes, hl7Util, defaultCharset); + return msh18Charset != null ? msh18Charset : defaultCharset; + } + /** + * Encodes an HL7 message in its charset: the charset named in MSH-18, or the given charset when the message has no + * MSH-18 or MSH-18 names a charset that is not supported. + * + * @param hl7Message the HL7 message + * @param hl7Util the HL7 utility to find MSH-18 with + * @param defaultCharset the charset to use when the message has no (supported) MSH-18 + * @return the encoded message + */ + public static byte[] encode(String hl7Message, Hl7Util hl7Util, Charset defaultCharset) { + byte[] hl7Bytes = hl7Message.getBytes(defaultCharset); + Charset msh18Charset = findMsh18Charset(hl7Bytes, hl7Util, defaultCharset); + if (msh18Charset != null && !msh18Charset.equals(defaultCharset)) { + hl7Bytes = hl7Message.getBytes(msh18Charset); + } + return hl7Bytes; + } + + private static Charset findMsh18Charset(byte[] hl7Bytes, Hl7Util hl7Util, Charset defaultCharset) { String msh18 = hl7Util.findMsh18(hl7Bytes, defaultCharset); if (msh18 != null && !msh18.isEmpty()) { if (MllpProtocolConstants.MSH18_VALUES.containsKey(msh18)) { @@ -57,18 +98,7 @@ public final class MllpCharsetHelper { // ignore } } - - String exchangeCharsetName = ExchangeHelper.getCharsetName(exchange, false); - if (exchangeCharsetName != null && !exchangeCharsetName.isEmpty()) { - try { - if (Charset.isSupported(exchangeCharsetName)) { - return Charset.forName(exchangeCharsetName); - } - } catch (Exception charsetEx) { - // ignore - } - } - return defaultCharset; + return null; } } diff --git a/components/camel-mllp/src/main/java/org/apache/camel/component/mllp/MllpConfiguration.java b/components/camel-mllp/src/main/java/org/apache/camel/component/mllp/MllpConfiguration.java index 172116b68eba..e60f852b0462 100644 --- a/components/camel-mllp/src/main/java/org/apache/camel/component/mllp/MllpConfiguration.java +++ b/components/camel-mllp/src/main/java/org/apache/camel/component/mllp/MllpConfiguration.java @@ -205,7 +205,10 @@ public class MllpConfiguration implements Cloneable { } /** - * Sets the default charset to use + * The charset of an HL7 message that has no MSH-18. The consumer decodes, and the producer encodes, a message in + * the charset named in MSH-18 of the message, and in this charset when the message has no MSH-18, or MSH-18 names a + * charset that is not supported. The CamelCharsetName header and exchange property are not used. The default is the + * defaultCharset of the component (ISO-8859-1). */ public void setCharsetName(String charsetName) { this.charsetName = charsetName; @@ -499,9 +502,8 @@ public class MllpConfiguration implements Cloneable { * * If enabled, HL7 Payloads received from external systems will be validated converted to a String. * - * If the charsetName property is set, that character set will be used for the conversion. If the charsetName - * property is not set, the value of MSH-18 will be used to determine th appropriate character set. If MSH-18 is not - * set, then the default ISO-8859-1 character set will be use. + * The value of MSH-18 is used to determine the character set for the conversion. If MSH-18 is not set, or names a + * character set that is not supported, the charsetName character set is used (ISO-8859-1 by default). * * @param stringPayload enabled if true, otherwise disabled */ diff --git a/components/camel-mllp/src/main/java/org/apache/camel/component/mllp/MllpEndpoint.java b/components/camel-mllp/src/main/java/org/apache/camel/component/mllp/MllpEndpoint.java index 958c4a182522..5f67ebe32933 100644 --- a/components/camel-mllp/src/main/java/org/apache/camel/component/mllp/MllpEndpoint.java +++ b/components/camel-mllp/src/main/java/org/apache/camel/component/mllp/MllpEndpoint.java @@ -25,7 +25,6 @@ import org.apache.camel.Category; import org.apache.camel.Consumer; import org.apache.camel.Exchange; import org.apache.camel.ExchangePattern; -import org.apache.camel.ExchangePropertyKey; import org.apache.camel.Processor; import org.apache.camel.Producer; import org.apache.camel.api.management.ManagedAttribute; @@ -66,8 +65,8 @@ import org.slf4j.Logger; + " property, or with autoAck an HL7 ACK (AA, or AE when the exchange" + " failed)"), producerIn = @BodyType(value = { Carrier.TEXT, Carrier.BYTES, Carrier.STREAM, Carrier.FILE }, - description = "The HL7 message as a String (in the charset of charsetName) or a byte" - + " array. Another body, such as a stream or a file, is converted to a byte" + description = "The HL7 message as a String (encoded in the charset of MSH-18, or" + + " charsetName) or a byte array. Another body, such as a stream or a file, is converted to a byte" + " array; no body, or a body that cannot be converted, fails"), producerOut = @BodyType(value = Carrier.UNCHANGED, description = "The body is left as it was: with InOut the acknowledgement is in the" @@ -113,13 +112,6 @@ public class MllpEndpoint extends DefaultEndpoint implements EndpointServiceLoca return "mllp"; } - @Override - public Exchange createExchange(ExchangePattern exchangePattern) { - Exchange mllpExchange = super.createExchange(exchangePattern); - setExchangeProperties(mllpExchange); - return mllpExchange; - } - @Override public void setExchangePattern(ExchangePattern exchangePattern) { configuration.setExchangePattern(exchangePattern); @@ -132,12 +124,6 @@ public class MllpEndpoint extends DefaultEndpoint implements EndpointServiceLoca super.setBridgeErrorHandler(configuration.isBridgeErrorHandler()); } - void setExchangeProperties(Exchange mllpExchange) { - if (configuration.hasCharsetName()) { - mllpExchange.setProperty(ExchangePropertyKey.CHARSET_NAME, configuration.getCharsetName()); - } - } - @Override public Producer createProducer() throws Exception { diff --git a/components/camel-mllp/src/main/java/org/apache/camel/component/mllp/MllpTcpClientProducer.java b/components/camel-mllp/src/main/java/org/apache/camel/component/mllp/MllpTcpClientProducer.java index 76c1ee305b2e..30acd1a86497 100644 --- a/components/camel-mllp/src/main/java/org/apache/camel/component/mllp/MllpTcpClientProducer.java +++ b/components/camel-mllp/src/main/java/org/apache/camel/component/mllp/MllpTcpClientProducer.java @@ -43,7 +43,6 @@ import org.apache.camel.api.management.ManagedResource; import org.apache.camel.component.mllp.internal.Hl7Util; import org.apache.camel.component.mllp.internal.MllpSocketBuffer; import org.apache.camel.support.DefaultProducer; -import org.apache.camel.support.ExchangeHelper; import org.apache.camel.support.jsse.SSLContextParameters; import org.apache.camel.util.StringHelper; import org.slf4j.Logger; @@ -169,19 +168,16 @@ public class MllpTcpClientProducer extends DefaultProducer implements Runnable { } else if (messageBody instanceof byte[]) { hl7MessageBytes = (byte[]) messageBody; } else if (messageBody instanceof CharSequence) { - String stringBody = messageBody.toString(); - Charset usedCharset = MllpCharsetHelper.getCharset(exchange, charset); - hl7MessageBytes = stringBody.getBytes(usedCharset); - if (getConfiguration().hasCharsetName()) { - // the charset the message was sent in - exchange.setProperty(ExchangePropertyKey.CHARSET_NAME, usedCharset.name()); - } + // the text is encoded in the charset of MSH-18, or charsetName when the message has no MSH-18; + // not in the charset of the exchange, which may come from another endpoint, such as an mllp consumer + hl7MessageBytes = MllpCharsetHelper.encode(messageBody.toString(), hl7Util, charset); } else { - // another body, such as a stream: convert it, with text in the same charset as a String body, - // and only for the conversion when the exchange has no charset of its own - boolean tempCharset = ExchangeHelper.getCharsetName(exchange, false) == null; - if (tempCharset) { - exchange.setProperty(ExchangePropertyKey.CHARSET_NAME, charset.name()); + // another body, such as a stream: convert it, with text in charsetName, for the conversion only + String originalCharsetProperty = exchange.getProperty(ExchangePropertyKey.CHARSET_NAME, String.class); + Object originalCharsetHeader = message.getHeader(Exchange.CHARSET_NAME); + exchange.setProperty(ExchangePropertyKey.CHARSET_NAME, charset.name()); + if (originalCharsetHeader != null) { + message.setHeader(Exchange.CHARSET_NAME, charset.name()); } try { hl7MessageBytes = exchange.getContext().getTypeConverter().mandatoryConvertTo(byte[].class, exchange, @@ -194,9 +190,14 @@ public class MllpTcpClientProducer extends DefaultProducer implements Runnable { new MllpInvalidMessageException(exceptionMessage, null, conversionEx, logPhi)); return; } finally { - if (tempCharset) { + if (originalCharsetProperty != null) { + exchange.setProperty(ExchangePropertyKey.CHARSET_NAME, originalCharsetProperty); + } else { exchange.removeProperty(ExchangePropertyKey.CHARSET_NAME); } + if (originalCharsetHeader != null) { + message.setHeader(Exchange.CHARSET_NAME, originalCharsetHeader); + } } } @@ -332,7 +333,7 @@ public class MllpTcpClientProducer extends DefaultProducer implements Runnable { if (acknowledgementBytes != null && acknowledgementBytes.length > 0) { message.setHeader(MllpConstants.MLLP_ACKNOWLEDGEMENT_STRING, new String( acknowledgementBytes, - MllpCharsetHelper.getCharset(exchange, acknowledgementBytes, hl7Util, charset))); + MllpCharsetHelper.getMsh18Charset(acknowledgementBytes, hl7Util, charset))); } else { message.setHeader(MllpConstants.MLLP_ACKNOWLEDGEMENT_STRING, ""); } diff --git a/components/camel-mllp/src/main/java/org/apache/camel/component/mllp/MllpTcpServerConsumer.java b/components/camel-mllp/src/main/java/org/apache/camel/component/mllp/MllpTcpServerConsumer.java index bc3f0afc44b4..15a602e8b9f6 100644 --- a/components/camel-mllp/src/main/java/org/apache/camel/component/mllp/MllpTcpServerConsumer.java +++ b/components/camel-mllp/src/main/java/org/apache/camel/component/mllp/MllpTcpServerConsumer.java @@ -270,9 +270,6 @@ public class MllpTcpServerConsumer extends DefaultConsumer { hl7MessageBytes == null ? -1 : hl7MessageBytes.length, consumerRunnable.getSocket()); Exchange exchange = createExchange(false); exchange.setPattern(ExchangePattern.InOut); - if (getConfiguration().hasCharsetName()) { - exchange.setProperty(ExchangePropertyKey.CHARSET_NAME, getConfiguration().getCharsetName()); - } try { createUoW(exchange); @@ -303,15 +300,20 @@ public class MllpTcpServerConsumer extends DefaultConsumer { populateHl7DataHeaders(exchange, message, hl7MessageBytes); if (getConfiguration().isStringPayload()) { + // the String body is not tied to a charset: the CamelCharsetName exchange property is not set, so the + // charset of this endpoint does not leak to other endpoints of the route, such as an mllp producer if (hl7MessageBytes != null && hl7MessageBytes.length > 0) { message.setBody( new String( hl7MessageBytes, - MllpCharsetHelper.getCharset(exchange, hl7MessageBytes, hl7Util, charset))); + MllpCharsetHelper.getMsh18Charset(hl7MessageBytes, hl7Util, charset))); } else { message.setBody("", String.class); } } else { + // the charset of the byte array body, for converting it to text later in the route + exchange.setProperty(ExchangePropertyKey.CHARSET_NAME, + MllpCharsetHelper.getMsh18Charset(hl7MessageBytes, hl7Util, charset).name()); message.setBody(hl7MessageBytes, byte[].class); } @@ -405,7 +407,7 @@ public class MllpTcpServerConsumer extends DefaultConsumer { log.warn("Population of message headers failed - unable to find the end of the MSH segment"); } else { log.debug("Populating the HL7 message headers"); - Charset charset = MllpCharsetHelper.getCharset(exchange, this.charset); + Charset charset = MllpCharsetHelper.getMsh18Charset(hl7MessageBytes, hl7Util, this.charset); for (int i = 2; i < fieldSeparatorIndexes.size(); ++i) { int startingFieldSeparatorIndex = fieldSeparatorIndexes.get(i - 1); @@ -453,11 +455,9 @@ public class MllpTcpServerConsumer extends DefaultConsumer { continue; } - String headerValue = (i == 17 && getConfiguration().hasCharsetName()) - ? getConfiguration().getCharsetName() - : new String( - hl7MessageBytes, startingFieldSeparatorIndex + 1, - endingFieldSeparatorIndex - startingFieldSeparatorIndex - 1, charset); + String headerValue = new String( + hl7MessageBytes, startingFieldSeparatorIndex + 1, + endingFieldSeparatorIndex - startingFieldSeparatorIndex - 1, charset); message.setHeader(headerName, headerValue); @@ -480,6 +480,18 @@ public class MllpTcpServerConsumer extends DefaultConsumer { } + /** + * The acknowledgement in an exchange property as a byte array, with text encoded in the charset of MSH-18 of the + * acknowledgement, or the charset of the exchange, or charsetName. + */ + private byte[] getAcknowledgementBytes(Exchange exchange, String propertyName) { + Object acknowledgement = exchange.getProperty(propertyName); + if (acknowledgement instanceof CharSequence text) { + return MllpCharsetHelper.encode(text.toString(), hl7Util, MllpCharsetHelper.getCharset(exchange, charset)); + } + return exchange.getProperty(propertyName, byte[].class); + } + void sendAcknowledgement(byte[] originalHl7MessageBytes, Exchange exchange, TcpSocketConsumerRunnable consumerRunnable) { log.trace("sendAcknowledgement(originalHl7MessageBytes[{}], Exchange[{}], {}) - entering", originalHl7MessageBytes == null ? -1 : originalHl7MessageBytes.length, exchange.getExchangeId(), @@ -488,9 +500,9 @@ public class MllpTcpServerConsumer extends DefaultConsumer { getEndpoint().checkBeforeSendProperties(exchange, consumerRunnable.getSocket(), log); // Find the acknowledgement body - byte[] acknowledgementMessageBytes = exchange.getProperty(MllpConstants.MLLP_ACKNOWLEDGEMENT, byte[].class); + byte[] acknowledgementMessageBytes = getAcknowledgementBytes(exchange, MllpConstants.MLLP_ACKNOWLEDGEMENT); if (acknowledgementMessageBytes == null) { - acknowledgementMessageBytes = exchange.getProperty(MllpConstants.MLLP_ACKNOWLEDGEMENT_STRING, byte[].class); + acknowledgementMessageBytes = getAcknowledgementBytes(exchange, MllpConstants.MLLP_ACKNOWLEDGEMENT_STRING); } String acknowledgementMessageType = null; diff --git a/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpCharsetTest.java b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpCharsetTest.java new file mode 100644 index 000000000000..edc6dc200d5e --- /dev/null +++ b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpCharsetTest.java @@ -0,0 +1,206 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.mllp; + +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.concurrent.TimeUnit; + +import org.apache.camel.EndpointInject; +import org.apache.camel.Exchange; +import org.apache.camel.builder.ExchangeBuilder; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.AvailablePortFinder; +import org.apache.camel.test.junit6.CamelTestSupport; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +/** + * The charset of a message: MSH-18 first, then charsetName of the endpoint, and not the CamelCharsetName exchange + * property, which may come from another endpoint. + */ +public class MllpCharsetTest extends CamelTestSupport { + + static final String PATIENT = "Sørensen^Łucja"; + static final String ACKNOWLEDGEMENT + = "MSH|^~\\&|RAPP|RFAC|APP|FAC|20261122120000||ACK^A04|00002|D|2.6||||||UNICODE UTF-8" + '\r' + + "MSA|AA|00001|Received for Łucja Sørensen"; + + @RegisterExtension + AvailablePortFinder.Port relayPort = AvailablePortFinder.find(); + + @RegisterExtension + AvailablePortFinder.Port sinkPort = AvailablePortFinder.find(); + + @RegisterExtension + AvailablePortFinder.Port textPort = AvailablePortFinder.find(); + + @RegisterExtension + AvailablePortFinder.Port acknowledgementPort = AvailablePortFinder.find(); + + @EndpointInject("mock://relayed") + MockEndpoint relayed; + + @EndpointInject("mock://sink") + MockEndpoint sink; + + @EndpointInject("mock://text") + MockEndpoint text; + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + // relays from an mllp consumer with the default charset to an mllp producer with charsetName=UTF-8 + fromF("mllp://localhost:%d?autoAck=true", relayPort.getPort()) + .toF("mllp://localhost:%d?receiveTimeout=5000&readTimeout=5000&charsetName=UTF-8", + sinkPort.getPort()) + .to(relayed); + + fromF("mllp://localhost:%d?autoAck=true&stringPayload=false", sinkPort.getPort()) + .to(sink); + + fromF("mllp://localhost:%d?autoAck=true", textPort.getPort()) + .to(text); + + fromF("mllp://localhost:%d?autoAck=false", acknowledgementPort.getPort()) + .setProperty(MllpConstants.MLLP_ACKNOWLEDGEMENT_STRING, constant(ACKNOWLEDGEMENT)); + } + }; + } + + private static String message(String msh18, String patientName) { + return "MSH|^~\\&|APP|FAC|RAPP|RFAC|20261122120000||ADT^A04|00001|D|2.6||||||" + msh18 + '\r' + + "PID|1||1234||" + patientName + '\r'; + } + + private String producerUri(int port, String charsetName) { + String uri = String.format("mllp://localhost:%d?receiveTimeout=5000&readTimeout=5000", port); + return charsetName != null ? uri + "&charsetName=" + charsetName : uri; + } + + private byte[] sendToSink(String producerCharsetName, Object body, String exchangeCharsetName) throws Exception { + sink.expectedMessageCount(1); + + ExchangeBuilder builder = ExchangeBuilder.anExchange(context).withBody(body); + if (exchangeCharsetName != null) { + builder.withProperty(Exchange.CHARSET_NAME, exchangeCharsetName); + } + Exchange exchange = template.send(producerUri(sinkPort.getPort(), producerCharsetName), builder.build()); + + MockEndpoint.assertIsSatisfied(context, 10, TimeUnit.SECONDS); + assertNull(exchange.getException()); + assertEquals(exchangeCharsetName, exchange.getProperty(Exchange.CHARSET_NAME), + "the producer leaves the charset of the exchange as it was"); + return sink.getExchanges().get(0).getMessage().getBody(byte[].class); + } + + @Test + public void testProducerEncodesInCharsetOfMsh18() throws Exception { + String testMessage = message("UNICODE UTF-8", PATIENT); + + // the producer has the default charset (ISO-8859-1), which has no Ł + assertArrayEquals(testMessage.getBytes(StandardCharsets.UTF_8), sendToSink(null, testMessage, null)); + assertEquals("UTF-8", sink.getExchanges().get(0).getProperty(Exchange.CHARSET_NAME), + "the consumer gives the charset of a byte array body"); + } + + @Test + public void testProducerEncodesInCharsetOfMsh18OverCharsetName() throws Exception { + String testMessage = message("8859/2", PATIENT); + + assertArrayEquals(testMessage.getBytes(Charset.forName("ISO-8859-2")), sendToSink("UTF-8", testMessage, null)); + } + + @Test + public void testProducerEncodesInCharsetNameWithoutMsh18() throws Exception { + String testMessage = message("", PATIENT); + + // the charset of the exchange, such as one set by another endpoint, does not win over charsetName + assertArrayEquals(testMessage.getBytes(StandardCharsets.UTF_8), sendToSink("UTF-8", testMessage, "ISO-8859-1")); + } + + @Test + public void testProducerEncodesTextBodyInCharsetName() throws Exception { + String testMessage = message("", PATIENT); + + assertArrayEquals(testMessage.getBytes(StandardCharsets.UTF_8), + sendToSink("UTF-8", new StringBuilder(testMessage), "ISO-8859-1")); + } + + @Test + public void testConsumerDecodesInCharsetOfMsh18() throws Exception { + String testMessage = message("UNICODE UTF-8", PATIENT); + text.expectedMessageCount(1); + + // a byte array is sent as it is + template.sendBody(producerUri(textPort.getPort(), null), testMessage.getBytes(StandardCharsets.UTF_8)); + + MockEndpoint.assertIsSatisfied(context, 10, TimeUnit.SECONDS); + Exchange received = text.getExchanges().get(0); + assertEquals(testMessage, received.getMessage().getBody(String.class)); + assertEquals("UNICODE UTF-8", received.getMessage().getHeader(MllpConstants.MLLP_CHARSET)); + assertNull(received.getProperty(Exchange.CHARSET_NAME), + "the consumer does not give the charset of a String body to the rest of the route"); + } + + @Test + public void testRelayKeepsCharsetOfMsh18() throws Exception { + String testMessage = message("UNICODE UTF-8", PATIENT); + relayed.expectedMessageCount(1); + sink.expectedMessageCount(1); + + template.sendBody(producerUri(relayPort.getPort(), null), testMessage.getBytes(StandardCharsets.UTF_8)); + + MockEndpoint.assertIsSatisfied(context, 10, TimeUnit.SECONDS); + assertArrayEquals(testMessage.getBytes(StandardCharsets.UTF_8), + sink.getExchanges().get(0).getMessage().getBody(byte[].class)); + assertNull(relayed.getExchanges().get(0).getProperty(Exchange.CHARSET_NAME)); + } + + @Test + public void testRelayUsesCharsetNameOfProducer() throws Exception { + String testMessage = message("", "Sørensen^Anne"); + relayed.expectedMessageCount(1); + sink.expectedMessageCount(1); + + // the relay consumer decodes in its default charset ISO-8859-1, the producer encodes in its charsetName UTF-8 + template.sendBody(producerUri(relayPort.getPort(), null), testMessage.getBytes(StandardCharsets.ISO_8859_1)); + + MockEndpoint.assertIsSatisfied(context, 10, TimeUnit.SECONDS); + assertArrayEquals(testMessage.getBytes(StandardCharsets.UTF_8), + sink.getExchanges().get(0).getMessage().getBody(byte[].class)); + assertNull(relayed.getExchanges().get(0).getProperty(Exchange.CHARSET_NAME)); + } + + @Test + public void testConsumerEncodesAcknowledgementInCharsetOfMsh18() throws Exception { + Exchange exchange = template.send(producerUri(acknowledgementPort.getPort(), null), + e -> e.getMessage().setBody(message("", "Sørensen^Anne"))); + + assertNull(exchange.getException()); + assertArrayEquals(ACKNOWLEDGEMENT.getBytes(StandardCharsets.UTF_8), + exchange.getMessage().getHeader(MllpConstants.MLLP_ACKNOWLEDGEMENT, byte[].class)); + assertEquals(ACKNOWLEDGEMENT, exchange.getMessage().getHeader(MllpConstants.MLLP_ACKNOWLEDGEMENT_STRING)); + } +} diff --git a/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpClientProducerBodyConversionTest.java b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpClientProducerBodyConversionTest.java index 9274e3c8d730..13cfcf5ca74e 100644 --- a/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpClientProducerBodyConversionTest.java +++ b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpClientProducerBodyConversionTest.java @@ -98,22 +98,40 @@ public class MllpTcpClientProducerBodyConversionTest extends CamelTestSupport { } @Test - public void testStringBodyCharsetPropertyIsTheCharsetUsed() throws Exception { + public void testStringBodyIgnoresCharsetProperty() throws Exception { String testMessage = Hl7TestMessageGenerator.generateMessage().replace("PID|", "PID|Ærø|"); received.expectedMessageCount(1); - // an exchange from another endpoint: its charset wins over charsetName=ISO-8859-1 of the endpoint + // an exchange from another endpoint: charsetName=ISO-8859-1 of the endpoint wins over its charset Exchange exchange = template.send(producerUri().replace("UTF-8", "ISO-8859-1"), ExchangeBuilder.anExchange(context).withProperty(Exchange.CHARSET_NAME, "UTF-8").withBody(testMessage) .build()); MockEndpoint.assertIsSatisfied(context, 10, TimeUnit.SECONDS); assertNull(exchange.getException()); - assertArrayEquals(testMessage.getBytes(StandardCharsets.UTF_8), + assertArrayEquals(testMessage.getBytes(StandardCharsets.ISO_8859_1), received.getExchanges().get(0).getMessage().getBody(byte[].class)); assertEquals("UTF-8", exchange.getProperty(Exchange.CHARSET_NAME)); } + @Test + public void testConvertedBodyIgnoresCharsetProperty() throws Exception { + String testMessage = Hl7TestMessageGenerator.generateMessage().replace("PID|", "PID|Ærø|"); + received.expectedMessageCount(1); + + // the text of a converted body is in charsetName=UTF-8, and the charset of the exchange is kept + Exchange exchange = template.send(producerUri(), + ExchangeBuilder.anExchange(context).withProperty(Exchange.CHARSET_NAME, "ISO-8859-1") + .withHeader(Exchange.CHARSET_NAME, "ISO-8859-1").withBody(new StringReader(testMessage)).build()); + + MockEndpoint.assertIsSatisfied(context, 10, TimeUnit.SECONDS); + assertNull(exchange.getException()); + assertArrayEquals(testMessage.getBytes(StandardCharsets.UTF_8), + received.getExchanges().get(0).getMessage().getBody(byte[].class)); + assertEquals("ISO-8859-1", exchange.getProperty(Exchange.CHARSET_NAME)); + assertEquals("ISO-8859-1", exchange.getMessage().getHeader(Exchange.CHARSET_NAME)); + } + @Test public void testConvertedBodyLeavesNoCharsetProperty() throws Exception { String testMessage = Hl7TestMessageGenerator.generateMessage().replace("PID|", "PID|Ærø|"); diff --git a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc index 60d1ed6ed3a2..5c6fc120542c 100644 --- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc +++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc @@ -5550,8 +5550,27 @@ charset. A body that cannot be converted, such as a `Map`, fails the exchange wi Before, such a body was sent as an empty MLLP message: with `InOut` the exchange then failed with a `MllpAcknowledgementTimeoutException` after the receive timeout, and with `InOnly` it completed without an error. -After sending a `String` body, the `CamelCharsetName` exchange property is now the charset the message was sent in. -Before, it was set to `charsetName` even when the charset of the exchange was used to encode the message. +=== camel-mllp - the charset of a message is MSH-18, then charsetName, and no longer the CamelCharsetName exchange property + +The mllp producer now encodes a `String` body in the charset named in MSH-18 of the message, and in its `charsetName` +when the message has no MSH-18 (or MSH-18 names a charset that is not supported), as the mllp consumer already +decoded it. The `CamelCharsetName` header and exchange property no longer choose the charset of the producer, and the +producer no longer sets the `CamelCharsetName` exchange property after sending. Before, the producer ignored MSH-18 and +used the `CamelCharsetName` header, then the exchange property, then `charsetName`: a message marked `UNICODE UTF-8` in +MSH-18 was sent in ISO-8859-1, and in a route from an mllp consumer to an mllp producer the `charsetName` of the +producer was ignored. A text body of another type, such as a `Reader`, is converted in `charsetName` too. To send a +message in a charset that is not its MSH-18, send it as a `byte[]`. + +The mllp consumer no longer sets the `CamelCharsetName` exchange property for a `String` body (the default, +`stringPayload=true`), so its charset (ISO-8859-1 by default) is no longer used by the later steps of the route that +turn text into bytes. With `stringPayload=false`, it sets the property to the charset of the `byte[]` body: the charset +of MSH-18, and `charsetName` when the message has no MSH-18. Before, the property was always the `charsetName` of the +consumer. An acknowledgement given as a `String` in the `CamelMllpAcknowledgement` or `CamelMllpAcknowledgementString` +exchange property is encoded in the charset of its MSH-18, and else in the charset of the exchange, or `charsetName`. + +The `CamelMllpCharset` header of the consumer now holds the MSH-18 value of the message (such as `UNICODE UTF-8`), as +documented. Before, it held the `charsetName` of the consumer (`ISO_8859_1` by default) whenever the message had an +MSH-18. === camel-lra - only an LRA hosted by the configured coordinator is joined diff --git a/dsl/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/MllpComponentBuilderFactory.java b/dsl/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/MllpComponentBuilderFactory.java index 4feef6fd9353..e404e2cd95de 100644 --- a/dsl/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/MllpComponentBuilderFactory.java +++ b/dsl/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/MllpComponentBuilderFactory.java @@ -69,7 +69,12 @@ public interface MllpComponentBuilderFactory { } /** - * Sets the default charset to use. + * The charset of an HL7 message that has no MSH-18. The consumer + * decodes, and the producer encodes, a message in the charset named in + * MSH-18 of the message, and in this charset when the message has no + * MSH-18, or MSH-18 names a charset that is not supported. The + * CamelCharsetName header and exchange property are not used. The + * default is the defaultCharset of the component (ISO-8859-1). * * The option is a: <code>java.lang.String</code> type. * @@ -142,11 +147,10 @@ public interface MllpComponentBuilderFactory { /** * Enable/Disable converting the payload to a String. If enabled, HL7 * Payloads received from external systems will be validated converted - * to a String. If the charsetName property is set, that character set - * will be used for the conversion. If the charsetName property is not - * set, the value of MSH-18 will be used to determine th appropriate - * character set. If MSH-18 is not set, then the default ISO-8859-1 - * character set will be use. + * to a String. The value of MSH-18 is used to determine the character + * set for the conversion. If MSH-18 is not set, or names a character + * set that is not supported, the charsetName character set is used + * (ISO-8859-1 by default). * * The option is a: <code>boolean</code> type. * diff --git a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/MllpEndpointBuilderFactory.java b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/MllpEndpointBuilderFactory.java index b4dc58980a83..58dec50501d9 100644 --- a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/MllpEndpointBuilderFactory.java +++ b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/MllpEndpointBuilderFactory.java @@ -76,7 +76,12 @@ public interface MllpEndpointBuilderFactory { return this; } /** - * Sets the default charset to use. + * The charset of an HL7 message that has no MSH-18. The consumer + * decodes, and the producer encodes, a message in the charset named in + * MSH-18 of the message, and in this charset when the message has no + * MSH-18, or MSH-18 names a charset that is not supported. The + * CamelCharsetName header and exchange property are not used. The + * default is the defaultCharset of the component (ISO-8859-1). * * The option is a: <code>java.lang.String</code> type. * @@ -162,11 +167,10 @@ public interface MllpEndpointBuilderFactory { /** * Enable/Disable converting the payload to a String. If enabled, HL7 * Payloads received from external systems will be validated converted - * to a String. If the charsetName property is set, that character set - * will be used for the conversion. If the charsetName property is not - * set, the value of MSH-18 will be used to determine th appropriate - * character set. If MSH-18 is not set, then the default ISO-8859-1 - * character set will be use. + * to a String. The value of MSH-18 is used to determine the character + * set for the conversion. If MSH-18 is not set, or names a character + * set that is not supported, the charsetName character set is used + * (ISO-8859-1 by default). * * The option is a: <code>boolean</code> type. * @@ -183,11 +187,10 @@ public interface MllpEndpointBuilderFactory { /** * Enable/Disable converting the payload to a String. If enabled, HL7 * Payloads received from external systems will be validated converted - * to a String. If the charsetName property is set, that character set - * will be used for the conversion. If the charsetName property is not - * set, the value of MSH-18 will be used to determine th appropriate - * character set. If MSH-18 is not set, then the default ISO-8859-1 - * character set will be use. + * to a String. The value of MSH-18 is used to determine the character + * set for the conversion. If MSH-18 is not set, or names a character + * set that is not supported, the charsetName character set is used + * (ISO-8859-1 by default). * * The option will be converted to a <code>boolean</code> type. * @@ -897,7 +900,12 @@ public interface MllpEndpointBuilderFactory { return this; } /** - * Sets the default charset to use. + * The charset of an HL7 message that has no MSH-18. The consumer + * decodes, and the producer encodes, a message in the charset named in + * MSH-18 of the message, and in this charset when the message has no + * MSH-18, or MSH-18 names a charset that is not supported. The + * CamelCharsetName header and exchange property are not used. The + * default is the defaultCharset of the component (ISO-8859-1). * * The option is a: <code>java.lang.String</code> type. * @@ -983,11 +991,10 @@ public interface MllpEndpointBuilderFactory { /** * Enable/Disable converting the payload to a String. If enabled, HL7 * Payloads received from external systems will be validated converted - * to a String. If the charsetName property is set, that character set - * will be used for the conversion. If the charsetName property is not - * set, the value of MSH-18 will be used to determine th appropriate - * character set. If MSH-18 is not set, then the default ISO-8859-1 - * character set will be use. + * to a String. The value of MSH-18 is used to determine the character + * set for the conversion. If MSH-18 is not set, or names a character + * set that is not supported, the charsetName character set is used + * (ISO-8859-1 by default). * * The option is a: <code>boolean</code> type. * @@ -1004,11 +1011,10 @@ public interface MllpEndpointBuilderFactory { /** * Enable/Disable converting the payload to a String. If enabled, HL7 * Payloads received from external systems will be validated converted - * to a String. If the charsetName property is set, that character set - * will be used for the conversion. If the charsetName property is not - * set, the value of MSH-18 will be used to determine th appropriate - * character set. If MSH-18 is not set, then the default ISO-8859-1 - * character set will be use. + * to a String. The value of MSH-18 is used to determine the character + * set for the conversion. If MSH-18 is not set, or names a character + * set that is not supported, the charsetName character set is used + * (ISO-8859-1 by default). * * The option will be converted to a <code>boolean</code> type. * @@ -1593,7 +1599,12 @@ public interface MllpEndpointBuilderFactory { return this; } /** - * Sets the default charset to use. + * The charset of an HL7 message that has no MSH-18. The consumer + * decodes, and the producer encodes, a message in the charset named in + * MSH-18 of the message, and in this charset when the message has no + * MSH-18, or MSH-18 names a charset that is not supported. The + * CamelCharsetName header and exchange property are not used. The + * default is the defaultCharset of the component (ISO-8859-1). * * The option is a: <code>java.lang.String</code> type. * @@ -1679,11 +1690,10 @@ public interface MllpEndpointBuilderFactory { /** * Enable/Disable converting the payload to a String. If enabled, HL7 * Payloads received from external systems will be validated converted - * to a String. If the charsetName property is set, that character set - * will be used for the conversion. If the charsetName property is not - * set, the value of MSH-18 will be used to determine th appropriate - * character set. If MSH-18 is not set, then the default ISO-8859-1 - * character set will be use. + * to a String. The value of MSH-18 is used to determine the character + * set for the conversion. If MSH-18 is not set, or names a character + * set that is not supported, the charsetName character set is used + * (ISO-8859-1 by default). * * The option is a: <code>boolean</code> type. * @@ -1700,11 +1710,10 @@ public interface MllpEndpointBuilderFactory { /** * Enable/Disable converting the payload to a String. If enabled, HL7 * Payloads received from external systems will be validated converted - * to a String. If the charsetName property is set, that character set - * will be used for the conversion. If the charsetName property is not - * set, the value of MSH-18 will be used to determine th appropriate - * character set. If MSH-18 is not set, then the default ISO-8859-1 - * character set will be use. + * to a String. The value of MSH-18 is used to determine the character + * set for the conversion. If MSH-18 is not set, or names a character + * set that is not supported, the charsetName character set is used + * (ISO-8859-1 by default). * * The option will be converted to a <code>boolean</code> type. *
