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.Locale;
025
026 import javax.portlet.CacheControl;
027 import javax.portlet.MimeResponse;
028 import javax.portlet.PortletRequest;
029 import javax.portlet.WindowState;
030
031 import javax.servlet.http.HttpServletResponse;
032
033
037 public abstract class MimeResponseImpl
038 extends PortletResponseImpl implements MimeResponse {
039
040 @Override
041 public void flushBuffer() throws IOException {
042 _response.flushBuffer();
043
044 _calledFlushBuffer = true;
045 }
046
047 @Override
048 public int getBufferSize() {
049 return _response.getBufferSize();
050 }
051
052 @Override
053 public CacheControl getCacheControl() {
054 return new CacheControlImpl(null, 0, false, false, this);
055 }
056
057 @Override
058 public String getCharacterEncoding() {
059 return _response.getCharacterEncoding();
060 }
061
062 @Override
063 public String getContentType() {
064 return _contentType;
065 }
066
067 @Override
068 public Locale getLocale() {
069 return _portletRequestImpl.getLocale();
070 }
071
072 @Override
073 public OutputStream getPortletOutputStream() throws IOException {
074 if (_calledGetWriter) {
075 throw new IllegalStateException(
076 "Unable to obtain OutputStream because Writer is already in " +
077 "use");
078 }
079
080 if (_contentType == null) {
081 setContentType(_portletRequestImpl.getResponseContentType());
082 }
083
084 _calledGetPortletOutputStream = true;
085
086 return _response.getOutputStream();
087 }
088
089 @Override
090 public PrintWriter getWriter() throws IOException {
091 if (_calledGetPortletOutputStream) {
092 throw new IllegalStateException(
093 "Cannot obtain Writer because OutputStream is already in use");
094 }
095
096 if (_contentType == null) {
097 setContentType(_portletRequestImpl.getResponseContentType());
098 }
099
100 _calledGetWriter = true;
101
102 return _response.getWriter();
103 }
104
105 public boolean isCalledFlushBuffer() {
106 return _calledFlushBuffer;
107 }
108
109 public boolean isCalledGetPortletOutputStream() {
110 return _calledGetPortletOutputStream;
111 }
112
113 public boolean isCalledGetWriter() {
114 return _calledGetWriter;
115 }
116
117 @Override
118 public boolean isCommitted() {
119 return false;
120 }
121
122 @Override
123 public void reset() {
124 if (_calledFlushBuffer) {
125 throw new IllegalStateException(
126 "Cannot reset a buffer that has been flushed");
127 }
128 }
129
130 @Override
131 public void resetBuffer() {
132 if (_calledFlushBuffer) {
133 throw new IllegalStateException(
134 "Cannot reset a buffer that has been flushed");
135 }
136
137 _response.resetBuffer();
138 }
139
140 @Override
141 public void setBufferSize(int bufferSize) {
142 _response.setBufferSize(bufferSize);
143 }
144
145 @Override
146 public void setContentType(String contentType) {
147 if (Validator.isNull(contentType)) {
148 throw new IllegalArgumentException("Content type is null");
149 }
150
151 String lifecycle = getLifecycle();
152 WindowState windowState = _portletRequestImpl.getWindowState();
153
154 if (!contentType.startsWith(
155 _portletRequestImpl.getResponseContentType()) &&
156 !lifecycle.equals(PortletRequest.RESOURCE_PHASE) &&
157 !windowState.equals(LiferayWindowState.EXCLUSIVE)) {
158
159 throw new IllegalArgumentException(
160 contentType + " is an unsupported content 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 }