1   /**
2    * Copyright (c) 2000-2009 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.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.portlet.LiferayPortletURL;
28  import com.liferay.portal.kernel.portlet.PortletModeFactory;
29  import com.liferay.portal.kernel.portlet.WindowStateFactory;
30  import com.liferay.portal.kernel.util.ParamUtil;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.portal.kernel.util.Validator;
33  import com.liferay.util.MapUtil;
34  
35  import java.util.Map;
36  
37  import javax.portlet.ActionRequest;
38  import javax.portlet.PortletRequest;
39  
40  import javax.servlet.http.HttpServletRequest;
41  import javax.servlet.jsp.JspException;
42  import javax.servlet.jsp.PageContext;
43  
44  /**
45   * <a href="ActionURLTagUtil.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   *
49   */
50  public class ActionURLTagUtil {
51  
52      public static String doEndTag(
53              String lifecycle, String windowState, String portletMode,
54              String var, String varImpl, Boolean secure,
55              Boolean copyCurrentRenderParameters, Boolean escapeXml, String name,
56              String resourceID, String cacheability, String portletName,
57              Boolean anchor, Boolean encrypt, long doAsUserId,
58              Boolean portletConfiguration, Map<String, String[]> params,
59              boolean writeOutput, PageContext pageContext)
60          throws JspException {
61  
62          try {
63              HttpServletRequest request =
64                  (HttpServletRequest)pageContext.getRequest();
65  
66              if (portletName == null) {
67                  portletName = TagUtil.getPortletName(request);
68              }
69  
70              LiferayPortletURL portletURL = TagUtil.getLiferayPortletURL(
71                  request, portletName, lifecycle);
72  
73              if (portletURL == null) {
74                  _log.error(
75                      "Render response is null because this tag is not being " +
76                          "called within the context of a portlet");
77  
78                  return StringPool.BLANK;
79              }
80  
81              if (Validator.isNotNull(windowState)) {
82                  portletURL.setWindowState(
83                      WindowStateFactory.getWindowState(windowState));
84              }
85  
86              if (Validator.isNotNull(portletMode)) {
87                  portletURL.setPortletMode(
88                      PortletModeFactory.getPortletMode(portletMode));
89              }
90  
91              if (secure != null) {
92                  portletURL.setSecure(secure.booleanValue());
93              }
94              else {
95                  portletURL.setSecure(request.isSecure());
96              }
97  
98              if (copyCurrentRenderParameters != null) {
99                  portletURL.setCopyCurrentRenderParameters(
100                     copyCurrentRenderParameters.booleanValue());
101             }
102 
103             if (escapeXml != null) {
104                 portletURL.setEscapeXml(escapeXml.booleanValue());
105             }
106 
107             if (lifecycle.equals(PortletRequest.ACTION_PHASE) &&
108                 Validator.isNotNull(name)) {
109 
110                 portletURL.setParameter(ActionRequest.ACTION_NAME, name);
111             }
112 
113             if (resourceID != null) {
114                 portletURL.setResourceID(resourceID);
115             }
116 
117             if (cacheability != null) {
118                 portletURL.setCacheability(cacheability);
119             }
120 
121             if (anchor != null) {
122                 portletURL.setAnchor(anchor.booleanValue());
123             }
124 
125             if (encrypt != null) {
126                 portletURL.setEncrypt(encrypt.booleanValue());
127             }
128 
129             if (doAsUserId > 0) {
130                 portletURL.setDoAsUserId(doAsUserId);
131             }
132 
133             if ((portletConfiguration != null) &&
134                 portletConfiguration.booleanValue()) {
135 
136                 String returnToFullPageURL = ParamUtil.getString(
137                     request, "returnToFullPageURL");
138                 String portletResource = ParamUtil.getString(
139                     request, "portletResource");
140                 String previewWidth = ParamUtil.getString(
141                     request, "previewWidth");
142 
143                 portletURL.setParameter(
144                     "struts_action",
145                     "/portlet_configuration/edit_configuration");
146                 portletURL.setParameter(
147                     "returnToFullPageURL", returnToFullPageURL);
148                 portletURL.setParameter("portletResource", portletResource);
149                 portletURL.setParameter("previewWidth", previewWidth);
150             }
151 
152             if (params != null) {
153                 MapUtil.merge(portletURL.getParameterMap(), params);
154 
155                 portletURL.setParameters(params);
156             }
157 
158             if (Validator.isNotNull(var)) {
159                 pageContext.setAttribute(var, portletURL.toString());
160             }
161             else if (Validator.isNotNull(varImpl)) {
162                 pageContext.setAttribute(varImpl, portletURL);
163             }
164             else if (writeOutput) {
165                 pageContext.getOut().print(portletURL.toString());
166             }
167 
168             return portletURL.toString();
169         }
170         catch (Exception e) {
171             _log.error(e, e);
172 
173             throw new JspException(e);
174         }
175     }
176 
177     private static Log _log = LogFactoryUtil.getLog(ActionURLTagUtil.class);
178 
179 }