HL7V2 HAPI parser exception while receiving data via TCP/IP - java

I'm using the HAPI hapi-structures-v25 library with version 2.3 to parse HL7v2 message & convert that into FHIR resources. I'm facing a strange issue while receiving and parsing the HL7V2 message using HAPI via TCP listener.
Determine encoding for message. The following is the first 50 chars of the message for reference, although this may not be where the issue is: MSH|^~\&|test|DrJhonDoe|TEST|UNKNOWN|20210216190432||ADT^A01^ADT_A01|60b647d4-b5a5-4fae-a928-d4a3849de3c8|T|2.5
Strange that I'm not getting this error when I'm trying to send this message as a string in main function. I'm getting this error only when I receive the data over TCP/IP to my Java function. I tried sending the HL7 message to my receiving TCP port using Mirth as well external tool & my result is same.
Here is the sample of my HL7v2 message Im trying to process
MSH|^~\\&|test|Dr.JhonDoe|TEST|UNKNOWN|20210216190432.7||ADT^A01^ADT_A01|60b647d4b5a54faea928d4a3849de3c8|T|2.5
EVN||20210216|20210216|
While receiving the data from tcp/ip im converting the byte to string using the UTF-8 charset.
InputStream in = connection.getInputStream();
OutputStream out = connection.getOutputStream();
receivedMessageSize = in.read(receivedByeBuffer);
String incomingHl7Message = new String(receivedByeBuffer, StandardCharsets.UTF_8);
Im getting the message properly. But not sure why the error comes.

As mentioned in the answer by Amit, it needs to be escaped in JAVA. The HL7v2 when transmitted via MLLP it adds <VT>, <CR> Unicode data to the text. The understanding needed here is that these are not junk characters. By the protocol of MLLP the starting and ending of the messages are marked by these unicode characters to describe the starting and ending of a frame.
The HAPI HL7 parse cannot parse these special (non-printable) characters. Happy that I've found a solution on the same forum to handle it in java wisely. How to remove control characters from java string?
A simple regex will do the trick as shown below:
.replaceAll("[\\p{Cntrl}&&[^\r\n\t]]", "");
Also make sure that you're encoding characters are also handled properly with JAVA. Usually JAVA is not good in handling backslash. So, escape the backslash .replace("\\", "\\\\")
This will do the trick.

Related

Not able to read special character in buffer from http response on DAP

I have a simple http get call from java using simple headers accept type and authorization in header .
“I’m using charset UTF-8” when I try the same code in local that is on IDE eclipse tomcat server I am getting proper response and but when put the same code on dap server response data is getting changed .
Example : Ó (actual data sent by the client) same as is data I am getting in LOCAL but in DAP only such char apart from alpha numeric (Ó) changing to some other char like this Á.
Stuck in this from couple of days .
Please provide me the solution .
Thanks ,
Mussaveer
Tried all charset while consuming input stream .
InputStreamReader in = new InputStreamReader(conn.getInputStream(),"");

Chinese lettes not read properly using Java Mail API

I am having an email listener which reads mail from gmail. When I send a mail from Outlook client which contains chinese character, the encoding is set to gb2312, which causes improper result in part.getContent() in Java mail api .
If encoding from client is set to Chinese Big5 program works properly but we can't change the encoding in Outlook Client . Is there a way to read from Java Mail API but setting the content type or any alternate approach to get the proper content??????
https://community.oracle.com/message/5440489#5440489
Used GBK charset to read the file for all GB2312 file since gb2312 is a subset of GBK.
The following then should work with a bit of luck:
String content = mail. ...
// The bytes as sent, and then interpreted as gb2312:
byte[] bytes = content.getBytes("gb2312");
// Now correctly interprete the bytes as Big5:
content = new String(bytes, "Big5");

Character Decoding Exception in Tomcat for '%'

I am getting this exception
Character decoding failed. Parameter [updatedLocalInfo] with value
org.apache.tomcat.util.buf.UDecoder$DecodeException: isHexDigit
I am passing request post data as JSON string and one of the values contains'%'
Because of this '%' i am getting this exception.
I am not able to figure out why this exception is Coming and how to fix it.
For information, The json which i am passing is
[{"taxInformation":"Applicable Taxes Extra","happyHourDesc":"40% off","happyHourTime":"4 to 8 PM","offer":"No Offers"}]
I am passing request post data as JSON string
My guess would be you're passing the JSON string without properly encoding it. When you send information from the client to the server via HTTP GET or POST, the information must be properly encoded. The most common way to do that is via URL encoding (even if it's POST data).
You haven't said how you're sending the data, but it sounds like you probably need to use encodeURIComponent at some stage during the generation of the data you're sending from the client to the server.

UTF-8 works from browser not application

This is the sample url
http://abc.com/ABCServlet/abc?cmd=1&id=123&content=%E8%AE%8A
From the browser i'm receiving 變 which is correct,
But from an application which does a http post using the same url I get �. Seems like a double encoding or something, anyone has any ideas?
Since you get three characters, my guess is that you read the input stream without specifying an encoding.
Wrap the stream in InputStreamReader( stream, "UTF-8" ) or, even better, get the encoding from the HTTP header (see the docs of your HTTP framework how to do that).

how to use i18n with jolt client?

I have a problem sending the euro sign within a jolt request. The server is set to use internationalization.
On the client side i did the folowing:
-the classpath contains jolti18n.jar and joltse.jar
-i set System.setProperty("bea.jolt.encoding", "ISO-8859-1"); this is the charset used on the server
-the connection is made using the parameter i18n=true
Does anyone know what is the problem?
Yes, there is no Euro sign in ISO-8859-1, thus it could not be converted.
Please try setting encoding to UTF-8 instead.

Categories