Coverage Report - org.apache.myfaces.shared.view.HttpServletResponseSwitch
 
Classes in this File Line Coverage Branch Coverage Complexity
HttpServletResponseSwitch
0%
0/32
0%
0/18
2
 
 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.shared.view;
 20  
 
 21  
 import java.io.IOException;
 22  
 import java.io.PrintWriter;
 23  
 
 24  
 import javax.servlet.ServletOutputStream;
 25  
 import javax.servlet.ServletResponse;
 26  
 import javax.servlet.http.HttpServletResponse;
 27  
 import javax.servlet.http.HttpServletResponseWrapper;
 28  
 
 29  
 /**
 30  
  * Implementation of a switching response wrapper to turn 
 31  
  * output on and off according to the JSF spec 2.0.
 32  
  * <p/>
 33  
  * Implemented as HttpServletResponseWrapper,
 34  
  * so that the switching does not interfere with methods that
 35  
  * expect a HttpServletResponse when invoking ExternalContext.getResponse().
 36  
  */
 37  
 public class HttpServletResponseSwitch extends HttpServletResponseWrapper implements ResponseSwitch
 38  
 {
 39  
     private PrintWriter _switchableWriter;
 40  
     private SwitchableOutputStream _switchableOutputStream;
 41  0
     private boolean _enabled = true;
 42  
 
 43  
     public HttpServletResponseSwitch(HttpServletResponse response)
 44  
     {
 45  0
         super(response);
 46  0
     }
 47  
 
 48  
     /**
 49  
      * Enables or disables the Response's Writer and OutputStream.
 50  
      * @param enabled
 51  
      */
 52  
     public void setEnabled(boolean enabled)
 53  
     {
 54  0
         _enabled = enabled;
 55  0
     }
 56  
 
 57  
     /**
 58  
      * Are the Response's Writer and OutputStream currently enabled?
 59  
      * @return
 60  
      */
 61  
     public boolean isEnabled()
 62  
     {
 63  0
         return _enabled;
 64  
     }
 65  
 
 66  
 
 67  
     @Override
 68  
     public int getBufferSize()
 69  
     {
 70  0
         if (isEnabled())
 71  
         {
 72  0
             return super.getBufferSize();
 73  
         }
 74  0
         return 0;
 75  
     }
 76  
 
 77  
     @Override
 78  
     public boolean isCommitted()
 79  
     {
 80  0
         if (isEnabled())
 81  
         {
 82  0
             return super.isCommitted();
 83  
         }
 84  0
         return false;
 85  
     }
 86  
 
 87  
     @Override
 88  
     public void reset()
 89  
     {
 90  0
         if (isEnabled())
 91  
         {
 92  0
             super.reset();
 93  
         }
 94  0
     }
 95  
 
 96  
     @Override
 97  
     public void resetBuffer()
 98  
     {
 99  0
         if (isEnabled())
 100  
         {
 101  0
             super.resetBuffer();
 102  
         }
 103  0
     }
 104  
     
 105  
     @Override
 106  
     public void flushBuffer() throws IOException
 107  
     {
 108  0
         if (isEnabled())
 109  
         {
 110  0
             super.flushBuffer();
 111  
         }
 112  0
     }
 113  
 
 114  
     @Override
 115  
     public void setResponse(ServletResponse response)
 116  
     {
 117  
         // only change the response if it is not the same object
 118  0
         if (response instanceof HttpServletResponse && response != getResponse())
 119  
         {
 120  
             // our OutputStream and our Writer are not valid for the new response
 121  0
             _switchableOutputStream = null;
 122  0
             _switchableWriter = null;
 123  
             
 124  
             // set the new response
 125  0
             super.setResponse(response);
 126  
         }
 127  0
     }
 128  
 
 129  
     @Override
 130  
     public ServletOutputStream getOutputStream() throws IOException
 131  
     {
 132  0
         if (_switchableOutputStream == null)
 133  
         {
 134  0
             _switchableOutputStream = new SwitchableOutputStream(super.getOutputStream(), this);
 135  
         }
 136  0
         return _switchableOutputStream;
 137  
     }
 138  
 
 139  
     @Override
 140  
     public PrintWriter getWriter() throws IOException
 141  
     {
 142  0
         if (_switchableWriter == null)
 143  
         {
 144  0
             _switchableWriter = new PrintWriter(new SwitchableWriter(super.getWriter(), this));
 145  
         }
 146  0
         return _switchableWriter;
 147  
     }
 148  
     
 149  
 }