1   /**
2    * Copyright (c) 2000-2008 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 res, PortletResponseImpl portletRes,
51          boolean include) {
52  
53          super(res);
54  
55          _res = res;
56          _portletRes = portletRes;
57          _include = include;
58          _lifecycle = _portletRes.getLifecycle();
59      }
60  
61      public void addCookie(Cookie cookie) {
62          if (!_include) {
63              _portletRes.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                  _portletRes.addProperty("header." + name, value);
77              }
78          }
79      }
80  
81      public void addIntHeader(String name, int value) {
82          addHeader(name, String.valueOf(value));
83      }
84  
85      public boolean containsHeader(String name) {
86          return false;
87      }
88  
89      public String encodeRedirectURL(String url) {
90          return null;
91      }
92  
93      public String encodeRedirectUrl(String url) {
94          return null;
95      }
96  
97      public String encodeURL(String url) {
98          return _portletRes.encodeURL(url);
99      }
100 
101     public String encodeUrl(String url) {
102         return _portletRes.encodeURL(url);
103     }
104 
105     public void flushBuffer() throws IOException {
106         if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
107             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
108 
109             _res.flushBuffer();
110         }
111     }
112 
113     public int getBufferSize() {
114         if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
115             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
116 
117             return _res.getBufferSize();
118         }
119         else {
120             return 0;
121         }
122     }
123 
124     public String getCharacterEncoding() {
125         if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
126             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
127 
128             return _res.getCharacterEncoding();
129         }
130         else {
131             return null;
132         }
133     }
134 
135     public String getContentType() {
136         if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
137             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
138 
139             return ((MimeResponse)_portletRes).getContentType();
140         }
141         else {
142             return null;
143         }
144     }
145 
146     public Locale getLocale() {
147         if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
148             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
149 
150             return _res.getLocale();
151         }
152         else {
153             return null;
154         }
155     }
156 
157     public ServletOutputStream getOutputStream() throws IOException {
158         if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
159             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
160 
161             return _res.getOutputStream();
162         }
163         else {
164             return new NullServletOutputStream();
165         }
166     }
167 
168     public PrintWriter getWriter() throws IOException {
169         if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
170             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
171 
172             return _res.getWriter();
173         }
174         else {
175             return new PrintWriter(new NullServletOutputStream());
176         }
177     }
178 
179     public boolean isCommitted() {
180         if (!_include) {
181             if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
182                 _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
183 
184                 return _res.isCommitted();
185             }
186             else {
187                 return false;
188             }
189         }
190         else {
191             if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
192                 _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
193 
194                 return _res.isCommitted();
195             }
196             else {
197                 return true;
198             }
199         }
200     }
201 
202     public void reset() {
203         if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
204             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
205 
206             _res.reset();
207         }
208     }
209 
210     public void resetBuffer() {
211         if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
212             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
213 
214             _res.resetBuffer();
215         }
216     }
217 
218     public void sendError(int sc) throws IOException {
219     }
220 
221     public void sendError(int sc, String msg) throws IOException {
222     }
223 
224     public void sendRedirect(String location) throws IOException {
225         if (!_include) {
226             if (_lifecycle.equals(PortletRequest.ACTION_PHASE)) {
227                 ((ActionResponse)_portletRes).sendRedirect(location);
228             }
229         }
230     }
231 
232     public void setBufferSize(int size) {
233         if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
234             _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
235 
236             _res.setBufferSize(size);
237         }
238     }
239 
240     public void setCharacterEncoding(String encoding) {
241         if (!_include) {
242             if (_lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
243                 _res.setCharacterEncoding(encoding);
244             }
245         }
246     }
247 
248     public void setContentLength(int length) {
249         if (!_include) {
250             if (_lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
251                 _res.setContentLength(length);
252             }
253         }
254     }
255 
256     public void setContentType(String contentType) {
257         if (!_include) {
258             if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
259                 _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
260 
261                 ((MimeResponse)_portletRes).setContentType(contentType);
262             }
263         }
264     }
265 
266     public void setDateHeader(String name, long date) {
267         setHeader(name, String.valueOf(date));
268     }
269 
270     public void setHeader(String name, String value) {
271         if (!_include) {
272             if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
273                 _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
274 
275                 _portletRes.setProperty("header." + name, value);
276             }
277         }
278     }
279 
280     public void setIntHeader(String name, int value) {
281         setHeader(name, String.valueOf(value));
282     }
283 
284     public void setLocale(Locale locale) {
285         if (!_include) {
286             if (_lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
287                 _res.setLocale(locale);
288             }
289         }
290     }
291 
292     public void setStatus(int sc) {
293         if (!_include) {
294             if (_lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
295                 _portletRes.setProperty("status.", String.valueOf("sc"));
296             }
297         }
298     }
299 
300     public void setStatus(int sc, String msg) {
301         setStatus(sc);
302     }
303 
304     private HttpServletResponse _res;
305     private PortletResponseImpl _portletRes;
306     private boolean _include;
307     private String _lifecycle;
308 
309 }