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.servlet.ServletHolder;
24  import org.eclipse.jetty.util.log.Log;
25  import org.eclipse.jetty.webapp.Descriptor;
26  import org.eclipse.jetty.webapp.MetaData;
27  import org.eclipse.jetty.webapp.WebAppContext;
28  
29  
30  public class RunAsAnnotationHandler extends AbstractIntrospectableAnnotationHandler
31  {
32      protected WebAppContext _context;
33  
34      public RunAsAnnotationHandler (WebAppContext wac)
35      {
36          //Introspect only the given class for a RunAs annotation, as it is a class level annotation,
37          //and according to Common Annotation Spec p2-6 a class-level annotation is not inheritable.
38          super(false);
39          _context = wac;
40      }
41      
42      public void doHandle (Class clazz)
43      {
44          if (!Servlet.class.isAssignableFrom(clazz))
45              return;
46          
47          javax.annotation.security.RunAs runAs = (javax.annotation.security.RunAs)clazz.getAnnotation(javax.annotation.security.RunAs.class);
48          if (runAs != null)
49          {
50              String role = runAs.value();
51              if (role != null)
52              {
53                  ServletHolder holder = getServletHolderForClass(clazz);
54                  if (holder != null)
55                  {
56                      MetaData metaData = _context.getMetaData();
57                      Descriptor d = metaData.getOriginDescriptor(holder.getName()+".servlet.run-as");
58                      //if a descriptor has already set the value for run-as, do not 
59                      //let the annotation override it
60                      if (d == null)
61                      {
62                          metaData.setOrigin(holder.getName()+".servlet.run-as");
63                          org.eclipse.jetty.plus.annotation.RunAs ra = new org.eclipse.jetty.plus.annotation.RunAs();
64                          ra.setTargetClassName(clazz.getCanonicalName());
65                          ra.setRoleName(role);
66                          RunAsCollection raCollection = (RunAsCollection)_context.getAttribute(RunAsCollection.RUNAS_COLLECTION);
67                          if (raCollection == null)
68                          {
69                              raCollection = new RunAsCollection();
70                              _context.setAttribute(RunAsCollection.RUNAS_COLLECTION, raCollection);
71                          }
72                          raCollection.add(ra);
73                      }
74                  }
75              }
76              else
77                  Log.warn("Bad value for @RunAs annotation on class "+clazz.getName());
78          }
79  
80      }
81  
82      public void handleField(String className, String fieldName, int access, String fieldType, String signature, Object value, String annotation,
83                              List<Value> values)
84      {
85         Log.warn ("@RunAs annotation not applicable for fields: "+className+"."+fieldName);
86      }
87  
88      public void handleMethod(String className, String methodName, int access, String params, String signature, String[] exceptions, String annotation,
89                               List<Value> values)
90      {
91          Log.warn("@RunAs annotation ignored on method: "+className+"."+methodName+" "+signature);
92      }
93  
94      private ServletHolder getServletHolderForClass (Class clazz)
95      {
96          ServletHolder holder = null;
97          ServletHolder[] holders = _context.getServletHandler().getServlets();
98          if (holders != null)
99          {
100             for (ServletHolder h : holders)
101             {
102                 if (h.getClassName().equals(clazz.getName()))
103                 {
104                     holder = h;
105                 }
106             }
107         }
108         return holder;
109     }
110 }