1 package ca.uhn.hl7v2.hoh.llp;
2
3 import ca.uhn.hl7v2.app.TwoPortService;
4 import ca.uhn.hl7v2.hoh.api.IAuthorizationClientCallback;
5 import ca.uhn.hl7v2.hoh.api.IAuthorizationServerCallback;
6 import ca.uhn.hl7v2.hoh.sign.ISigner;
7 import ca.uhn.hl7v2.hoh.util.ServerRoleEnum;
8 import ca.uhn.hl7v2.llp.HL7Reader;
9 import ca.uhn.hl7v2.llp.HL7Writer;
10 import ca.uhn.hl7v2.llp.LLPException;
11 import ca.uhn.hl7v2.llp.LowerLayerProtocol;
12
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.io.OutputStream;
16 import java.nio.charset.Charset;
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 public class Hl7OverHttpLowerLayerProtocol extends LowerLayerProtocol {
32
33 private final ServerRoleEnum myRole;
34 private IAuthorizationClientCallback myAuthorizationClientCallback;
35 private IAuthorizationServerCallback myAuthorizationServerCallback;
36 private HohLlpReader myNextReader;
37 private HohLlpWriter myNextWriter;
38 private ISigner mySigner;
39 private String myUriPath = "/";
40 private Charset myPreferredCharset;
41 private String myHost;
42
43 public Hl7OverHttpLowerLayerProtocol(ServerRoleEnum theRole) {
44 myRole = theRole;
45
46 if (myRole == null) {
47 throw new NullPointerException("Role can not be null");
48 }
49 }
50
51
52
53
54 IAuthorizationClientCallback getAuthorizationClientCallback() {
55 return myAuthorizationClientCallback;
56 }
57
58
59
60
61 IAuthorizationServerCallback getAuthorizationServerCallback() {
62 return myAuthorizationServerCallback;
63 }
64
65
66
67
68 @Override
69 public HL7Reader getReader(InputStream theArg0) throws LLPException {
70 if (myNextReader == null && myNextWriter != null) {
71 myNextWriter = null;
72 throw new LLPException("Hl7OverHttpLowerLayerProtocol can not be used with a multi socket implementation");
73 }
74 prepareReadersIfNeeded();
75 HohLlpReader retVal = myNextReader;
76 try {
77 retVal.setInputStream(theArg0);
78 } catch (IOException e) {
79 throw new LLPException("Failed to set stream: " + e.getMessage(), e);
80 }
81
82 myNextReader = null;
83 return retVal;
84 }
85
86
87
88
89 public ServerRoleEnum getRole() {
90 return myRole;
91 }
92
93
94
95
96
97 ISigner getSigner() {
98 return mySigner;
99 }
100
101
102
103
104 public void setSigner(ISigner theSigner) {
105 mySigner = theSigner;
106 }
107
108
109
110
111
112 public String getUriPath() {
113 return myUriPath;
114 }
115
116
117
118
119
120
121
122
123 public void setUriPath(String theUriPath) {
124 myUriPath = theUriPath;
125 }
126
127 public String getHost() {
128 return myHost;
129 }
130
131 public void setHost(String thehost) {
132 myHost = thehost;
133 }
134
135
136
137
138 @Override
139 public HL7Writer getWriter(OutputStream theArg0) throws LLPException {
140 if (myNextReader != null && myNextWriter == null) {
141 myNextReader = null;
142 throw new LLPException("Hl7OverHttpLowerLayerProtocol can not be used with a multi socket implementation");
143 }
144 prepareReadersIfNeeded();
145 HohLlpWriter retVal = myNextWriter;
146 retVal.setPreferredCharset(myPreferredCharset);
147 try {
148 retVal.setOutputStream(theArg0);
149 } catch (IOException e) {
150 throw new LLPException("Failed to set stream: " + e.getMessage(), e);
151 }
152
153 myNextWriter = null;
154 return retVal;
155 }
156
157 private void prepareReadersIfNeeded() {
158 if (myNextReader == null && myNextWriter == null) {
159 myNextReader = new HohLlpReader(this);
160 myNextWriter = new HohLlpWriter(this);
161 myNextReader.setWriter(myNextWriter);
162 }
163 }
164
165
166
167
168
169 public void setAuthorizationCallback(IAuthorizationClientCallback theAuthorizationClientCallback) {
170 if (myRole == ServerRoleEnum.SERVER) {
171 throw new IllegalStateException("This LLP implementation is in CLIENT mode, so it can not use an authorization callback");
172 }
173 myAuthorizationClientCallback = theAuthorizationClientCallback;
174 }
175
176
177
178
179
180 public void setAuthorizationCallback(IAuthorizationServerCallback theAuthorizationCallback) {
181 if (myRole == ServerRoleEnum.CLIENT) {
182 throw new IllegalStateException("This LLP implementation is in CLIENT mode, so it can not use an authorization callback");
183 }
184 myAuthorizationServerCallback = theAuthorizationCallback;
185 }
186
187
188
189
190
191
192
193
194
195 public void setPreferredCharset(Charset thePreferredCharset) {
196 myPreferredCharset = thePreferredCharset;
197 }
198 }