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.portlet.LiferayWindowState;
018    import com.liferay.portal.kernel.util.Validator;
019    
020    import java.io.IOException;
021    import java.io.OutputStream;
022    import java.io.PrintWriter;
023    
024    import java.util.Enumeration;
025    import java.util.Locale;
026    
027    import javax.portlet.CacheControl;
028    import javax.portlet.MimeResponse;
029    import javax.portlet.PortletRequest;
030    
031    import javax.servlet.http.HttpServletResponse;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     */
036    public abstract class MimeResponseImpl
037            extends PortletResponseImpl implements MimeResponse {
038    
039            @Override
040            public void flushBuffer() throws IOException {
041                    _response.flushBuffer();
042    
043                    _calledFlushBuffer = true;
044            }
045    
046            @Override
047            public int getBufferSize() {
048                    return _response.getBufferSize();
049            }
050    
051            @Override
052            public CacheControl getCacheControl() {
053                    return new CacheControlImpl(null, 0, false, false, this);
054            }
055    
056            @Override
057            public String getCharacterEncoding() {
058                    return _response.getCharacterEncoding();
059            }
060    
061            @Override
062            public String getContentType() {
063                    return _contentType;
064            }
065    
066            @Override
067            public Locale getLocale() {
068                    return _portletRequestImpl.getLocale();
069            }
070    
071            @Override
072            public OutputStream getPortletOutputStream() throws IOException {
073                    if (_calledGetWriter) {
074                            throw new IllegalStateException(
075                                    "Cannot obtain OutputStream because Writer is already in use");
076                    }
077    
078                    if (_contentType == null) {
079                            setContentType(_portletRequestImpl.getResponseContentType());
080                    }
081    
082                    _calledGetPortletOutputStream = true;
083    
084                    return _response.getOutputStream();
085            }
086    
087            @Override
088            public PrintWriter getWriter() throws IOException {
089                    if (_calledGetPortletOutputStream) {
090                            throw new IllegalStateException(
091                                    "Cannot obtain Writer because OutputStream is already in use");
092                    }
093    
094                    if (_contentType == null) {
095                            setContentType(_portletRequestImpl.getResponseContentType());
096                    }
097    
098                    _calledGetWriter = true;
099    
100                    return _response.getWriter();
101            }
102    
103            public boolean isCalledFlushBuffer() {
104                    return _calledFlushBuffer;
105            }
106    
107            public boolean isCalledGetPortletOutputStream() {
108                    return _calledGetPortletOutputStream;
109            }
110    
111            public boolean isCalledGetWriter() {
112                    return _calledGetWriter;
113            }
114    
115            @Override
116            public boolean isCommitted() {
117                    return false;
118            }
119    
120            @Override
121            public void reset() {
122                    if (_calledFlushBuffer) {
123                            throw new IllegalStateException(
124                                    "Cannot reset a buffer that has been flushed");
125                    }
126            }
127    
128            @Override
129            public void resetBuffer() {
130                    if (_calledFlushBuffer) {
131                            throw new IllegalStateException(
132                                    "Cannot reset a buffer that has been flushed");
133                    }
134    
135                    _response.resetBuffer();
136            }
137    
138            @Override
139            public void setBufferSize(int bufferSize) {
140                    _response.setBufferSize(bufferSize);
141            }
142    
143            @Override
144            public void setContentType(String contentType) {
145                    if (Validator.isNull(contentType)) {
146                            throw new IllegalArgumentException("Content type cannot be null");
147                    }
148    
149                    Enumeration<String> enu = _portletRequestImpl.getResponseContentTypes();
150    
151                    boolean valid = false;
152    
153                    if (getLifecycle().equals(PortletRequest.RESOURCE_PHASE) ||
154                            _portletRequestImpl.getWindowState().equals(
155                                    LiferayWindowState.EXCLUSIVE)) {
156    
157                            valid = true;
158                    }
159                    else {
160                            while (enu.hasMoreElements()) {
161                                    String resContentType = enu.nextElement();
162    
163                                    if (contentType.startsWith(resContentType)) {
164                                            valid = true;
165    
166                                            break;
167                                    }
168                            }
169                    }
170    
171                    if (!valid) {
172                            throw new IllegalArgumentException(
173                                    contentType + " is not a supported mime type");
174                    }
175    
176                    _contentType = contentType;
177    
178                    _response.setContentType(contentType);
179            }
180    
181            @Override
182            protected void init(
183                    PortletRequestImpl portletRequestImpl, HttpServletResponse response,
184                    String portletName, long companyId, long plid) {
185    
186                    super.init(portletRequestImpl, response, portletName, companyId, plid);
187    
188                    _portletRequestImpl = portletRequestImpl;
189                    _response = response;
190            }
191    
192            private boolean _calledFlushBuffer;
193            private boolean _calledGetPortletOutputStream;
194            private boolean _calledGetWriter;
195            private String _contentType;
196            private PortletRequestImpl _portletRequestImpl;
197            private HttpServletResponse _response;
198    
199    }