View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.annotations;
20  
21  import javax.servlet.Servlet;
22  
23  import org.eclipse.jetty.annotations.AnnotationIntrospector.AbstractIntrospectableAnnotationHandler;
24  import org.eclipse.jetty.plus.annotation.RunAsCollection;
25  import org.eclipse.jetty.servlet.ServletHolder;
26  import org.eclipse.jetty.util.log.Log;
27  import org.eclipse.jetty.util.log.Logger;
28  import org.eclipse.jetty.webapp.Descriptor;
29  import org.eclipse.jetty.webapp.MetaData;
30  import org.eclipse.jetty.webapp.WebAppContext;
31  
32  
33  public class RunAsAnnotationHandler extends AbstractIntrospectableAnnotationHandler
34  {
35      private static final Logger LOG = Log.getLogger(RunAsAnnotationHandler.class);
36  
37      protected WebAppContext _context;
38  
39      public RunAsAnnotationHandler (WebAppContext wac)
40      {
41          //Introspect only the given class for a RunAs annotation, as it is a class level annotation,
42          //and according to Common Annotation Spec p2-6 a class-level annotation is not inheritable.
43          super(false);
44          _context = wac;
45      }
46  
47      public void doHandle (Class clazz)
48      {
49          if (!Servlet.class.isAssignableFrom(clazz))
50              return;
51  
52          javax.annotation.security.RunAs runAs = (javax.annotation.security.RunAs)clazz.getAnnotation(javax.annotation.security.RunAs.class);
53          if (runAs != null)
54          {
55              String role = runAs.value();
56              if (role != null)
57              {
58                  ServletHolder holder = getServletHolderForClass(clazz);
59                  if (holder != null)
60                  {
61                      MetaData metaData = _context.getMetaData();
62                      Descriptor d = metaData.getOriginDescriptor(holder.getName()+".servlet.run-as");
63                      //if a descriptor has already set the value for run-as, do not
64                      //let the annotation override it
65                      if (d == null)
66                      {
67                          metaData.setOrigin(holder.getName()+".servlet.run-as",runAs,clazz);
68                          org.eclipse.jetty.plus.annotation.RunAs ra = new org.eclipse.jetty.plus.annotation.RunAs();
69                          ra.setTargetClassName(clazz.getCanonicalName());
70                          ra.setRoleName(role);
71                          RunAsCollection raCollection = (RunAsCollection)_context.getAttribute(RunAsCollection.RUNAS_COLLECTION);
72                          if (raCollection == null)
73                          {
74                              raCollection = new RunAsCollection();
75                              _context.setAttribute(RunAsCollection.RUNAS_COLLECTION, raCollection);
76                          }
77                          raCollection.add(ra);
78                      }
79                  }
80              }
81              else
82                  LOG.warn("Bad value for @RunAs annotation on class "+clazz.getName());
83          }
84  
85      }
86  
87      public void handleField(String className, String fieldName, int access, String fieldType, String signature, Object value, String annotation)
88      {
89         LOG.warn ("@RunAs annotation not applicable for fields: "+className+"."+fieldName);
90      }
91  
92      public void handleMethod(String className, String methodName, int access, String params, String signature, String[] exceptions, String annotation)
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() != null && h.getClassName().equals(clazz.getName()))
106                 {
107                     holder = h;
108                 }
109             }
110         }
111         return holder;
112     }
113 }