1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.application.jsp; |
20 | |
|
21 | |
import org.apache.myfaces.shared.view.ViewResponseWrapper; |
22 | |
|
23 | |
import javax.servlet.ServletOutputStream; |
24 | |
import javax.servlet.http.HttpServletResponse; |
25 | |
import javax.servlet.http.HttpServletResponseWrapper; |
26 | |
import java.io.ByteArrayOutputStream; |
27 | |
import java.io.CharArrayWriter; |
28 | |
import java.io.IOException; |
29 | |
import java.io.PrintWriter; |
30 | |
import java.io.Writer; |
31 | |
import java.nio.ByteBuffer; |
32 | |
import java.nio.CharBuffer; |
33 | |
import java.nio.charset.Charset; |
34 | |
import java.nio.charset.CharsetDecoder; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public class ServletViewResponseWrapper extends HttpServletResponseWrapper implements ViewResponseWrapper |
41 | |
{ |
42 | |
private PrintWriter _writer; |
43 | |
private CharArrayWriter _charArrayWriter; |
44 | 0 | private int _status = HttpServletResponse.SC_OK; |
45 | |
private WrappedServletOutputStream _byteArrayWriter; |
46 | |
|
47 | |
public ServletViewResponseWrapper(HttpServletResponse httpServletResponse) |
48 | |
{ |
49 | 0 | super(httpServletResponse); |
50 | 0 | } |
51 | |
|
52 | |
@Override |
53 | |
public void sendError(int status) throws IOException |
54 | |
{ |
55 | 0 | super.sendError(status); |
56 | 0 | _status = status; |
57 | 0 | } |
58 | |
|
59 | |
@Override |
60 | |
public void sendError(int status, String errorMessage) throws IOException |
61 | |
{ |
62 | 0 | super.sendError(status, errorMessage); |
63 | 0 | _status = status; |
64 | 0 | } |
65 | |
|
66 | |
@Override |
67 | |
public void setStatus(int status) |
68 | |
{ |
69 | 0 | super.setStatus(status); |
70 | 0 | _status = status; |
71 | 0 | } |
72 | |
|
73 | |
@Override |
74 | |
public void setStatus(int status, String errorMessage) |
75 | |
{ |
76 | 0 | super.setStatus(status, errorMessage); |
77 | 0 | _status = status; |
78 | 0 | } |
79 | |
|
80 | |
public int getStatus() |
81 | |
{ |
82 | 0 | return _status; |
83 | |
} |
84 | |
|
85 | |
public void flushToWrappedResponse() throws IOException |
86 | |
{ |
87 | 0 | if (_charArrayWriter != null) |
88 | |
{ |
89 | 0 | _charArrayWriter.writeTo(getResponse().getWriter()); |
90 | 0 | _charArrayWriter.reset(); |
91 | 0 | _writer.flush(); |
92 | |
} |
93 | 0 | else if (_byteArrayWriter != null) |
94 | |
{ |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
try |
103 | |
{ |
104 | 0 | _byteArrayWriter.writeTo(getResponse().getWriter(), getResponse().getCharacterEncoding()); |
105 | |
} |
106 | 0 | catch (IllegalStateException e) |
107 | |
{ |
108 | 0 | getResponse().getOutputStream().write(_byteArrayWriter.toByteArray()); |
109 | 0 | } |
110 | 0 | _byteArrayWriter.reset(); |
111 | 0 | _byteArrayWriter.flush(); |
112 | |
} |
113 | 0 | } |
114 | |
|
115 | |
public void flushToWriter(Writer writer,String encoding) throws IOException |
116 | |
{ |
117 | 0 | if (_charArrayWriter != null) |
118 | |
{ |
119 | 0 | _charArrayWriter.writeTo(writer); |
120 | 0 | _charArrayWriter.reset(); |
121 | 0 | _writer.flush(); |
122 | |
} |
123 | 0 | else if (_byteArrayWriter != null) |
124 | |
{ |
125 | 0 | _byteArrayWriter.writeTo(writer,encoding); |
126 | 0 | _byteArrayWriter.reset(); |
127 | 0 | _byteArrayWriter.flush(); |
128 | |
} |
129 | 0 | writer.flush(); |
130 | 0 | } |
131 | |
|
132 | |
@Override |
133 | |
public ServletOutputStream getOutputStream() throws IOException |
134 | |
{ |
135 | 0 | if (_charArrayWriter != null) |
136 | |
{ |
137 | 0 | throw new IllegalStateException(); |
138 | |
} |
139 | 0 | if (_byteArrayWriter == null) |
140 | |
{ |
141 | 0 | _byteArrayWriter = new WrappedServletOutputStream(); |
142 | |
} |
143 | 0 | return _byteArrayWriter; |
144 | |
} |
145 | |
|
146 | |
@Override |
147 | |
public PrintWriter getWriter() throws IOException |
148 | |
{ |
149 | 0 | if (_byteArrayWriter != null) |
150 | |
{ |
151 | 0 | throw new IllegalStateException(); |
152 | |
} |
153 | 0 | if (_writer == null) |
154 | |
{ |
155 | 0 | _charArrayWriter = new CharArrayWriter(4096); |
156 | 0 | _writer = new PrintWriter(_charArrayWriter); |
157 | |
} |
158 | 0 | return _writer; |
159 | |
} |
160 | |
|
161 | |
@Override |
162 | |
public void reset() |
163 | |
{ |
164 | 0 | if (_charArrayWriter != null) |
165 | |
{ |
166 | 0 | _charArrayWriter.reset(); |
167 | |
} |
168 | 0 | } |
169 | |
|
170 | |
@Override |
171 | |
public String toString() |
172 | |
{ |
173 | 0 | if (_charArrayWriter != null) |
174 | |
{ |
175 | 0 | return _charArrayWriter.toString(); |
176 | |
} |
177 | 0 | return null; |
178 | |
} |
179 | |
|
180 | 0 | static class WrappedServletOutputStream extends ServletOutputStream |
181 | |
{ |
182 | |
private WrappedByteArrayOutputStream _byteArrayOutputStream; |
183 | |
|
184 | |
|
185 | |
public WrappedServletOutputStream() |
186 | 0 | { |
187 | 0 | _byteArrayOutputStream = new WrappedByteArrayOutputStream(1024); |
188 | 0 | } |
189 | |
|
190 | |
@Override |
191 | |
public void write(int i) throws IOException |
192 | |
{ |
193 | 0 | _byteArrayOutputStream.write(i); |
194 | 0 | } |
195 | |
|
196 | |
public byte[] toByteArray() |
197 | |
{ |
198 | 0 | return _byteArrayOutputStream.toByteArray(); |
199 | |
} |
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
private void writeTo(Writer out, String encoding) throws IOException |
210 | |
{ |
211 | |
|
212 | |
|
213 | 0 | Charset charset = (encoding == null) ? |
214 | |
Charset.defaultCharset() : Charset.forName(encoding); |
215 | 0 | CharsetDecoder decoder = charset.newDecoder(); |
216 | 0 | CharBuffer decodedBuffer = decoder.decode( |
217 | |
ByteBuffer.wrap(_byteArrayOutputStream.getInnerArray(), |
218 | |
0,_byteArrayOutputStream.getInnerCount())); |
219 | 0 | if (decodedBuffer.hasArray()) |
220 | |
{ |
221 | 0 | out.write(decodedBuffer.array()); |
222 | |
} |
223 | 0 | } |
224 | |
|
225 | |
public void reset() |
226 | |
{ |
227 | 0 | _byteArrayOutputStream.reset(); |
228 | 0 | } |
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | 0 | static class WrappedByteArrayOutputStream extends ByteArrayOutputStream |
237 | |
{ |
238 | |
|
239 | |
public WrappedByteArrayOutputStream() |
240 | |
{ |
241 | 0 | super(); |
242 | 0 | } |
243 | |
|
244 | |
public WrappedByteArrayOutputStream(int size) |
245 | |
{ |
246 | 0 | super(size); |
247 | 0 | } |
248 | |
|
249 | |
private byte[] getInnerArray() |
250 | |
{ |
251 | 0 | return buf; |
252 | |
} |
253 | |
|
254 | |
private int getInnerCount() |
255 | |
{ |
256 | 0 | return count; |
257 | |
} |
258 | |
} |
259 | |
|
260 | |
} |
261 | |
} |