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  
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         _publicRenderParameters.remove(key);
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         String[] oldValues = _publicRenderParameters.get(name);
305 
306         if (oldValues != null) {
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 }