Coverage Report - org.apache.myfaces.application.jsp.ViewResponseWrapper
 
Classes in this File Line Coverage Branch Coverage Complexity
ViewResponseWrapper
0%
0/53
0%
0/20
0
ViewResponseWrapper$WrappedServletOutputStream
0%
0/15
0%
0/4
0
ViewResponseWrapper$WrappedServletOutputStream$WrappedByteArrayOutputStream
0%
0/7
N/A
0
 
 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.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  
  * @author Bruno Aranda (latest modification by $Author: lu4242 $)
 32  
  * @version $Revision: 796388 $ $Date: 2009-07-21 12:10:32 -0500 (Tue, 21 Jul 2009) $
 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  
             // MYFACES-1955 cannot call getWriter() after getOutputStream()
 90  
             // _byteArrayWriter is not null only if getOutputStream() was called
 91  
             // before. This method is called from f:view to flush data before tag
 92  
             // start, or if an error page is flushed after dispatch.
 93  
             // A resource inside /faces/* (see MYFACES-1815) is handled on flushToWriter.
 94  
             // If response.getOuputStream() was called before, an IllegalStateException
 95  
             // is raised on response.getWriter(), so we should try through stream.
 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  
          * Write the data of this stream to the writer, using
 190  
          * the charset encoding supplied or if null the default charset.
 191  
          * 
 192  
          * @param out
 193  
          * @param encoding
 194  
          * @throws IOException
 195  
          */
 196  
         private void writeTo(Writer out, String encoding) throws IOException{
 197  
             //Get the charset based on the encoding or return the default if 
 198  
             //encoding == null
 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  
          * This Wrapper is used to provide additional methods to 
 217  
          * get the buf and count variables, to use it to decode
 218  
          * in WrappedServletOutputStream.writeTo and avoid buffer
 219  
          * duplication.
 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  
 }