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.portal.servlet.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  
30  import java.util.Map;
31  
32  import javax.portlet.ActionRequest;
33  import javax.portlet.PortletRequest;
34  
35  import javax.servlet.http.HttpServletRequest;
36  import javax.servlet.jsp.JspException;
37  import javax.servlet.jsp.PageContext;
38  
39  /**
40   * <a href="ActionURLTagUtil.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   */
44  public class ActionURLTagUtil {
45  
46      public static String doEndTag(
47              String lifecycle, String windowState, String portletMode,
48              String var, String varImpl, Boolean secure,
49              Boolean copyCurrentRenderParameters, Boolean escapeXml, String name,
50              String resourceID, String cacheability, long plid,
51              String portletName, Boolean anchor, Boolean encrypt,
52              long doAsUserId, Boolean portletConfiguration,
53              Map<String, String[]> params, boolean writeOutput,
54              PageContext pageContext)
55          throws JspException {
56  
57          try {
58              HttpServletRequest request =
59                  (HttpServletRequest)pageContext.getRequest();
60  
61              if (portletName == null) {
62                  portletName = _getPortletName(request);
63              }
64  
65              LiferayPortletURL portletURL = _getLiferayPortletURL(
66                  request, plid, portletName, lifecycle);
67  
68              if (portletURL == null) {
69                  _log.error(
70                      "Render response is null because this tag is not being " +
71                          "called within the context of a portlet");
72  
73                  return StringPool.BLANK;
74              }
75  
76              if (Validator.isNotNull(windowState)) {
77                  portletURL.setWindowState(
78                      WindowStateFactory.getWindowState(windowState));
79              }
80  
81              if (Validator.isNotNull(portletMode)) {
82                  portletURL.setPortletMode(
83                      PortletModeFactory.getPortletMode(portletMode));
84              }
85  
86              if (secure != null) {
87                  portletURL.setSecure(secure.booleanValue());
88              }
89              else {
90                  portletURL.setSecure(request.isSecure());
91              }
92  
93              if (copyCurrentRenderParameters != null) {
94                  portletURL.setCopyCurrentRenderParameters(
95                      copyCurrentRenderParameters.booleanValue());
96              }
97  
98              if (escapeXml != null) {
99                  portletURL.setEscapeXml(escapeXml.booleanValue());
100             }
101 
102             if (lifecycle.equals(PortletRequest.ACTION_PHASE) &&
103                 Validator.isNotNull(name)) {
104 
105                 portletURL.setParameter(ActionRequest.ACTION_NAME, name);
106             }
107 
108             if (resourceID != null) {
109                 portletURL.setResourceID(resourceID);
110             }
111 
112             if (cacheability != null) {
113                 portletURL.setCacheability(cacheability);
114             }
115 
116             if (anchor != null) {
117                 portletURL.setAnchor(anchor.booleanValue());
118             }
119 
120             if (encrypt != null) {
121                 portletURL.setEncrypt(encrypt.booleanValue());
122             }
123 
124             if (doAsUserId > 0) {
125                 portletURL.setDoAsUserId(doAsUserId);
126             }
127 
128             if ((portletConfiguration != null) &&
129                 portletConfiguration.booleanValue()) {
130 
131                 String returnToFullPageURL = ParamUtil.getString(
132                     request, "returnToFullPageURL");
133                 String portletResource = ParamUtil.getString(
134                     request, "portletResource");
135                 String previewWidth = ParamUtil.getString(
136                     request, "previewWidth");
137 
138                 portletURL.setParameter(
139                     "struts_action",
140                     "/portlet_configuration/edit_configuration");
141                 portletURL.setParameter(
142                     "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             if (Validator.isNotNull(var)) {
154                 pageContext.setAttribute(var, portletURL.toString());
155             }
156             else if (Validator.isNotNull(varImpl)) {
157                 pageContext.setAttribute(varImpl, portletURL);
158             }
159             else if (writeOutput) {
160                 pageContext.getOut().print(portletURL.toString());
161             }
162 
163             return portletURL.toString();
164         }
165         catch (Exception e) {
166             _log.error(e, e);
167 
168             throw new JspException(e);
169         }
170     }
171 
172     private static LiferayPortletURL _getLiferayPortletURL(
173         HttpServletRequest request, long plid, String portletName,
174         String lifecycle) {
175 
176         PortletRequest portletRequest = (PortletRequest)request.getAttribute(
177             JavaConstants.JAVAX_PORTLET_REQUEST);
178 
179         if (portletRequest == null) {
180             return null;
181         }
182 
183         LiferayPortletResponse portletResponse =
184             (LiferayPortletResponse)request.getAttribute(
185                 JavaConstants.JAVAX_PORTLET_RESPONSE);
186 
187         return portletResponse.createLiferayPortletURL(
188             plid, portletName, lifecycle);
189     }
190 
191     private static String _getPortletName(HttpServletRequest request) {
192         PortletRequest portletRequest = (PortletRequest)request.getAttribute(
193             JavaConstants.JAVAX_PORTLET_REQUEST);
194 
195         if (portletRequest == null) {
196             return null;
197         }
198 
199         LiferayPortletConfig liferayPortletConfig =
200             (LiferayPortletConfig)request.getAttribute(
201                 JavaConstants.JAVAX_PORTLET_CONFIG);
202 
203         return liferayPortletConfig.getPortletId();
204     }
205 
206     private static Log _log = LogFactoryUtil.getLog(ActionURLTagUtil.class);
207 
208 }