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 _res.flushBuffer();
56
57 _calledFlushBuffer = true;
58 }
59
60 public int getBufferSize() {
61 return _res.getBufferSize();
62 }
63
64 public CacheControl getCacheControl() {
65 return new CacheControlImpl(null, 0, false, false);
66 }
67
68 public String getCharacterEncoding() {
69 return _res.getCharacterEncoding();
70 }
71
72 public String getContentType() {
73 return _contentType;
74 }
75
76 public Locale getLocale() {
77 return _req.getLocale();
78 }
79
80 public OutputStream getPortletOutputStream() throws IOException {
81 if (_calledGetWriter) {
82 throw new IllegalStateException();
83 }
84
85 if (_contentType == null) {
86 throw new IllegalStateException();
87 }
88
89 _calledGetPortletOutputStream = true;
90
91 return _res.getOutputStream();
92 }
93
94 public PrintWriter getWriter() throws IOException {
95 if (_calledGetPortletOutputStream) {
96 throw new IllegalStateException();
97 }
98
99 if (_contentType == null) {
100 throw new IllegalStateException();
101 }
102
103 _calledGetWriter = true;
104
105 return _res.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 }
122
123 public void resetBuffer() {
124 _res.resetBuffer();
125 }
126
127 public void setBufferSize(int size) {
128 _res.setBufferSize(size);
129 }
130
131 public void setContentType(String contentType) {
132 if (Validator.isNull(contentType)) {
133 throw new IllegalArgumentException();
134 }
135
136 Enumeration<String> enu = _req.getResponseContentTypes();
137
138 boolean valid = false;
139
140 if (getLifecycle().equals(PortletRequest.RESOURCE_PHASE) ||
141 _req.getWindowState().equals(LiferayWindowState.EXCLUSIVE)) {
142
143 valid = true;
144 }
145 else {
146 while (enu.hasMoreElements()) {
147 String resContentType = enu.nextElement();
148
149 if (contentType.startsWith(resContentType)) {
150 valid = true;
151
152 break;
153 }
154 }
155 }
156
157 if (!valid) {
158 throw new IllegalArgumentException();
159 }
160
161 _contentType = contentType;
162 }
163
164 protected void init(
165 PortletRequestImpl req, HttpServletResponse res, String portletName,
166 long companyId, long plid) {
167
168 super.init(req, res, portletName, companyId, plid);
169
170 _req = req;
171 _res = res;
172 }
173
174 protected void recycle() {
175 super.recycle();
176
177 _req = null;
178 _res = null;
179 _contentType = null;
180 _calledGetPortletOutputStream = false;
181 _calledGetWriter = false;
182 _calledFlushBuffer = true;
183 }
184
185 private PortletRequestImpl _req;
186 private HttpServletResponse _res;
187 private String _contentType;
188 private boolean _calledGetPortletOutputStream;
189 private boolean _calledGetWriter;
190 private boolean _calledFlushBuffer;
191
192 }