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