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.LongWrapper;
29  import com.liferay.portal.kernel.util.MethodInvoker;
30  import com.liferay.portal.kernel.util.MethodWrapper;
31  import com.liferay.portal.kernel.util.NullWrapper;
32  import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
33  import com.liferay.portal.kernel.util.StringPool;
34  
35  import javax.servlet.jsp.JspException;
36  import javax.servlet.jsp.PageContext;
37  import javax.servlet.jsp.tagext.TagSupport;
38  
39  /**
40   * <a href="DoAsURLTag.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   *
44   */
45  public class DoAsURLTag extends TagSupport {
46  
47      public static String doTag(
48              long doAsUserId, String var, boolean writeOutput,
49              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 varWrapper = var;
63  
64              if (varWrapper == null) {
65                  varWrapper = new NullWrapper(String.class.getName());
66              }
67  
68              MethodWrapper methodWrapper = new MethodWrapper(
69                  _TAG_CLASS, _TAG_DO_END_METHOD,
70                  new Object[] {
71                      new LongWrapper(doAsUserId), varWrapper,
72                      new BooleanWrapper(writeOutput), pageContext
73                  });
74  
75              returnObj = MethodInvoker.invoke(methodWrapper);
76          }
77          catch (Exception e) {
78              _log.error(e, e);
79          }
80          finally {
81              currentThread.setContextClassLoader(contextClassLoader);
82          }
83  
84          if (returnObj != null) {
85              return returnObj.toString();
86          }
87          else {
88              return StringPool.BLANK;
89          }
90      }
91  
92      public int doEndTag() throws JspException {
93          try {
94              doTag(_doAsUserId, _var, true, pageContext);
95          }
96          catch (Exception e) {
97              if (e instanceof JspException) {
98                  throw (JspException)e;
99              }
100             else {
101                 throw new JspException(e);
102             }
103         }
104 
105         return EVAL_PAGE;
106     }
107 
108     public void setDoAsUserId(long doAsUserId) {
109         _doAsUserId = doAsUserId;
110     }
111 
112     public void setVar(String var) {
113         _var = var;
114     }
115 
116     private static final String _TAG_CLASS =
117         "com.liferay.portal.servlet.taglib.security.DoAsURLTagUtil";
118 
119     private static final String _TAG_DO_END_METHOD = "doEndTag";
120 
121     private static Log _log = LogFactoryUtil.getLog(DoAsURLTag.class);
122 
123     private long _doAsUserId;
124     private String _var;
125 
126 }