1
22
23 package com.liferay.portlet;
24
25 import com.liferay.portal.kernel.util.Validator;
26 import com.liferay.portal.model.Layout;
27 import com.liferay.portal.model.Portlet;
28 import com.liferay.portal.model.User;
29 import com.liferay.portal.service.PortletLocalServiceUtil;
30 import com.liferay.portal.util.PortalUtil;
31 import com.liferay.portal.util.WebKeys;
32 import com.liferay.util.CollectionFactory;
33
34 import java.io.IOException;
35
36 import java.lang.reflect.Constructor;
37
38 import java.util.Iterator;
39 import java.util.LinkedHashMap;
40 import java.util.Map;
41
42 import javax.portlet.ActionResponse;
43 import javax.portlet.PortletMode;
44 import javax.portlet.PortletModeException;
45 import javax.portlet.PortletURL;
46 import javax.portlet.WindowState;
47 import javax.portlet.WindowStateException;
48
49 import javax.servlet.http.HttpServletResponse;
50
51 import org.apache.commons.logging.Log;
52 import org.apache.commons.logging.LogFactory;
53
54
60 public class ActionResponseImpl implements ActionResponse {
61
62 public void addProperty(String key, String value) {
63 }
64
65 public void setProperty(String key, String value) {
66 if (_properties == null) {
67 _properties = CollectionFactory.getHashMap();
68 }
69
70 _properties.put(key, new String[] {value});
71 }
72
73 public PortletURL createActionURL() {
74 PortletURL portletURL = createPortletURL(true);
75
76 try {
77 portletURL.setWindowState(_req.getWindowState());
78 }
79 catch (WindowStateException wse) {
80 }
81
82 try {
83 portletURL.setPortletMode(_req.getPortletMode());
84 }
85 catch (PortletModeException pme) {
86 }
87
88 return portletURL;
89 }
90
91 public PortletURL createRenderURL() {
92 PortletURL portletURL = createPortletURL(false);
93
94 try {
95 portletURL.setWindowState(_req.getWindowState());
96 }
97 catch (WindowStateException wse) {
98 }
99
100 try {
101 portletURL.setPortletMode(_req.getPortletMode());
102 }
103 catch (PortletModeException pme) {
104 }
105
106 return portletURL;
107 }
108
109 public String getNamespace() {
110 return PortalUtil.getPortletNamespace(_portletName);
111 }
112
113 public String encodeURL(String path) {
114 return path;
115 }
116
117 public void setWindowState(WindowState windowState)
118 throws WindowStateException {
119
120 if (_redirectLocation != null) {
121 throw new IllegalStateException();
122 }
123
124 if (!_req.isWindowStateAllowed(windowState)) {
125 throw new WindowStateException(windowState.toString(), windowState);
126 }
127
128 try {
129 _windowState = PortalUtil.updateWindowState(
130 _portletName, _user, _layout, windowState,
131 _req.getHttpServletRequest());
132
133 _req.setWindowState(_windowState);
134 }
135 catch (Exception e) {
136 throw new WindowStateException(e, windowState);
137 }
138
139 _calledSetRenderParameter = true;
140 }
141
142 public void setPortletMode(PortletMode portletMode)
143 throws PortletModeException {
144
145 if (_redirectLocation != null) {
146 throw new IllegalStateException();
147 }
148
149 if (!_req.isPortletModeAllowed(portletMode)) {
150 throw new PortletModeException(portletMode.toString(), portletMode);
151 }
152
153 try {
154 _portletMode = PortalUtil.updatePortletMode(
155 _portletName, _user, _layout, portletMode,
156 _req.getHttpServletRequest());
157
158 _req.setPortletMode(_portletMode);
159 }
160 catch (Exception e) {
161 throw new PortletModeException(e, portletMode);
162 }
163
164 _calledSetRenderParameter = true;
165 }
166
167 public Map getRenderParameters() {
168 return _params;
169 }
170
171 public void setRenderParameter(String name, String value) {
172 if (_redirectLocation != null) {
173 throw new IllegalStateException();
174 }
175
176 if ((name == null) || (value == null)) {
177 throw new IllegalArgumentException();
178 }
179
180 setRenderParameter(name, new String[] {value});
181 }
182
183 public void setRenderParameter(String name, String[] values) {
184 if (_redirectLocation != null) {
185 throw new IllegalStateException();
186 }
187
188 if ((name == null) || (values == null)) {
189 throw new IllegalArgumentException();
190 }
191
192 for (int i = 0; i < values.length; i++) {
193 if (values[i] == null) {
194 throw new IllegalArgumentException();
195 }
196 }
197
198 _params.put(
199 PortalUtil.getPortletNamespace(_portletName) + name,
200 values);
201
202 _calledSetRenderParameter = true;
203 }
204
205 public void setRenderParameters(Map params) {
206 if (_redirectLocation != null) {
207 throw new IllegalStateException();
208 }
209
210 if (params == null) {
211 throw new IllegalArgumentException();
212 }
213 else {
214 Map newParams = new LinkedHashMap();
215
216 Iterator itr = params.entrySet().iterator();
217
218 while (itr.hasNext()) {
219 Map.Entry entry = (Map.Entry)itr.next();
220
221 Object key = entry.getKey();
222 Object value = entry.getValue();
223
224 if (key == null) {
225 throw new IllegalArgumentException();
226 }
227 else if (value == null) {
228 throw new IllegalArgumentException();
229 }
230
231 if (value instanceof String[]) {
232 newParams.put(
233 PortalUtil.getPortletNamespace(_portletName) + key,
234 value);
235 }
236 else {
237 throw new IllegalArgumentException();
238 }
239 }
240
241 _params = newParams;
242 }
243
244 _calledSetRenderParameter = true;
245 }
246
247 public String getRedirectLocation() {
248 return _redirectLocation;
249 }
250
251 public void sendRedirect(String location) throws IOException {
252 if ((location == null) ||
253 (!location.startsWith("/") && (location.indexOf("://") == -1))) {
254
255 throw new IllegalArgumentException(
256 location + " is not a valid redirect");
257 }
258
259 if (_calledSetRenderParameter) {
260 throw new IllegalStateException(
261 "Set render parameter has already been called");
262 }
263
264 _redirectLocation = location;
265 }
266
267 public HttpServletResponse getHttpServletResponse() {
268 return _res;
269 }
270
271 protected PortletURL createPortletURL(boolean action) {
272
273
276 Portlet portlet = getPortlet();
277
278 String portletURLClass = portlet.getPortletURLClass();
279
280 if (Validator.isNotNull(portletURLClass)) {
281 try {
282 Class portletURLClassObj = Class.forName(portletURLClass);
283
284 Constructor constructor = portletURLClassObj.getConstructor(
285 new Class[] {
286 com.liferay.portlet.ActionResponseImpl.class,
287 boolean.class
288 });
289
290 return (PortletURL)constructor.newInstance(
291 new Object[] {this, Boolean.valueOf(action)});
292 }
293 catch (Exception e) {
294 _log.error(e);
295 }
296 }
297
298 return new PortletURLImpl(
299 _req, _portletName, _layout.getPlid(), action);
300 }
301
302 protected Layout getLayout() {
303 return _layout;
304 }
305
306 protected long getPlid() {
307 return _plid;
308 }
309
310 protected void setPlid(long plid) {
311 _plid = plid;
312
313 if (_plid <= 0) {
314 Layout layout = (Layout)_req.getAttribute(WebKeys.LAYOUT);
315
316 if (layout != null) {
317 _plid = layout.getPlid();
318 }
319 }
320 }
321
322 protected Map getParameterMap() {
323 return _params;
324 }
325
326 protected PortletMode getPortletMode() {
327 return _portletMode;
328 }
329
330 protected String getPortletName() {
331 return _portletName;
332 }
333
334 public Portlet getPortlet() {
335 if (_portlet == null) {
336 try {
337 _portlet = PortletLocalServiceUtil.getPortletById(
338 _companyId, _portletName);
339 }
340 catch (Exception e) {
341 _log.error(e);
342 }
343 }
344
345 return _portlet;
346 }
347
348 protected ActionResponseImpl() {
349 if (_log.isDebugEnabled()) {
350 _log.debug("Creating new instance " + hashCode());
351 }
352 }
353
354 protected void init(
355 ActionRequestImpl req, HttpServletResponse res, String portletName,
356 User user, Layout layout, WindowState windowState,
357 PortletMode portletMode)
358 throws PortletModeException, WindowStateException {
359
360 _req = req;
361 _res = res;
362 _portletName = portletName;
363 _companyId = layout.getCompanyId();
364 _user = user;
365 _layout = layout;
366 setPlid(layout.getPlid());
367 setWindowState(windowState);
368 setPortletMode(portletMode);
369 _params = new LinkedHashMap();
370 _calledSetRenderParameter = false;
371 }
372
373 protected void recycle() {
374 if (_log.isDebugEnabled()) {
375 _log.debug("Recycling instance " + hashCode());
376 }
377
378 _req = null;
379 _res = null;
380 _portletName = null;
381 _companyId = 0;
382 _user = null;
383 _layout = null;
384 _plid = 0;
385 _windowState = null;
386 _portletMode = null;
387 _params = new LinkedHashMap();
388 _redirectLocation = null;
389 _calledSetRenderParameter = false;
390 }
391
392 protected ActionRequestImpl getReq() {
393 return _req;
394 }
395
396 protected User getUser() {
397 return _user;
398 }
399
400 protected Map getProperties() {
401 return _properties;
402 }
403
404 protected WindowState getWindowState() {
405 return _windowState;
406 }
407
408 protected boolean isCalledSetRenderParameter() {
409 return _calledSetRenderParameter;
410 }
411
412 private static Log _log = LogFactory.getLog(ActionResponseImpl.class);
413
414 private ActionRequestImpl _req;
415 private HttpServletResponse _res;
416 private String _portletName;
417 private Portlet _portlet;
418 private long _companyId;
419 private User _user;
420 private Layout _layout;
421 private long _plid;
422 private Map _properties;
423 private WindowState _windowState;
424 private PortletMode _portletMode;
425 private Map _params;
426 private String _redirectLocation;
427 private boolean _calledSetRenderParameter;
428
429 }