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