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.taglib.security;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.BooleanWrapper;
28  import com.liferay.portal.kernel.util.MethodInvoker;
29  import com.liferay.portal.kernel.util.MethodWrapper;
30  import com.liferay.portal.kernel.util.NullWrapper;
31  import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
32  import com.liferay.portal.kernel.util.StringPool;
33  
34  import javax.servlet.jsp.JspException;
35  import javax.servlet.jsp.PageContext;
36  import javax.servlet.jsp.tagext.TagSupport;
37  
38  /**
39   * <a href="PermissionsURLTag.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   *
43   */
44  public class PermissionsURLTag extends TagSupport {
45  
46      public static String doTag(
47              String redirect, String modelResource,
48              String modelResourceDescription, String resourcePrimKey, String var,
49              boolean writeOutput, PageContext pageContext)
50          throws Exception {
51  
52          Object returnObj = null;
53  
54          Thread currentThread = Thread.currentThread();
55  
56          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
57  
58          try {
59              currentThread.setContextClassLoader(
60                  PortalClassLoaderUtil.getClassLoader());
61  
62              Object redirectWrapper = redirect;
63  
64              if (redirectWrapper == null) {
65                  redirectWrapper = new NullWrapper(String.class.getName());
66              }
67  
68              Object varWrapper = var;
69  
70              if (varWrapper == null) {
71                  varWrapper = new NullWrapper(String.class.getName());
72              }
73  
74              MethodWrapper methodWrapper = new MethodWrapper(
75                  _TAG_CLASS, _TAG_DO_END_METHOD,
76                  new Object[] {
77                      redirectWrapper, modelResource, modelResourceDescription,
78                      resourcePrimKey, varWrapper,
79                      new BooleanWrapper(writeOutput), pageContext
80                  });
81  
82              returnObj = MethodInvoker.invoke(methodWrapper);
83          }
84          catch (Exception e) {
85              _log.error(e, e);
86          }
87          finally {
88              currentThread.setContextClassLoader(contextClassLoader);
89          }
90  
91          if (returnObj != null) {
92              return returnObj.toString();
93          }
94          else {
95              return StringPool.BLANK;
96          }
97      }
98  
99      public int doEndTag() throws JspException {
100         try {
101             doTag(
102                 _redirect, _modelResource, _modelResourceDescription,
103                 _resourcePrimKey, _var, true, pageContext);
104         }
105         catch (Exception e) {
106             if (e instanceof JspException) {
107                 throw (JspException)e;
108             }
109             else {
110                 throw new JspException(e);
111             }
112         }
113 
114         return EVAL_PAGE;
115     }
116 
117     public void setRedirect(String redirect) {
118         _redirect = redirect;
119     }
120 
121     public void setModelResource(String modelResource) {
122         _modelResource = modelResource;
123     }
124 
125     public void setModelResourceDescription(String modelResourceDescription) {
126         _modelResourceDescription = modelResourceDescription;
127     }
128 
129     public void setResourcePrimKey(String resourcePrimKey) {
130         _resourcePrimKey = resourcePrimKey;
131     }
132 
133     public void setVar(String var) {
134         _var = var;
135     }
136 
137     private static final String _TAG_CLASS =
138         "com.liferay.portal.servlet.taglib.security.PermissionsURLTagUtil";
139 
140     private static final String _TAG_DO_END_METHOD = "doEndTag";
141 
142     private static Log _log = LogFactoryUtil.getLog(PermissionsURLTag.class);
143 
144     private String _redirect;
145     private String _modelResource;
146     private String _modelResourceDescription;
147     private String _resourcePrimKey;
148     private String _var;
149 
150 }