Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
AbstractClient |
|
| 1.037037037037037;1.037 |
1 | package ca.uhn.hl7v2.hoh.hapi.client; | |
2 | ||
3 | import java.io.IOException; | |
4 | import java.net.URL; | |
5 | import java.nio.charset.Charset; | |
6 | ||
7 | import ca.uhn.hl7v2.HL7Exception; | |
8 | import ca.uhn.hl7v2.hoh.api.DecodeException; | |
9 | import ca.uhn.hl7v2.hoh.api.EncodeException; | |
10 | import ca.uhn.hl7v2.hoh.api.IAuthorizationClientCallback; | |
11 | import ca.uhn.hl7v2.hoh.api.IClient; | |
12 | import ca.uhn.hl7v2.hoh.api.IReceivable; | |
13 | import ca.uhn.hl7v2.hoh.api.ISendable; | |
14 | import ca.uhn.hl7v2.hoh.hapi.api.MessageReceivable; | |
15 | import ca.uhn.hl7v2.hoh.hapi.api.MessageSendable; | |
16 | import ca.uhn.hl7v2.hoh.raw.client.AbstractRawClient; | |
17 | import ca.uhn.hl7v2.hoh.sign.ISigner; | |
18 | import ca.uhn.hl7v2.hoh.sockets.ISocketFactory; | |
19 | import ca.uhn.hl7v2.hoh.util.Validate; | |
20 | import ca.uhn.hl7v2.model.Message; | |
21 | import ca.uhn.hl7v2.parser.EncodingNotSupportedException; | |
22 | import ca.uhn.hl7v2.parser.Parser; | |
23 | ||
24 | public abstract class AbstractClient<T extends AbstractRawClient> implements IClient { | |
25 | ||
26 | private Parser myParser; | |
27 | private T myRawClient; | |
28 | ||
29 | /** | |
30 | * Constructor | |
31 | * | |
32 | * @param theRawClient | |
33 | * The Raw client to wrap (may not be null) | |
34 | */ | |
35 | public AbstractClient(T theRawClient) { | |
36 | 20 | this(theRawClient, null); |
37 | 20 | } |
38 | ||
39 | /** | |
40 | * {@inheritDoc} | |
41 | */ | |
42 | public void setKeepAlive(boolean theKeepAlive) { | |
43 | 5 | getRawClient().setKeepAlive(theKeepAlive); |
44 | 5 | } |
45 | ||
46 | /** | |
47 | * {@inheritDoc} | |
48 | */ | |
49 | public boolean isKeepAlive() { | |
50 | 0 | return getRawClient().isKeepAlive(); |
51 | } | |
52 | ||
53 | /** | |
54 | * {@inheritDoc} | |
55 | */ | |
56 | public void setSoTimeout(int theSoTimeout) { | |
57 | 5 | getRawClient().setSoTimeout(theSoTimeout); |
58 | 5 | } |
59 | ||
60 | /** | |
61 | * {@inheritDoc} | |
62 | */ | |
63 | public int getSoTimeout() { | |
64 | 0 | return getRawClient().getSoTimeout(); |
65 | } | |
66 | ||
67 | /** | |
68 | * Constructor | |
69 | * | |
70 | * @param theRawClient | |
71 | * The Raw client to wrap (may not be null) | |
72 | * @param theParser | |
73 | * The parser to use (may be null, in which case the parser | |
74 | * contained within the sent message will be used | |
75 | */ | |
76 | 25 | protected AbstractClient(T theRawClient, Parser theParser) { |
77 | 25 | Validate.notNull(theRawClient, "rawClient"); |
78 | 25 | myRawClient = theRawClient; |
79 | 25 | myParser = theParser; |
80 | 25 | } |
81 | ||
82 | public String getHost() { | |
83 | 5 | return myRawClient.getHost(); |
84 | } | |
85 | ||
86 | /** | |
87 | * Returns the {@link Parser} to use to parsing and encoding messages within | |
88 | * this client (may return null) | |
89 | */ | |
90 | public Parser getParser() { | |
91 | 0 | return myParser; |
92 | } | |
93 | ||
94 | /** | |
95 | * {@inheritDoc} | |
96 | */ | |
97 | public int getPort() { | |
98 | 5 | return myRawClient.getPort(); |
99 | } | |
100 | ||
101 | /** | |
102 | * {@inheritDoc} | |
103 | */ | |
104 | protected T getRawClient() { | |
105 | 15 | return myRawClient; |
106 | } | |
107 | ||
108 | /** | |
109 | * {@inheritDoc} | |
110 | */ | |
111 | public ISocketFactory getSocketFactory() { | |
112 | 0 | return myRawClient.getSocketFactory(); |
113 | } | |
114 | ||
115 | /** | |
116 | * {@inheritDoc} | |
117 | */ | |
118 | public String getUriPath() { | |
119 | 5 | return myRawClient.getUriPath(); |
120 | } | |
121 | ||
122 | /** | |
123 | * {@inheritDoc} | |
124 | */ | |
125 | public URL getUrl() { | |
126 | 70 | return myRawClient.getUrl(); |
127 | } | |
128 | ||
129 | /** | |
130 | * {@inheritDoc} | |
131 | */ | |
132 | public String getUrlString() { | |
133 | 0 | return myRawClient.getUrlString(); |
134 | } | |
135 | ||
136 | /** | |
137 | * Sends a message, waits for the response, and then returns the response if | |
138 | * any | |
139 | * | |
140 | * @param theMessageToSend | |
141 | * The message to send | |
142 | * @return The returned message, as well as associated metadata | |
143 | * @throws DecodeException | |
144 | * If a problem occurs (read error, socket disconnect, etc.) | |
145 | * during communication, or the response is invalid in some way. | |
146 | * Note that IO errors in trying to connect to the remote host | |
147 | * or sending the message are thrown directly (i.e. as | |
148 | * {@link IOException}), but IO errors in reading the response | |
149 | * are thrown as DecodeException | |
150 | * @throws IOException | |
151 | * If the client is unable to connect to the remote host | |
152 | * @throws EncodeException | |
153 | * If a failure occurs while encoding the message into a | |
154 | * sendable HTTP request | |
155 | * @throws HL7Exception | |
156 | * If the response can not be parsed | |
157 | * @throws EncodingNotSupportedException | |
158 | * If the encoding is not supported | |
159 | */ | |
160 | public IReceivable<Message> sendAndReceiveMessage(ISendable<Message> theMessageToSend) throws DecodeException, IOException, EncodeException, EncodingNotSupportedException, HL7Exception { | |
161 | 25 | IReceivable<String> response = myRawClient.sendAndReceive(theMessageToSend); |
162 | 20 | Parser parser = myParser != null ? myParser : theMessageToSend.getMessage().getParser(); |
163 | 20 | return new MessageReceivable(parser.parse(response.getMessage())); |
164 | } | |
165 | ||
166 | /** | |
167 | * Sends a message, waits for the response, and then returns the response if | |
168 | * any (this method is a convenience method for {@link #sendAndReceiveMessage(ISendable)} | |
169 | * | |
170 | * @param theMessageToSend | |
171 | * The message to send | |
172 | * @return The returned message, as well as associated metadata | |
173 | * @throws DecodeException | |
174 | * If a problem occurs (read error, socket disconnect, etc.) | |
175 | * during communication, or the response is invalid in some way. | |
176 | * Note that IO errors in trying to connect to the remote host | |
177 | * or sending the message are thrown directly (i.e. as | |
178 | * {@link IOException}), but IO errors in reading the response | |
179 | * are thrown as DecodeException | |
180 | * @throws IOException | |
181 | * If the client is unable to connect to the remote host | |
182 | * @throws EncodeException | |
183 | * If a failure occurs while encoding the message into a | |
184 | * sendable HTTP request | |
185 | * @throws HL7Exception | |
186 | * If the response can not be parsed | |
187 | * @throws EncodingNotSupportedException | |
188 | * If the encoding is not supported | |
189 | */ | |
190 | public IReceivable<Message> sendAndReceiveMessage(Message theMessageToSend) throws DecodeException, IOException, EncodeException, EncodingNotSupportedException, HL7Exception { | |
191 | 0 | MessageSendable sendable = new MessageSendable(theMessageToSend); |
192 | 0 | return sendAndReceiveMessage(sendable); |
193 | } | |
194 | ||
195 | /** | |
196 | * {@inheritDoc} | |
197 | */ | |
198 | public void setAuthorizationCallback(IAuthorizationClientCallback theAuthorizationCallback) { | |
199 | 15 | myRawClient.setAuthorizationCallback(theAuthorizationCallback); |
200 | 15 | } |
201 | ||
202 | /** | |
203 | * {@inheritDoc} | |
204 | */ | |
205 | public void setCharset(Charset theCharset) { | |
206 | 0 | myRawClient.setCharset(theCharset); |
207 | 0 | } |
208 | ||
209 | /** | |
210 | * {@inheritDoc} | |
211 | */ | |
212 | public void setHost(String theHost) { | |
213 | 0 | myRawClient.setHost(theHost); |
214 | 0 | } |
215 | ||
216 | /** | |
217 | * Sets the {@link Parser} to use to parsing and encoding messages within | |
218 | * this client | |
219 | */ | |
220 | public void setParser(Parser theParser) { | |
221 | 0 | Validate.notNull(theParser, "parser"); |
222 | 0 | myParser = theParser; |
223 | 0 | } |
224 | ||
225 | /** | |
226 | * {@inheritDoc} | |
227 | */ | |
228 | public void setUriPath(String thePath) { | |
229 | 0 | myRawClient.setUriPath(thePath); |
230 | 0 | } |
231 | ||
232 | /** | |
233 | * {@inheritDoc} | |
234 | */ | |
235 | public void setPort(int thePort) { | |
236 | 0 | myRawClient.setPort(thePort); |
237 | 0 | } |
238 | ||
239 | /** | |
240 | * {@inheritDoc} | |
241 | */ | |
242 | public void setResponseTimeout(long theResponseTimeout) { | |
243 | 0 | myRawClient.setResponseTimeout(theResponseTimeout); |
244 | 0 | } |
245 | ||
246 | /** | |
247 | * {@inheritDoc} | |
248 | */ | |
249 | public void setSigner(ISigner theSigner) { | |
250 | 0 | myRawClient.setSigner(theSigner); |
251 | 0 | } |
252 | ||
253 | /** | |
254 | * {@inheritDoc} | |
255 | */ | |
256 | public void setSocketFactory(ISocketFactory theSocketFactory) { | |
257 | 5 | myRawClient.setSocketFactory(theSocketFactory); |
258 | 5 | } |
259 | ||
260 | /** | |
261 | * {@inheritDoc} | |
262 | */ | |
263 | public void setUrl(URL theUrl) { | |
264 | 15 | myRawClient.setUrl(theUrl); |
265 | 15 | } |
266 | ||
267 | /** | |
268 | * {@inheritDoc} | |
269 | */ | |
270 | public void setUrlString(String theString) { | |
271 | 5 | myRawClient.setUrlString(theString); |
272 | 5 | } |
273 | ||
274 | } |