001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.ArrayUtil;
020    import com.liferay.portal.model.Layout;
021    import com.liferay.portal.model.Portlet;
022    import com.liferay.portal.model.PublicRenderParameter;
023    import com.liferay.portal.model.User;
024    import com.liferay.portal.util.PortalUtil;
025    
026    import java.io.Serializable;
027    
028    import java.util.ArrayList;
029    import java.util.LinkedHashMap;
030    import java.util.List;
031    import java.util.Map;
032    
033    import javax.portlet.Event;
034    import javax.portlet.PortletMode;
035    import javax.portlet.PortletModeException;
036    import javax.portlet.StateAwareResponse;
037    import javax.portlet.WindowState;
038    import javax.portlet.WindowStateException;
039    
040    import javax.servlet.http.HttpServletResponse;
041    
042    import javax.xml.XMLConstants;
043    import javax.xml.namespace.QName;
044    
045    /**
046     * @author Brian Wing Shun Chan
047     */
048    public abstract class StateAwareResponseImpl
049            extends PortletResponseImpl implements StateAwareResponse {
050    
051            public String getDefaultNamespace() {
052                    Portlet portlet = getPortlet();
053    
054                    if (portlet != null) {
055                            return portlet.getPortletApp().getDefaultNamespace();
056                    }
057                    else {
058                            return XMLConstants.NULL_NS_URI;
059                    }
060            }
061    
062            public List<Event> getEvents() {
063                    return _events;
064            }
065    
066            public Layout getLayout() {
067                    return _layout;
068            }
069    
070            public PortletMode getPortletMode() {
071                    return _portletMode;
072            }
073    
074            public String getRedirectLocation() {
075                    return _redirectLocation;
076            }
077    
078            public Map<String, String[]> getRenderParameterMap() {
079                    return _params;
080            }
081    
082            public User getUser() {
083                    return _user;
084            }
085    
086            public WindowState getWindowState() {
087                    return _windowState;
088            }
089    
090            public boolean isCalledSetRenderParameter() {
091                    return _calledSetRenderParameter;
092            }
093    
094            public void removePublicRenderParameter(String name) {
095                    if (name == null) {
096                            throw new IllegalArgumentException();
097                    }
098    
099                    PublicRenderParameter publicRenderParameter =
100                            getPortlet().getPublicRenderParameter(name);
101    
102                    if (publicRenderParameter == null) {
103                            if (_log.isWarnEnabled()) {
104                                    _log.warn("Public parameter " + name + "does not exist");
105                            }
106    
107                            return;
108                    }
109    
110                    com.liferay.portal.kernel.xml.QName qName =
111                            publicRenderParameter.getQName();
112    
113                    String key = PortletQNameUtil.getKey(qName);
114    
115                    if (_publicRenderParameters.containsKey(key)) {
116                            _publicRenderParameters.remove(key);
117                    }
118            }
119    
120            public void setEvent(QName name, Serializable value) {
121                    if (name == null) {
122                            throw new IllegalArgumentException();
123                    }
124    
125                    _events.add(new EventImpl(name.getLocalPart(), name, value));
126            }
127    
128            public void setEvent(String name, Serializable value) {
129                    if (name == null) {
130                            throw new IllegalArgumentException();
131                    }
132    
133                    setEvent(new QName(getDefaultNamespace(), name), value);
134            }
135    
136            public void setPortletMode(PortletMode portletMode)
137                    throws PortletModeException {
138    
139                    if (_redirectLocation != null) {
140                            throw new IllegalStateException();
141                    }
142    
143                    if (!_portletRequestImpl.isPortletModeAllowed(portletMode)) {
144                            throw new PortletModeException(portletMode.toString(), portletMode);
145                    }
146    
147                    try {
148                            _portletMode = PortalUtil.updatePortletMode(
149                                    _portletName, _user, _layout, portletMode,
150                                    _portletRequestImpl.getHttpServletRequest());
151    
152                            _portletRequestImpl.setPortletMode(_portletMode);
153                    }
154                    catch (Exception e) {
155                            throw new PortletModeException(e, portletMode);
156                    }
157    
158                    _calledSetRenderParameter = true;
159            }
160    
161            public void setRedirectLocation(String redirectLocation) {
162                    _redirectLocation = redirectLocation;
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                    if (!setPublicRenderParameter(name, values)) {
193                            _params.put(name, values);
194                    }
195    
196                    _calledSetRenderParameter = true;
197            }
198    
199            public void setRenderParameters(Map<String, String[]> params) {
200                    if (_redirectLocation != null) {
201                            throw new IllegalStateException();
202                    }
203    
204                    if (params == null) {
205                            throw new IllegalArgumentException();
206                    }
207                    else {
208                            Map<String, String[]> newParams =
209                                    new LinkedHashMap<String, String[]>();
210    
211                            for (Map.Entry<String, String[]> entry : params.entrySet()) {
212                                    String key = entry.getKey();
213                                    String[] value = entry.getValue();
214    
215                                    if (key == null) {
216                                            throw new IllegalArgumentException();
217                                    }
218                                    else if (value == null) {
219                                            throw new IllegalArgumentException();
220                                    }
221    
222                                    if (setPublicRenderParameter(key, value)) {
223                                            continue;
224                                    }
225    
226                                    newParams.put(key, value);
227                            }
228    
229                            _params = newParams;
230                    }
231    
232                    _calledSetRenderParameter = true;
233            }
234    
235            public void setWindowState(WindowState windowState)
236                    throws WindowStateException {
237    
238                    if (_redirectLocation != null) {
239                            throw new IllegalStateException();
240                    }
241    
242                    if (!_portletRequestImpl.isWindowStateAllowed(windowState)) {
243                            throw new WindowStateException(windowState.toString(), windowState);
244                    }
245    
246                    try {
247                            _windowState = PortalUtil.updateWindowState(
248                                    _portletName, _user, _layout, windowState,
249                                    _portletRequestImpl.getHttpServletRequest());
250    
251                            _portletRequestImpl.setWindowState(_windowState);
252                    }
253                    catch (Exception e) {
254                            throw new WindowStateException(e, windowState);
255                    }
256    
257                    _calledSetRenderParameter = true;
258            }
259    
260            protected void init(
261                            PortletRequestImpl portletRequestImpl, HttpServletResponse response,
262                            String portletName, User user, Layout layout,
263                            WindowState windowState, PortletMode portletMode)
264                    throws PortletModeException, WindowStateException {
265    
266                    super.init(
267                            portletRequestImpl, response, portletName, layout.getCompanyId(),
268                            layout.getPlid());
269    
270                    _portletRequestImpl = portletRequestImpl;
271                    _portletName = portletName;
272                    _user = user;
273                    _layout = layout;
274                    _publicRenderParameters = PublicRenderParametersPool.get(
275                            getHttpServletRequest(), layout.getPlid());
276    
277                    if (windowState != null) {
278                            setWindowState(windowState);
279                    }
280    
281                    if (portletMode != null) {
282                            setPortletMode(portletMode);
283                    }
284    
285                    // Set _calledSetRenderParameter to false because setWindowState and
286                    // setPortletMode sets it to true
287    
288                    _calledSetRenderParameter = false;
289            }
290    
291            protected boolean setPublicRenderParameter(String name, String[] values) {
292                    Portlet portlet = getPortlet();
293    
294                    PublicRenderParameter publicRenderParameter =
295                            portlet.getPublicRenderParameter(name);
296    
297                    if (publicRenderParameter == null) {
298                            return false;
299                    }
300    
301                    com.liferay.portal.kernel.xml.QName qName =
302                            publicRenderParameter.getQName();
303    
304                    if (_publicRenderParameters.containsKey(name)) {
305                            String[] oldValues = _publicRenderParameters.get(name);
306    
307                            values = ArrayUtil.append(oldValues, values);
308                    }
309    
310                    _publicRenderParameters.put(PortletQNameUtil.getKey(qName), values);
311    
312                    return true;
313            }
314    
315            private static Log _log = LogFactoryUtil.getLog(
316                    StateAwareResponseImpl.class);
317    
318            private boolean _calledSetRenderParameter;
319            private List<Event> _events = new ArrayList<Event>();
320            private Layout _layout;
321            private Map<String, String[]> _params =
322                    new LinkedHashMap<String, String[]>();
323            private PortletMode _portletMode;
324            private String _portletName;
325            private PortletRequestImpl _portletRequestImpl;
326            private Map<String, String[]> _publicRenderParameters;
327            private String _redirectLocation;
328            private User _user;
329            private WindowState _windowState;
330    
331    }