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.security;
24  
25  import com.liferay.portal.kernel.portlet.LiferayWindowState;
26  import com.liferay.portal.kernel.util.UnicodeFormatter;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.model.Layout;
29  import com.liferay.portal.theme.PortletDisplay;
30  import com.liferay.portal.theme.ThemeDisplay;
31  import com.liferay.portal.util.PortalUtil;
32  import com.liferay.portal.util.PortletKeys;
33  import com.liferay.portal.util.WebKeys;
34  import com.liferay.portlet.PortletURLImpl;
35  
36  import javax.portlet.PortletURL;
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  import javax.servlet.jsp.tagext.TagSupport;
43  
44  /**
45   * <a href="PermissionsURLTagUtil.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   *
49   */
50  public class PermissionsURLTagUtil extends TagSupport {
51  
52      public static String doEndTag(
53              String redirect, String modelResource,
54              String modelResourceDescription, String resourcePrimKey, String var,
55              boolean writeOutput, PageContext pageContext)
56          throws JspException {
57  
58          try {
59              HttpServletRequest req =
60                  (HttpServletRequest)pageContext.getRequest();
61  
62              ThemeDisplay themeDisplay =
63                  (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
64  
65              PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
66  
67              Layout layout = themeDisplay.getLayout();
68  
69              if (Validator.isNull(redirect)) {
70                  redirect = PortalUtil.getCurrentURL(req);
71              }
72  
73              PortletURL portletURL = new PortletURLImpl(
74                  req, PortletKeys.PORTLET_CONFIGURATION, layout.getPlid(),
75                  false);
76  
77              if (themeDisplay.isStatePopUp()) {
78                  portletURL.setWindowState(LiferayWindowState.POP_UP);
79              }
80              else {
81                  portletURL.setWindowState(WindowState.MAXIMIZED);
82              }
83  
84              portletURL.setParameter(
85                  "struts_action", "/portlet_configuration/edit_permissions");
86              portletURL.setParameter("redirect", redirect);
87              portletURL.setParameter("backURL", redirect);
88              portletURL.setParameter("portletResource", portletDisplay.getId());
89              portletURL.setParameter("modelResource", modelResource);
90              portletURL.setParameter(
91                  "modelResourceDescription",
92                  UnicodeFormatter.toString(modelResourceDescription));
93              portletURL.setParameter("resourcePrimKey", resourcePrimKey);
94  
95              String portletURLToString = portletURL.toString();
96  
97              if (Validator.isNotNull(var)) {
98                  pageContext.setAttribute(var, portletURLToString);
99              }
100             else if (writeOutput) {
101                 pageContext.getOut().print(portletURLToString);
102             }
103 
104             return portletURL.toString();
105         }
106         catch (Exception e) {
107             throw new JspException(e);
108         }
109     }
110 
111 }