1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.ArrayUtil;
20  import com.liferay.portal.model.Layout;
21  import com.liferay.portal.model.Portlet;
22  import com.liferay.portal.model.PublicRenderParameter;
23  import com.liferay.portal.model.User;
24  import com.liferay.portal.util.PortalUtil;
25  
26  import java.io.Serializable;
27  
28  import java.util.ArrayList;
29  import java.util.LinkedHashMap;
30  import java.util.List;
31  import java.util.Map;
32  
33  import javax.portlet.Event;
34  import javax.portlet.PortletMode;
35  import javax.portlet.PortletModeException;
36  import javax.portlet.StateAwareResponse;
37  import javax.portlet.WindowState;
38  import javax.portlet.WindowStateException;
39  
40  import javax.servlet.http.HttpServletResponse;
41  
42  import javax.xml.XMLConstants;
43  import javax.xml.namespace.QName;
44  
45  /**
46   * <a href="StateAwareResponseImpl.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   */
50  public abstract class StateAwareResponseImpl
51      extends PortletResponseImpl implements StateAwareResponse {
52  
53      public String getDefaultNamespace() {
54          Portlet portlet = getPortlet();
55  
56          if (portlet != null) {
57              return portlet.getPortletApp().getDefaultNamespace();
58          }
59          else {
60              return XMLConstants.NULL_NS_URI;
61          }
62      }
63  
64      public List<Event> getEvents() {
65          return _events;
66      }
67  
68      public Layout getLayout() {
69          return _layout;
70      }
71  
72      public PortletMode getPortletMode() {
73          return _portletMode;
74      }
75  
76      public String getRedirectLocation() {
77          return _redirectLocation;
78      }
79  
80      public Map<String, String[]> getRenderParameterMap() {
81          return _params;
82      }
83  
84      public User getUser() {
85          return _user;
86      }
87  
88      public WindowState getWindowState() {
89          return _windowState;
90      }
91  
92      public boolean isCalledSetRenderParameter() {
93          return _calledSetRenderParameter;
94      }
95  
96      public void removePublicRenderParameter(String name) {
97          if (name == null) {
98              throw new IllegalArgumentException();
99          }
100 
101         PublicRenderParameter publicRenderParameter =
102             getPortlet().getPublicRenderParameter(name);
103 
104         if (publicRenderParameter == null) {
105             if (_log.isWarnEnabled()) {
106                 _log.warn("Public parameter " + name + "does not exist");
107             }
108 
109             return;
110         }
111 
112         com.liferay.portal.kernel.xml.QName qName =
113             publicRenderParameter.getQName();
114 
115         String key = PortletQNameUtil.getKey(qName);
116 
117         if (_publicRenderParameters.containsKey(key)) {
118             _publicRenderParameters.remove(key);
119         }
120     }
121 
122     public void setEvent(QName name, Serializable value) {
123         if (name == null) {
124             throw new IllegalArgumentException();
125         }
126 
127         _events.add(new EventImpl(name.getLocalPart(), name, value));
128     }
129 
130     public void setEvent(String name, Serializable value) {
131         if (name == null) {
132             throw new IllegalArgumentException();
133         }
134 
135         setEvent(new QName(getDefaultNamespace(), name), value);
136     }
137 
138     public void setPortletMode(PortletMode portletMode)
139         throws PortletModeException {
140 
141         if (_redirectLocation != null) {
142             throw new IllegalStateException();
143         }
144 
145         if (!_portletRequestImpl.isPortletModeAllowed(portletMode)) {
146             throw new PortletModeException(portletMode.toString(), portletMode);
147         }
148 
149         try {
150             _portletMode = PortalUtil.updatePortletMode(
151                 _portletName, _user, _layout, portletMode,
152                 _portletRequestImpl.getHttpServletRequest());
153 
154             _portletRequestImpl.setPortletMode(_portletMode);
155         }
156         catch (Exception e) {
157             throw new PortletModeException(e, portletMode);
158         }
159 
160         _calledSetRenderParameter = true;
161     }
162 
163     public void setRedirectLocation(String redirectLocation) {
164         _redirectLocation = redirectLocation;
165     }
166 
167     public void setRenderParameter(String name, String value) {
168         if (_redirectLocation != null) {
169             throw new IllegalStateException();
170         }
171 
172         if ((name == null) || (value == null)) {
173             throw new IllegalArgumentException();
174         }
175 
176         setRenderParameter(name, new String[] {value});
177     }
178 
179     public void setRenderParameter(String name, String[] values) {
180         if (_redirectLocation != null) {
181             throw new IllegalStateException();
182         }
183 
184         if ((name == null) || (values == null)) {
185             throw new IllegalArgumentException();
186         }
187 
188         for (int i = 0; i < values.length; i++) {
189             if (values[i] == null) {
190                 throw new IllegalArgumentException();
191             }
192         }
193 
194         if (!setPublicRenderParameter(name, values)) {
195             _params.put(name, values);
196         }
197 
198         _calledSetRenderParameter = true;
199     }
200 
201     public void setRenderParameters(Map<String, String[]> params) {
202         if (_redirectLocation != null) {
203             throw new IllegalStateException();
204         }
205 
206         if (params == null) {
207             throw new IllegalArgumentException();
208         }
209         else {
210             Map<String, String[]> newParams =
211                 new LinkedHashMap<String, String[]>();
212 
213             for (Map.Entry<String, String[]> entry : params.entrySet()) {
214                 String key = entry.getKey();
215                 String[] value = entry.getValue();
216 
217                 if (key == null) {
218                     throw new IllegalArgumentException();
219                 }
220                 else if (value == null) {
221                     throw new IllegalArgumentException();
222                 }
223 
224                 if (setPublicRenderParameter(key, value)) {
225                     continue;
226                 }
227 
228                 newParams.put(key, value);
229             }
230 
231             _params = newParams;
232         }
233 
234         _calledSetRenderParameter = true;
235     }
236 
237     public void setWindowState(WindowState windowState)
238         throws WindowStateException {
239 
240         if (_redirectLocation != null) {
241             throw new IllegalStateException();
242         }
243 
244         if (!_portletRequestImpl.isWindowStateAllowed(windowState)) {
245             throw new WindowStateException(windowState.toString(), windowState);
246         }
247 
248         try {
249             _windowState = PortalUtil.updateWindowState(
250                 _portletName, _user, _layout, windowState,
251                 _portletRequestImpl.getHttpServletRequest());
252 
253             _portletRequestImpl.setWindowState(_windowState);
254         }
255         catch (Exception e) {
256             throw new WindowStateException(e, windowState);
257         }
258 
259         _calledSetRenderParameter = true;
260     }
261 
262     protected void init(
263             PortletRequestImpl portletRequestImpl, HttpServletResponse response,
264             String portletName, User user, Layout layout,
265             WindowState windowState, PortletMode portletMode)
266         throws PortletModeException, WindowStateException {
267 
268         super.init(
269             portletRequestImpl, response, portletName, layout.getCompanyId(),
270             layout.getPlid());
271 
272         _portletRequestImpl = portletRequestImpl;
273         _portletName = portletName;
274         _user = user;
275         _layout = layout;
276         _publicRenderParameters = PublicRenderParametersPool.get(
277             getHttpServletRequest(), layout.getPlid());
278 
279         if (windowState != null) {
280             setWindowState(windowState);
281         }
282 
283         if (portletMode != null) {
284             setPortletMode(portletMode);
285         }
286 
287         // Set _calledSetRenderParameter to false because setWindowState and
288         // setPortletMode sets it to true
289 
290         _calledSetRenderParameter = false;
291     }
292 
293     protected boolean setPublicRenderParameter(String name, String[] values) {
294         Portlet portlet = getPortlet();
295 
296         PublicRenderParameter publicRenderParameter =
297             portlet.getPublicRenderParameter(name);
298 
299         if (publicRenderParameter == null) {
300             return false;
301         }
302 
303         com.liferay.portal.kernel.xml.QName qName =
304             publicRenderParameter.getQName();
305 
306         if (_publicRenderParameters.containsKey(name)) {
307             String[] oldValues = _publicRenderParameters.get(name);
308 
309             values = ArrayUtil.append(oldValues, values);
310         }
311 
312         _publicRenderParameters.put(PortletQNameUtil.getKey(qName), values);
313 
314         return true;
315     }
316 
317     private static Log _log = LogFactoryUtil.getLog(
318         StateAwareResponseImpl.class);
319 
320     private boolean _calledSetRenderParameter;
321     private List<Event> _events = new ArrayList<Event>();
322     private Layout _layout;
323     private Map<String, String[]> _params =
324         new LinkedHashMap<String, String[]>();
325     private PortletMode _portletMode;
326     private String _portletName;
327     private PortletRequestImpl _portletRequestImpl;
328     private Map<String, String[]> _publicRenderParameters;
329     private String _redirectLocation;
330     private User _user;
331     private WindowState _windowState;
332 
333 }