1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.servlet.taglib.portlet;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.portlet.LiferayPortletURL;
25  import com.liferay.portal.kernel.portlet.PortletModeFactory;
26  import com.liferay.portal.kernel.portlet.WindowStateFactory;
27  import com.liferay.portal.kernel.util.MapUtil;
28  import com.liferay.portal.kernel.util.ParamUtil;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.Validator;
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="ActionURLTagUtil.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public class ActionURLTagUtil {
48  
49      public static String doEndTag(
50              String lifecycle, String windowState, String portletMode,
51              String var, String varImpl, Boolean secure,
52              Boolean copyCurrentRenderParameters, Boolean escapeXml, String name,
53              String resourceID, String cacheability, long plid,
54              String portletName, Boolean anchor, Boolean encrypt,
55              long doAsUserId, Boolean portletConfiguration,
56              Map<String, String[]> params, boolean writeOutput,
57              PageContext pageContext)
58          throws JspException {
59  
60          try {
61              HttpServletRequest request =
62                  (HttpServletRequest)pageContext.getRequest();
63  
64              if (portletName == null) {
65                  portletName = TagUtil.getPortletName(request);
66              }
67  
68              LiferayPortletURL portletURL = TagUtil.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(
139                     request, "previewWidth");
140 
141                 portletURL.setParameter(
142                     "struts_action",
143                     "/portlet_configuration/edit_configuration");
144                 portletURL.setParameter(
145                     "returnToFullPageURL", returnToFullPageURL);
146                 portletURL.setParameter("portletResource", portletResource);
147                 portletURL.setParameter("previewWidth", previewWidth);
148             }
149 
150             if (params != null) {
151                 MapUtil.merge(portletURL.getParameterMap(), params);
152 
153                 portletURL.setParameters(params);
154             }
155 
156             if (Validator.isNotNull(var)) {
157                 pageContext.setAttribute(var, portletURL.toString());
158             }
159             else if (Validator.isNotNull(varImpl)) {
160                 pageContext.setAttribute(varImpl, portletURL);
161             }
162             else if (writeOutput) {
163                 pageContext.getOut().print(portletURL.toString());
164             }
165 
166             return portletURL.toString();
167         }
168         catch (Exception e) {
169             _log.error(e, e);
170 
171             throw new JspException(e);
172         }
173     }
174 
175     private static Log _log = LogFactoryUtil.getLog(ActionURLTagUtil.class);
176 
177 }