1 package ca.uhn.hl7v2.hoh.llp;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.nio.charset.Charset;
6
7 import ca.uhn.hl7v2.hoh.api.DecodeException;
8 import ca.uhn.hl7v2.hoh.api.IAuthorizationServerCallback;
9 import ca.uhn.hl7v2.hoh.encoder.AbstractHl7OverHttpDecoder;
10 import ca.uhn.hl7v2.hoh.encoder.Hl7OverHttpRequestDecoder;
11 import ca.uhn.hl7v2.hoh.encoder.Hl7OverHttpResponseDecoder;
12 import ca.uhn.hl7v2.hoh.encoder.NoMessageReceivedException;
13 import ca.uhn.hl7v2.hoh.sign.SignatureVerificationException;
14 import ca.uhn.hl7v2.hoh.util.HTTPUtils;
15 import ca.uhn.hl7v2.hoh.util.ServerRoleEnum;
16 import ca.uhn.hl7v2.llp.HL7Reader;
17 import ca.uhn.hl7v2.llp.LLPException;
18
19 class HohLlpReader implements HL7Reader {
20 private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(HohLlpReader.class);
21
22 private InputStream myInputStream;
23 private final Hl7OverHttpLowerLayerProtocol myProtocol;
24 private HohLlpWriter myWriter;
25
26
27
28
29 public HohLlpReader(Hl7OverHttpLowerLayerProtocol theProtocol) {
30 myProtocol = theProtocol;
31 }
32
33
34
35
36 public void close() throws IOException {
37 myInputStream.close();
38 }
39
40 public String getMessage() throws LLPException, IOException {
41 AbstractHl7OverHttpDecoder decoder;
42
43 if (myProtocol.getRole() == ServerRoleEnum.CLIENT) {
44 decoder = new Hl7OverHttpResponseDecoder();
45 } else {
46 Hl7OverHttpRequestDecoder requestDecoder = new Hl7OverHttpRequestDecoder();
47 requestDecoder.setAuthorizationCallback(myProtocol.getAuthorizationServerCallback());
48 decoder = requestDecoder;
49 decoder.setSigner(myProtocol.getSigner());
50 }
51
52
53 try {
54 decoder.readHeadersAndContentsFromInputStreamAndDecode(myInputStream);
55 } catch (DecodeException e) {
56 if (myProtocol.getRole() == ServerRoleEnum.CLIENT) {
57 throw new LLPException("Failed to process response", e);
58 } else {
59 ourLog.info("Failed to read contents", e);
60 HTTPUtils.write400BadRequest(myWriter.getOutputStream(), e.getMessage());
61
62 myInputStream.close();
63 myWriter.getOutputStream().close();
64
65 throw new LLPException("Failed to read message", e);
66 }
67 } catch (NoMessageReceivedException e) {
68 return null;
69 } catch (SignatureVerificationException e) {
70 throw new LLPException("Failed to verify message signature", e);
71 }
72
73 if (myProtocol.getRole() == ServerRoleEnum.SERVER) {
74 Charset charset = decoder.getCharset();
75 myWriter.setCharsetForNextMessage(charset);
76
77 IAuthorizationServerCallback authorizationCallback = myProtocol.getAuthorizationServerCallback();
78 if (authorizationCallback != null) {
79 boolean auth = authorizationCallback.authorize(decoder.getPath(), decoder.getUsername(), decoder.getPassword());
80 if (!auth) {
81 HTTPUtils.write401Unauthorized(myWriter.getOutputStream());
82 throw new LLPException("Authorization at URI[" + decoder.getPath() + "] failed for user[" + decoder.getUsername() + "]");
83 }
84 }
85 }
86
87 return decoder.getMessage();
88
89 }
90
91 @Override
92 public void setInputStream(InputStream theInputStream) throws IOException {
93 myInputStream = theInputStream;
94 }
95
96 void setWriter(HohLlpWriter theWriter) {
97 myWriter = theWriter;
98 }
99
100 }