1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet;
24  
25  import com.liferay.util.servlet.NullServletOutputStream;
26  
27  import java.io.IOException;
28  import java.io.PrintWriter;
29  
30  import java.util.Locale;
31  
32  import javax.portlet.ActionResponse;
33  import javax.portlet.MimeResponse;
34  import javax.portlet.PortletRequest;
35  
36  import javax.servlet.ServletOutputStream;
37  import javax.servlet.http.Cookie;
38  import javax.servlet.http.HttpServletResponse;
39  import javax.servlet.http.HttpServletResponseWrapper;
40  
41  /**
42   * <a href="PortletServletResponse.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public class PortletServletResponse extends HttpServletResponseWrapper {
48  
49      public PortletServletResponse(
50          HttpServletResponse response, PortletResponseImpl portletResponseImpl,
51          boolean include) {
52  
53          super(response);
54  
55          _response = response;
56          _portletResponseImpl = portletResponseImpl;
57          _include = include;
58          _lifecycle = _portletResponseImpl.getLifecycle();
59      }
60  
61      public void addCookie(Cookie cookie) {
62          if (!_include) {
63              _portletResponseImpl.addProperty(cookie);
64          }
65      }
66  
67      public void addDateHeader(String name, long date) {
68          addHeader(name, String.valueOf(date));
69      }
70  
71      public void addHeader(String name, String value) {
72          if (!_include) {
73              if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
74                  _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
75  
76                  HttpServletResponse response =
77                      _portletResponseImpl.getHttpServletResponse();
78  
79                  response.addHeader(name, value);
80              }
81          }
82      }
83  
84      public void addIntHeader(String name, int value) {
85          addHeader(name, String.valueOf(value));
86      }
87  
88      public boolean containsHeader(String name) {
89          return false;
90      }
91  
92      public String encodeRedirectURL(String url) {
93          return null;
94      }
95  
96      public String encodeRedirectUrl(String url) {
97          return null;
98      }
99  
100     public String encodeURL(String url) {
101         return _portletResponseImpl.encodeURL(url);
102     }
103 
104     public String encodeUrl(String url) {
105         return _portletResponseImpl.encodeURL(url);
106     }
107 
108     public void flushBuffer() throws IOException {
109         if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
110             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
111 
112             _response.flushBuffer();
113         }
114     }
115 
116     public int getBufferSize() {
117         if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
118             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
119 
120             return _response.getBufferSize();
121         }
122         else {
123             return 0;
124         }
125     }
126 
127     public String getCharacterEncoding() {
128         if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
129             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
130 
131             return _response.getCharacterEncoding();
132         }
133         else {
134             return null;
135         }
136     }
137 
138     public String getContentType() {
139         if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
140             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
141 
142             return ((MimeResponse)_portletResponseImpl).getContentType();
143         }
144         else {
145             return null;
146         }
147     }
148 
149     public Locale getLocale() {
150         if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
151             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
152 
153             return _response.getLocale();
154         }
155         else {
156             return null;
157         }
158     }
159 
160     public ServletOutputStream getOutputStream() throws IOException {
161         if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
162             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
163 
164             return _response.getOutputStream();
165         }
166         else {
167             return new NullServletOutputStream();
168         }
169     }
170 
171     public PrintWriter getWriter() throws IOException {
172         if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
173             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
174 
175             return _response.getWriter();
176         }
177         else {
178             return new PrintWriter(new NullServletOutputStream());
179         }
180     }
181 
182     public boolean isCommitted() {
183         if (!_include) {
184             if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
185                 _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
186 
187                 return _response.isCommitted();
188             }
189             else {
190                 return false;
191             }
192         }
193         else {
194             if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
195                 _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
196 
197                 return _response.isCommitted();
198             }
199             else {
200                 return true;
201             }
202         }
203     }
204 
205     public void reset() {
206         if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
207             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
208 
209             _response.reset();
210         }
211     }
212 
213     public void resetBuffer() {
214         if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
215             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
216 
217             _response.resetBuffer();
218         }
219     }
220 
221     public void sendError(int sc) {
222     }
223 
224     public void sendError(int sc, String msg) {
225     }
226 
227     public void sendRedirect(String location) throws IOException {
228         if (!_include) {
229             if (_lifecycle.equals(PortletRequest.ACTION_PHASE)) {
230                 ((ActionResponse)_portletResponseImpl).sendRedirect(location);
231             }
232         }
233     }
234 
235     public void setBufferSize(int size) {
236         if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
237             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
238 
239             _response.setBufferSize(size);
240         }
241     }
242 
243     public void setCharacterEncoding(String encoding) {
244         if (!_include) {
245             if (_lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
246                 _response.setCharacterEncoding(encoding);
247             }
248         }
249     }
250 
251     public void setContentLength(int length) {
252         if (!_include) {
253             if (_lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
254                 _response.setContentLength(length);
255             }
256         }
257     }
258 
259     public void setContentType(String contentType) {
260         if (!_include) {
261             if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
262                 _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
263 
264                 ((MimeResponse)_portletResponseImpl).setContentType(
265                     contentType);
266             }
267         }
268     }
269 
270     public void setDateHeader(String name, long date) {
271         setHeader(name, String.valueOf(date));
272     }
273 
274     public void setHeader(String name, String value) {
275         if (!_include) {
276             if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
277                 _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
278 
279                 HttpServletResponse response =
280                     _portletResponseImpl.getHttpServletResponse();
281 
282                 response.setHeader(name, value);
283             }
284         }
285     }
286 
287     public void setIntHeader(String name, int value) {
288         setHeader(name, String.valueOf(value));
289     }
290 
291     public void setLocale(Locale locale) {
292         if (!_include) {
293             if (_lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
294                 _response.setLocale(locale);
295             }
296         }
297     }
298 
299     public void setStatus(int sc) {
300         if (!_include) {
301             if (_lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
302                 _response.setStatus(sc);
303             }
304         }
305     }
306 
307     public void setStatus(int sc, String msg) {
308         setStatus(sc);
309     }
310 
311     private HttpServletResponse _response;
312     private PortletResponseImpl _portletResponseImpl;
313     private boolean _include;
314     private String _lifecycle;
315 
316 }