Can I intercept or preprocess messages? How to handle systems that send too much or invalid data?

0 votes
asked Sep 22, 2016 by rich-c-2789 (16,020 points)

Occacionally, the messages from a sending system are inconsistent.  These inconsistentcies may cause message model parse exception or other errors.  When the sending system is unable to fix the problems, I need a way to fix or discard the message before it is processed.  Below are a few examples of the inconsistencies encountered:

Use case 1: Sending system is inconsistent with the message format or occasionally sends a corrupt or invalid format

  • Some messages contain new line characters in fields
  • Some messages are prefixed with HEX characters
  • Some messages are encoded
  • Etc.

Use case 2: Sending system sends too many messages (need to filter based on content).

  • Sending system sends message types that the channel was not intended to process.  Example: ADT vs. ORU
  • Sending system sends messages that should be ignored.  For example the message may contain a code for messages to be ignored.
  • Etc.

1 Answer

0 votes
As of version 44 the receivers below have a new feature/section: "Preprocess Received Bytes". 
HTTP Listener
Network Share
Web Service
Secure Socket
This new section can be used to execute a script prior to the message being added to the channels inbound queue.
If an HL7 message has misplaced new lines character in the middle of fields, you can look for a new line that is not followed by a starting segment.  These new lines could be be replaced by a space with the following:
bytesOut = new java.lang.String(bytesIn, 'UTF-8').replaceAll('\\n(?![A-Z0-9]{3}\\|)', ' ').getBytes();
Note that in the screenshot above this section has been enabled. In this example the script will ONLY run when the incoming message fails to be parsed into the channels message model type.  Alternatively the script can be configured to run for all messages.
The following is a description of the bound variables available in the preprocess script
bytesIn - The original bytes received.  Used to access the original bytes modification to this variable will be ignored by the channel.  
bytesOut - The modified bytes that will be used by the channel.  Note if set to null the message will be discarded.  bytesOut can be set to bytesIn if no changes are needed.
responseBytes - Applicable only to receivers that send acknowledgements.  Used to send a response to the sending system when bytesOut = null.  Otherwise responses are sent based on the channel configuration.
See this question for an example of how to fix HL7 parse exceptions caused when a new line character is embedded in an HL7 field:
See also:
answered Sep 22, 2016 by rich-c-2789 (16,020 points)
edited Sep 9, 2019 by gary-t-8719