View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * @author Sylvain Vieujot (latest modification by $Author: skitching $)
33   * @version $Revision: 673833 $ $Date: 2008-07-03 16:58:05 -0500 (Thu, 03 Jul 2008) $
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              // an attempt to set an invalid character encoding would have caused this exception before
59              throw new RuntimeException("Response accepted invalid character encoding " + getCharacterEncoding());
60          }
61      }
62  
63      /** This method is used by Tomcat.
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              //printWriter = new PrintWriter(stream, true); // autoFlush is true
70          }
71          return printWriter;
72      }
73  
74      /** This method is used by Jetty.
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       *  Prevent content-length being set as the page might be modified.
87       */
88      public void setContentLength(int contentLength) {
89          // noop
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     /** Used in the <code>getOutputStream()</code> method.
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 }