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.BufferedInputStream;
30 import java.io.BufferedOutputStream;
31 import java.io.IOException;
32 import java.io.InputStreamReader;
33 import java.io.OutputStreamWriter;
34 import java.io.Reader;
35 import java.io.Writer;
36 import java.net.URL;
37 import java.net.URLConnection;
38
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 import ca.uhn.hl7v2.protocol.TransportException;
43 import ca.uhn.hl7v2.protocol.TransportLayer;
44 import ca.uhn.hl7v2.protocol.Transportable;
45
46
47
48
49
50
51
52
53
54 public class URLTransport extends AbstractTransport implements TransportLayer {
55
56 private static final Logger log = LoggerFactory.getLogger(URLTransport.class);
57
58
59
60
61 public static final String URL_KEY = "URL";
62
63 private String myContentType = "application/hl7+doc+xml";
64 private final URL myURL;
65 private URLConnection myConnection;
66 protected final int myBufferSize = 3000;
67
68 private final boolean myConnectOnSend;
69 private final boolean myConnectOnReceive;
70 private final boolean myConnectOnConnect;
71
72
73
74
75
76
77
78
79
80
81
82
83
84 public URLTransport(URL theURL, boolean connectOnSend, boolean connectOnReceive, boolean connectOnConnect) {
85 myURL = theURL;
86 getCommonMetadata().put(URL_KEY, theURL);
87
88 myConnectOnSend = connectOnSend;
89 myConnectOnReceive = connectOnReceive;
90 myConnectOnConnect = connectOnConnect;
91 }
92
93
94
95
96
97
98 public void doSend(Transportable theMessage) throws TransportException {
99 if (myConnectOnSend) {
100 makeConnection();
101 }
102
103 try {
104 Writer out = new OutputStreamWriter(new BufferedOutputStream(myConnection.getOutputStream()));
105 out.write(theMessage.getMessage());
106 out.flush();
107 } catch (IOException e) {
108 throw new TransportException(e);
109 }
110 }
111
112 public Transportable doReceive() throws TransportException {
113
114 if (myConnectOnReceive) {
115 makeConnection();
116 }
117
118 StringBuilder response = new StringBuilder();
119
120 try {
121 log.debug("Getting InputStream from URLConnection");
122 Reader in = new InputStreamReader(new BufferedInputStream(myConnection.getInputStream()));
123 log.debug("Got InputStream from URLConnection");
124
125 char[] buf = new char[myBufferSize];
126 int bytesRead = 0;
127
128 IntRefef.html#IntRef">IntRef bytesReadRef = new IntRef();
129
130 while (bytesRead >= 0) {
131
132 try {
133 ReaderThreadad.html#ReaderThread">ReaderThread readerThread = new ReaderThread(in, buf, bytesReadRef);
134 readerThread.start();
135 readerThread.join(10000);
136
137 bytesRead = bytesReadRef.getValue();
138
139 if (bytesRead == 0) {
140 throw new TransportException("Timeout waiting for response");
141 }
142 }
143 catch (InterruptedException ignored) {
144 }
145
146 if (bytesRead > 0) {
147 response.append(buf, 0, bytesRead);
148 }
149
150 }
151
152 in.close();
153 } catch (IOException e) {
154 log.error(e.getMessage(), e);
155 }
156
157 if (response.length() == 0) {
158 throw new TransportException("Timeout waiting for response");
159 }
160
161 return new TransportableImpl(response.toString());
162 }
163
164
165
166
167
168
169
170 public void doConnect() throws TransportException {
171 if (myConnectOnConnect) {
172 makeConnection();
173 }
174 }
175
176
177 private void makeConnection() throws TransportException {
178 try {
179 myConnection = myURL.openConnection();
180 myConnection.setDoOutput(true);
181 myConnection.setDoInput(true);
182 myConnection.setRequestProperty("Content-Type", getContentType());
183 myConnection.connect();
184 } catch (IOException e) {
185 throw new TransportException(e);
186 }
187 log.debug("Made connection to {}", myURL.toExternalForm());
188 }
189
190
191
192
193
194 public String getContentType() {
195 return myContentType;
196 }
197
198
199
200
201
202 public void setContentType(String theContentType) {
203 myContentType = theContentType;
204 }
205
206
207
208
209 public void doDisconnect() {
210 myConnection = null;
211 }
212
213 }