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