1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.openehealth.ipf.platform.camel.ihe.hl7v2;
17
18 import java.io.BufferedReader;
19 import java.io.File;
20 import java.io.InputStream;
21 import java.io.InputStreamReader;
22
23 import ca.uhn.hl7v2.parser.Parser;
24 import org.apache.camel.Message;
25 import org.apache.camel.WrappedFile;
26 import org.apache.camel.converter.IOConverter;
27
28
29
30
31
32
33 public class Hl7v2MarshalUtils {
34
35 private Hl7v2MarshalUtils() {
36 throw new IllegalStateException("Cannot instantiate helper class");
37 }
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 public static String marshalStandardTypes(Message message, String charset, Parser parser) throws Exception {
55 Object body = message.getBody();
56 if( ! typeSupported(body)) {
57 return null;
58 }
59
60 String s = null;
61 if(body instanceof String) {
62 s = (String) body;
63 } else if(body instanceof ca.uhn.hl7v2.model.Message) {
64 s = parser.encode((ca.uhn.hl7v2.model.Message) body);
65 } else if(body instanceof File) {
66 s = readFile(body, charset);
67 } else if(body instanceof WrappedFile<?>) {
68 Object file = ((WrappedFile<?>) body).getFile();
69 if(file instanceof File) {
70 s = readFile(file, charset);
71 }
72 } else {
73
74
75
76 byte[] bytes = message.getBody(byte[].class);
77 if(bytes != null) {
78 s = new String(bytes, charset);
79 }
80 }
81 return s;
82 }
83
84
85 private static String readFile(Object file, String charset) throws Exception {
86 byte[] bytes = IOConverter.toByteArray((File) file);
87 return new String(bytes, charset).replace('\n', '\r');
88 }
89
90
91
92
93
94
95
96
97
98
99
100 public static boolean typeSupported(Object body) {
101 final Class<?>[] knownTypes = new Class<?>[] {
102 String.class,
103 ca.uhn.hl7v2.model.Message.class,
104 File.class,
105 InputStream.class,
106 java.nio.ByteBuffer.class,
107 byte[].class,
108 WrappedFile.class
109 };
110
111 for(Class<?> type : knownTypes) {
112 try {
113 type.cast(body);
114 return true;
115 } catch (ClassCastException cce) {
116
117 }
118 }
119
120 return false;
121 }
122
123
124
125
126
127
128
129 public static String convertBodyToString(
130 Message message,
131 String charset,
132 boolean defragmentSegments) throws Exception
133 {
134 InputStream stream = message.getBody(InputStream.class);
135 BufferedReader br = new BufferedReader(new InputStreamReader(stream, charset));
136 String s = IOConverter.toString(br);
137 s = s.replace('\n', '\r');
138 if (defragmentSegments) {
139 s = s.replace("\rADD" + s.charAt(3), "");
140 }
141 return s;
142 }
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158 public static ca.uhn.hl7v2.model.Message extractHapiMessage(
159 Message message,
160 String charset,
161 Parser parser) throws Exception
162 {
163 Object body = message.getBody();
164 ca.uhn.hl7v2.model.Message msg = null;
165 if (body instanceof ca.uhn.hl7v2.model.Message) {
166 msg = (ca.uhn.hl7v2.model.Message) body;
167 } else {
168
169
170 String s = marshalStandardTypes(message, charset, parser);
171 if(s != null) {
172 s = s.replace('\n', '\r');
173 msg = parser.parse(s);
174 }
175 }
176 return msg;
177 }
178
179 }