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