1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
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.StringPool;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.model.LayoutConstants;
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  
37  import javax.servlet.http.HttpServletRequest;
38  import javax.servlet.jsp.JspException;
39  import javax.servlet.jsp.PageContext;
40  
41  /**
42   * <a href="ActionURLTag.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   */
46  public class ActionURLTag extends ParamAndPropertyAncestorTagImpl {
47  
48      public static String doTag(
49              String lifecycle, String windowState, String portletMode,
50              String var, String varImpl, Boolean secure,
51              Boolean copyCurrentRenderParameters, Boolean escapeXml, String name,
52              String resourceID, String cacheability, long plid,
53              String portletName, Boolean anchor, Boolean encrypt,
54              long doAsUserId, Boolean portletConfiguration,
55              Map<String, String[]> params, boolean writeOutput,
56              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 StringPool.BLANK;
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         if (Validator.isNotNull(var)) {
152             pageContext.setAttribute(var, portletURL.toString());
153         }
154         else if (Validator.isNotNull(varImpl)) {
155             pageContext.setAttribute(varImpl, portletURL);
156         }
157         else if (writeOutput) {
158             pageContext.getOut().print(portletURL.toString());
159         }
160 
161         return portletURL.toString();
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(), true,
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         LiferayPortletResponse portletResponse =
269             (LiferayPortletResponse)request.getAttribute(
270                 JavaConstants.JAVAX_PORTLET_RESPONSE);
271 
272         return portletResponse.createLiferayPortletURL(
273             plid, portletName, lifecycle);
274     }
275 
276     private static String _getPortletName(HttpServletRequest request) {
277         PortletRequest portletRequest = (PortletRequest)request.getAttribute(
278             JavaConstants.JAVAX_PORTLET_REQUEST);
279 
280         if (portletRequest == null) {
281             return null;
282         }
283 
284         LiferayPortletConfig liferayPortletConfig =
285             (LiferayPortletConfig)request.getAttribute(
286                 JavaConstants.JAVAX_PORTLET_CONFIG);
287 
288         return liferayPortletConfig.getPortletId();
289     }
290 
291     private static Log _log = LogFactoryUtil.getLog(ActionURLTag.class);
292 
293     private String _windowState;
294     private String _portletMode;
295     private String _var;
296     private String _varImpl;
297     private Boolean _secure;
298     private Boolean _copyCurrentRenderParameters;
299     private Boolean _escapeXml;
300     private String  _name;
301     private String _resourceID;
302     private String _cacheability;
303     private long _plid = LayoutConstants.DEFAULT_PLID;
304     private String _portletName;
305     private Boolean _anchor;
306     private Boolean _encrypt;
307     private long _doAsUserId;
308     private Boolean _portletConfiguration;
309 
310 }