1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet;
21  
22  import com.liferay.portal.model.Layout;
23  import com.liferay.portal.model.Portlet;
24  import com.liferay.portal.model.User;
25  import com.liferay.portal.util.PortalUtil;
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   */
52  public abstract class StateAwareResponseImpl
53      extends PortletResponseImpl implements StateAwareResponse {
54  
55      public String getDefaultNamespace() {
56          Portlet portlet = getPortlet();
57  
58          if (portlet != null) {
59              return portlet.getPortletApp().getDefaultNamespace();
60          }
61          else {
62              return XMLConstants.NULL_NS_URI;
63          }
64      }
65  
66      public List<Event> getEvents() {
67          return _events;
68      }
69  
70      public Layout getLayout() {
71          return _layout;
72      }
73  
74      public PortletMode getPortletMode() {
75          return _portletMode;
76      }
77  
78      public String getRedirectLocation() {
79          return _redirectLocation;
80      }
81  
82      public Map<String, String[]> getRenderParameterMap() {
83          return _params;
84      }
85  
86      public User getUser() {
87          return _user;
88      }
89  
90      public WindowState getWindowState() {
91          return _windowState;
92      }
93  
94      public boolean isCalledSetRenderParameter() {
95          return _calledSetRenderParameter;
96      }
97  
98      public void removePublicRenderParameter(String name) {
99          if (name == null) {
100             throw new IllegalArgumentException();
101         }
102     }
103 
104     public void setEvent(QName name, Serializable value) {
105         if (name == null) {
106             throw new IllegalArgumentException();
107         }
108 
109         _events.add(new EventImpl(name.getLocalPart(), name, value));
110     }
111 
112     public void setEvent(String name, Serializable value) {
113         if (name == null) {
114             throw new IllegalArgumentException();
115         }
116 
117         setEvent(new QName(getDefaultNamespace(), name), value);
118     }
119 
120     public void setPortletMode(PortletMode portletMode)
121         throws PortletModeException {
122 
123         if (_redirectLocation != null) {
124             throw new IllegalStateException();
125         }
126 
127         if (!_portletRequestImpl.isPortletModeAllowed(portletMode)) {
128             throw new PortletModeException(portletMode.toString(), portletMode);
129         }
130 
131         try {
132             _portletMode = PortalUtil.updatePortletMode(
133                 _portletName, _user, _layout, portletMode,
134                 _portletRequestImpl.getHttpServletRequest());
135 
136             _portletRequestImpl.setPortletMode(_portletMode);
137         }
138         catch (Exception e) {
139             throw new PortletModeException(e, portletMode);
140         }
141 
142         _calledSetRenderParameter = true;
143     }
144 
145     public void setRedirectLocation(String redirectLocation) {
146         _redirectLocation = redirectLocation;
147     }
148 
149     public void setRenderParameter(String name, String value) {
150         if (_redirectLocation != null) {
151             throw new IllegalStateException();
152         }
153 
154         if ((name == null) || (value == null)) {
155             throw new IllegalArgumentException();
156         }
157 
158         setRenderParameter(name, new String[] {value});
159     }
160 
161     public void setRenderParameter(String name, String[] values) {
162         if (_redirectLocation != null) {
163             throw new IllegalStateException();
164         }
165 
166         if ((name == null) || (values == null)) {
167             throw new IllegalArgumentException();
168         }
169 
170         for (int i = 0; i < values.length; i++) {
171             if (values[i] == null) {
172                 throw new IllegalArgumentException();
173             }
174         }
175 
176         _params.put(
177             PortalUtil.getPortletNamespace(_portletName) + name,
178             values);
179 
180         _calledSetRenderParameter = true;
181     }
182 
183     public void setRenderParameters(Map<String, String[]> params) {
184         if (_redirectLocation != null) {
185             throw new IllegalStateException();
186         }
187 
188         if (params == null) {
189             throw new IllegalArgumentException();
190         }
191         else {
192             Map<String, String[]> newParams =
193                 new LinkedHashMap<String, String[]>();
194 
195             for (Map.Entry<String, String[]> entry : params.entrySet()) {
196                 String key = entry.getKey();
197                 String[] value = entry.getValue();
198 
199                 if (key == null) {
200                     throw new IllegalArgumentException();
201                 }
202                 else if (value == null) {
203                     throw new IllegalArgumentException();
204                 }
205 
206                 newParams.put(
207                     PortalUtil.getPortletNamespace(_portletName) + key,
208                     value);
209             }
210 
211             _params = newParams;
212         }
213 
214         _calledSetRenderParameter = true;
215     }
216 
217     public void setWindowState(WindowState windowState)
218         throws WindowStateException {
219 
220         if (_redirectLocation != null) {
221             throw new IllegalStateException();
222         }
223 
224         if (!_portletRequestImpl.isWindowStateAllowed(windowState)) {
225             throw new WindowStateException(windowState.toString(), windowState);
226         }
227 
228         try {
229             _windowState = PortalUtil.updateWindowState(
230                 _portletName, _user, _layout, windowState,
231                 _portletRequestImpl.getHttpServletRequest());
232 
233             _portletRequestImpl.setWindowState(_windowState);
234         }
235         catch (Exception e) {
236             throw new WindowStateException(e, windowState);
237         }
238 
239         _calledSetRenderParameter = true;
240     }
241 
242     protected void init(
243             PortletRequestImpl portletRequestImpl, HttpServletResponse response,
244             String portletName, User user, Layout layout,
245             WindowState windowState, PortletMode portletMode)
246         throws PortletModeException, WindowStateException {
247 
248         super.init(
249             portletRequestImpl, response, portletName, layout.getCompanyId(),
250             layout.getPlid());
251 
252         _portletRequestImpl = portletRequestImpl;
253         _portletName = portletName;
254         _user = user;
255         _layout = layout;
256         setWindowState(windowState);
257         setPortletMode(portletMode);
258 
259         // Set _calledSetRenderParameter to false because setWindowState and
260         // setPortletMode sets it to true
261 
262         _calledSetRenderParameter = false;
263     }
264 
265     protected void recycle() {
266         super.recycle();
267 
268         _portletRequestImpl = null;
269         _portletName = null;
270         _user = null;
271         _layout = null;
272         _windowState = null;
273         _portletMode = null;
274         _params.clear();
275         _events.clear();
276         _redirectLocation = null;
277         _calledSetRenderParameter = false;
278     }
279 
280     private PortletRequestImpl _portletRequestImpl;
281     private String _portletName;
282     private User _user;
283     private Layout _layout;
284     private WindowState _windowState;
285     private PortletMode _portletMode;
286     private Map<String, String[]> _params =
287         new LinkedHashMap<String, String[]>();
288     private List<Event> _events = new ArrayList<Event>();
289     private String _redirectLocation;
290     private boolean _calledSetRenderParameter;
291 
292 }