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