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