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