1
14
15 package com.liferay.taglib.portlet;
16
17 import com.liferay.portal.kernel.log.Log;
18 import com.liferay.portal.kernel.log.LogFactoryUtil;
19 import com.liferay.portal.kernel.portlet.LiferayPortletConfig;
20 import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
21 import com.liferay.portal.kernel.portlet.LiferayPortletURL;
22 import com.liferay.portal.kernel.portlet.PortletModeFactory;
23 import com.liferay.portal.kernel.portlet.WindowStateFactory;
24 import com.liferay.portal.kernel.util.JavaConstants;
25 import com.liferay.portal.kernel.util.MapUtil;
26 import com.liferay.portal.kernel.util.ParamUtil;
27 import com.liferay.portal.kernel.util.Validator;
28 import com.liferay.portal.model.LayoutConstants;
29 import com.liferay.portal.util.PortalUtil;
30 import com.liferay.taglib.util.ParamAndPropertyAncestorTagImpl;
31
32 import java.util.Map;
33
34 import javax.portlet.ActionRequest;
35 import javax.portlet.PortletRequest;
36 import javax.portlet.PortletResponse;
37
38 import javax.servlet.http.HttpServletRequest;
39 import javax.servlet.jsp.JspException;
40 import javax.servlet.jsp.PageContext;
41
42
47 public class ActionURLTag extends ParamAndPropertyAncestorTagImpl {
48
49 public static void doTag(
50 String lifecycle, String windowState, String portletMode,
51 String var, String varImpl, Boolean secure,
52 Boolean copyCurrentRenderParameters, Boolean escapeXml, String name,
53 String resourceID, String cacheability, long plid,
54 String portletName, Boolean anchor, Boolean encrypt,
55 long doAsUserId, Boolean portletConfiguration,
56 Map<String, String[]> params, PageContext pageContext)
57 throws Exception {
58
59 HttpServletRequest request =
60 (HttpServletRequest)pageContext.getRequest();
61
62 if (portletName == null) {
63 portletName = _getPortletName(request);
64 }
65
66 LiferayPortletURL portletURL = _getLiferayPortletURL(
67 request, plid, portletName, lifecycle);
68
69 if (portletURL == null) {
70 _log.error(
71 "Render response is null because this tag is not being " +
72 "called within the context of a portlet");
73
74 return;
75 }
76
77 if (Validator.isNotNull(windowState)) {
78 portletURL.setWindowState(
79 WindowStateFactory.getWindowState(windowState));
80 }
81
82 if (Validator.isNotNull(portletMode)) {
83 portletURL.setPortletMode(
84 PortletModeFactory.getPortletMode(portletMode));
85 }
86
87 if (secure != null) {
88 portletURL.setSecure(secure.booleanValue());
89 }
90 else {
91 portletURL.setSecure(request.isSecure());
92 }
93
94 if (copyCurrentRenderParameters != null) {
95 portletURL.setCopyCurrentRenderParameters(
96 copyCurrentRenderParameters.booleanValue());
97 }
98
99 if (escapeXml != null) {
100 portletURL.setEscapeXml(escapeXml.booleanValue());
101 }
102
103 if (lifecycle.equals(PortletRequest.ACTION_PHASE) &&
104 Validator.isNotNull(name)) {
105
106 portletURL.setParameter(ActionRequest.ACTION_NAME, name);
107 }
108
109 if (resourceID != null) {
110 portletURL.setResourceID(resourceID);
111 }
112
113 if (cacheability != null) {
114 portletURL.setCacheability(cacheability);
115 }
116
117 if (anchor != null) {
118 portletURL.setAnchor(anchor.booleanValue());
119 }
120
121 if (encrypt != null) {
122 portletURL.setEncrypt(encrypt.booleanValue());
123 }
124
125 if (doAsUserId > 0) {
126 portletURL.setDoAsUserId(doAsUserId);
127 }
128
129 if ((portletConfiguration != null) &&
130 portletConfiguration.booleanValue()) {
131
132 String returnToFullPageURL = ParamUtil.getString(
133 request, "returnToFullPageURL");
134 String portletResource = ParamUtil.getString(
135 request, "portletResource");
136 String previewWidth = ParamUtil.getString(request, "previewWidth");
137
138 portletURL.setParameter(
139 "struts_action", "/portlet_configuration/edit_configuration");
140 portletURL.setParameter("returnToFullPageURL", returnToFullPageURL);
141 portletURL.setParameter("portletResource", portletResource);
142 portletURL.setParameter("previewWidth", previewWidth);
143 }
144
145 if (params != null) {
146 MapUtil.merge(portletURL.getParameterMap(), params);
147
148 portletURL.setParameters(params);
149 }
150
151 String portletURLToString = portletURL.toString();
152
153 if (Validator.isNotNull(var)) {
154 pageContext.setAttribute(var, portletURLToString);
155 }
156 else if (Validator.isNotNull(varImpl)) {
157 pageContext.setAttribute(varImpl, portletURL);
158 }
159 else {
160 pageContext.getOut().print(portletURLToString);
161 }
162 }
163
164 public int doEndTag() throws JspException {
165 try {
166 doTag(
167 getLifecycle(), _windowState, _portletMode, _var, _varImpl,
168 _secure, _copyCurrentRenderParameters, _escapeXml, _name,
169 _resourceID, _cacheability, _plid, _portletName, _anchor,
170 _encrypt, _doAsUserId, _portletConfiguration, getParams(),
171 pageContext);
172
173 return EVAL_PAGE;
174 }
175 catch (Exception e) {
176 throw new JspException(e);
177 }
178 finally {
179 clearParams();
180 clearProperties();
181
182 _plid = LayoutConstants.DEFAULT_PLID;
183 }
184 }
185
186 public String getLifecycle() {
187 return PortletRequest.ACTION_PHASE;
188 }
189
190 public void setWindowState(String windowState) {
191 _windowState = windowState;
192 }
193
194 public void setPortletMode(String portletMode) {
195 _portletMode = portletMode;
196 }
197
198 public void setVar(String var) {
199 _var = var;
200 }
201
202 public void setVarImpl(String varImpl) {
203 _varImpl = varImpl;
204 }
205
206 public void setSecure(boolean secure) {
207 _secure = Boolean.valueOf(secure);
208 }
209
210 public void setCopyCurrentRenderParameters(
211 boolean copyCurrentRenderParameters) {
212
213 _copyCurrentRenderParameters = Boolean.valueOf(
214 copyCurrentRenderParameters);
215 }
216
217 public void setEscapeXml(boolean escapeXml) {
218 _escapeXml = Boolean.valueOf(escapeXml);
219 }
220
221 public void setName(String name) {
222 _name = name;
223 }
224
225 public void setId(String resourceID) {
226 _resourceID = resourceID;
227 }
228
229 public void setCacheability(String cacheability) {
230 _cacheability = cacheability;
231 }
232
233 public void setPlid(long plid) {
234 _plid = plid;
235 }
236
237 public void setPortletName(String portletName) {
238 _portletName = portletName;
239 }
240
241 public void setAnchor(boolean anchor) {
242 _anchor = Boolean.valueOf(anchor);
243 }
244
245 public void setEncrypt(boolean encrypt) {
246 _encrypt = Boolean.valueOf(encrypt);
247 }
248
249 public void setDoAsUserId(long doAsUserId) {
250 _doAsUserId = doAsUserId;
251 }
252
253 public void setPortletConfiguration(boolean portletConfiguration) {
254 _portletConfiguration = Boolean.valueOf(portletConfiguration);
255 }
256
257 private static LiferayPortletURL _getLiferayPortletURL(
258 HttpServletRequest request, long plid, String portletName,
259 String lifecycle) {
260
261 PortletRequest portletRequest = (PortletRequest)request.getAttribute(
262 JavaConstants.JAVAX_PORTLET_REQUEST);
263
264 if (portletRequest == null) {
265 return null;
266 }
267
268 PortletResponse portletResponse = (PortletResponse)request.getAttribute(
269 JavaConstants.JAVAX_PORTLET_RESPONSE);
270
271 LiferayPortletResponse liferayPortletResponse =
272 PortalUtil.getLiferayPortletResponse(portletResponse);
273
274 return liferayPortletResponse.createLiferayPortletURL(
275 plid, portletName, lifecycle);
276 }
277
278 private static String _getPortletName(HttpServletRequest request) {
279 PortletRequest portletRequest = (PortletRequest)request.getAttribute(
280 JavaConstants.JAVAX_PORTLET_REQUEST);
281
282 if (portletRequest == null) {
283 return null;
284 }
285
286 LiferayPortletConfig liferayPortletConfig =
287 (LiferayPortletConfig)request.getAttribute(
288 JavaConstants.JAVAX_PORTLET_CONFIG);
289
290 return liferayPortletConfig.getPortletId();
291 }
292
293 private static Log _log = LogFactoryUtil.getLog(ActionURLTag.class);
294
295 private String _windowState;
296 private String _portletMode;
297 private String _var;
298 private String _varImpl;
299 private Boolean _secure;
300 private Boolean _copyCurrentRenderParameters;
301 private Boolean _escapeXml;
302 private String _name;
303 private String _resourceID;
304 private String _cacheability;
305 private long _plid = LayoutConstants.DEFAULT_PLID;
306 private String _portletName;
307 private Boolean _anchor;
308 private Boolean _encrypt;
309 private long _doAsUserId;
310 private Boolean _portletConfiguration;
311
312 }