1 package ca.uhn.hl7v2.hoh.llp;
2
3 import java.io.DataOutputStream;
4 import java.io.IOException;
5 import java.io.OutputStream;
6 import java.nio.charset.Charset;
7
8 import ca.uhn.hl7v2.hoh.api.EncodeException;
9 import ca.uhn.hl7v2.hoh.encoder.AbstractHl7OverHttpEncoder;
10 import ca.uhn.hl7v2.hoh.encoder.Hl7OverHttpRequestEncoder;
11 import ca.uhn.hl7v2.hoh.encoder.Hl7OverHttpResponseEncoder;
12 import ca.uhn.hl7v2.hoh.util.ServerRoleEnum;
13 import ca.uhn.hl7v2.llp.HL7Writer;
14 import ca.uhn.hl7v2.llp.LLPException;
15
16 class HohLlpWriter implements HL7Writer {
17
18 private OutputStream myOutputStream;
19 private Charset myPreferredCharset;
20 private Hl7OverHttpLowerLayerProtocol myProtocol;
21 private Charset myCharsetForNextMessage;
22
23
24
25
26 public HohLlpWriter(Hl7OverHttpLowerLayerProtocol theProtocol) {
27 myProtocol = theProtocol;
28 }
29
30
31
32
33 public void close() throws IOException {
34 myOutputStream.close();
35 }
36
37 OutputStream getOutputStream() {
38 return myOutputStream;
39 }
40
41
42
43
44 public Charset getPreferredCharset() {
45 return myPreferredCharset;
46 }
47
48
49
50
51 public void setOutputStream(OutputStream theOutputStream) throws IOException {
52 myOutputStream = theOutputStream;
53 }
54
55
56
57
58
59 public void setPreferredCharset(Charset thePreferredCharset) {
60 myPreferredCharset = thePreferredCharset;
61 }
62
63
64
65
66 public void writeMessage(String theRawMessage) throws LLPException, IOException {
67
68 AbstractHl7OverHttpEncoder e;
69 if (myProtocol.getRole() == ServerRoleEnum.CLIENT) {
70 e = new Hl7OverHttpRequestEncoder();
71 if (myProtocol.getAuthorizationClientCallback() != null) {
72 e.setUsername(myProtocol.getAuthorizationClientCallback().provideUsername(myProtocol.getUriPath()));
73 e.setPassword(myProtocol.getAuthorizationClientCallback().providePassword(myProtocol.getUriPath()));
74 }
75 } else {
76 e = new Hl7OverHttpResponseEncoder();
77 }
78
79 if (myProtocol.getRole() == ServerRoleEnum.CLIENT) {
80 e.setSigner(myProtocol.getSigner());
81 }
82
83 e.setMessage(theRawMessage);
84 if (myCharsetForNextMessage != null) {
85 e.setCharset(myCharsetForNextMessage);
86 myCharsetForNextMessage = null;
87 } else if (getPreferredCharset() != null) {
88 e.setCharset(getPreferredCharset());
89 }
90
91 e.setPath(myProtocol.getUriPath());
92 DataOutputStream dos = new DataOutputStream(myOutputStream);
93 try {
94 e.encodeToOutputStream(dos);
95 } catch (EncodeException e1) {
96 throw new LLPException("Failed to encode message", e1);
97 }
98
99 dos.flush();
100
101 }
102
103 void setCharsetForNextMessage(Charset theCharset) {
104 myCharsetForNextMessage = theCharset;
105 }
106
107 }