001
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
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 "Unable to obtain OutputStream because Writer is already in " +
076 "use");
077 }
078
079 if (_contentType == null) {
080 setContentType(_portletRequestImpl.getResponseContentType());
081 }
082
083 _calledGetPortletOutputStream = true;
084
085 return _response.getOutputStream();
086 }
087
088 @Override
089 public PrintWriter getWriter() throws IOException {
090 if (_calledGetPortletOutputStream) {
091 throw new IllegalStateException(
092 "Cannot obtain Writer because OutputStream is already in use");
093 }
094
095 if (_contentType == null) {
096 setContentType(_portletRequestImpl.getResponseContentType());
097 }
098
099 _calledGetWriter = true;
100
101 return _response.getWriter();
102 }
103
104 public boolean isCalledFlushBuffer() {
105 return _calledFlushBuffer;
106 }
107
108 public boolean isCalledGetPortletOutputStream() {
109 return _calledGetPortletOutputStream;
110 }
111
112 public boolean isCalledGetWriter() {
113 return _calledGetWriter;
114 }
115
116 @Override
117 public boolean isCommitted() {
118 return false;
119 }
120
121 @Override
122 public void reset() {
123 if (_calledFlushBuffer) {
124 throw new IllegalStateException(
125 "Cannot reset a buffer that has been flushed");
126 }
127 }
128
129 @Override
130 public void resetBuffer() {
131 if (_calledFlushBuffer) {
132 throw new IllegalStateException(
133 "Cannot reset a buffer that has been flushed");
134 }
135
136 _response.resetBuffer();
137 }
138
139 @Override
140 public void setBufferSize(int bufferSize) {
141 _response.setBufferSize(bufferSize);
142 }
143
144 @Override
145 public void setContentType(String contentType) {
146 if (Validator.isNull(contentType)) {
147 throw new IllegalArgumentException("Content type cannot be null");
148 }
149
150 Enumeration<String> enu = _portletRequestImpl.getResponseContentTypes();
151
152 boolean valid = false;
153
154 if (getLifecycle().equals(PortletRequest.RESOURCE_PHASE) ||
155 _portletRequestImpl.getWindowState().equals(
156 LiferayWindowState.EXCLUSIVE)) {
157
158 valid = true;
159 }
160 else {
161 while (enu.hasMoreElements()) {
162 String resContentType = enu.nextElement();
163
164 if (contentType.startsWith(resContentType)) {
165 valid = true;
166
167 break;
168 }
169 }
170 }
171
172 if (!valid) {
173 throw new IllegalArgumentException(
174 contentType + " is not a supported mime type");
175 }
176
177 _contentType = contentType;
178
179 _response.setContentType(contentType);
180 }
181
182 @Override
183 protected void init(
184 PortletRequestImpl portletRequestImpl, HttpServletResponse response,
185 String portletName, long companyId, long plid) {
186
187 super.init(portletRequestImpl, response, portletName, companyId, plid);
188
189 _portletRequestImpl = portletRequestImpl;
190 _response = response;
191 }
192
193 private boolean _calledFlushBuffer;
194 private boolean _calledGetPortletOutputStream;
195 private boolean _calledGetWriter;
196 private String _contentType;
197 private PortletRequestImpl _portletRequestImpl;
198 private HttpServletResponse _response;
199
200 }