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