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 private String myUriPath = "/";
40 private Charset myPreferredCharset;
41
42 public Hl7OverHttpLowerLayerProtocol(ServerRoleEnum theRole) {
43 myRole = theRole;
44
45 if (myRole == null) {
46 throw new NullPointerException("Role can not be null");
47 }
48 }
49
50
51
52 IAuthorizationClientCallback getAuthorizationClientCallback() {
53 return myAuthorizationClientCallback;
54 }
55
56
57
58
59 IAuthorizationServerCallback getAuthorizationServerCallback() {
60 return myAuthorizationServerCallback;
61 }
62
63
64
65
66 @Override
67 public HL7Reader getReader(InputStream theArg0) throws LLPException {
68 if (myNextReader == null && myNextWriter != null) {
69 myNextWriter = null;
70 throw new LLPException("Hl7OverHttpLowerLayerProtocol can not be used with a multi socket implementation");
71 }
72 prepareReadersIfNeeded();
73 HohLlpReader retVal = myNextReader;
74 try {
75 retVal.setInputStream(theArg0);
76 } catch (IOException e) {
77 throw new LLPException("Failed to set stream: " + e.getMessage(), e);
78 }
79
80 myNextReader = null;
81 return retVal;
82 }
83
84
85
86
87 public ServerRoleEnum getRole() {
88 return myRole;
89 }
90
91
92
93
94
95 ISigner getSigner() {
96 return mySigner;
97 }
98
99
100
101
102
103 public String getUriPath() {
104 return myUriPath;
105 }
106
107
108
109
110 @Override
111 public HL7Writer getWriter(OutputStream theArg0) throws LLPException {
112 if (myNextReader != null && myNextWriter == null) {
113 myNextReader = null;
114 throw new LLPException("Hl7OverHttpLowerLayerProtocol can not be used with a multi socket implementation");
115 }
116 prepareReadersIfNeeded();
117 HohLlpWriter retVal = myNextWriter;
118 retVal.setPreferredCharset(myPreferredCharset);
119 try {
120 retVal.setOutputStream(theArg0);
121 } catch (IOException e) {
122 throw new LLPException("Failed to set stream: " + e.getMessage(), e);
123 }
124
125 myNextWriter = null;
126 return retVal;
127 }
128
129 private void prepareReadersIfNeeded() {
130 if (myNextReader == null && myNextWriter == null) {
131 myNextReader = new HohLlpReader(this);
132 myNextWriter = new HohLlpWriter(this);
133 myNextReader.setWriter(myNextWriter);
134 }
135 }
136
137
138
139
140
141 public void setAuthorizationCallback(IAuthorizationClientCallback theAuthorizationClientCallback) {
142 if (myRole == ServerRoleEnum.SERVER) {
143 throw new IllegalStateException("This LLP implementation is in CLIENT mode, so it can not use an authorization callback");
144 }
145 myAuthorizationClientCallback = theAuthorizationClientCallback;
146 }
147
148
149
150
151
152 public void setAuthorizationCallback(IAuthorizationServerCallback theAuthorizationCallback) {
153 if (myRole == ServerRoleEnum.CLIENT) {
154 throw new IllegalStateException("This LLP implementation is in CLIENT mode, so it can not use an authorization callback");
155 }
156 myAuthorizationServerCallback = theAuthorizationCallback;
157 }
158
159
160
161
162 public void setSigner(ISigner theSigner) {
163 mySigner = theSigner;
164 }
165
166
167
168
169
170
171
172
173 public void setUriPath(String theUriPath) {
174 myUriPath = theUriPath;
175 }
176
177
178
179
180
181
182
183
184
185 public void setPreferredCharset(Charset thePreferredCharset) {
186 myPreferredCharset = thePreferredCharset;
187 }
188
189 }