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