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.model.Layout;
26  import com.liferay.portal.model.Portlet;
27  import com.liferay.portal.model.User;
28  import com.liferay.portal.util.PortalUtil;
29  
30  import java.io.Serializable;
31  
32  import java.util.ArrayList;
33  import java.util.LinkedHashMap;
34  import java.util.List;
35  import java.util.Map;
36  
37  import javax.portlet.Event;
38  import javax.portlet.PortletMode;
39  import javax.portlet.PortletModeException;
40  import javax.portlet.StateAwareResponse;
41  import javax.portlet.WindowState;
42  import javax.portlet.WindowStateException;
43  
44  import javax.servlet.http.HttpServletResponse;
45  
46  import javax.xml.XMLConstants;
47  import javax.xml.namespace.QName;
48  
49  /**
50   * <a href="StateAwareResponseImpl.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Brian Wing Shun Chan
53   *
54   */
55  public abstract class StateAwareResponseImpl
56      extends PortletResponseImpl implements StateAwareResponse {
57  
58      public String getDefaultNamespace() {
59          Portlet portlet = getPortlet();
60  
61          if (portlet != null) {
62              return portlet.getPortletApp().getDefaultNamespace();
63          }
64          else {
65              return XMLConstants.NULL_NS_URI;
66          }
67      }
68  
69      public List<Event> getEvents() {
70          return _events;
71      }
72  
73      public Layout getLayout() {
74          return _layout;
75      }
76  
77      public PortletMode getPortletMode() {
78          return _portletMode;
79      }
80  
81      public String getRedirectLocation() {
82          return _redirectLocation;
83      }
84  
85      public Map<String, String[]> getRenderParameterMap() {
86          return _params;
87      }
88  
89      public User getUser() {
90          return _user;
91      }
92  
93      public WindowState getWindowState() {
94          return _windowState;
95      }
96  
97      public boolean isCalledSetRenderParameter() {
98          return _calledSetRenderParameter;
99      }
100 
101     public void removePublicRenderParameter(String name) {
102         if (name == null) {
103             throw new IllegalArgumentException();
104         }
105     }
106 
107     public void setEvent(QName name, Serializable value) {
108         if (name == null) {
109             throw new IllegalArgumentException();
110         }
111 
112         _events.add(new EventImpl(name.getLocalPart(), name, value));
113     }
114 
115     public void setEvent(String name, Serializable value) {
116         if (name == null) {
117             throw new IllegalArgumentException();
118         }
119 
120         setEvent(new QName(getDefaultNamespace(), name), value);
121     }
122 
123     public void setPortletMode(PortletMode portletMode)
124         throws PortletModeException {
125 
126         if (_redirectLocation != null) {
127             throw new IllegalStateException();
128         }
129 
130         if (!_portletRequestImpl.isPortletModeAllowed(portletMode)) {
131             throw new PortletModeException(portletMode.toString(), portletMode);
132         }
133 
134         try {
135             _portletMode = PortalUtil.updatePortletMode(
136                 _portletName, _user, _layout, portletMode,
137                 _portletRequestImpl.getHttpServletRequest());
138 
139             _portletRequestImpl.setPortletMode(_portletMode);
140         }
141         catch (Exception e) {
142             throw new PortletModeException(e, portletMode);
143         }
144 
145         _calledSetRenderParameter = true;
146     }
147 
148     public void setRedirectLocation(String redirectLocation) {
149         _redirectLocation = redirectLocation;
150     }
151 
152     public void setRenderParameter(String name, String value) {
153         if (_redirectLocation != null) {
154             throw new IllegalStateException();
155         }
156 
157         if ((name == null) || (value == null)) {
158             throw new IllegalArgumentException();
159         }
160 
161         setRenderParameter(name, new String[] {value});
162     }
163 
164     public void setRenderParameter(String name, String[] values) {
165         if (_redirectLocation != null) {
166             throw new IllegalStateException();
167         }
168 
169         if ((name == null) || (values == null)) {
170             throw new IllegalArgumentException();
171         }
172 
173         for (int i = 0; i < values.length; i++) {
174             if (values[i] == null) {
175                 throw new IllegalArgumentException();
176             }
177         }
178 
179         _params.put(
180             PortalUtil.getPortletNamespace(_portletName) + name,
181             values);
182 
183         _calledSetRenderParameter = true;
184     }
185 
186     public void setRenderParameters(Map<String, String[]> params) {
187         if (_redirectLocation != null) {
188             throw new IllegalStateException();
189         }
190 
191         if (params == null) {
192             throw new IllegalArgumentException();
193         }
194         else {
195             Map<String, String[]> newParams =
196                 new LinkedHashMap<String, String[]>();
197 
198             for (Map.Entry<String, String[]> entry : params.entrySet()) {
199                 String key = entry.getKey();
200                 String[] value = entry.getValue();
201 
202                 if (key == null) {
203                     throw new IllegalArgumentException();
204                 }
205                 else if (value == null) {
206                     throw new IllegalArgumentException();
207                 }
208 
209                 newParams.put(
210                     PortalUtil.getPortletNamespace(_portletName) + key,
211                     value);
212             }
213 
214             _params = newParams;
215         }
216 
217         _calledSetRenderParameter = true;
218     }
219 
220     public void setWindowState(WindowState windowState)
221         throws WindowStateException {
222 
223         if (_redirectLocation != null) {
224             throw new IllegalStateException();
225         }
226 
227         if (!_portletRequestImpl.isWindowStateAllowed(windowState)) {
228             throw new WindowStateException(windowState.toString(), windowState);
229         }
230 
231         try {
232             _windowState = PortalUtil.updateWindowState(
233                 _portletName, _user, _layout, windowState,
234                 _portletRequestImpl.getHttpServletRequest());
235 
236             _portletRequestImpl.setWindowState(_windowState);
237         }
238         catch (Exception e) {
239             throw new WindowStateException(e, windowState);
240         }
241 
242         _calledSetRenderParameter = true;
243     }
244 
245     protected void init(
246             PortletRequestImpl portletRequestImpl, HttpServletResponse response,
247             String portletName, User user, Layout layout,
248             WindowState windowState, PortletMode portletMode)
249         throws PortletModeException, WindowStateException {
250 
251         super.init(
252             portletRequestImpl, response, portletName, layout.getCompanyId(),
253             layout.getPlid());
254 
255         _portletRequestImpl = portletRequestImpl;
256         _portletName = portletName;
257         _user = user;
258         _layout = layout;
259         setWindowState(windowState);
260         setPortletMode(portletMode);
261 
262         // Set _calledSetRenderParameter to false because setWindowState and
263         // setPortletMode sets it to true
264 
265         _calledSetRenderParameter = false;
266     }
267 
268     private PortletRequestImpl _portletRequestImpl;
269     private String _portletName;
270     private User _user;
271     private Layout _layout;
272     private WindowState _windowState;
273     private PortletMode _portletMode;
274     private Map<String, String[]> _params =
275         new LinkedHashMap<String, String[]>();
276     private List<Event> _events = new ArrayList<Event>();
277     private String _redirectLocation;
278     private boolean _calledSetRenderParameter;
279 
280 }