1 package org.kohsuke.stapler; 2 3 import javax.servlet.http.HttpServletRequest ; 4 import java.lang.reflect.InvocationTargetException ; 5 import java.lang.reflect.Method ; 6 7 13 abstract class Function { 14 abstract String getName(); 15 abstract Class [] getParameterTypes(); 16 abstract Object invoke(HttpServletRequest req, Object o, Object ... args) throws IllegalAccessException , InvocationTargetException ; 17 18 final Function protectBy(Method m) { 19 try { 20 LimitedTo a = m.getAnnotation(LimitedTo.class); 21 if(a==null) 22 return this; else 24 return new ProtectedFunction(this,a.value()); 25 } catch (LinkageError e) { 26 return this; 28 } 29 } 30 31 34 static final class InstanceFunction extends Function { 35 private final Method m; 36 37 public InstanceFunction(Method m) { 38 this.m = m; 39 } 40 41 public String getName() { 42 return m.getName(); 43 } 44 45 public Class [] getParameterTypes() { 46 return m.getParameterTypes(); 47 } 48 49 public Object invoke(HttpServletRequest req, Object o, Object ... args) throws IllegalAccessException , InvocationTargetException { 50 return m.invoke(o,args); 51 } 52 } 53 54 57 static final class StaticFunction extends Function { 58 private final Method m; 59 60 public StaticFunction(Method m) { 61 this.m = m; 62 } 63 64 public String getName() { 65 return m.getName(); 66 } 67 68 public Class [] getParameterTypes() { 69 Class [] p = m.getParameterTypes(); 70 Class [] r = new Class [p.length-1]; 71 System.arraycopy(p,1,r,0,r.length); 72 return r; 73 } 74 75 public Object invoke(HttpServletRequest req, Object o, Object ... args) throws IllegalAccessException , InvocationTargetException { 76 Object [] r = new Object [args.length+1]; 77 r[0] = o; 78 System.arraycopy(args,0,r,1,args.length); 79 return m.invoke(null,r); 80 } 81 } 82 83 86 static final class ProtectedFunction extends Function { 87 private final String role; 88 private final Function core; 89 90 public ProtectedFunction(Function core, String role) { 91 this.role = role; 92 this.core = core; 93 } 94 95 public String getName() { 96 return core.getName(); 97 } 98 99 public Class [] getParameterTypes() { 100 return core.getParameterTypes(); 101 } 102 103 public Object invoke(HttpServletRequest req, Object o, Object ... args) throws IllegalAccessException , InvocationTargetException { 104 if(req.isUserInRole(role)) 105 return core.invoke(req, o, args); 106 else 107 throw new IllegalAccessException ("Needs to be in role "+role); 108 } 109 } 110 } 111 | Popular Tags |