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.JavaConstants;
29  import com.liferay.portal.kernel.util.ParamUtil;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portlet.PortletConfigImpl;
33  import com.liferay.portlet.PortletResponseImpl;
34  import com.liferay.util.MapUtil;
35  
36  import java.util.Map;
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 req =
65                  (HttpServletRequest)pageContext.getRequest();
66  
67              if (portletName == null) {
68                  PortletConfigImpl portletConfig =
69                      (PortletConfigImpl)req.getAttribute(
70                          JavaConstants.JAVAX_PORTLET_CONFIG);
71  
72                  portletName = portletConfig.getPortletId();
73              }
74  
75              PortletResponseImpl portletRes =
76                  (PortletResponseImpl)req.getAttribute(
77                      JavaConstants.JAVAX_PORTLET_RESPONSE);
78  
79              if (portletRes == null) {
80                  _log.error(
81                      "Render response is null because this tag is not being " +
82                          "called within the context of a portlet");
83  
84                  return StringPool.BLANK;
85              }
86  
87              LiferayPortletURL portletURL = portletRes.createPortletURLImpl(
88                  portletName, lifecycle);
89  
90              if (Validator.isNotNull(windowState)) {
91                  portletURL.setWindowState(
92                      WindowStateFactory.getWindowState(windowState));
93              }
94  
95              if (Validator.isNotNull(portletMode)) {
96                  portletURL.setPortletMode(
97                      PortletModeFactory.getPortletMode(portletMode));
98              }
99  
100             if (secure != null) {
101                 portletURL.setSecure(secure.booleanValue());
102             }
103             else {
104                 portletURL.setSecure(req.isSecure());
105             }
106 
107             if (copyCurrentRenderParameters != null) {
108                 portletURL.setCopyCurrentRenderParameters(
109                     copyCurrentRenderParameters.booleanValue());
110             }
111 
112             if (escapeXml != null) {
113                 portletURL.setEscapeXml(escapeXml.booleanValue());
114             }
115 
116             if (resourceID != null) {
117                 portletURL.setResourceID(resourceID);
118             }
119 
120             if (cacheability != null) {
121                 portletURL.setCacheability(cacheability);
122             }
123 
124             if (anchor != null) {
125                 portletURL.setAnchor(anchor.booleanValue());
126             }
127 
128             if (encrypt != null) {
129                 portletURL.setEncrypt(encrypt.booleanValue());
130             }
131 
132             if (doAsUserId > 0) {
133                 portletURL.setDoAsUserId(doAsUserId);
134             }
135 
136             if ((portletConfiguration != null) &&
137                 portletConfiguration.booleanValue()) {
138 
139                 String returnToFullPageURL = ParamUtil.getString(
140                     req, "returnToFullPageURL");
141                 String portletResource = ParamUtil.getString(
142                     req, "portletResource");
143                 String previewWidth = ParamUtil.getString(req, "previewWidth");
144 
145                 portletURL.setParameter(
146                     "struts_action",
147                     "/portlet_configuration/edit_configuration");
148                 portletURL.setParameter(
149                     "returnToFullPageURL", returnToFullPageURL);
150                 portletURL.setParameter("portletResource", portletResource);
151                 portletURL.setParameter("previewWidth", previewWidth);
152             }
153 
154             if (params != null) {
155                 MapUtil.merge(portletURL.getParameterMap(), params);
156 
157                 portletURL.setParameters(params);
158             }
159 
160             if (Validator.isNotNull(var)) {
161                 pageContext.setAttribute(var, portletURL.toString());
162             }
163             else if (Validator.isNotNull(varImpl)) {
164                 pageContext.setAttribute(varImpl, portletURL);
165             }
166             else if (writeOutput) {
167                 pageContext.getOut().print(portletURL.toString());
168             }
169 
170             return portletURL.toString();
171         }
172         catch (Exception e) {
173             _log.error(e, e);
174 
175             throw new JspException(e);
176         }
177     }
178 
179     private static Log _log = LogFactory.getLog(ActionURLTagUtil.class);
180 
181 }