1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
package ca.uhn.hl7v2.protocol.impl; |
28 | |
|
29 | |
import java.io.IOException; |
30 | |
import java.io.InputStream; |
31 | |
import java.util.Properties; |
32 | |
|
33 | |
import ca.uhn.hl7v2.llp.LLPException; |
34 | |
import ca.uhn.hl7v2.llp.MinLLPReader; |
35 | |
import ca.uhn.hl7v2.llp.MinLLPWriter; |
36 | |
import ca.uhn.hl7v2.protocol.StreamSource; |
37 | |
import ca.uhn.hl7v2.protocol.TransportException; |
38 | |
import ca.uhn.hl7v2.protocol.TransportLayer; |
39 | |
import ca.uhn.hl7v2.protocol.Transportable; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
public class MLLPTransport extends AbstractTransport implements TransportLayer { |
50 | |
|
51 | |
private MinLLPReader myReader; |
52 | |
private MinLLPWriter myWriter; |
53 | |
|
54 | |
private StreamSource myStreamSource; |
55 | |
private Properties myCharsetMappings; |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | 60 | public MLLPTransport(StreamSource theStreamSource) throws TransportException { |
62 | 60 | myStreamSource = theStreamSource; |
63 | 60 | myCharsetMappings = loadCharsetMappings(); |
64 | 60 | } |
65 | |
|
66 | |
private static Properties loadCharsetMappings() throws TransportException { |
67 | 60 | Properties mappings = new Properties(); |
68 | 60 | String resource = "ca/uhn/hl7v2/protocol/impl/charset_map.properties"; |
69 | 60 | InputStream in = MLLPTransport.class.getClassLoader().getResourceAsStream(resource); |
70 | |
try { |
71 | 60 | mappings.load(in); |
72 | 0 | } catch (IOException e) { |
73 | 0 | throw new TransportException("Can't load character set mappings from " + resource, e); |
74 | 60 | } |
75 | 60 | return mappings; |
76 | |
} |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
public void doSend(Transportable theMessage) throws TransportException { |
82 | |
try { |
83 | 35 | String charset = (String) theMessage.getMetadata().get("MSH-18"); |
84 | 35 | if (charset != null) { |
85 | 5 | charset = myCharsetMappings.getProperty(charset, charset); |
86 | 5 | myWriter.writeMessage(theMessage.getMessage(), charset); |
87 | |
} else { |
88 | 30 | myWriter.writeMessage(theMessage.getMessage()); |
89 | |
} |
90 | 0 | } catch (LLPException e) { |
91 | 0 | throw new TransportException(e); |
92 | 0 | } catch (IOException e) { |
93 | 0 | throw new TransportException(e); |
94 | 35 | } |
95 | 35 | } |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
public Transportable doReceive() throws TransportException { |
101 | 120 | Transportable result = null; |
102 | |
try { |
103 | 120 | String message = myReader.getMessage(); |
104 | 110 | if (message != null) { |
105 | 30 | result = new TransportableImpl(message); |
106 | |
} |
107 | 0 | } catch (LLPException e) { |
108 | 0 | throw new TransportException(e); |
109 | 10 | } catch (IOException e) { |
110 | 10 | throw new TransportException(e); |
111 | 110 | } |
112 | 110 | return result; |
113 | |
} |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
public void doConnect() throws TransportException { |
119 | 70 | myStreamSource.connect(); |
120 | |
try { |
121 | 50 | myReader = new MinLLPReader(myStreamSource.getInboundStream()); |
122 | 50 | myWriter = new MinLLPWriter(myStreamSource.getOutboundStream()); |
123 | 0 | } catch (IOException e) { |
124 | 0 | throw new TransportException(e); |
125 | 50 | } |
126 | 50 | } |
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
public void doDisconnect() throws TransportException { |
132 | |
try { |
133 | 35 | if (myReader != null) myReader.close(); |
134 | 35 | if (myWriter != null) myWriter.close(); |
135 | 0 | } catch (IOException e) { |
136 | 0 | throw new TransportException(e); |
137 | |
} finally { |
138 | 35 | myReader = null; |
139 | 35 | myWriter = null; |
140 | 35 | } |
141 | 35 | myStreamSource.disconnect(); |
142 | 35 | } |
143 | |
|
144 | |
} |