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