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.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         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 if (writeOutput) {
160             pageContext.getOut().print(portletURLToString);
161         }
162 
163         return portletURLToString;
164     }
165 
166     public int doEndTag() throws JspException {
167         try {
168             doTag(
169                 getLifecycle(), _windowState, _portletMode, _var, _varImpl,
170                 _secure, _copyCurrentRenderParameters, _escapeXml, _name,
171                 _resourceID, _cacheability, _plid, _portletName, _anchor,
172                 _encrypt, _doAsUserId, _portletConfiguration, getParams(), true,
173                 pageContext);
174 
175             return EVAL_PAGE;
176         }
177         catch (Exception e) {
178             throw new JspException(e);
179         }
180         finally {
181             clearParams();
182             clearProperties();
183 
184             _plid = LayoutConstants.DEFAULT_PLID;
185         }
186     }
187 
188     public String getLifecycle() {
189         return PortletRequest.ACTION_PHASE;
190     }
191 
192     public void setWindowState(String windowState) {
193         _windowState = windowState;
194     }
195 
196     public void setPortletMode(String portletMode) {
197         _portletMode = portletMode;
198     }
199 
200     public void setVar(String var) {
201         _var = var;
202     }
203 
204     public void setVarImpl(String varImpl) {
205         _varImpl = varImpl;
206     }
207 
208     public void setSecure(boolean secure) {
209         _secure = Boolean.valueOf(secure);
210     }
211 
212     public void setCopyCurrentRenderParameters(
213         boolean copyCurrentRenderParameters) {
214 
215         _copyCurrentRenderParameters = Boolean.valueOf(
216             copyCurrentRenderParameters);
217     }
218 
219     public void setEscapeXml(boolean escapeXml) {
220         _escapeXml = Boolean.valueOf(escapeXml);
221     }
222 
223     public void setName(String name) {
224         _name = name;
225     }
226 
227     public void setId(String resourceID) {
228         _resourceID = resourceID;
229     }
230 
231     public void setCacheability(String cacheability) {
232         _cacheability = cacheability;
233     }
234 
235     public void setPlid(long plid) {
236         _plid = plid;
237     }
238 
239     public void setPortletName(String portletName) {
240         _portletName = portletName;
241     }
242 
243     public void setAnchor(boolean anchor) {
244         _anchor = Boolean.valueOf(anchor);
245     }
246 
247     public void setEncrypt(boolean encrypt) {
248         _encrypt = Boolean.valueOf(encrypt);
249     }
250 
251     public void setDoAsUserId(long doAsUserId) {
252         _doAsUserId = doAsUserId;
253     }
254 
255     public void setPortletConfiguration(boolean portletConfiguration) {
256         _portletConfiguration = Boolean.valueOf(portletConfiguration);
257     }
258 
259     private static LiferayPortletURL _getLiferayPortletURL(
260         HttpServletRequest request, long plid, String portletName,
261         String lifecycle) {
262 
263         PortletRequest portletRequest = (PortletRequest)request.getAttribute(
264             JavaConstants.JAVAX_PORTLET_REQUEST);
265 
266         if (portletRequest == null) {
267             return null;
268         }
269 
270         LiferayPortletResponse portletResponse =
271             (LiferayPortletResponse)request.getAttribute(
272                 JavaConstants.JAVAX_PORTLET_RESPONSE);
273 
274         return portletResponse.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 }