1   /**
2    * Copyright (c) 2000-2009 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.PortletRequest;
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 request =
60                  (HttpServletRequest)pageContext.getRequest();
61  
62              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
63                  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(request);
71              }
72  
73              PortletURL portletURL = new PortletURLImpl(
74                  request, PortletKeys.PORTLET_CONFIGURATION, layout.getPlid(),
75                  PortletRequest.RENDER_PHASE);
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  
88              if (!themeDisplay.isStateMaximized()) {
89                  portletURL.setParameter("returnToFullPageURL", redirect);
90              }
91  
92              portletURL.setParameter("portletResource", portletDisplay.getId());
93              portletURL.setParameter("modelResource", modelResource);
94              portletURL.setParameter(
95                  "modelResourceDescription", modelResourceDescription);
96              portletURL.setParameter("resourcePrimKey", resourcePrimKey);
97  
98              String portletURLToString = portletURL.toString();
99  
100             if (Validator.isNotNull(var)) {
101                 pageContext.setAttribute(var, portletURLToString);
102             }
103             else if (writeOutput) {
104                 pageContext.getOut().print(portletURLToString);
105             }
106 
107             return portletURL.toString();
108         }
109         catch (Exception e) {
110             throw new JspException(e);
111         }
112     }
113 
114 }