1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.servlet.taglib.portlet;
24  
25  import com.liferay.portal.kernel.portlet.LiferayPortletURL;
26  import com.liferay.portal.kernel.portlet.PortletModeFactory;
27  import com.liferay.portal.kernel.portlet.WindowStateFactory;
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  import com.liferay.util.MapUtil;
32  
33  import java.util.Map;
34  
35  import javax.portlet.ActionRequest;
36  import javax.portlet.PortletRequest;
37  
38  import javax.servlet.http.HttpServletRequest;
39  import javax.servlet.jsp.JspException;
40  import javax.servlet.jsp.PageContext;
41  
42  import org.apache.commons.logging.Log;
43  import org.apache.commons.logging.LogFactory;
44  
45  /**
46   * <a href="ActionURLTagUtil.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   *
50   */
51  public class ActionURLTagUtil {
52  
53      public static String doEndTag(
54              String lifecycle, String windowState, String portletMode,
55              String var, String varImpl, Boolean secure,
56              Boolean copyCurrentRenderParameters, Boolean escapeXml, String name,
57              String resourceID, String cacheability, String portletName,
58              Boolean anchor, Boolean encrypt, long doAsUserId,
59              Boolean portletConfiguration, Map<String, String[]> params,
60              boolean writeOutput, PageContext pageContext)
61          throws JspException {
62  
63          try {
64              HttpServletRequest request =
65                  (HttpServletRequest)pageContext.getRequest();
66  
67              if (portletName == null) {
68                  portletName = TagUtil.getPortletName(request);
69              }
70  
71              LiferayPortletURL portletURL = TagUtil.getLiferayPortletURL(
72                  request, portletName, lifecycle);
73  
74              if (portletURL == null) {
75                  _log.error(
76                      "Render response is null because this tag is not being " +
77                          "called within the context of a portlet");
78  
79                  return StringPool.BLANK;
80              }
81  
82              if (Validator.isNotNull(windowState)) {
83                  portletURL.setWindowState(
84                      WindowStateFactory.getWindowState(windowState));
85              }
86  
87              if (Validator.isNotNull(portletMode)) {
88                  portletURL.setPortletMode(
89                      PortletModeFactory.getPortletMode(portletMode));
90              }
91  
92              if (secure != null) {
93                  portletURL.setSecure(secure.booleanValue());
94              }
95              else {
96                  portletURL.setSecure(request.isSecure());
97              }
98  
99              if (copyCurrentRenderParameters != null) {
100                 portletURL.setCopyCurrentRenderParameters(
101                     copyCurrentRenderParameters.booleanValue());
102             }
103 
104             if (escapeXml != null) {
105                 portletURL.setEscapeXml(escapeXml.booleanValue());
106             }
107 
108             if (lifecycle.equals(PortletRequest.ACTION_PHASE) &&
109                 Validator.isNotNull(name)) {
110 
111                 portletURL.setParameter(ActionRequest.ACTION_NAME, name);
112             }
113 
114             if (resourceID != null) {
115                 portletURL.setResourceID(resourceID);
116             }
117 
118             if (cacheability != null) {
119                 portletURL.setCacheability(cacheability);
120             }
121 
122             if (anchor != null) {
123                 portletURL.setAnchor(anchor.booleanValue());
124             }
125 
126             if (encrypt != null) {
127                 portletURL.setEncrypt(encrypt.booleanValue());
128             }
129 
130             if (doAsUserId > 0) {
131                 portletURL.setDoAsUserId(doAsUserId);
132             }
133 
134             if ((portletConfiguration != null) &&
135                 portletConfiguration.booleanValue()) {
136 
137                 String returnToFullPageURL = ParamUtil.getString(
138                     request, "returnToFullPageURL");
139                 String portletResource = ParamUtil.getString(
140                     request, "portletResource");
141                 String previewWidth = ParamUtil.getString(
142                     request, "previewWidth");
143 
144                 portletURL.setParameter(
145                     "struts_action",
146                     "/portlet_configuration/edit_configuration");
147                 portletURL.setParameter(
148                     "returnToFullPageURL", returnToFullPageURL);
149                 portletURL.setParameter("portletResource", portletResource);
150                 portletURL.setParameter("previewWidth", previewWidth);
151             }
152 
153             if (params != null) {
154                 MapUtil.merge(portletURL.getParameterMap(), params);
155 
156                 portletURL.setParameters(params);
157             }
158 
159             if (Validator.isNotNull(var)) {
160                 pageContext.setAttribute(var, portletURL.toString());
161             }
162             else if (Validator.isNotNull(varImpl)) {
163                 pageContext.setAttribute(varImpl, portletURL);
164             }
165             else if (writeOutput) {
166                 pageContext.getOut().print(portletURL.toString());
167             }
168 
169             return portletURL.toString();
170         }
171         catch (Exception e) {
172             _log.error(e, e);
173 
174             throw new JspException(e);
175         }
176     }
177 
178     private static Log _log = LogFactory.getLog(ActionURLTagUtil.class);
179 
180 }