1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
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  import com.liferay.portal.util.QNameUtil;
26  
27  import java.io.Serializable;
28  
29  import java.util.ArrayList;
30  import java.util.LinkedHashMap;
31  import java.util.List;
32  import java.util.Map;
33  
34  import javax.portlet.Event;
35  import javax.portlet.PortletMode;
36  import javax.portlet.PortletModeException;
37  import javax.portlet.StateAwareResponse;
38  import javax.portlet.WindowState;
39  import javax.portlet.WindowStateException;
40  
41  import javax.servlet.http.HttpServletResponse;
42  
43  import javax.xml.XMLConstants;
44  import javax.xml.namespace.QName;
45  
46  /**
47   * <a href="StateAwareResponseImpl.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
50   */
51  public abstract class StateAwareResponseImpl
52      extends PortletResponseImpl implements StateAwareResponse {
53  
54      public String getDefaultNamespace() {
55          Portlet portlet = getPortlet();
56  
57          if (portlet != null) {
58              return portlet.getPortletApp().getDefaultNamespace();
59          }
60          else {
61              return XMLConstants.NULL_NS_URI;
62          }
63      }
64  
65      public List<Event> getEvents() {
66          return _events;
67      }
68  
69      public Layout getLayout() {
70          return _layout;
71      }
72  
73      public PortletMode getPortletMode() {
74          return _portletMode;
75      }
76  
77      public String getRedirectLocation() {
78          return _redirectLocation;
79      }
80  
81      public Map<String, String[]> getRenderParameterMap() {
82          return _params;
83      }
84  
85      public User getUser() {
86          return _user;
87      }
88  
89      public WindowState getWindowState() {
90          return _windowState;
91      }
92  
93      public boolean isCalledSetRenderParameter() {
94          return _calledSetRenderParameter;
95      }
96  
97      public void removePublicRenderParameter(String name) {
98          if (name == null) {
99              throw new IllegalArgumentException();
100         }
101 
102         PublicRenderParameter publicRenderParameter =
103             getPortlet().getPublicRenderParameter(name);
104 
105         if (publicRenderParameter == null) {
106             if (_log.isWarnEnabled()) {
107                 _log.warn("Public parameter " + name + "does not exist");
108             }
109 
110             return;
111         }
112 
113         QName qName = publicRenderParameter.getQName();
114 
115         String key = QNameUtil.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 void recycle() {
294         super.recycle();
295 
296         _portletRequestImpl = null;
297         _portletName = null;
298         _user = null;
299         _layout = null;
300         _windowState = null;
301         _portletMode = null;
302         _params.clear();
303         _publicRenderParameters = null;
304         _events.clear();
305         _redirectLocation = null;
306         _calledSetRenderParameter = false;
307     }
308 
309     protected boolean setPublicRenderParameter(String name, String[] values) {
310         Portlet portlet = getPortlet();
311 
312         PublicRenderParameter publicRenderParameter =
313             portlet.getPublicRenderParameter(name);
314 
315         if (publicRenderParameter == null) {
316             return false;
317         }
318 
319         QName qName = publicRenderParameter.getQName();
320 
321         if (_publicRenderParameters.containsKey(name)) {
322             String[] oldValues = _publicRenderParameters.get(name);
323 
324             values = ArrayUtil.append(oldValues, values);
325         }
326 
327         _publicRenderParameters.put(QNameUtil.getKey(qName), values);
328 
329         return true;
330     }
331 
332     private static Log _log = LogFactoryUtil.getLog(
333         StateAwareResponseImpl.class);
334 
335     private boolean _calledSetRenderParameter;
336     private List<Event> _events = new ArrayList<Event>();
337     private Layout _layout;
338     private Map<String, String[]> _params =
339         new LinkedHashMap<String, String[]>();
340     private PortletMode _portletMode;
341     private String _portletName;
342     private PortletRequestImpl _portletRequestImpl;
343     private Map<String, String[]> _publicRenderParameters;
344     private String _redirectLocation;
345     private User _user;
346     private WindowState _windowState;
347 
348 }