1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.metricshub.wbem.sblim.cimclient.internal.http;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 import java.io.ByteArrayOutputStream;
51 import java.io.IOException;
52 import java.io.OutputStream;
53 import java.util.logging.Level;
54 import org.metricshub.wbem.sblim.cimclient.internal.http.io.ASCIIPrintStream;
55 import org.metricshub.wbem.sblim.cimclient.internal.http.io.ChunkedOutputStream;
56 import org.metricshub.wbem.sblim.cimclient.internal.http.io.PersistentOutputStream;
57 import org.metricshub.wbem.sblim.cimclient.internal.logging.LogAndTraceBroker;
58
59
60
61
62
63 public class MessageWriter {
64 HttpHeader iHeader = null;
65
66 HttpServerMethod iMethod = null;
67
68 HttpHeader iTrailer = null;
69
70 boolean iChunked = false;
71
72 boolean iPersistent = false;
73
74 ASCIIPrintStream iRealOS;
75
76 ASCIIPrintStream iClientOS;
77
78 ByteArrayOutputStream iBufferedOS;
79
80
81
82
83
84
85
86
87 public MessageWriter(OutputStream pStream, boolean pPersistent, boolean pChunked) {
88 this.iRealOS = new ASCIIPrintStream(pStream);
89 this.iChunked = pChunked;
90 this.iPersistent = pPersistent;
91 this.iBufferedOS = new ByteArrayOutputStream();
92 if (pChunked) {
93 this.iClientOS =
94 new ASCIIPrintStream(new ChunkedOutputStream(new PersistentOutputStream(this.iBufferedOS, pPersistent), 512));
95 } else {
96 this.iClientOS = new ASCIIPrintStream(new PersistentOutputStream(this.iBufferedOS, pPersistent));
97 }
98 this.iHeader = new HttpHeader();
99 this.iMethod =
100 new HttpServerMethod(HttpConnectionHandler.MAJOR_VERSION, HttpConnectionHandler.MINOR_VERSION, 200, "OK");
101 }
102
103
104
105
106 public void reset() {
107 this.iBufferedOS.reset();
108 }
109
110
111
112
113
114
115
116 public void setHeader(HttpHeader header) {
117 this.iHeader = header;
118 }
119
120
121
122
123
124
125
126 public void setMethod(HttpServerMethod method) {
127 this.iMethod = method;
128 }
129
130
131
132
133
134
135 public HttpHeader getHeader() {
136 return this.iHeader;
137 }
138
139
140
141
142
143
144 public HttpServerMethod getMethod() {
145 return this.iMethod;
146 }
147
148
149
150
151
152
153 public ASCIIPrintStream getOutputStream() {
154 return this.iClientOS;
155 }
156
157
158
159
160
161
162 public void close() throws IOException {
163 this.iMethod.write(this.iRealOS);
164 this.iRealOS.flush();
165 if (!this.iChunked) this.iHeader.removeField("Transfer-Encoding"); else this.iHeader.addField(
166 "Transfer-Encoding",
167 "chunked"
168 );
169 if (this.iPersistent) this.iHeader.addField("Connection", "Keep-iAlive"); else this.iHeader.addField(
170 "Connection",
171 "close"
172 );
173
174 this.iHeader.addField("Content-Type", "application/xml;charset=\"utf-8\"");
175 if (!this.iChunked) this.iHeader.addField("Content-length", Integer.toString(this.iBufferedOS.size()));
176 LogAndTraceBroker.getBroker().trace(Level.FINER, "Indication Response HTTP Headers= " + this.iHeader.toString());
177 this.iHeader.write(this.iRealOS);
178 this.iRealOS.flush();
179 if (this.iChunked) this.iClientOS.close();
180 this.iBufferedOS.writeTo(this.iRealOS);
181 if (this.iChunked && (this.iTrailer != null)) {
182 LogAndTraceBroker
183 .getBroker()
184 .trace(Level.FINER, "Indication Response HTTP Trailer Headers= " + this.iTrailer.toString());
185 this.iTrailer.write(this.iRealOS);
186 }
187 this.iRealOS.flush();
188 }
189
190
191
192
193
194
195
196 public void setTrailer(HttpHeader pTrailer) {
197 this.iTrailer = pTrailer;
198 }
199 }