View Javadoc

1   // ========================================================================
2   // Copyright (c) 2006-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.plus.annotation;
15  
16  import javax.servlet.ServletException;
17  
18  import org.eclipse.jetty.security.SecurityHandler;
19  import org.eclipse.jetty.servlet.ServletHolder;
20  
21  /**
22   * RunAs
23   * <p/>
24   * Represents a &lt;run-as&gt; element in web.xml, or a runAs annotation.
25   */
26  public class RunAs
27  {
28      private Class _targetClass;
29      private String _roleName;
30  
31      public RunAs()
32      {}
33  
34  
35      public void setTargetClass (Class clazz)
36      {
37          _targetClass=clazz;
38      }
39  
40      public Class getTargetClass ()
41      {
42          return _targetClass;
43      }
44  
45      public void setRoleName (String roleName)
46      {
47          _roleName = roleName;
48      }
49  
50      public String getRoleName ()
51      {
52          return _roleName;
53      }
54  
55  
56      public void setRunAs (ServletHolder holder, SecurityHandler securityHandler)
57      throws ServletException
58      {
59          if (holder == null)
60              return;
61          String className = getServletClassNameForHolder(holder);
62  
63          if (className.equals(_targetClass.getName()))
64              holder.setRunAsRole(_roleName);
65      }
66  
67      public static String getServletClassNameForHolder (ServletHolder holder)
68      throws ServletException
69      {
70          return holder.getClassName();
71      }
72  
73  }