001    /**
002     * Copyright (c) 2000-2013 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.util.UnsyncPrintWriterPool;
018    import com.liferay.util.servlet.GenericServletOutputStream;
019    import com.liferay.util.servlet.NullServletOutputStream;
020    
021    import java.io.IOException;
022    import java.io.OutputStream;
023    import java.io.PrintWriter;
024    
025    import java.util.Locale;
026    
027    import javax.portlet.ActionResponse;
028    import javax.portlet.MimeResponse;
029    import javax.portlet.PortletRequest;
030    import javax.portlet.PortletResponse;
031    import javax.portlet.ResourceResponse;
032    
033    import javax.servlet.ServletOutputStream;
034    import javax.servlet.http.Cookie;
035    import javax.servlet.http.HttpServletResponse;
036    import javax.servlet.http.HttpServletResponseWrapper;
037    
038    /**
039     * @author Brian Wing Shun Chan
040     */
041    public class PortletServletResponse extends HttpServletResponseWrapper {
042    
043            public PortletServletResponse(
044                    HttpServletResponse response, PortletResponse portletResponse,
045                    boolean include) {
046    
047                    super(response);
048    
049                    _portletResponse = portletResponse;
050                    _include = include;
051    
052                    PortletResponseImpl portletResponseImpl =
053                            PortletResponseImpl.getPortletResponseImpl(portletResponse);
054    
055                    _lifecycle = portletResponseImpl.getLifecycle();
056            }
057    
058            @Override
059            public void addCookie(Cookie cookie) {
060                    if (!_include) {
061                            _portletResponse.addProperty(cookie);
062                    }
063            }
064    
065            @Override
066            public void addDateHeader(String name, long date) {
067                    addHeader(name, String.valueOf(date));
068            }
069    
070            @Override
071            public void addHeader(String name, String value) {
072                    if (!_include) {
073                            if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
074                                    _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
075    
076                                    MimeResponse mimeResponse = _getMimeResponse();
077    
078                                    mimeResponse.setProperty(name, value);
079                            }
080                    }
081            }
082    
083            @Override
084            public void addIntHeader(String name, int value) {
085                    addHeader(name, String.valueOf(value));
086            }
087    
088            @Override
089            public boolean containsHeader(String name) {
090                    return false;
091            }
092    
093            @Override
094            public String encodeRedirectUrl(String url) {
095                    return null;
096            }
097    
098            @Override
099            public String encodeRedirectURL(String url) {
100                    return null;
101            }
102    
103            @Override
104            public String encodeUrl(String url) {
105                    return _portletResponse.encodeURL(url);
106            }
107    
108            @Override
109            public String encodeURL(String url) {
110                    return _portletResponse.encodeURL(url);
111            }
112    
113            @Override
114            public void flushBuffer() throws IOException {
115                    if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
116                            _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
117    
118                            MimeResponse mimeResponse = _getMimeResponse();
119    
120                            mimeResponse.flushBuffer();
121                    }
122            }
123    
124            @Override
125            public int getBufferSize() {
126                    if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
127                            _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
128    
129                            MimeResponse mimeResponse = _getMimeResponse();
130    
131                            return mimeResponse.getBufferSize();
132                    }
133                    else {
134                            return 0;
135                    }
136            }
137    
138            @Override
139            public String getCharacterEncoding() {
140                    if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
141                            _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
142    
143                            MimeResponse mimeResponse = _getMimeResponse();
144    
145                            return mimeResponse.getCharacterEncoding();
146                    }
147                    else {
148                            return null;
149                    }
150            }
151    
152            @Override
153            public String getContentType() {
154                    if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
155                            _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
156    
157                            MimeResponse mimeResponse = _getMimeResponse();
158    
159                            return mimeResponse.getContentType();
160                    }
161                    else {
162                            return null;
163                    }
164            }
165    
166            @Override
167            public Locale getLocale() {
168                    if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
169                            _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
170    
171                            MimeResponse mimeResponse = _getMimeResponse();
172    
173                            return mimeResponse.getLocale();
174                    }
175                    else {
176                            return null;
177                    }
178            }
179    
180            @Override
181            public ServletOutputStream getOutputStream() throws IOException {
182                    if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
183                            _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
184    
185                            MimeResponse mimeResponse = _getMimeResponse();
186    
187                            OutputStream portletOutputStream =
188                                    mimeResponse.getPortletOutputStream();
189    
190                            ServletOutputStream servletOutputStream =
191                                    new GenericServletOutputStream(portletOutputStream);
192    
193                            return servletOutputStream;
194                    }
195                    else {
196                            return new NullServletOutputStream();
197                    }
198            }
199    
200            @Override
201            public PrintWriter getWriter() throws IOException {
202                    if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
203                            _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
204    
205                            MimeResponse mimeResponse = _getMimeResponse();
206    
207                            return mimeResponse.getWriter();
208                    }
209                    else {
210                            return UnsyncPrintWriterPool.borrow(new NullServletOutputStream());
211                    }
212            }
213    
214            @Override
215            public boolean isCommitted() {
216                    if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
217                            _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
218    
219                            MimeResponse mimeResponse = _getMimeResponse();
220    
221                            return mimeResponse.isCommitted();
222                    }
223                    else if (!_include) {
224                            return false;
225                    }
226                    else {
227                            return true;
228                    }
229            }
230    
231            @Override
232            public void reset() {
233                    if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
234                            _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
235    
236                            MimeResponse mimeResponse = _getMimeResponse();
237    
238                            mimeResponse.reset();
239                    }
240            }
241    
242            @Override
243            public void resetBuffer() {
244                    if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
245                            _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
246    
247                            MimeResponse mimeResponse = _getMimeResponse();
248    
249                            mimeResponse.resetBuffer();
250                    }
251            }
252    
253            @Override
254            public void sendError(int status) {
255            }
256    
257            @Override
258            public void sendError(int status, String msg) {
259            }
260    
261            @Override
262            public void sendRedirect(String location) throws IOException {
263                    if (!_include) {
264                            if (_lifecycle.equals(PortletRequest.ACTION_PHASE)) {
265                                    ActionResponse actionResponse = _getActionResponse();
266    
267                                    actionResponse.sendRedirect(location);
268                            }
269                    }
270            }
271    
272            @Override
273            public void setBufferSize(int bufferSize) {
274                    if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
275                            _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
276    
277                            MimeResponse mimeResponse = _getMimeResponse();
278    
279                            mimeResponse.setBufferSize(bufferSize);
280                    }
281            }
282    
283            @Override
284            public void setCharacterEncoding(String encoding) {
285                    if (!_include) {
286                            if (_lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
287                                    ResourceResponse resourceResponse = _getResourceResponse();
288    
289                                    resourceResponse.setCharacterEncoding(encoding);
290                            }
291                    }
292            }
293    
294            @Override
295            public void setContentLength(int length) {
296                    if (!_include) {
297                            if (_lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
298                                    ResourceResponse resourceResponse = _getResourceResponse();
299    
300                                    resourceResponse.setContentLength(length);
301                            }
302                    }
303            }
304    
305            @Override
306            public void setContentType(String contentType) {
307                    if (!_include) {
308                            if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
309                                    _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
310    
311                                    MimeResponse mimeResponse = _getMimeResponse();
312    
313                                    mimeResponse.setContentType(contentType);
314                            }
315                    }
316            }
317    
318            @Override
319            public void setDateHeader(String name, long date) {
320                    setHeader(name, String.valueOf(date));
321            }
322    
323            @Override
324            public void setHeader(String name, String value) {
325                    if (!_include) {
326                            if (_lifecycle.equals(PortletRequest.RENDER_PHASE) ||
327                                    _lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
328    
329                                    MimeResponse mimeResponse = _getMimeResponse();
330    
331                                    mimeResponse.setProperty(name, value);
332                            }
333                    }
334            }
335    
336            @Override
337            public void setIntHeader(String name, int value) {
338                    setHeader(name, String.valueOf(value));
339            }
340    
341            @Override
342            public void setLocale(Locale locale) {
343                    if (!_include) {
344                            if (_lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
345                                    ResourceResponse resourceResponse = _getResourceResponse();
346    
347                                    resourceResponse.setLocale(locale);
348                            }
349                    }
350            }
351    
352            @Override
353            public void setStatus(int status) {
354                    if (!_include) {
355                            if (_lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
356                                    ResourceResponse resourceResponse = _getResourceResponse();
357    
358                                    resourceResponse.setProperty(
359                                            ResourceResponse.HTTP_STATUS_CODE, String.valueOf(status));
360                            }
361                    }
362            }
363    
364            @Override
365            public void setStatus(int status, String msg) {
366                    setStatus(status);
367            }
368    
369            private ActionResponse _getActionResponse() {
370                    return (ActionResponse)_portletResponse;
371            }
372    
373            private MimeResponse _getMimeResponse() {
374                    return (MimeResponse)_portletResponse;
375            }
376    
377            private ResourceResponse _getResourceResponse() {
378                    return (ResourceResponse)_portletResponse;
379            }
380    
381            private boolean _include;
382            private String _lifecycle;
383            private PortletResponse _portletResponse;
384    
385    }