1 | |
package ca.uhn.hl7v2.hoh.raw.client; |
2 | |
|
3 | |
import java.io.IOException; |
4 | |
import java.net.Socket; |
5 | |
import java.net.URL; |
6 | |
|
7 | |
import ca.uhn.hl7v2.hoh.api.DecodeException; |
8 | |
import ca.uhn.hl7v2.hoh.api.EncodeException; |
9 | |
import ca.uhn.hl7v2.hoh.api.IAuthorizationClientCallback; |
10 | |
import ca.uhn.hl7v2.hoh.api.IClientSimple; |
11 | |
import ca.uhn.hl7v2.hoh.api.IReceivable; |
12 | |
import ca.uhn.hl7v2.hoh.api.ISendable; |
13 | |
import ca.uhn.hl7v2.hoh.api.MessageMetadataKeys; |
14 | |
import ca.uhn.hl7v2.hoh.auth.SingleCredentialClientCallback; |
15 | |
import ca.uhn.hl7v2.hoh.raw.api.RawSendable; |
16 | |
import ca.uhn.hl7v2.hoh.sockets.TlsSocketFactory; |
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
public class HohRawClientSimple extends AbstractRawClient implements IClientSimple { |
30 | |
|
31 | 55 | private boolean myAutoClose = true; |
32 | |
private Socket mySocket; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
public HohRawClientSimple(String theHost, int thePort, String theUriPath) { |
46 | 50 | super(theHost, thePort, theUriPath); |
47 | 35 | } |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
@Override |
53 | |
public synchronized IReceivable<String> sendAndReceive(ISendable<?> theMessageToSend) throws DecodeException, IOException, EncodeException { |
54 | |
|
55 | 45 | return super.sendAndReceive(theMessageToSend); |
56 | |
} |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
public HohRawClientSimple(URL theUrl) { |
65 | 20 | super(theUrl); |
66 | 20 | } |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
public void close() { |
72 | 45 | if (mySocket != null) { |
73 | 45 | closeSocket(mySocket); |
74 | |
} |
75 | 45 | } |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
public boolean isAutoClose() { |
81 | 45 | return myAutoClose; |
82 | |
} |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
public boolean isConnected() { |
88 | 0 | return isSocketConnected(mySocket); |
89 | |
} |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
@Override |
96 | |
protected Socket provideSocket() throws IOException { |
97 | 45 | if (mySocket == null || mySocket.isClosed() || mySocket.isInputShutdown() || mySocket.isOutputShutdown()) { |
98 | 45 | mySocket = connect(); |
99 | |
} |
100 | 45 | return mySocket; |
101 | |
} |
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
@Override |
107 | |
protected void returnSocket(Socket theSocket) { |
108 | 45 | if (isAutoClose()) { |
109 | 45 | close(); |
110 | |
} |
111 | 45 | } |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
public void setAutoClose(boolean theAutoClose) { |
117 | 5 | myAutoClose = theAutoClose; |
118 | 5 | } |
119 | |
|
120 | |
public static void main(String[] args) { |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | 0 | String host = "localhost"; |
126 | 0 | int port = 8080; |
127 | 0 | String uri = "/AppContext"; |
128 | |
|
129 | |
|
130 | 0 | HohRawClientSimple client = new HohRawClientSimple(host, port, uri); |
131 | |
|
132 | |
|
133 | 0 | client.setSocketFactory(new TlsSocketFactory()); |
134 | |
|
135 | |
|
136 | |
|
137 | 0 | IAuthorizationClientCallback authCalback = new SingleCredentialClientCallback("ausername", "somepassword"); |
138 | 0 | client.setAuthorizationCallback(authCalback); |
139 | |
|
140 | |
|
141 | |
|
142 | 0 | String message = "MSH|^~\\&|||||200803051508||ADT^A31|2|P|2.5\r" + "EVN||200803051509\r" + "PID|||ZZZZZZ83M64Z148R^^^SSN^SSN^^20070103\r"; |
143 | 0 | ISendable<?> sendable = new RawSendable(message); |
144 | |
|
145 | |
try { |
146 | |
|
147 | 0 | IReceivable<String> receivable = client.sendAndReceive(sendable); |
148 | |
|
149 | |
|
150 | 0 | System.out.println("Response was:\n" + receivable.getMessage()); |
151 | |
|
152 | |
|
153 | 0 | String remoteHostIp = (String) receivable.getMetadata().get(MessageMetadataKeys.REMOTE_HOST_ADDRESS); |
154 | 0 | System.out.println("From:\n" + remoteHostIp); |
155 | |
|
156 | 0 | } catch (DecodeException e) { |
157 | |
|
158 | 0 | e.printStackTrace(); |
159 | 0 | } catch (IOException e) { |
160 | |
|
161 | 0 | e.printStackTrace(); |
162 | 0 | } catch (EncodeException e) { |
163 | |
|
164 | |
|
165 | 0 | e.printStackTrace(); |
166 | 0 | } |
167 | |
|
168 | 0 | } |
169 | |
|
170 | |
} |