1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.annotations;
20
21 import java.util.List;
22 import javax.servlet.Servlet;
23
24 import org.eclipse.jetty.annotations.AnnotationIntrospector.AbstractIntrospectableAnnotationHandler;
25 import org.eclipse.jetty.annotations.AnnotationParser.Value;
26 import org.eclipse.jetty.plus.annotation.RunAsCollection;
27 import org.eclipse.jetty.servlet.ServletHolder;
28 import org.eclipse.jetty.util.log.Log;
29 import org.eclipse.jetty.util.log.Logger;
30 import org.eclipse.jetty.webapp.Descriptor;
31 import org.eclipse.jetty.webapp.MetaData;
32 import org.eclipse.jetty.webapp.WebAppContext;
33
34
35 public class RunAsAnnotationHandler extends AbstractIntrospectableAnnotationHandler
36 {
37 private static final Logger LOG = Log.getLogger(RunAsAnnotationHandler.class);
38
39 protected WebAppContext _context;
40
41 public RunAsAnnotationHandler (WebAppContext wac)
42 {
43
44
45 super(false);
46 _context = wac;
47 }
48
49 public void doHandle (Class clazz)
50 {
51 if (!Servlet.class.isAssignableFrom(clazz))
52 return;
53
54 javax.annotation.security.RunAs runAs = (javax.annotation.security.RunAs)clazz.getAnnotation(javax.annotation.security.RunAs.class);
55 if (runAs != null)
56 {
57 String role = runAs.value();
58 if (role != null)
59 {
60 ServletHolder holder = getServletHolderForClass(clazz);
61 if (holder != null)
62 {
63 MetaData metaData = _context.getMetaData();
64 Descriptor d = metaData.getOriginDescriptor(holder.getName()+".servlet.run-as");
65
66
67 if (d == null)
68 {
69 metaData.setOrigin(holder.getName()+".servlet.run-as");
70 org.eclipse.jetty.plus.annotation.RunAs ra = new org.eclipse.jetty.plus.annotation.RunAs();
71 ra.setTargetClassName(clazz.getCanonicalName());
72 ra.setRoleName(role);
73 RunAsCollection raCollection = (RunAsCollection)_context.getAttribute(RunAsCollection.RUNAS_COLLECTION);
74 if (raCollection == null)
75 {
76 raCollection = new RunAsCollection();
77 _context.setAttribute(RunAsCollection.RUNAS_COLLECTION, raCollection);
78 }
79 raCollection.add(ra);
80 }
81 }
82 }
83 else
84 LOG.warn("Bad value for @RunAs annotation on class "+clazz.getName());
85 }
86
87 }
88
89 public void handleField(String className, String fieldName, int access, String fieldType, String signature, Object value, String annotation,
90 List<Value> values)
91 {
92 LOG.warn ("@RunAs annotation not applicable for fields: "+className+"."+fieldName);
93 }
94
95 public void handleMethod(String className, String methodName, int access, String params, String signature, String[] exceptions, String annotation,
96 List<Value> values)
97 {
98 LOG.warn("@RunAs annotation ignored on method: "+className+"."+methodName+" "+signature);
99 }
100
101 private ServletHolder getServletHolderForClass (Class clazz)
102 {
103 ServletHolder holder = null;
104 ServletHolder[] holders = _context.getServletHandler().getServlets();
105 if (holders != null)
106 {
107 for (ServletHolder h : holders)
108 {
109 if (h.getClassName() != null && h.getClassName().equals(clazz.getName()))
110 {
111 holder = h;
112 }
113 }
114 }
115 return holder;
116 }
117 }