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