001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncPrintWriter;
018    import com.liferay.util.servlet.NullServletOutputStream;
019    
020    import java.io.IOException;
021    import java.io.PrintWriter;
022    
023    import java.util.Locale;
024    
025    import javax.portlet.ActionResponse;
026    import javax.portlet.MimeResponse;
027    import javax.portlet.PortletRequest;
028    
029    import javax.servlet.ServletOutputStream;
030    import javax.servlet.http.Cookie;
031    import javax.servlet.http.HttpServletResponse;
032    import javax.servlet.http.HttpServletResponseWrapper;
033    
034    /**
035     * @author Brian Wing Shun Chan
036     */
037    public class PortletServletResponse extends HttpServletResponseWrapper {
038    
039            public PortletServletResponse(
040                    HttpServletResponse response, PortletResponseImpl portletResponseImpl,
041                    boolean include) {
042    
043                    super(response);
044    
045                    _response = response;
046                    _portletResponseImpl = portletResponseImpl;
047                    _include = include;
048                    _lifecycle = _portletResponseImpl.getLifecycle();
049            }
050    
051            public void addCookie(Cookie cookie) {
052                    if (!_include) {
053                            _portletResponseImpl.addProperty(cookie);
054                    }
055            }
056    
057            public void addDateHeader(String name, long date) {
058                    addHeader(name, String.valueOf(date));
059            }
060    
061            public void addHeader(String name, String value) {
062                    if (!_include) {
063                            if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
064                                    _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
065    
066                                    HttpServletResponse response =
067                                            _portletResponseImpl.getHttpServletResponse();
068    
069                                    response.addHeader(name, value);
070                            }
071                    }
072            }
073    
074            public void addIntHeader(String name, int value) {
075                    addHeader(name, String.valueOf(value));
076            }
077    
078            public boolean containsHeader(String name) {
079                    return false;
080            }
081    
082            public String encodeRedirectURL(String url) {
083                    return null;
084            }
085    
086            public String encodeRedirectUrl(String url) {
087                    return null;
088            }
089    
090            public String encodeURL(String url) {
091                    return _portletResponseImpl.encodeURL(url);
092            }
093    
094            public String encodeUrl(String url) {
095                    return _portletResponseImpl.encodeURL(url);
096            }
097    
098            public void flushBuffer() throws IOException {
099                    if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
100                            _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
101    
102                            _response.flushBuffer();
103                    }
104            }
105    
106            public int getBufferSize() {
107                    if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
108                            _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
109    
110                            return _response.getBufferSize();
111                    }
112                    else {
113                            return 0;
114                    }
115            }
116    
117            public String getCharacterEncoding() {
118                    if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
119                            _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
120    
121                            return _response.getCharacterEncoding();
122                    }
123                    else {
124                            return null;
125                    }
126            }
127    
128            public String getContentType() {
129                    if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
130                            _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
131    
132                            return ((MimeResponse)_portletResponseImpl).getContentType();
133                    }
134                    else {
135                            return null;
136                    }
137            }
138    
139            public Locale getLocale() {
140                    if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
141                            _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
142    
143                            return _response.getLocale();
144                    }
145                    else {
146                            return null;
147                    }
148            }
149    
150            public ServletOutputStream getOutputStream() throws IOException {
151                    if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
152                            _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
153    
154                            return _response.getOutputStream();
155                    }
156                    else {
157                            return new NullServletOutputStream();
158                    }
159            }
160    
161            public PrintWriter getWriter() throws IOException {
162                    if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
163                            _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
164    
165                            return _response.getWriter();
166                    }
167                    else {
168                            return new UnsyncPrintWriter(new NullServletOutputStream());
169                    }
170            }
171    
172            public boolean isCommitted() {
173                    if (!_include) {
174                            if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
175                                    _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
176    
177                                    return _response.isCommitted();
178                            }
179                            else {
180                                    return false;
181                            }
182                    }
183                    else {
184                            if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
185                                    _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
186    
187                                    return _response.isCommitted();
188                            }
189                            else {
190                                    return true;
191                            }
192                    }
193            }
194    
195            public void reset() {
196                    if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
197                            _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
198    
199                            _response.reset();
200                    }
201            }
202    
203            public void resetBuffer() {
204                    if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
205                            _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
206    
207                            _response.resetBuffer();
208                    }
209            }
210    
211            public void sendError(int status) {
212            }
213    
214            public void sendError(int status, String msg) {
215            }
216    
217            public void sendRedirect(String location) throws IOException {
218                    if (!_include) {
219                            if (_lifecycle.equals(PortletRequest.ACTION_PHASE)) {
220                                    ((ActionResponse)_portletResponseImpl).sendRedirect(location);
221                            }
222                    }
223            }
224    
225            public void setBufferSize(int bufferSize) {
226                    if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
227                            _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
228    
229                            _response.setBufferSize(bufferSize);
230                    }
231            }
232    
233            public void setCharacterEncoding(String encoding) {
234                    if (!_include) {
235                            if (_lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
236                                    _response.setCharacterEncoding(encoding);
237                            }
238                    }
239            }
240    
241            public void setContentLength(int length) {
242                    if (!_include) {
243                            if (_lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
244                                    _response.setContentLength(length);
245                            }
246                    }
247            }
248    
249            public void setContentType(String contentType) {
250                    if (!_include) {
251                            if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
252                                    _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
253    
254                                    ((MimeResponse)_portletResponseImpl).setContentType(
255                                            contentType);
256                            }
257                    }
258            }
259    
260            public void setDateHeader(String name, long date) {
261                    setHeader(name, String.valueOf(date));
262            }
263    
264            public void setHeader(String name, String value) {
265                    if (!_include) {
266                            if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
267                                    _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
268    
269                                    HttpServletResponse response =
270                                            _portletResponseImpl.getHttpServletResponse();
271    
272                                    response.setHeader(name, value);
273                            }
274                    }
275            }
276    
277            public void setIntHeader(String name, int value) {
278                    setHeader(name, String.valueOf(value));
279            }
280    
281            public void setLocale(Locale locale) {
282                    if (!_include) {
283                            if (_lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
284                                    _response.setLocale(locale);
285                            }
286                    }
287            }
288    
289            public void setStatus(int status) {
290                    if (!_include) {
291                            if (_lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
292                                    _response.setStatus(status);
293                            }
294                    }
295            }
296    
297            public void setStatus(int status, String msg) {
298                    setStatus(status);
299            }
300    
301            private HttpServletResponse _response;
302            private PortletResponseImpl _portletResponseImpl;
303            private boolean _include;
304            private String _lifecycle;
305    
306    }