Coverage Report - org.apache.myfaces.application.jsp.ServletViewResponseWrapper
 
Classes in this File Line Coverage Branch Coverage Complexity
ServletViewResponseWrapper
0%
0/55
0%
0/20
1.762
ServletViewResponseWrapper$WrappedServletOutputStream
0%
0/15
0%
0/4
1.762
ServletViewResponseWrapper$WrappedServletOutputStream$WrappedByteArrayOutputStream
0%
0/7
N/A
1.762
 
 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 org.apache.myfaces.shared.view.ViewResponseWrapper;
 22  
 
 23  
 import javax.servlet.ServletOutputStream;
 24  
 import javax.servlet.http.HttpServletResponse;
 25  
 import javax.servlet.http.HttpServletResponseWrapper;
 26  
 import java.io.ByteArrayOutputStream;
 27  
 import java.io.CharArrayWriter;
 28  
 import java.io.IOException;
 29  
 import java.io.PrintWriter;
 30  
 import java.io.Writer;
 31  
 import java.nio.ByteBuffer;
 32  
 import java.nio.CharBuffer;
 33  
 import java.nio.charset.Charset;
 34  
 import java.nio.charset.CharsetDecoder;
 35  
 
 36  
 /**
 37  
  * @author Bruno Aranda (latest modification by $Author$)
 38  
  * @version $Revision$ $Date$
 39  
  */
 40  
 public class ServletViewResponseWrapper extends HttpServletResponseWrapper implements ViewResponseWrapper
 41  
 {
 42  
     private PrintWriter _writer;
 43  
     private CharArrayWriter _charArrayWriter;
 44  0
     private int _status = HttpServletResponse.SC_OK;
 45  
     private WrappedServletOutputStream _byteArrayWriter;
 46  
 
 47  
     public ServletViewResponseWrapper(HttpServletResponse httpServletResponse)
 48  
     {
 49  0
         super(httpServletResponse);
 50  0
     }
 51  
 
 52  
     @Override
 53  
     public void sendError(int status) throws IOException
 54  
     {
 55  0
         super.sendError(status);
 56  0
         _status = status;
 57  0
     }
 58  
 
 59  
     @Override
 60  
     public void sendError(int status, String errorMessage) throws IOException
 61  
     {
 62  0
         super.sendError(status, errorMessage);
 63  0
         _status = status;
 64  0
     }
 65  
 
 66  
     @Override
 67  
     public void setStatus(int status)
 68  
     {
 69  0
         super.setStatus(status);
 70  0
         _status = status;
 71  0
     }
 72  
 
 73  
     @Override
 74  
     public void setStatus(int status, String errorMessage)
 75  
     {
 76  0
         super.setStatus(status, errorMessage);
 77  0
         _status = status;
 78  0
     }
 79  
 
 80  
     public int getStatus()
 81  
     {
 82  0
         return _status;
 83  
     }
 84  
 
 85  
     public void flushToWrappedResponse() throws IOException
 86  
     {
 87  0
         if (_charArrayWriter != null)
 88  
         {
 89  0
             _charArrayWriter.writeTo(getResponse().getWriter());
 90  0
             _charArrayWriter.reset();
 91  0
             _writer.flush();
 92  
         }
 93  0
         else if (_byteArrayWriter != null)
 94  
         {
 95  
             // MYFACES-1955 cannot call getWriter() after getOutputStream()
 96  
             // _byteArrayWriter is not null only if getOutputStream() was called
 97  
             // before. This method is called from f:view to flush data before tag
 98  
             // start, or if an error page is flushed after dispatch.
 99  
             // A resource inside /faces/* (see MYFACES-1815) is handled on flushToWriter.
 100  
             // If response.getOuputStream() was called before, an IllegalStateException
 101  
             // is raised on response.getWriter(), so we should try through stream.
 102  
             try
 103  
             {
 104  0
                 _byteArrayWriter.writeTo(getResponse().getWriter(), getResponse().getCharacterEncoding());
 105  
             }
 106  0
             catch (IllegalStateException e)
 107  
             {
 108  0
             getResponse().getOutputStream().write(_byteArrayWriter.toByteArray());
 109  0
             }
 110  0
             _byteArrayWriter.reset();
 111  0
             _byteArrayWriter.flush();
 112  
         }
 113  0
     }
 114  
 
 115  
     public void flushToWriter(Writer writer,String encoding) throws IOException
 116  
     {
 117  0
         if (_charArrayWriter != null)
 118  
         {
 119  0
             _charArrayWriter.writeTo(writer);
 120  0
             _charArrayWriter.reset();
 121  0
             _writer.flush();
 122  
         }
 123  0
         else if (_byteArrayWriter != null)
 124  
         {
 125  0
             _byteArrayWriter.writeTo(writer,encoding);
 126  0
             _byteArrayWriter.reset();
 127  0
             _byteArrayWriter.flush();
 128  
         }
 129  0
         writer.flush();
 130  0
     }
 131  
 
 132  
     @Override
 133  
     public ServletOutputStream getOutputStream() throws IOException
 134  
     {
 135  0
         if (_charArrayWriter != null)
 136  
         {
 137  0
             throw new IllegalStateException();
 138  
         }
 139  0
         if (_byteArrayWriter == null)
 140  
         {
 141  0
             _byteArrayWriter = new WrappedServletOutputStream();
 142  
         }
 143  0
         return _byteArrayWriter;
 144  
     }
 145  
 
 146  
     @Override
 147  
     public PrintWriter getWriter() throws IOException
 148  
     {
 149  0
         if (_byteArrayWriter != null)
 150  
         {
 151  0
             throw new IllegalStateException();
 152  
         }
 153  0
         if (_writer == null)
 154  
         {
 155  0
             _charArrayWriter = new CharArrayWriter(4096);
 156  0
             _writer = new PrintWriter(_charArrayWriter);
 157  
         }
 158  0
         return _writer;
 159  
     }
 160  
 
 161  
     @Override
 162  
     public void reset()
 163  
     {
 164  0
         if (_charArrayWriter != null)
 165  
         {
 166  0
             _charArrayWriter.reset();
 167  
         }
 168  0
     }
 169  
 
 170  
     @Override
 171  
     public String toString()
 172  
     {
 173  0
         if (_charArrayWriter != null)
 174  
         {
 175  0
             return _charArrayWriter.toString();
 176  
         }
 177  0
         return null;
 178  
     }
 179  
 
 180  0
     static class WrappedServletOutputStream extends ServletOutputStream
 181  
     {
 182  
         private WrappedByteArrayOutputStream _byteArrayOutputStream;
 183  
 
 184  
 
 185  
         public WrappedServletOutputStream()
 186  0
         {
 187  0
             _byteArrayOutputStream = new WrappedByteArrayOutputStream(1024);
 188  0
         }
 189  
 
 190  
         @Override
 191  
         public void write(int i) throws IOException
 192  
         {
 193  0
             _byteArrayOutputStream.write(i);
 194  0
         }
 195  
 
 196  
         public byte[] toByteArray()
 197  
         {
 198  0
             return _byteArrayOutputStream.toByteArray();
 199  
         }
 200  
         
 201  
         /**
 202  
          * Write the data of this stream to the writer, using
 203  
          * the charset encoding supplied or if null the default charset.
 204  
          * 
 205  
          * @param out
 206  
          * @param encoding
 207  
          * @throws IOException
 208  
          */
 209  
         private void writeTo(Writer out, String encoding) throws IOException
 210  
         {
 211  
             //Get the charset based on the encoding or return the default if 
 212  
             //encoding == null
 213  0
             Charset charset = (encoding == null) ? 
 214  
                     Charset.defaultCharset() : Charset.forName(encoding);
 215  0
             CharsetDecoder decoder = charset.newDecoder();
 216  0
             CharBuffer decodedBuffer = decoder.decode(
 217  
                     ByteBuffer.wrap(_byteArrayOutputStream.getInnerArray(),
 218  
                             0,_byteArrayOutputStream.getInnerCount()));
 219  0
             if (decodedBuffer.hasArray())
 220  
             {
 221  0
                 out.write(decodedBuffer.array());
 222  
             }
 223  0
         }
 224  
 
 225  
         public void reset()
 226  
         {
 227  0
             _byteArrayOutputStream.reset();
 228  0
         }
 229  
         
 230  
         /**
 231  
          * This Wrapper is used to provide additional methods to 
 232  
          * get the buf and count variables, to use it to decode
 233  
          * in WrappedServletOutputStream.writeTo and avoid buffer
 234  
          * duplication.
 235  
          */
 236  0
         static class WrappedByteArrayOutputStream extends ByteArrayOutputStream
 237  
         {
 238  
             
 239  
             public WrappedByteArrayOutputStream()
 240  
             {
 241  0
                 super();
 242  0
             }
 243  
             
 244  
             public WrappedByteArrayOutputStream(int size)
 245  
             {
 246  0
                 super(size);                
 247  0
             }
 248  
             
 249  
             private byte[] getInnerArray()
 250  
             {
 251  0
                 return buf; 
 252  
             }
 253  
             
 254  
             private int getInnerCount()
 255  
             {
 256  0
                 return count;
 257  
             }
 258  
         }
 259  
 
 260  
     }
 261  
 }