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