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 boolean isCalledFlushBuffer() {
040 return _calledFlushBuffer;
041 }
042
043 public void flushBuffer() throws IOException {
044 _response.flushBuffer();
045
046 _calledFlushBuffer = true;
047 }
048
049 public int getBufferSize() {
050 return _response.getBufferSize();
051 }
052
053 public CacheControl getCacheControl() {
054 return new CacheControlImpl(null, 0, false, false, this);
055 }
056
057 public String getCharacterEncoding() {
058 return _response.getCharacterEncoding();
059 }
060
061 public String getContentType() {
062 return _contentType;
063 }
064
065 public Locale getLocale() {
066 return _portletRequestImpl.getLocale();
067 }
068
069 public OutputStream getPortletOutputStream() throws IOException {
070 if (_calledGetWriter) {
071 throw new IllegalStateException(
072 "Cannot obtain OutputStream because Writer is already in use");
073 }
074
075 if (_contentType == null) {
076 setContentType(_portletRequestImpl.getResponseContentType());
077 }
078
079 _calledGetPortletOutputStream = true;
080
081 return _response.getOutputStream();
082 }
083
084 public PrintWriter getWriter() throws IOException {
085 if (_calledGetPortletOutputStream) {
086 throw new IllegalStateException(
087 "Cannot obtain Writer because OutputStream is already in use");
088 }
089
090 if (_contentType == null) {
091 setContentType(_portletRequestImpl.getResponseContentType());
092 }
093
094 _calledGetWriter = true;
095
096 return _response.getWriter();
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 protected void init(
169 PortletRequestImpl portletRequestImpl, HttpServletResponse response,
170 String portletName, long companyId, long plid) {
171
172 super.init(portletRequestImpl, response, portletName, companyId, plid);
173
174 _portletRequestImpl = portletRequestImpl;
175 _response = response;
176 }
177
178 private PortletRequestImpl _portletRequestImpl;
179 private HttpServletResponse _response;
180 private String _contentType;
181 private boolean _calledGetPortletOutputStream;
182 private boolean _calledGetWriter;
183 private boolean _calledFlushBuffer;
184
185 }