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.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  public class DoAsURLTag extends TagSupport {
45  
46      public static String doTag(
47              long doAsUserId, String var, boolean writeOutput,
48              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 varWrapper = var;
62  
63              if (varWrapper == null) {
64                  varWrapper = new NullWrapper(String.class.getName());
65              }
66  
67              MethodWrapper methodWrapper = new MethodWrapper(
68                  _TAG_CLASS, _TAG_DO_END_METHOD,
69                  new Object[] {
70                      new LongWrapper(doAsUserId), varWrapper,
71                      new BooleanWrapper(writeOutput), pageContext
72                  });
73  
74              returnObj = MethodInvoker.invoke(methodWrapper);
75          }
76          catch (Exception e) {
77              _log.error(e, e);
78          }
79          finally {
80              currentThread.setContextClassLoader(contextClassLoader);
81          }
82  
83          if (returnObj != null) {
84              return returnObj.toString();
85          }
86          else {
87              return StringPool.BLANK;
88          }
89      }
90  
91      public int doEndTag() throws JspException {
92          try {
93              doTag(_doAsUserId, _var, true, pageContext);
94          }
95          catch (Exception e) {
96              if (e instanceof JspException) {
97                  throw (JspException)e;
98              }
99              else {
100                 throw new JspException(e);
101             }
102         }
103 
104         return EVAL_PAGE;
105     }
106 
107     public void setDoAsUserId(long doAsUserId) {
108         _doAsUserId = doAsUserId;
109     }
110 
111     public void setVar(String var) {
112         _var = var;
113     }
114 
115     private static final String _TAG_CLASS =
116         "com.liferay.portal.servlet.taglib.security.DoAsURLTagUtil";
117 
118     private static final String _TAG_DO_END_METHOD = "doEndTag";
119 
120     private static Log _log = LogFactoryUtil.getLog(DoAsURLTag.class);
121 
122     private long _doAsUserId;
123     private String _var;
124 
125 }