1
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
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 (!_req.isPortletModeAllowed(portletMode)) {
131 throw new PortletModeException(portletMode.toString(), portletMode);
132 }
133
134 try {
135 _portletMode = PortalUtil.updatePortletMode(
136 _portletName, _user, _layout, portletMode,
137 _req.getHttpServletRequest());
138
139 _req.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 (!_req.isWindowStateAllowed(windowState)) {
228 throw new WindowStateException(windowState.toString(), windowState);
229 }
230
231 try {
232 _windowState = PortalUtil.updateWindowState(
233 _portletName, _user, _layout, windowState,
234 _req.getHttpServletRequest());
235
236 _req.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 req, HttpServletResponse res, String portletName,
247 User user, Layout layout, WindowState windowState,
248 PortletMode portletMode)
249 throws PortletModeException, WindowStateException {
250
251 super.init(
252 req, res, portletName, layout.getCompanyId(), layout.getPlid());
253
254 _req = req;
255 _portletName = portletName;
256 _user = user;
257 _layout = layout;
258 setWindowState(windowState);
259 setPortletMode(portletMode);
260
261
264 _calledSetRenderParameter = false;
265 }
266
267 protected void recycle() {
268 super.recycle();
269
270 _req = null;
271 _portletName = null;
272 _user = null;
273 _layout = null;
274 _windowState = null;
275 _portletMode = null;
276 _params.clear();
277 _events.clear();
278 _redirectLocation = null;
279 _calledSetRenderParameter = false;
280 }
281
282 private PortletRequestImpl _req;
283 private String _portletName;
284 private User _user;
285 private Layout _layout;
286 private WindowState _windowState;
287 private PortletMode _portletMode;
288 private Map<String, String[]> _params =
289 new LinkedHashMap<String, String[]>();
290 private List<Event> _events = new ArrayList<Event>();
291 private String _redirectLocation;
292 private boolean _calledSetRenderParameter;
293
294 }