View Javadoc

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  		this(theRawClient, null);
37  	}
38  
39  	/**
40  	 * Constructor
41  	 * 
42  	 * @param theRawClient
43  	 *            The Raw client to wrap (may not be null)
44  	 * @param theParser
45  	 *            The parser to use (may be null, in which case the parser
46  	 *            contained within the sent message will be used
47  	 */
48  	protected AbstractClient(T theRawClient, Parser theParser) {
49  		Validate.notNull(theRawClient, "rawClient");
50  		myRawClient = theRawClient;
51  		myParser = theParser;
52  	}
53  
54  	public String getHost() {
55  		return myRawClient.getHost();
56  	}
57  
58  	/**
59  	 * Returns the {@link Parser} to use to parsing and encoding messages within
60  	 * this client (may return null)
61  	 */
62  	public Parser getParser() {
63  		return myParser;
64  	}
65  
66  	public int getPort() {
67  		return myRawClient.getPort();
68  	}
69  
70  	protected T getRawClient() {
71  		return myRawClient;
72  	}
73  
74  	public ISocketFactory getSocketFactory() {
75  		return myRawClient.getSocketFactory();
76  	}
77  
78  	public String getUriPath() {
79  		return myRawClient.getUriPath();
80  	}
81  
82  	public URL getUrl() {
83  		return myRawClient.getUrl();
84  	}
85  
86  	/**
87  	 * {@inheritDoc}
88  	 */
89  	public String getUrlString() {
90  		return myRawClient.getUrlString();
91  	}
92  
93  	/**
94  	 * Sends a message, waits for the response, and then returns the response if
95  	 * any
96  	 * 
97  	 * @param theMessageToSend
98  	 *            The message to send
99  	 * @return The returned message, as well as associated metadata
100 	 * @throws DecodeException
101 	 *             If a problem occurs (read error, socket disconnect, etc.)
102 	 *             during communication, or the response is invalid in some way.
103 	 *             Note that IO errors in trying to connect to the remote host
104 	 *             or sending the message are thrown directly (i.e. as
105 	 *             {@link IOException}), but IO errors in reading the response
106 	 *             are thrown as DecodeException
107 	 * @throws IOException
108 	 *             If the client is unable to connect to the remote host
109 	 * @throws EncodeException
110 	 *             If a failure occurs while encoding the message into a
111 	 *             sendable HTTP request
112 	 * @throws HL7Exception
113 	 *             If the response can not be parsed
114 	 * @throws EncodingNotSupportedException
115 	 *             If the encoding is not supported
116 	 */
117 	public IReceivable<Message> sendAndReceiveMessage(ISendable<Message> theMessageToSend) throws DecodeException, IOException, EncodeException, EncodingNotSupportedException, HL7Exception {
118 		IReceivable<String> response = myRawClient.sendAndReceive(theMessageToSend);
119 		Parser parser = myParser != null ? myParser : theMessageToSend.getMessage().getParser();
120 		return new MessageReceivable(parser.parse(response.getMessage()));
121 	}
122 
123 	/**
124 	 * Sends a message, waits for the response, and then returns the response if
125 	 * any (this method is a convenience method for {@link #sendAndReceiveMessage(ISendable)}
126 	 * 
127 	 * @param theMessageToSend
128 	 *            The message to send
129 	 * @return The returned message, as well as associated metadata
130 	 * @throws DecodeException
131 	 *             If a problem occurs (read error, socket disconnect, etc.)
132 	 *             during communication, or the response is invalid in some way.
133 	 *             Note that IO errors in trying to connect to the remote host
134 	 *             or sending the message are thrown directly (i.e. as
135 	 *             {@link IOException}), but IO errors in reading the response
136 	 *             are thrown as DecodeException
137 	 * @throws IOException
138 	 *             If the client is unable to connect to the remote host
139 	 * @throws EncodeException
140 	 *             If a failure occurs while encoding the message into a
141 	 *             sendable HTTP request
142 	 * @throws HL7Exception
143 	 *             If the response can not be parsed
144 	 * @throws EncodingNotSupportedException
145 	 *             If the encoding is not supported
146 	 */
147 	public IReceivable<Message> sendAndReceiveMessage(Message theMessageToSend) throws DecodeException, IOException, EncodeException, EncodingNotSupportedException, HL7Exception {
148 		MessageSendable sendable = new MessageSendable(theMessageToSend);
149 		return sendAndReceiveMessage(sendable);
150 	}
151 
152 	/**
153 	 * {@inheritDoc}
154 	 */
155 	public void setAuthorizationCallback(IAuthorizationClientCallback theAuthorizationCallback) {
156 		myRawClient.setAuthorizationCallback(theAuthorizationCallback);
157 	}
158 
159 	/**
160 	 * {@inheritDoc}
161 	 */
162 	public void setCharset(Charset theCharset) {
163 		myRawClient.setCharset(theCharset);
164 	}
165 
166 	/**
167 	 * {@inheritDoc}
168 	 */
169 	public void setHost(String theHost) {
170 		myRawClient.setHost(theHost);
171 	}
172 
173 	/**
174 	 * Sets the {@link Parser} to use to parsing and encoding messages within
175 	 * this client
176 	 */
177 	public void setParser(Parser theParser) {
178 		Validate.notNull(theParser, "parser");
179 		myParser = theParser;
180 	}
181 
182 	/**
183 	 * {@inheritDoc}
184 	 */
185 	public void setUriPath(String thePath) {
186 		myRawClient.setUriPath(thePath);
187 	}
188 
189 	/**
190 	 * {@inheritDoc}
191 	 */
192 	public void setPort(int thePort) {
193 		myRawClient.setPort(thePort);
194 	}
195 
196 	/**
197 	 * {@inheritDoc}
198 	 */
199 	public void setResponseTimeout(long theResponseTimeout) {
200 		myRawClient.setResponseTimeout(theResponseTimeout);
201 	}
202 
203 	/**
204 	 * {@inheritDoc}
205 	 */
206 	public void setSigner(ISigner theSigner) {
207 		myRawClient.setSigner(theSigner);
208 	}
209 
210 	/**
211 	 * {@inheritDoc}
212 	 */
213 	public void setSocketFactory(ISocketFactory theSocketFactory) {
214 		myRawClient.setSocketFactory(theSocketFactory);
215 	}
216 
217 	/**
218 	 * {@inheritDoc}
219 	 */
220 	public void setUrl(URL theUrl) {
221 		myRawClient.setUrl(theUrl);
222 	}
223 
224 	/**
225 	 * {@inheritDoc}
226 	 */
227 	public void setUrlString(String theString) {
228 		myRawClient.setUrlString(theString);
229 	}
230 
231 }