001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet;
022:
023: import com.liferay.portal.kernel.util.Validator;
024: import com.liferay.portal.model.Layout;
025: import com.liferay.portal.model.Portlet;
026: import com.liferay.portal.model.User;
027: import com.liferay.portal.service.PortletLocalServiceUtil;
028: import com.liferay.portal.util.PortalUtil;
029: import com.liferay.portal.util.WebKeys;
030: import com.liferay.util.CollectionFactory;
031:
032: import java.io.IOException;
033:
034: import java.lang.reflect.Constructor;
035:
036: import java.util.Iterator;
037: import java.util.LinkedHashMap;
038: import java.util.Map;
039:
040: import javax.portlet.ActionResponse;
041: import javax.portlet.PortletMode;
042: import javax.portlet.PortletModeException;
043: import javax.portlet.PortletURL;
044: import javax.portlet.WindowState;
045: import javax.portlet.WindowStateException;
046:
047: import javax.servlet.http.HttpServletResponse;
048:
049: import org.apache.commons.logging.Log;
050: import org.apache.commons.logging.LogFactory;
051:
052: /**
053: * <a href="ActionResponseImpl.java.html"><b><i>View Source</i></b></a>
054: *
055: * @author Brian Wing Shun Chan
056: *
057: */
058: public class ActionResponseImpl implements ActionResponse {
059:
060: public void addProperty(String key, String value) {
061: }
062:
063: public void setProperty(String key, String value) {
064: if (_properties == null) {
065: _properties = CollectionFactory.getHashMap();
066: }
067:
068: _properties.put(key, new String[] { value });
069: }
070:
071: public PortletURL createActionURL() {
072: PortletURL portletURL = createPortletURL(true);
073:
074: try {
075: portletURL.setWindowState(_req.getWindowState());
076: } catch (WindowStateException wse) {
077: }
078:
079: try {
080: portletURL.setPortletMode(_req.getPortletMode());
081: } catch (PortletModeException pme) {
082: }
083:
084: return portletURL;
085: }
086:
087: public PortletURL createRenderURL() {
088: PortletURL portletURL = createPortletURL(false);
089:
090: try {
091: portletURL.setWindowState(_req.getWindowState());
092: } catch (WindowStateException wse) {
093: }
094:
095: try {
096: portletURL.setPortletMode(_req.getPortletMode());
097: } catch (PortletModeException pme) {
098: }
099:
100: return portletURL;
101: }
102:
103: public String getNamespace() {
104: return PortalUtil.getPortletNamespace(_portletName);
105: }
106:
107: public String encodeURL(String path) {
108: return path;
109: }
110:
111: public void setWindowState(WindowState windowState)
112: throws WindowStateException {
113:
114: if (_redirectLocation != null) {
115: throw new IllegalStateException();
116: }
117:
118: if (!_req.isWindowStateAllowed(windowState)) {
119: throw new WindowStateException(windowState.toString(),
120: windowState);
121: }
122:
123: try {
124: _windowState = PortalUtil.updateWindowState(_portletName,
125: _user, _layout, windowState, _req
126: .getHttpServletRequest());
127:
128: _req.setWindowState(_windowState);
129: } catch (Exception e) {
130: throw new WindowStateException(e, windowState);
131: }
132:
133: _calledSetRenderParameter = true;
134: }
135:
136: public void setPortletMode(PortletMode portletMode)
137: throws PortletModeException {
138:
139: if (_redirectLocation != null) {
140: throw new IllegalStateException();
141: }
142:
143: if (!_req.isPortletModeAllowed(portletMode)) {
144: throw new PortletModeException(portletMode.toString(),
145: portletMode);
146: }
147:
148: try {
149: _portletMode = PortalUtil.updatePortletMode(_portletName,
150: _user, _layout, portletMode, _req
151: .getHttpServletRequest());
152:
153: _req.setPortletMode(_portletMode);
154: } catch (Exception e) {
155: throw new PortletModeException(e, portletMode);
156: }
157:
158: _calledSetRenderParameter = true;
159: }
160:
161: public Map getRenderParameters() {
162: return _params;
163: }
164:
165: public void setRenderParameter(String name, String value) {
166: if (_redirectLocation != null) {
167: throw new IllegalStateException();
168: }
169:
170: if ((name == null) || (value == null)) {
171: throw new IllegalArgumentException();
172: }
173:
174: setRenderParameter(name, new String[] { value });
175: }
176:
177: public void setRenderParameter(String name, String[] values) {
178: if (_redirectLocation != null) {
179: throw new IllegalStateException();
180: }
181:
182: if ((name == null) || (values == null)) {
183: throw new IllegalArgumentException();
184: }
185:
186: for (int i = 0; i < values.length; i++) {
187: if (values[i] == null) {
188: throw new IllegalArgumentException();
189: }
190: }
191:
192: _params.put(
193: PortalUtil.getPortletNamespace(_portletName) + name,
194: values);
195:
196: _calledSetRenderParameter = true;
197: }
198:
199: public void setRenderParameters(Map params) {
200: if (_redirectLocation != null) {
201: throw new IllegalStateException();
202: }
203:
204: if (params == null) {
205: throw new IllegalArgumentException();
206: } else {
207: Map newParams = new LinkedHashMap();
208:
209: Iterator itr = params.entrySet().iterator();
210:
211: while (itr.hasNext()) {
212: Map.Entry entry = (Map.Entry) itr.next();
213:
214: Object key = entry.getKey();
215: Object value = entry.getValue();
216:
217: if (key == null) {
218: throw new IllegalArgumentException();
219: } else if (value == null) {
220: throw new IllegalArgumentException();
221: }
222:
223: if (value instanceof String[]) {
224: newParams.put(PortalUtil
225: .getPortletNamespace(_portletName)
226: + key, value);
227: } else {
228: throw new IllegalArgumentException();
229: }
230: }
231:
232: _params = newParams;
233: }
234:
235: _calledSetRenderParameter = true;
236: }
237:
238: public String getRedirectLocation() {
239: return _redirectLocation;
240: }
241:
242: public void sendRedirect(String location) throws IOException {
243: if ((location == null)
244: || (!location.startsWith("/") && (location
245: .indexOf("://") == -1))) {
246:
247: throw new IllegalArgumentException(location
248: + " is not a valid redirect");
249: }
250:
251: if (_calledSetRenderParameter) {
252: throw new IllegalStateException(
253: "Set render parameter has already been called");
254: }
255:
256: _redirectLocation = location;
257: }
258:
259: public HttpServletResponse getHttpServletResponse() {
260: return _res;
261: }
262:
263: protected PortletURL createPortletURL(boolean action) {
264:
265: // Wrap portlet URL with a custom wrapper if and only if a custom
266: // wrapper for the portlet has been defined
267:
268: Portlet portlet = getPortlet();
269:
270: String portletURLClass = portlet.getPortletURLClass();
271:
272: if (Validator.isNotNull(portletURLClass)) {
273: try {
274: Class portletURLClassObj = Class
275: .forName(portletURLClass);
276:
277: Constructor constructor = portletURLClassObj
278: .getConstructor(new Class[] {
279: com.liferay.portlet.ActionResponseImpl.class,
280: boolean.class });
281:
282: return (PortletURL) constructor
283: .newInstance(new Object[] { this ,
284: Boolean.valueOf(action) });
285: } catch (Exception e) {
286: _log.error(e);
287: }
288: }
289:
290: return new PortletURLImpl(_req, _portletName,
291: _layout.getPlid(), action);
292: }
293:
294: protected Layout getLayout() {
295: return _layout;
296: }
297:
298: protected long getPlid() {
299: return _plid;
300: }
301:
302: protected void setPlid(long plid) {
303: _plid = plid;
304:
305: if (_plid <= 0) {
306: Layout layout = (Layout) _req.getAttribute(WebKeys.LAYOUT);
307:
308: if (layout != null) {
309: _plid = layout.getPlid();
310: }
311: }
312: }
313:
314: protected Map getParameterMap() {
315: return _params;
316: }
317:
318: protected PortletMode getPortletMode() {
319: return _portletMode;
320: }
321:
322: protected String getPortletName() {
323: return _portletName;
324: }
325:
326: public Portlet getPortlet() {
327: if (_portlet == null) {
328: try {
329: _portlet = PortletLocalServiceUtil.getPortletById(
330: _companyId, _portletName);
331: } catch (Exception e) {
332: _log.error(e);
333: }
334: }
335:
336: return _portlet;
337: }
338:
339: protected ActionResponseImpl() {
340: if (_log.isDebugEnabled()) {
341: _log.debug("Creating new instance " + hashCode());
342: }
343: }
344:
345: protected void init(ActionRequestImpl req, HttpServletResponse res,
346: String portletName, User user, Layout layout,
347: WindowState windowState, PortletMode portletMode)
348: throws PortletModeException, WindowStateException {
349:
350: _req = req;
351: _res = res;
352: _portletName = portletName;
353: _companyId = layout.getCompanyId();
354: _user = user;
355: _layout = layout;
356: setPlid(layout.getPlid());
357: setWindowState(windowState);
358: setPortletMode(portletMode);
359: _params = new LinkedHashMap();
360: _calledSetRenderParameter = false;
361: }
362:
363: protected void recycle() {
364: if (_log.isDebugEnabled()) {
365: _log.debug("Recycling instance " + hashCode());
366: }
367:
368: _req = null;
369: _res = null;
370: _portletName = null;
371: _companyId = 0;
372: _user = null;
373: _layout = null;
374: _plid = 0;
375: _windowState = null;
376: _portletMode = null;
377: _params = new LinkedHashMap();
378: _redirectLocation = null;
379: _calledSetRenderParameter = false;
380: }
381:
382: protected ActionRequestImpl getReq() {
383: return _req;
384: }
385:
386: protected User getUser() {
387: return _user;
388: }
389:
390: protected Map getProperties() {
391: return _properties;
392: }
393:
394: protected WindowState getWindowState() {
395: return _windowState;
396: }
397:
398: protected boolean isCalledSetRenderParameter() {
399: return _calledSetRenderParameter;
400: }
401:
402: private static Log _log = LogFactory
403: .getLog(ActionResponseImpl.class);
404:
405: private ActionRequestImpl _req;
406: private HttpServletResponse _res;
407: private String _portletName;
408: private Portlet _portlet;
409: private long _companyId;
410: private User _user;
411: private Layout _layout;
412: private long _plid;
413: private Map _properties;
414: private WindowState _windowState;
415: private PortletMode _portletMode;
416: private Map _params;
417: private String _redirectLocation;
418: private boolean _calledSetRenderParameter;
419:
420: }
|