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.shared.view;
20  
21  import java.io.IOException;
22  
23  import javax.servlet.ServletOutputStream;
24  import javax.servlet.WriteListener;
25  
26  /**
27   * Delegates the standard OutputStream-methods (close, flush, write)
28   * to the given ServletOutputStream, if the ResponseSwitch is enabled
29   */
30  class SwitchableOutputStream extends ServletOutputStream
31  {
32  
33      ServletOutputStream _delegate = null;
34      ResponseSwitch _responseSwitch = null;
35  
36      public SwitchableOutputStream(ServletOutputStream delegate, ResponseSwitch responseSwitch)
37      {
38          _delegate = delegate;
39          _responseSwitch = responseSwitch;
40      }
41  
42      @Override
43      public void close() throws IOException
44      {
45          if (_responseSwitch.isEnabled())
46          {
47              _delegate.close();
48          }
49      }
50  
51      @Override
52      public void flush() throws IOException
53      {
54          if (_responseSwitch.isEnabled())
55          {
56              _delegate.flush();
57          }
58      }
59  
60      @Override
61      public void write(byte[] b, int off, int len) throws IOException
62      {
63          if (_responseSwitch.isEnabled())
64          {
65              _delegate.write(b, off, len);
66          }
67      }
68  
69      @Override
70      public void write(byte[] b) throws IOException
71      {
72          if (_responseSwitch.isEnabled())
73          {
74              _delegate.write(b);
75          }
76      }
77  
78      @Override
79      public void write(int b) throws IOException
80      {
81          if (_responseSwitch.isEnabled())
82          {
83              _delegate.write(b);
84          }
85      }
86  
87      @Override
88      public boolean isReady()
89      {
90          if (_responseSwitch.isEnabled())
91          {
92              return _delegate.isReady();
93          }
94          
95          return true;
96      }
97  
98      @Override
99      public void setWriteListener(WriteListener wl)
100     {
101         if (_responseSwitch.isEnabled())
102         {
103             _delegate.setWriteListener(wl);
104         }
105     }
106     
107 }