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