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