KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > component > html > util > ExtensionsResponseWrapper


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.component.html.util;
17
18 import java.io.ByteArrayInputStream JavaDoc;
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.OutputStreamWriter JavaDoc;
22 import java.io.PrintWriter JavaDoc;
23 import java.io.UnsupportedEncodingException JavaDoc;
24 import java.nio.charset.Charset JavaDoc;
25
26 import javax.servlet.ServletOutputStream JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28 import javax.servlet.http.HttpServletResponseWrapper JavaDoc;
29
30 import org.xml.sax.InputSource JavaDoc;
31
32 /**
33  * @author Sylvain Vieujot (latest modification by $Author: svieujot $)
34  * @version $Revision: 1.6 $ $Date: 2005/03/08 19:08:14 $
35  * $Log: ExtensionsResponseWrapper.java,v $
36  * Revision 1.6 2005/03/08 19:08:14 svieujot
37  * Fix some i18n issue (Marek Sikl comment on MYFACES-79)
38  *
39  * Revision 1.5 2004/12/22 01:55:29 svieujot
40  * Fix a buffering problem.
41  *
42  * Revision 1.4 2004/12/02 02:13:15 svieujot
43  * *** empty log message ***
44  *
45  * Revision 1.3 2004/12/02 02:12:32 svieujot
46  * Clean log
47  *
48  * Revision 1.2 2004/12/02 00:26:58 oros
49  * i18n issues
50  *
51  * Revision 1.1 2004/12/01 20:25:10 svieujot
52  * Make the Extensions filter support css and image resources.
53  * Convert the popup calendar to use this new filter.
54  *
55  */

56 public class ExtensionsResponseWrapper extends HttpServletResponseWrapper JavaDoc {
57     private ByteArrayOutputStream JavaDoc stream = null;
58     private PrintWriter JavaDoc printWriter = null;
59
60     public ExtensionsResponseWrapper(HttpServletResponse JavaDoc response){
61         super( response );
62         stream = new ByteArrayOutputStream JavaDoc();
63     }
64
65
66     public byte[] getBytes() {
67         return stream.toByteArray();
68     }
69
70     public String JavaDoc toString(){
71         try{
72             return stream.toString(getCharacterEncoding());
73         }catch(UnsupportedEncodingException JavaDoc e){
74             // an attempt to set an invalid character encoding would have caused this exception before
75
throw new RuntimeException JavaDoc("Response accepted invalid character encoding " + getCharacterEncoding());
76         }
77     }
78     
79     /** This method is used by Tomcat.
80      */

81     public PrintWriter JavaDoc getWriter(){
82         if( printWriter == null ){
83             OutputStreamWriter JavaDoc streamWriter = new OutputStreamWriter JavaDoc(stream, Charset.forName(getCharacterEncoding()));
84             printWriter = new PrintWriter JavaDoc(streamWriter, true);
85             //printWriter = new PrintWriter(stream, true); // autoFlush is true
86
}
87         return printWriter;
88     }
89     
90     /** This method is used by Jetty.
91     */

92     public ServletOutputStream JavaDoc getOutputStream(){
93         return new MyServletOutputStream( stream );
94     }
95     
96     public InputSource JavaDoc getInputSource(){
97         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc( stream.toByteArray() );
98         return new InputSource JavaDoc( bais );
99     }
100
101      /**
102      * Prevent content-length being set as the page might be modified.
103      */

104     public void setContentLength(int contentLength) {
105         // noop
106
}
107     
108     public void flushBuffer() throws IOException JavaDoc{
109         stream.flush();
110     }
111     
112     public void finishResponse() {
113         try {
114             if (printWriter != null) {
115                 printWriter.close();
116             } else {
117                 if (stream != null) {
118                     stream.close();
119                 }
120             }
121         } catch (IOException JavaDoc e) {
122             e.printStackTrace();
123         }
124     }
125     
126     /** Used in the <code>getOutputStream()</code> method.
127      */

128     private class MyServletOutputStream extends ServletOutputStream JavaDoc {
129         private ByteArrayOutputStream JavaDoc outputStream;
130         
131         public MyServletOutputStream(ByteArrayOutputStream JavaDoc outputStream){
132             this.outputStream = outputStream;
133         }
134         
135         public void write(int b){
136             outputStream.write( b );
137         }
138         
139         public void write(byte[] bytes) throws IOException JavaDoc{
140             outputStream.write( bytes );
141         }
142         
143         public void write(byte[] bytes, int off, int len){
144             outputStream.write(bytes, off, len);
145         }
146     }
147 }
Popular Tags