001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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                    _publicRenderParameters.remove(key);
116            }
117    
118            public void setEvent(QName name, Serializable value) {
119                    if (name == null) {
120                            throw new IllegalArgumentException();
121                    }
122    
123                    _events.add(new EventImpl(name.getLocalPart(), name, value));
124            }
125    
126            public void setEvent(String name, Serializable value) {
127                    if (name == null) {
128                            throw new IllegalArgumentException();
129                    }
130    
131                    setEvent(new QName(getDefaultNamespace(), name), value);
132            }
133    
134            public void setPortletMode(PortletMode portletMode)
135                    throws PortletModeException {
136    
137                    if (_redirectLocation != null) {
138                            throw new IllegalStateException();
139                    }
140    
141                    if (!_portletRequestImpl.isPortletModeAllowed(portletMode)) {
142                            throw new PortletModeException(portletMode.toString(), portletMode);
143                    }
144    
145                    try {
146                            _portletMode = PortalUtil.updatePortletMode(
147                                    _portletName, _user, _layout, portletMode,
148                                    _portletRequestImpl.getHttpServletRequest());
149    
150                            _portletRequestImpl.setPortletMode(_portletMode);
151                    }
152                    catch (Exception e) {
153                            throw new PortletModeException(e, portletMode);
154                    }
155    
156                    _calledSetRenderParameter = true;
157            }
158    
159            public void setRedirectLocation(String redirectLocation) {
160                    _redirectLocation = redirectLocation;
161            }
162    
163            public void setRenderParameter(String name, String value) {
164                    if (_redirectLocation != null) {
165                            throw new IllegalStateException();
166                    }
167    
168                    if ((name == null) || (value == null)) {
169                            throw new IllegalArgumentException();
170                    }
171    
172                    setRenderParameter(name, new String[] {value});
173            }
174    
175            public void setRenderParameter(String name, String[] values) {
176                    if (_redirectLocation != null) {
177                            throw new IllegalStateException();
178                    }
179    
180                    if ((name == null) || (values == null)) {
181                            throw new IllegalArgumentException();
182                    }
183    
184                    for (int i = 0; i < values.length; i++) {
185                            if (values[i] == null) {
186                                    throw new IllegalArgumentException();
187                            }
188                    }
189    
190                    if (!setPublicRenderParameter(name, values)) {
191                            _params.put(name, values);
192                    }
193    
194                    _calledSetRenderParameter = true;
195            }
196    
197            public void setRenderParameters(Map<String, String[]> params) {
198                    if (_redirectLocation != null) {
199                            throw new IllegalStateException();
200                    }
201    
202                    if (params == null) {
203                            throw new IllegalArgumentException();
204                    }
205                    else {
206                            Map<String, String[]> newParams =
207                                    new LinkedHashMap<String, String[]>();
208    
209                            for (Map.Entry<String, String[]> entry : params.entrySet()) {
210                                    String key = entry.getKey();
211                                    String[] value = entry.getValue();
212    
213                                    if (key == null) {
214                                            throw new IllegalArgumentException();
215                                    }
216                                    else if (value == null) {
217                                            throw new IllegalArgumentException();
218                                    }
219    
220                                    if (setPublicRenderParameter(key, value)) {
221                                            continue;
222                                    }
223    
224                                    newParams.put(key, value);
225                            }
226    
227                            _params = newParams;
228                    }
229    
230                    _calledSetRenderParameter = true;
231            }
232    
233            public void setWindowState(WindowState windowState)
234                    throws WindowStateException {
235    
236                    if (_redirectLocation != null) {
237                            throw new IllegalStateException();
238                    }
239    
240                    if (!_portletRequestImpl.isWindowStateAllowed(windowState)) {
241                            throw new WindowStateException(windowState.toString(), windowState);
242                    }
243    
244                    try {
245                            _windowState = PortalUtil.updateWindowState(
246                                    _portletName, _user, _layout, windowState,
247                                    _portletRequestImpl.getHttpServletRequest());
248    
249                            _portletRequestImpl.setWindowState(_windowState);
250                    }
251                    catch (Exception e) {
252                            throw new WindowStateException(e, windowState);
253                    }
254    
255                    _calledSetRenderParameter = true;
256            }
257    
258            protected void init(
259                            PortletRequestImpl portletRequestImpl, HttpServletResponse response,
260                            String portletName, User user, Layout layout,
261                            WindowState windowState, PortletMode portletMode)
262                    throws PortletModeException, WindowStateException {
263    
264                    super.init(
265                            portletRequestImpl, response, portletName, layout.getCompanyId(),
266                            layout.getPlid());
267    
268                    _portletRequestImpl = portletRequestImpl;
269                    _portletName = portletName;
270                    _user = user;
271                    _layout = layout;
272                    _publicRenderParameters = PublicRenderParametersPool.get(
273                            getHttpServletRequest(), layout.getPlid());
274    
275                    if (windowState != null) {
276                            setWindowState(windowState);
277                    }
278    
279                    if (portletMode != null) {
280                            setPortletMode(portletMode);
281                    }
282    
283                    // Set _calledSetRenderParameter to false because setWindowState and
284                    // setPortletMode sets it to true
285    
286                    _calledSetRenderParameter = false;
287            }
288    
289            protected boolean setPublicRenderParameter(String name, String[] values) {
290                    Portlet portlet = getPortlet();
291    
292                    PublicRenderParameter publicRenderParameter =
293                            portlet.getPublicRenderParameter(name);
294    
295                    if (publicRenderParameter == null) {
296                            return false;
297                    }
298    
299                    com.liferay.portal.kernel.xml.QName qName =
300                            publicRenderParameter.getQName();
301    
302                    String[] oldValues = _publicRenderParameters.get(name);
303    
304                    if (oldValues != null) {
305                            values = ArrayUtil.append(oldValues, values);
306                    }
307    
308                    _publicRenderParameters.put(PortletQNameUtil.getKey(qName), values);
309    
310                    return true;
311            }
312    
313            private static Log _log = LogFactoryUtil.getLog(
314                    StateAwareResponseImpl.class);
315    
316            private boolean _calledSetRenderParameter;
317            private List<Event> _events = new ArrayList<Event>();
318            private Layout _layout;
319            private Map<String, String[]> _params =
320                    new LinkedHashMap<String, String[]>();
321            private PortletMode _portletMode;
322            private String _portletName;
323            private PortletRequestImpl _portletRequestImpl;
324            private Map<String, String[]> _publicRenderParameters;
325            private String _redirectLocation;
326            private User _user;
327            private WindowState _windowState;
328    
329    }