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
47 public abstract class MimeResponseImpl
48 extends PortletResponseImpl implements MimeResponse {
49
50 public boolean isCalledFlushBuffer() {
51 return _calledFlushBuffer;
52 }
53
54 public void flushBuffer() throws IOException {
55 _response.flushBuffer();
56
57 _calledFlushBuffer = true;
58 }
59
60 public int getBufferSize() {
61 return _response.getBufferSize();
62 }
63
64 public CacheControl getCacheControl() {
65 return new CacheControlImpl(null, 0, false, false);
66 }
67
68 public String getCharacterEncoding() {
69 return _response.getCharacterEncoding();
70 }
71
72 public String getContentType() {
73 return _contentType;
74 }
75
76 public Locale getLocale() {
77 return _portletRequestImpl.getLocale();
78 }
79
80 public OutputStream getPortletOutputStream() throws IOException {
81 if (_calledGetWriter) {
82 throw new IllegalStateException();
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 }
98
99 if (_contentType == null) {
100 setContentType(_portletRequestImpl.getResponseContentType());
101 }
102
103 _calledGetWriter = true;
104
105 return _response.getWriter();
106 }
107
108 public boolean isCalledGetPortletOutputStream() {
109 return _calledGetPortletOutputStream;
110 }
111
112 public boolean isCalledGetWriter() {
113 return _calledGetWriter;
114 }
115
116 public boolean isCommitted() {
117 return false;
118 }
119
120 public void reset() {
121 if (_calledFlushBuffer) {
122 throw new IllegalStateException();
123 }
124 }
125
126 public void resetBuffer() {
127 if (_calledFlushBuffer) {
128 throw new IllegalStateException();
129 }
130
131 _response.resetBuffer();
132 }
133
134 public void setBufferSize(int bufferSize) {
135 _response.setBufferSize(bufferSize);
136 }
137
138 public void setContentType(String contentType) {
139 if (Validator.isNull(contentType)) {
140 throw new IllegalArgumentException();
141 }
142
143 Enumeration<String> enu = _portletRequestImpl.getResponseContentTypes();
144
145 boolean valid = false;
146
147 if (getLifecycle().equals(PortletRequest.RESOURCE_PHASE) ||
148 _portletRequestImpl.getWindowState().equals(
149 LiferayWindowState.EXCLUSIVE)) {
150
151 valid = true;
152 }
153 else {
154 while (enu.hasMoreElements()) {
155 String resContentType = enu.nextElement();
156
157 if (contentType.startsWith(resContentType)) {
158 valid = true;
159
160 break;
161 }
162 }
163 }
164
165 if (!valid) {
166 throw new IllegalArgumentException();
167 }
168
169 _contentType = contentType;
170
171 _response.setContentType(contentType);
172 }
173
174 protected void init(
175 PortletRequestImpl portletRequestImpl, HttpServletResponse response,
176 String portletName, long companyId, long plid) {
177
178 super.init(portletRequestImpl, response, portletName, companyId, plid);
179
180 _portletRequestImpl = portletRequestImpl;
181 _response = response;
182 }
183
184 protected void recycle() {
185 super.recycle();
186
187 _portletRequestImpl = null;
188 _response = null;
189 _contentType = null;
190 _calledGetPortletOutputStream = false;
191 _calledGetWriter = false;
192 _calledFlushBuffer = true;
193 }
194
195 private PortletRequestImpl _portletRequestImpl;
196 private HttpServletResponse _response;
197 private String _contentType;
198 private boolean _calledGetPortletOutputStream;
199 private boolean _calledGetWriter;
200 private boolean _calledFlushBuffer;
201
202 }