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