1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.webapp.filter;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.xml.sax.InputSource;
24
25 import javax.servlet.ServletOutputStream;
26 import javax.servlet.http.HttpServletResponse;
27 import javax.servlet.http.HttpServletResponseWrapper;
28 import java.io.*;
29 import java.nio.charset.Charset;
30
31
32
33
34
35 public class ExtensionsResponseWrapper extends HttpServletResponseWrapper {
36 private ByteArrayOutputStream stream = null;
37 private PrintWriter printWriter = null;
38 private String contentType;
39 private HttpServletResponse delegate;
40
41 private static final Log log = LogFactory.getLog(ExtensionsResponseWrapper.class);
42
43 public ExtensionsResponseWrapper(HttpServletResponse response){
44 super( response );
45 this.delegate = response;
46 stream = new ByteArrayOutputStream();
47 }
48
49
50 public byte[] getBytes() {
51 return stream.toByteArray();
52 }
53
54 public String toString(){
55 try{
56 return stream.toString(getCharacterEncoding());
57 }catch(UnsupportedEncodingException e){
58
59 throw new RuntimeException("Response accepted invalid character encoding " + getCharacterEncoding());
60 }
61 }
62
63
64
65 public PrintWriter getWriter(){
66 if( printWriter == null ){
67 OutputStreamWriter streamWriter = new OutputStreamWriter(stream, Charset.forName(getCharacterEncoding()));
68 printWriter = new PrintWriter(streamWriter, true);
69
70 }
71 return printWriter;
72 }
73
74
75
76 public ServletOutputStream getOutputStream(){
77 return new MyServletOutputStream( stream );
78 }
79
80 public InputSource getInputSource(){
81 ByteArrayInputStream bais = new ByteArrayInputStream( stream.toByteArray() );
82 return new InputSource( bais );
83 }
84
85
86
87
88 public void setContentLength(int contentLength) {
89
90 }
91
92 public void setContentType(String contentType) {
93 super.setContentType(contentType);
94 this.contentType = contentType;
95 }
96
97 public String getContentType() {
98 return contentType;
99 }
100
101 public void flushBuffer() throws IOException{
102 stream.flush();
103 }
104
105 public void finishResponse() {
106 try {
107 if (printWriter != null) {
108 printWriter.close();
109 } else {
110 if (stream != null) {
111 stream.close();
112 }
113 }
114 } catch (IOException e) {
115 log.error(e.getMessage(),e);
116 }
117 }
118
119 public HttpServletResponse getDelegate() {
120 return delegate;
121 }
122
123
124
125 private class MyServletOutputStream extends ServletOutputStream {
126 private ByteArrayOutputStream outputStream;
127
128 public MyServletOutputStream(ByteArrayOutputStream outputStream){
129 this.outputStream = outputStream;
130 }
131
132 public void write(int b){
133 outputStream.write( b );
134 }
135
136 public void write(byte[] bytes) throws IOException{
137 outputStream.write( bytes );
138 }
139
140 public void write(byte[] bytes, int off, int len){
141 outputStream.write(bytes, off, len);
142 }
143 }
144 }