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 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 super(theHost, thePort, theUriPath);
47 }
48
49
50
51
52 @Override
53 public synchronized IReceivable<String> sendAndReceive(ISendable<?> theMessageToSend) throws DecodeException, IOException, EncodeException {
54
55 return super.sendAndReceive(theMessageToSend);
56 }
57
58
59
60
61
62
63
64 public HohRawClientSimple(URL theUrl) {
65 super(theUrl);
66 }
67
68
69
70
71 public void close() {
72 if (mySocket != null) {
73 closeSocket(mySocket);
74 }
75 }
76
77
78
79
80 public boolean isAutoClose() {
81 return myAutoClose;
82 }
83
84
85
86
87 public boolean isConnected() {
88 return isSocketConnected(mySocket);
89 }
90
91
92
93
94
95 @Override
96 protected Socket provideSocket() throws IOException {
97 if (mySocket == null || mySocket.isClosed() || mySocket.isInputShutdown() || mySocket.isOutputShutdown()) {
98 mySocket = connect();
99 }
100 return mySocket;
101 }
102
103
104
105
106 @Override
107 protected void returnSocket(Socket theSocket) {
108 if (isAutoClose()) {
109 close();
110 }
111 }
112
113
114
115
116 public void setAutoClose(boolean theAutoClose) {
117 myAutoClose = theAutoClose;
118 }
119
120 public static void main(String[] args) {
121
122
123
124
125 String host = "localhost";
126 int port = 8080;
127 String uri = "/AppContext";
128
129
130 HohRawClientSimple client = new HohRawClientSimple(host, port, uri);
131
132
133 client.setSocketFactory(new TlsSocketFactory());
134
135
136
137 IAuthorizationClientCallback authCalback = new SingleCredentialClientCallback("ausername", "somepassword");
138 client.setAuthorizationCallback(authCalback);
139
140
141
142 String message = "MSH|^~\\&|||||200803051508||ADT^A31|2|P|2.5\r" + "EVN||200803051509\r" + "PID|||ZZZZZZ83M64Z148R^^^SSN^SSN^^20070103\r";
143 ISendable<?> sendable = new RawSendable(message);
144
145 try {
146
147 IReceivable<String> receivable = client.sendAndReceive(sendable);
148
149
150 System.out.println("Response was:\n" + receivable.getMessage());
151
152
153 String remoteHostIp = (String) receivable.getMetadata().get(MessageMetadataKeys.REMOTE_HOST_ADDRESS);
154 System.out.println("From:\n" + remoteHostIp);
155
156 } catch (DecodeException | EncodeException | IOException e) {
157
158 e.printStackTrace();
159 }
160
161
162
163
164 }
165
166 }