1
2
3
4
5
6
7
8
9
10
11
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
37
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
59
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 }