1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet;
16  
17  import com.liferay.portal.kernel.io.unsync.UnsyncPrintWriter;
18  import com.liferay.util.servlet.NullServletOutputStream;
19  
20  import java.io.IOException;
21  import java.io.PrintWriter;
22  
23  import java.util.Locale;
24  
25  import javax.portlet.ActionResponse;
26  import javax.portlet.MimeResponse;
27  import javax.portlet.PortletRequest;
28  
29  import javax.servlet.ServletOutputStream;
30  import javax.servlet.http.Cookie;
31  import javax.servlet.http.HttpServletResponse;
32  import javax.servlet.http.HttpServletResponseWrapper;
33  
34  /**
35   * <a href="PortletServletResponse.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   */
39  public class PortletServletResponse extends HttpServletResponseWrapper {
40  
41      public PortletServletResponse(
42          HttpServletResponse response, PortletResponseImpl portletResponseImpl,
43          boolean include) {
44  
45          super(response);
46  
47          _response = response;
48          _portletResponseImpl = portletResponseImpl;
49          _include = include;
50          _lifecycle = _portletResponseImpl.getLifecycle();
51      }
52  
53      public void addCookie(Cookie cookie) {
54          if (!_include) {
55              _portletResponseImpl.addProperty(cookie);
56          }
57      }
58  
59      public void addDateHeader(String name, long date) {
60          addHeader(name, String.valueOf(date));
61      }
62  
63      public void addHeader(String name, String value) {
64          if (!_include) {
65              if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
66                  _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
67  
68                  HttpServletResponse response =
69                      _portletResponseImpl.getHttpServletResponse();
70  
71                  response.addHeader(name, value);
72              }
73          }
74      }
75  
76      public void addIntHeader(String name, int value) {
77          addHeader(name, String.valueOf(value));
78      }
79  
80      public boolean containsHeader(String name) {
81          return false;
82      }
83  
84      public String encodeRedirectURL(String url) {
85          return null;
86      }
87  
88      public String encodeRedirectUrl(String url) {
89          return null;
90      }
91  
92      public String encodeURL(String url) {
93          return _portletResponseImpl.encodeURL(url);
94      }
95  
96      public String encodeUrl(String url) {
97          return _portletResponseImpl.encodeURL(url);
98      }
99  
100     public void flushBuffer() throws IOException {
101         if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
102             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
103 
104             _response.flushBuffer();
105         }
106     }
107 
108     public int getBufferSize() {
109         if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
110             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
111 
112             return _response.getBufferSize();
113         }
114         else {
115             return 0;
116         }
117     }
118 
119     public String getCharacterEncoding() {
120         if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
121             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
122 
123             return _response.getCharacterEncoding();
124         }
125         else {
126             return null;
127         }
128     }
129 
130     public String getContentType() {
131         if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
132             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
133 
134             return ((MimeResponse)_portletResponseImpl).getContentType();
135         }
136         else {
137             return null;
138         }
139     }
140 
141     public Locale getLocale() {
142         if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
143             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
144 
145             return _response.getLocale();
146         }
147         else {
148             return null;
149         }
150     }
151 
152     public ServletOutputStream getOutputStream() throws IOException {
153         if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
154             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
155 
156             return _response.getOutputStream();
157         }
158         else {
159             return new NullServletOutputStream();
160         }
161     }
162 
163     public PrintWriter getWriter() throws IOException {
164         if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
165             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
166 
167             return _response.getWriter();
168         }
169         else {
170             return new UnsyncPrintWriter(new NullServletOutputStream());
171         }
172     }
173 
174     public boolean isCommitted() {
175         if (!_include) {
176             if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
177                 _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
178 
179                 return _response.isCommitted();
180             }
181             else {
182                 return false;
183             }
184         }
185         else {
186             if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
187                 _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
188 
189                 return _response.isCommitted();
190             }
191             else {
192                 return true;
193             }
194         }
195     }
196 
197     public void reset() {
198         if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
199             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
200 
201             _response.reset();
202         }
203     }
204 
205     public void resetBuffer() {
206         if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
207             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
208 
209             _response.resetBuffer();
210         }
211     }
212 
213     public void sendError(int status) {
214     }
215 
216     public void sendError(int status, String msg) {
217     }
218 
219     public void sendRedirect(String location) throws IOException {
220         if (!_include) {
221             if (_lifecycle.equals(PortletRequest.ACTION_PHASE)) {
222                 ((ActionResponse)_portletResponseImpl).sendRedirect(location);
223             }
224         }
225     }
226 
227     public void setBufferSize(int bufferSize) {
228         if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
229             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
230 
231             _response.setBufferSize(bufferSize);
232         }
233     }
234 
235     public void setCharacterEncoding(String encoding) {
236         if (!_include) {
237             if (_lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
238                 _response.setCharacterEncoding(encoding);
239             }
240         }
241     }
242 
243     public void setContentLength(int length) {
244         if (!_include) {
245             if (_lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
246                 _response.setContentLength(length);
247             }
248         }
249     }
250 
251     public void setContentType(String contentType) {
252         if (!_include) {
253             if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
254                 _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
255 
256                 ((MimeResponse)_portletResponseImpl).setContentType(
257                     contentType);
258             }
259         }
260     }
261 
262     public void setDateHeader(String name, long date) {
263         setHeader(name, String.valueOf(date));
264     }
265 
266     public void setHeader(String name, String value) {
267         if (!_include) {
268             if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
269                 _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
270 
271                 HttpServletResponse response =
272                     _portletResponseImpl.getHttpServletResponse();
273 
274                 response.setHeader(name, value);
275             }
276         }
277     }
278 
279     public void setIntHeader(String name, int value) {
280         setHeader(name, String.valueOf(value));
281     }
282 
283     public void setLocale(Locale locale) {
284         if (!_include) {
285             if (_lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
286                 _response.setLocale(locale);
287             }
288         }
289     }
290 
291     public void setStatus(int status) {
292         if (!_include) {
293             if (_lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
294                 _response.setStatus(status);
295             }
296         }
297     }
298 
299     public void setStatus(int status, String msg) {
300         setStatus(status);
301     }
302 
303     private HttpServletResponse _response;
304     private PortletResponseImpl _portletResponseImpl;
305     private boolean _include;
306     private String _lifecycle;
307 
308 }