1 package org.andromda.cartridges.jsf.portlet.myfaces.tomahawk.support;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.ByteArrayOutputStream;
5 import java.io.IOException;
6 import java.io.OutputStream;
7 import java.io.OutputStreamWriter;
8 import java.io.PrintWriter;
9 import java.io.UnsupportedEncodingException;
10 import java.nio.charset.Charset;
11 import java.util.Locale;
12 import javax.portlet.PortletURL;
13 import javax.portlet.RenderResponse;
14 import javax.servlet.ServletOutputStream;
15 import javax.servlet.http.HttpServletResponse;
16 import javax.servlet.http.HttpServletResponseWrapper;
17 import org.xml.sax.InputSource;
18
19
20
21
22
23 public class ExtensionsResponseWrapper
24 extends HttpServletResponseWrapper
25 implements RenderResponse
26 {
27 private ByteArrayOutputStream stream = null;
28 private PrintWriter printWriter = null;
29 private String contentType;
30
31 private final RenderResponse response;
32
33
34
35
36
37 public ExtensionsResponseWrapper(
38 final HttpServletResponse servletResponse,
39 final RenderResponse response)
40 {
41 super(servletResponse);
42 this.response = response;
43 stream = new ByteArrayOutputStream();
44 }
45
46
47
48
49 public byte[] getBytes()
50 {
51 return stream.toByteArray();
52 }
53
54
55
56
57 @Override
58 public String toString()
59 {
60 try
61 {
62 return stream.toString(getCharacterEncoding());
63 }
64 catch (final UnsupportedEncodingException e)
65 {
66
67
68 throw new RuntimeException("Response accepted invalid character encoding "
69 + getCharacterEncoding());
70 }
71 }
72
73
74
75
76 @Override
77 public PrintWriter getWriter()
78 {
79 if (printWriter == null)
80 {
81 final OutputStreamWriter streamWriter = new OutputStreamWriter(
82 stream, Charset.forName(getCharacterEncoding()));
83 printWriter = new PrintWriter(streamWriter, true);
84 }
85 return printWriter;
86 }
87
88
89
90
91
92
93 @Override
94 public ServletOutputStream getOutputStream() throws IOException
95 {
96 return new MyServletOutputStream(stream);
97 }
98
99
100
101
102 public InputSource getInputSource()
103 {
104 final ByteArrayInputStream bais = new ByteArrayInputStream(stream.toByteArray());
105 return new InputSource(bais);
106 }
107
108
109
110
111 @Override
112 public void setContentLength(final int contentLength)
113 {
114 }
115
116
117
118
119 @Override
120 public void setContentType(final String contentType)
121 {
122 super.setContentType(contentType);
123 this.contentType = contentType;
124 }
125
126
127
128
129 @Override
130 public String getContentType()
131 {
132 return contentType;
133 }
134
135
136
137
138 @Override
139 public void flushBuffer() throws IOException
140 {
141 stream.flush();
142 }
143
144
145
146
147 public void finishResponse()
148 {
149 try
150 {
151 if (printWriter != null)
152 {
153 printWriter.close();
154 }
155 else
156 {
157 if (stream != null)
158 {
159 stream.close();
160 }
161 }
162 }
163 catch (final IOException e)
164 {
165
166 }
167 }
168
169
170
171
172 private class MyServletOutputStream
173 extends ServletOutputStream
174 {
175 private final OutputStream outputStream;
176
177 public MyServletOutputStream(
178 final OutputStream outputStream)
179 {
180 this.outputStream = outputStream;
181 }
182
183 @Override
184 public void write(final int b) throws IOException
185 {
186 outputStream.write(b);
187 }
188
189 @Override
190 public void write(final byte[] bytes) throws IOException
191 {
192 outputStream.write(bytes);
193 }
194
195 @Override
196 public void write(final byte[] bytes, final int off, final int len) throws IOException
197 {
198 outputStream.write(bytes, off, len);
199 }
200 }
201
202
203
204
205 public PortletURL createActionURL()
206 {
207 return response.createActionURL();
208 }
209
210
211
212
213 public PortletURL createRenderURL()
214 {
215 return response.createRenderURL();
216 }
217
218
219
220
221 @Override
222 public int getBufferSize()
223 {
224 return response.getBufferSize();
225 }
226
227
228
229
230 @Override
231 public String getCharacterEncoding()
232 {
233 return response.getCharacterEncoding();
234 }
235
236
237
238
239 @Override
240 public Locale getLocale()
241 {
242 return response.getLocale();
243 }
244
245
246
247
248 public String getNamespace()
249 {
250 return response.getNamespace();
251 }
252
253
254
255
256 public OutputStream getPortletOutputStream() throws IOException
257 {
258 return stream;
259 }
260
261
262
263
264 @Override
265 public boolean isCommitted()
266 {
267 return response.isCommitted();
268 }
269
270
271
272
273 @Override
274 public void reset()
275 {
276 response.reset();
277 }
278
279
280
281
282 @Override
283 public void resetBuffer()
284 {
285 response.resetBuffer();
286 }
287
288
289
290
291 @Override
292 public void setBufferSize(final int bufferSize)
293 {
294 response.setBufferSize(bufferSize);
295 }
296
297
298
299
300 public void setTitle(final String title)
301 {
302 response.setTitle(title);
303 }
304
305
306
307
308 public void addProperty(final String arg0, final String arg1)
309 {
310 response.addProperty(arg0, arg1);
311 }
312
313
314
315
316 @Override
317 public String encodeURL(final String arg0)
318 {
319 return response.encodeURL(arg0);
320 }
321
322
323
324
325 public void setProperty(final String arg0, final String arg1)
326 {
327 response.setProperty(arg0, arg1);
328 }
329 }