View Javadoc

1   // ========================================================================
2   // Copyright (c) 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.annotations;
15  
16  import java.util.List;
17  
18  import javax.servlet.Servlet;
19  
20  import org.eclipse.jetty.annotations.AnnotationIntrospector.AbstractIntrospectableAnnotationHandler;
21  import org.eclipse.jetty.annotations.AnnotationParser.Value;
22  import org.eclipse.jetty.plus.annotation.RunAsCollection;
23  import org.eclipse.jetty.util.log.Log;
24  import org.eclipse.jetty.webapp.WebAppContext;
25  
26  public class RunAsAnnotationHandler extends AbstractIntrospectableAnnotationHandler
27  {
28      protected WebAppContext _wac;
29  
30      public RunAsAnnotationHandler (WebAppContext wac)
31      {
32          //Introspect only the given class for a RunAs annotation, as it is a class level annotation,
33          //and according to Common Annotation Spec p2-6 a class-level annotation is not inheritable.
34          super(false);
35          _wac = wac;
36      }
37      
38      public void doHandle (Class clazz)
39      {
40          RunAsCollection runAsCollection = (RunAsCollection)_wac.getAttribute(RunAsCollection.RUNAS_COLLECTION);
41  
42          if (!Servlet.class.isAssignableFrom(clazz))
43              return;
44          
45          javax.annotation.security.RunAs runAs = (javax.annotation.security.RunAs)clazz.getAnnotation(javax.annotation.security.RunAs.class);
46          if (runAs != null)
47          {
48              String role = runAs.value();
49              if (role != null)
50              {
51                  org.eclipse.jetty.plus.annotation.RunAs ra = new org.eclipse.jetty.plus.annotation.RunAs();
52                  ra.setTargetClassName(clazz.getCanonicalName());
53                  ra.setRoleName(role);
54                  runAsCollection.add(ra);
55              }
56              else
57                  Log.warn("Bad value for @RunAs annotation on class "+clazz.getName());
58          }
59  
60      }
61  
62      public void handleField(String className, String fieldName, int access, String fieldType, String signature, Object value, String annotation,
63                              List<Value> values)
64      {
65         Log.warn ("@RunAs annotation not applicable for fields: "+className+"."+fieldName);
66      }
67  
68      public void handleMethod(String className, String methodName, int access, String params, String signature, String[] exceptions, String annotation,
69                               List<Value> values)
70      {
71          Log.warn("@RunAs annotation ignored on method: "+className+"."+methodName+" "+signature);
72      }
73  
74  }