1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.ArrayUtil;
28  import com.liferay.portal.model.Layout;
29  import com.liferay.portal.model.Portlet;
30  import com.liferay.portal.model.PublicRenderParameter;
31  import com.liferay.portal.model.User;
32  import com.liferay.portal.util.PortalUtil;
33  import com.liferay.portal.util.QNameUtil;
34  
35  import java.io.Serializable;
36  
37  import java.util.ArrayList;
38  import java.util.LinkedHashMap;
39  import java.util.List;
40  import java.util.Map;
41  
42  import javax.portlet.Event;
43  import javax.portlet.PortletMode;
44  import javax.portlet.PortletModeException;
45  import javax.portlet.StateAwareResponse;
46  import javax.portlet.WindowState;
47  import javax.portlet.WindowStateException;
48  
49  import javax.servlet.http.HttpServletResponse;
50  
51  import javax.xml.XMLConstants;
52  import javax.xml.namespace.QName;
53  
54  /**
55   * <a href="StateAwareResponseImpl.java.html"><b><i>View Source</i></b></a>
56   *
57   * @author Brian Wing Shun Chan
58   */
59  public abstract class StateAwareResponseImpl
60      extends PortletResponseImpl implements StateAwareResponse {
61  
62      public String getDefaultNamespace() {
63          Portlet portlet = getPortlet();
64  
65          if (portlet != null) {
66              return portlet.getPortletApp().getDefaultNamespace();
67          }
68          else {
69              return XMLConstants.NULL_NS_URI;
70          }
71      }
72  
73      public List<Event> getEvents() {
74          return _events;
75      }
76  
77      public Layout getLayout() {
78          return _layout;
79      }
80  
81      public PortletMode getPortletMode() {
82          return _portletMode;
83      }
84  
85      public String getRedirectLocation() {
86          return _redirectLocation;
87      }
88  
89      public Map<String, String[]> getRenderParameterMap() {
90          return _params;
91      }
92  
93      public User getUser() {
94          return _user;
95      }
96  
97      public WindowState getWindowState() {
98          return _windowState;
99      }
100 
101     public boolean isCalledSetRenderParameter() {
102         return _calledSetRenderParameter;
103     }
104 
105     public void removePublicRenderParameter(String name) {
106         if (name == null) {
107             throw new IllegalArgumentException();
108         }
109 
110         PublicRenderParameter publicRenderParameter =
111             getPortlet().getPublicRenderParameter(name);
112 
113         if (publicRenderParameter == null) {
114             if (_log.isWarnEnabled()) {
115                 _log.warn("Public parameter " + name + "does not exist");
116             }
117 
118             return;
119         }
120 
121         QName qName = publicRenderParameter.getQName();
122 
123         String key = QNameUtil.getKey(qName);
124 
125         if (_publicRenderParameters.containsKey(key)) {
126             _publicRenderParameters.remove(key);
127         }
128     }
129 
130     public void setEvent(QName name, Serializable value) {
131         if (name == null) {
132             throw new IllegalArgumentException();
133         }
134 
135         _events.add(new EventImpl(name.getLocalPart(), name, value));
136     }
137 
138     public void setEvent(String name, Serializable value) {
139         if (name == null) {
140             throw new IllegalArgumentException();
141         }
142 
143         setEvent(new QName(getDefaultNamespace(), name), value);
144     }
145 
146     public void setPortletMode(PortletMode portletMode)
147         throws PortletModeException {
148 
149         if (_redirectLocation != null) {
150             throw new IllegalStateException();
151         }
152 
153         if (!_portletRequestImpl.isPortletModeAllowed(portletMode)) {
154             throw new PortletModeException(portletMode.toString(), portletMode);
155         }
156 
157         try {
158             _portletMode = PortalUtil.updatePortletMode(
159                 _portletName, _user, _layout, portletMode,
160                 _portletRequestImpl.getHttpServletRequest());
161 
162             _portletRequestImpl.setPortletMode(_portletMode);
163         }
164         catch (Exception e) {
165             throw new PortletModeException(e, portletMode);
166         }
167 
168         _calledSetRenderParameter = true;
169     }
170 
171     public void setRedirectLocation(String redirectLocation) {
172         _redirectLocation = redirectLocation;
173     }
174 
175     public void setRenderParameter(String name, String value) {
176         if (_redirectLocation != null) {
177             throw new IllegalStateException();
178         }
179 
180         if ((name == null) || (value == null)) {
181             throw new IllegalArgumentException();
182         }
183 
184         setRenderParameter(name, new String[] {value});
185     }
186 
187     public void setRenderParameter(String name, String[] values) {
188         if (_redirectLocation != null) {
189             throw new IllegalStateException();
190         }
191 
192         if ((name == null) || (values == null)) {
193             throw new IllegalArgumentException();
194         }
195 
196         for (int i = 0; i < values.length; i++) {
197             if (values[i] == null) {
198                 throw new IllegalArgumentException();
199             }
200         }
201 
202         if (!setPublicRenderParameter(name, values)) {
203             _params.put(name, values);
204         }
205 
206         _calledSetRenderParameter = true;
207     }
208 
209     public void setRenderParameters(Map<String, String[]> params) {
210         if (_redirectLocation != null) {
211             throw new IllegalStateException();
212         }
213 
214         if (params == null) {
215             throw new IllegalArgumentException();
216         }
217         else {
218             Map<String, String[]> newParams =
219                 new LinkedHashMap<String, String[]>();
220 
221             for (Map.Entry<String, String[]> entry : params.entrySet()) {
222                 String key = entry.getKey();
223                 String[] value = entry.getValue();
224 
225                 if (key == null) {
226                     throw new IllegalArgumentException();
227                 }
228                 else if (value == null) {
229                     throw new IllegalArgumentException();
230                 }
231 
232                 if (setPublicRenderParameter(key, value)) {
233                     continue;
234                 }
235 
236                 newParams.put(key, value);
237             }
238 
239             _params = newParams;
240         }
241 
242         _calledSetRenderParameter = true;
243     }
244 
245     public void setWindowState(WindowState windowState)
246         throws WindowStateException {
247 
248         if (_redirectLocation != null) {
249             throw new IllegalStateException();
250         }
251 
252         if (!_portletRequestImpl.isWindowStateAllowed(windowState)) {
253             throw new WindowStateException(windowState.toString(), windowState);
254         }
255 
256         try {
257             _windowState = PortalUtil.updateWindowState(
258                 _portletName, _user, _layout, windowState,
259                 _portletRequestImpl.getHttpServletRequest());
260 
261             _portletRequestImpl.setWindowState(_windowState);
262         }
263         catch (Exception e) {
264             throw new WindowStateException(e, windowState);
265         }
266 
267         _calledSetRenderParameter = true;
268     }
269 
270     protected void init(
271             PortletRequestImpl portletRequestImpl, HttpServletResponse response,
272             String portletName, User user, Layout layout,
273             WindowState windowState, PortletMode portletMode)
274         throws PortletModeException, WindowStateException {
275 
276         super.init(
277             portletRequestImpl, response, portletName, layout.getCompanyId(),
278             layout.getPlid());
279 
280         _portletRequestImpl = portletRequestImpl;
281         _portletName = portletName;
282         _user = user;
283         _layout = layout;
284         _publicRenderParameters = PublicRenderParametersPool.get(
285             getHttpServletRequest(), layout.getPlid());
286 
287         setWindowState(windowState);
288         setPortletMode(portletMode);
289 
290         // Set _calledSetRenderParameter to false because setWindowState and
291         // setPortletMode sets it to true
292 
293         _calledSetRenderParameter = false;
294     }
295 
296     protected void recycle() {
297         super.recycle();
298 
299         _portletRequestImpl = null;
300         _portletName = null;
301         _user = null;
302         _layout = null;
303         _windowState = null;
304         _portletMode = null;
305         _params.clear();
306         _publicRenderParameters = null;
307         _events.clear();
308         _redirectLocation = null;
309         _calledSetRenderParameter = false;
310     }
311 
312     protected boolean setPublicRenderParameter(String name, String[] values) {
313         Portlet portlet = getPortlet();
314 
315         PublicRenderParameter publicRenderParameter =
316             portlet.getPublicRenderParameter(name);
317 
318         if (publicRenderParameter == null) {
319             return false;
320         }
321 
322         QName qName = publicRenderParameter.getQName();
323 
324         if (_publicRenderParameters.containsKey(name)) {
325             String[] oldValues = _publicRenderParameters.get(name);
326 
327             values = ArrayUtil.append(oldValues, values);
328         }
329 
330         _publicRenderParameters.put(QNameUtil.getKey(qName), values);
331 
332         return true;
333     }
334 
335     private static Log _log =
336         LogFactoryUtil.getLog(StateAwareResponseImpl.class);
337 
338     private boolean _calledSetRenderParameter;
339     private List<Event> _events = new ArrayList<Event>();
340     private Layout _layout;
341     private Map<String, String[]> _params =
342         new LinkedHashMap<String, String[]>();
343     private PortletMode _portletMode;
344     private String _portletName;
345     private PortletRequestImpl _portletRequestImpl;
346     private Map<String, String[]> _publicRenderParameters;
347     private String _redirectLocation;
348     private User _user;
349     private WindowState _windowState;
350 
351 }