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