1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class PermissionsURLTag extends TagSupport {
44  
45      public static String doTag(
46              String redirect, String modelResource,
47              String modelResourceDescription, String resourcePrimKey, String var,
48              boolean writeOutput, PageContext pageContext)
49          throws Exception {
50  
51          Object returnObj = null;
52  
53          Thread currentThread = Thread.currentThread();
54  
55          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
56  
57          try {
58              currentThread.setContextClassLoader(
59                  PortalClassLoaderUtil.getClassLoader());
60  
61              Object redirectWrapper = redirect;
62  
63              if (redirectWrapper == null) {
64                  redirectWrapper = new NullWrapper(String.class.getName());
65              }
66  
67              Object varWrapper = var;
68  
69              if (varWrapper == null) {
70                  varWrapper = new NullWrapper(String.class.getName());
71              }
72  
73              MethodWrapper methodWrapper = new MethodWrapper(
74                  _TAG_CLASS, _TAG_DO_END_METHOD,
75                  new Object[] {
76                      redirectWrapper, modelResource, modelResourceDescription,
77                      resourcePrimKey, varWrapper,
78                      new BooleanWrapper(writeOutput), pageContext
79                  });
80  
81              returnObj = MethodInvoker.invoke(methodWrapper);
82          }
83          catch (Exception e) {
84              _log.error(e, e);
85          }
86          finally {
87              currentThread.setContextClassLoader(contextClassLoader);
88          }
89  
90          if (returnObj != null) {
91              return returnObj.toString();
92          }
93          else {
94              return StringPool.BLANK;
95          }
96      }
97  
98      public int doEndTag() throws JspException {
99          try {
100             doTag(
101                 _redirect, _modelResource, _modelResourceDescription,
102                 _resourcePrimKey, _var, true, pageContext);
103         }
104         catch (Exception e) {
105             if (e instanceof JspException) {
106                 throw (JspException)e;
107             }
108             else {
109                 throw new JspException(e);
110             }
111         }
112 
113         return EVAL_PAGE;
114     }
115 
116     public void setRedirect(String redirect) {
117         _redirect = redirect;
118     }
119 
120     public void setModelResource(String modelResource) {
121         _modelResource = modelResource;
122     }
123 
124     public void setModelResourceDescription(String modelResourceDescription) {
125         _modelResourceDescription = modelResourceDescription;
126     }
127 
128     public void setResourcePrimKey(String resourcePrimKey) {
129         _resourcePrimKey = resourcePrimKey;
130     }
131 
132     public void setVar(String var) {
133         _var = var;
134     }
135 
136     private static final String _TAG_CLASS =
137         "com.liferay.portal.servlet.taglib.security.PermissionsURLTagUtil";
138 
139     private static final String _TAG_DO_END_METHOD = "doEndTag";
140 
141     private static Log _log = LogFactoryUtil.getLog(PermissionsURLTag.class);
142 
143     private String _redirect;
144     private String _modelResource;
145     private String _modelResourceDescription;
146     private String _resourcePrimKey;
147     private String _var;
148 
149 }