1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.taglib.security;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.util.BooleanWrapper;
25  import com.liferay.portal.kernel.util.MethodInvoker;
26  import com.liferay.portal.kernel.util.MethodWrapper;
27  import com.liferay.portal.kernel.util.NullWrapper;
28  import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
29  import com.liferay.portal.kernel.util.StringPool;
30  
31  import javax.servlet.jsp.JspException;
32  import javax.servlet.jsp.PageContext;
33  import javax.servlet.jsp.tagext.TagSupport;
34  
35  /**
36   * <a href="PermissionsURLTag.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   *
40   */
41  public class PermissionsURLTag extends TagSupport {
42  
43      public static String doTag(
44              String redirect, String modelResource,
45              String modelResourceDescription, String resourcePrimKey, String var,
46              boolean writeOutput, PageContext pageContext)
47          throws Exception {
48  
49          Object returnObj = null;
50  
51          Thread currentThread = Thread.currentThread();
52  
53          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
54  
55          try {
56              currentThread.setContextClassLoader(
57                  PortalClassLoaderUtil.getClassLoader());
58  
59              Object redirectWrapper = redirect;
60  
61              if (redirectWrapper == null) {
62                  redirectWrapper = new NullWrapper(String.class.getName());
63              }
64  
65              Object varWrapper = var;
66  
67              if (varWrapper == null) {
68                  varWrapper = new NullWrapper(String.class.getName());
69              }
70  
71              MethodWrapper methodWrapper = new MethodWrapper(
72                  _TAG_CLASS, _TAG_DO_END_METHOD,
73                  new Object[] {
74                      redirectWrapper, modelResource, modelResourceDescription,
75                      resourcePrimKey, varWrapper,
76                      new BooleanWrapper(writeOutput), pageContext
77                  });
78  
79              returnObj = MethodInvoker.invoke(methodWrapper);
80          }
81          catch (Exception e) {
82              _log.error(e, e);
83          }
84          finally {
85              currentThread.setContextClassLoader(contextClassLoader);
86          }
87  
88          if (returnObj != null) {
89              return returnObj.toString();
90          }
91          else {
92              return StringPool.BLANK;
93          }
94      }
95  
96      public int doEndTag() throws JspException {
97          try {
98              doTag(
99                  _redirect, _modelResource, _modelResourceDescription,
100                 _resourcePrimKey, _var, true, pageContext);
101         }
102         catch (Exception e) {
103             if (e instanceof JspException) {
104                 throw (JspException)e;
105             }
106             else {
107                 throw new JspException(e);
108             }
109         }
110 
111         return EVAL_PAGE;
112     }
113 
114     public void setRedirect(String redirect) {
115         _redirect = redirect;
116     }
117 
118     public void setModelResource(String modelResource) {
119         _modelResource = modelResource;
120     }
121 
122     public void setModelResourceDescription(String modelResourceDescription) {
123         _modelResourceDescription = modelResourceDescription;
124     }
125 
126     public void setResourcePrimKey(String resourcePrimKey) {
127         _resourcePrimKey = resourcePrimKey;
128     }
129 
130     public void setVar(String var) {
131         _var = var;
132     }
133 
134     private static final String _TAG_CLASS =
135         "com.liferay.portal.servlet.taglib.security.PermissionsURLTagUtil";
136 
137     private static final String _TAG_DO_END_METHOD = "doEndTag";
138 
139     private static Log _log = LogFactoryUtil.getLog(PermissionsURLTag.class);
140 
141     private String _redirect;
142     private String _modelResource;
143     private String _modelResourceDescription;
144     private String _resourcePrimKey;
145     private String _var;
146 
147 }