1
22
23 package com.liferay.portlet;
24
25 import com.liferay.portal.kernel.portlet.LiferayWindowState;
26 import com.liferay.portal.kernel.util.Validator;
27
28 import java.io.IOException;
29 import java.io.OutputStream;
30 import java.io.PrintWriter;
31
32 import java.util.Enumeration;
33 import java.util.Locale;
34
35 import javax.portlet.CacheControl;
36 import javax.portlet.MimeResponse;
37 import javax.portlet.PortletRequest;
38
39 import javax.servlet.http.HttpServletResponse;
40
41
46 public abstract class MimeResponseImpl
47 extends PortletResponseImpl implements MimeResponse {
48
49 public boolean isCalledFlushBuffer() {
50 return _calledFlushBuffer;
51 }
52
53 public void flushBuffer() throws IOException {
54 _response.flushBuffer();
55
56 _calledFlushBuffer = true;
57 }
58
59 public int getBufferSize() {
60 return _response.getBufferSize();
61 }
62
63 public CacheControl getCacheControl() {
64 return new CacheControlImpl(null, 0, false, false, this);
65 }
66
67 public String getCharacterEncoding() {
68 return _response.getCharacterEncoding();
69 }
70
71 public String getContentType() {
72 return _contentType;
73 }
74
75 public Locale getLocale() {
76 return _portletRequestImpl.getLocale();
77 }
78
79 public OutputStream getPortletOutputStream() throws IOException {
80 if (_calledGetWriter) {
81 throw new IllegalStateException(
82 "Cannot obtain OutputStream because Writer is already in use");
83 }
84
85 if (_contentType == null) {
86 setContentType(_portletRequestImpl.getResponseContentType());
87 }
88
89 _calledGetPortletOutputStream = true;
90
91 return _response.getOutputStream();
92 }
93
94 public PrintWriter getWriter() throws IOException {
95 if (_calledGetPortletOutputStream) {
96 throw new IllegalStateException(
97 "Cannot obtain Writer because OutputStream is already in use");
98 }
99
100 if (_contentType == null) {
101 setContentType(_portletRequestImpl.getResponseContentType());
102 }
103
104 _calledGetWriter = true;
105
106 return _response.getWriter();
107 }
108
109 public boolean isCalledGetPortletOutputStream() {
110 return _calledGetPortletOutputStream;
111 }
112
113 public boolean isCalledGetWriter() {
114 return _calledGetWriter;
115 }
116
117 public boolean isCommitted() {
118 return false;
119 }
120
121 public void reset() {
122 if (_calledFlushBuffer) {
123 throw new IllegalStateException(
124 "Cannot reset a buffer that has been flushed");
125 }
126 }
127
128 public void resetBuffer() {
129 if (_calledFlushBuffer) {
130 throw new IllegalStateException(
131 "Cannot reset a buffer that has been flushed");
132 }
133
134 _response.resetBuffer();
135 }
136
137 public void setBufferSize(int bufferSize) {
138 _response.setBufferSize(bufferSize);
139 }
140
141 public void setContentType(String contentType) {
142 if (Validator.isNull(contentType)) {
143 throw new IllegalArgumentException("Content type cannot be null");
144 }
145
146 Enumeration<String> enu = _portletRequestImpl.getResponseContentTypes();
147
148 boolean valid = false;
149
150 if (getLifecycle().equals(PortletRequest.RESOURCE_PHASE) ||
151 _portletRequestImpl.getWindowState().equals(
152 LiferayWindowState.EXCLUSIVE)) {
153
154 valid = true;
155 }
156 else {
157 while (enu.hasMoreElements()) {
158 String resContentType = enu.nextElement();
159
160 if (contentType.startsWith(resContentType)) {
161 valid = true;
162
163 break;
164 }
165 }
166 }
167
168 if (!valid) {
169 throw new IllegalArgumentException(
170 contentType + " is not a supported mime type");
171 }
172
173 _contentType = contentType;
174
175 _response.setContentType(contentType);
176 }
177
178 protected void init(
179 PortletRequestImpl portletRequestImpl, HttpServletResponse response,
180 String portletName, long companyId, long plid) {
181
182 super.init(portletRequestImpl, response, portletName, companyId, plid);
183
184 _portletRequestImpl = portletRequestImpl;
185 _response = response;
186 }
187
188 protected void recycle() {
189 super.recycle();
190
191 _portletRequestImpl = null;
192 _response = null;
193 _contentType = null;
194 _calledGetPortletOutputStream = false;
195 _calledGetWriter = false;
196 _calledFlushBuffer = true;
197 }
198
199 private PortletRequestImpl _portletRequestImpl;
200 private HttpServletResponse _response;
201 private String _contentType;
202 private boolean _calledGetPortletOutputStream;
203 private boolean _calledGetWriter;
204 private boolean _calledFlushBuffer;
205
206 }