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  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   * @author Werner Punz (latest modification by $Author: lu4242 $)
36   * @author Jakob Korherr
37   * @version $Revision: 1151650 $ $Date: 2011-07-27 22:14:17 +0000 (Wed, 27 Jul 2011) $
38   */
39  public class ServletResponseSwitch extends ServletResponseWrapper implements ResponseSwitch
40  {
41  
42      private PrintWriter _switchableWriter;
43      private SwitchableOutputStream _switchableOutputStream;
44      private boolean _enabled = true;
45  
46      public ServletResponseSwitch(ServletResponse response)
47      {
48          super(response);
49      }
50  
51      /**
52       * Enables or disables the Response's Writer and OutputStream.
53       * @param enabled
54       */
55      public void setEnabled(boolean enabled)
56      {
57          _enabled = enabled;
58      }
59  
60      /**
61       * Are the Response's Writer and OutputStream currently enabled?
62       * @return
63       */
64      public boolean isEnabled()
65      {
66          return _enabled;
67      }
68  
69      @Override
70      public int getBufferSize()
71      {
72          if (isEnabled())
73          {
74              return super.getBufferSize();
75          }
76          return 0;
77      }
78  
79      @Override
80      public boolean isCommitted()
81      {
82          if (isEnabled())
83          {
84              return super.isCommitted();
85          }
86          return false;
87      }
88  
89      @Override
90      public void reset()
91      {
92          if (isEnabled())
93          {
94              super.reset();
95          }
96      }
97  
98      @Override
99      public void resetBuffer()
100     {
101         if (isEnabled())
102         {
103             super.resetBuffer();
104         }
105     }
106     
107     @Override
108     public void flushBuffer() throws IOException
109     {
110         if (isEnabled())
111         {
112             super.flushBuffer();
113         }
114     }
115 
116     @Override
117     public void setResponse(ServletResponse response)
118     {
119         // only change the response if it is not the same object
120         if (response != getResponse())
121         {
122             // our OutputStream and our Writer are not valid for the new response
123             _switchableOutputStream = null;
124             _switchableWriter = null;
125             
126             // set the new response
127             super.setResponse(response);
128         }
129     }
130 
131     @Override
132     public ServletOutputStream getOutputStream() throws IOException
133     {
134         if (_switchableOutputStream == null)
135         {
136             _switchableOutputStream = new SwitchableOutputStream(super.getOutputStream(), this);
137         }
138         return _switchableOutputStream;
139     }
140 
141     @Override
142     public PrintWriter getWriter() throws IOException
143     {
144         if (_switchableWriter == null)
145         {
146             _switchableWriter = new PrintWriter(new SwitchableWriter(super.getWriter(), this));
147         }
148         return _switchableWriter;
149     }
150     
151 }