[8780cbec] | 1 | package com.nailabs.abac.credential; |
---|
| 2 | |
---|
| 3 | import java.util.Iterator; |
---|
| 4 | import java.util.StringTokenizer; |
---|
| 5 | import edu.stanford.peer.rbtm.credential.Entity; |
---|
| 6 | import edu.stanford.peer.rbtm.credential.EntityExpression; |
---|
| 7 | import edu.stanford.peer.rbtm.credential.StaticCredential; |
---|
| 8 | import edu.stanford.peer.rbtm.credential.Role; |
---|
| 9 | import edu.stanford.peer.rbtm.credential.LinkedRole; |
---|
| 10 | import edu.stanford.peer.rbtm.credential.Intersection; |
---|
| 11 | import edu.stanford.rt.credential.*; |
---|
| 12 | |
---|
| 13 | /** |
---|
| 14 | * An enitty expression factory which generates the appropriate entity |
---|
| 15 | * expression from various pieces of an RTML credential domain. |
---|
| 16 | */ |
---|
| 17 | public class RtmlExpression implements EntityExpression { |
---|
| 18 | |
---|
| 19 | public static RtmlEntity convert(Entity expr) throws Exception { |
---|
| 20 | StringTokenizer st = |
---|
| 21 | new StringTokenizer(((Entity)expr).getName(), "!"); |
---|
| 22 | String hashCode = st.nextToken(); |
---|
| 23 | if(st.hasMoreTokens()) { |
---|
| 24 | String shortName = st.nextToken(); |
---|
| 25 | return new RtmlEntity(shortName, hashCode); |
---|
| 26 | } else { |
---|
| 27 | return new RtmlEntity(hashCode); |
---|
| 28 | } |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | public static Role convert(Role r) throws Exception { |
---|
| 32 | return new Role(convert(r.getBase()), r.getName()); |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | public static LinkedRole convert(LinkedRole lr) throws Exception { |
---|
| 36 | return new |
---|
| 37 | LinkedRole(convert(lr.getFirstRole()), lr.getSecondRoleName()); |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | public static Intersection convert(Intersection i) throws Exception { |
---|
| 41 | Intersection intersection = new Intersection(); |
---|
| 42 | Iterator parts = intersection.getParts(); |
---|
| 43 | while(parts.hasNext()) { |
---|
| 44 | intersection.and(convert((EntityExpression)parts.next())); |
---|
| 45 | } |
---|
| 46 | return intersection; |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | public static EntityExpression convert(EntityExpression expr) |
---|
| 50 | throws Exception{ |
---|
| 51 | if(expr instanceof Entity) { |
---|
| 52 | return convert((Entity)expr); |
---|
| 53 | } |
---|
| 54 | if(expr instanceof Role) { |
---|
| 55 | return convert((Role)expr); |
---|
| 56 | } |
---|
| 57 | if(expr instanceof LinkedRole) { |
---|
| 58 | return convert((LinkedRole)expr); |
---|
| 59 | } |
---|
| 60 | if(expr instanceof Intersection) { |
---|
| 61 | return convert((Intersection)expr); |
---|
| 62 | } |
---|
| 63 | return null; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | public static EntityExpression getEntityExpression(String text) |
---|
| 67 | throws Exception { |
---|
| 68 | return convert(StaticCredential.getEntityExpression(text)); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | /** converts a principal into an entity */ |
---|
| 72 | public static RtmlEntity convert(Principal p, CredentialDomain domain) { |
---|
| 73 | return new RtmlEntity(domain.getPrincipal(p), p); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | /** converts an rtml role into and rbtm role */ |
---|
| 77 | public static edu.stanford.peer.rbtm.credential.Role convert |
---|
| 78 | (edu.stanford.rt.credential.Role rtmlRole, CredentialDomain domain) { |
---|
| 79 | RtmlEntity entity = convert(rtmlRole.getPrincipal(), domain); |
---|
| 80 | //System.out.print(entity.getName() + "."); |
---|
| 81 | String roleName = rtmlRole.getName(); |
---|
| 82 | return new Role(entity, roleName); |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | /** converts an rtml linked role into an rbtm linked role */ |
---|
| 86 | public static edu.stanford.peer.rbtm.credential.LinkedRole convert |
---|
| 87 | (edu.stanford.rt.credential.LinkedRole rtml, CredentialDomain domain) { |
---|
| 88 | RtmlEntity entity = convert(domain.getIssuer(), domain); |
---|
| 89 | String primaryRole = |
---|
| 90 | rtml.getRoleTermAt(0).getRoleDeclaration().getName(); |
---|
| 91 | String secondaryRole = |
---|
| 92 | rtml.getRoleTermAt(1).getRoleDeclaration().getName(); |
---|
| 93 | return new LinkedRole(entity, primaryRole, secondaryRole); |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | /** converts a role intersection into an rbtm intersection */ |
---|
| 97 | public static edu.stanford.peer.rbtm.credential.Intersection convert |
---|
| 98 | (edu.stanford.rt.credential.RoleIntersection rtml, CredentialDomain domain) { |
---|
| 99 | System.out.println("RTML Intersection:"); |
---|
| 100 | RtmlEntity issuer = convert(domain.getIssuer(), domain); |
---|
| 101 | Iterator parts = rtml.getParts().iterator(); |
---|
| 102 | Intersection intersect = new Intersection(); |
---|
| 103 | int pCount = 0; |
---|
| 104 | while(parts.hasNext()) { |
---|
| 105 | RoleTerm term = (RoleTerm)parts.next(); |
---|
| 106 | edu.stanford.peer.rbtm.credential.Role role = |
---|
| 107 | new Role(issuer, term.getRoleDeclaration().getName()); |
---|
| 108 | System.out.println("Part number " + ++pCount); |
---|
| 109 | System.out.println(role); |
---|
| 110 | intersect.and(role); |
---|
| 111 | } |
---|
| 112 | return intersect; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | /** |
---|
| 116 | * a polymorphic conversion method, which acts as an entity expression |
---|
| 117 | * factory. |
---|
| 118 | */ |
---|
| 119 | public static EntityExpression convert(PrincipalExpression expr, CredentialDomain domain) { |
---|
| 120 | if(expr instanceof edu.stanford.rt.credential.Principal) { |
---|
| 121 | return convert((edu.stanford.rt.credential.Principal)expr, domain); |
---|
| 122 | } |
---|
| 123 | if(expr instanceof edu.stanford.rt.credential.Role) { |
---|
| 124 | return convert((edu.stanford.rt.credential.Role)expr, domain); |
---|
| 125 | } |
---|
| 126 | if(expr instanceof edu.stanford.rt.credential.LinkedRole) { |
---|
| 127 | return convert((edu.stanford.rt.credential.LinkedRole)expr, domain); |
---|
| 128 | } |
---|
| 129 | if(expr instanceof edu.stanford.rt.credential.RoleIntersection) { |
---|
| 130 | return convert((edu.stanford.rt.credential.RoleIntersection)expr, |
---|
| 131 | domain); |
---|
| 132 | } |
---|
| 133 | System.out.println("$$$ Skipping over " + expr); |
---|
| 134 | return null; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | public static String toString(Entity e) { |
---|
| 138 | if(e instanceof RtmlEntity) { |
---|
| 139 | return ((RtmlEntity)e).getHash(); |
---|
| 140 | } |
---|
| 141 | return e.toString(); |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | public static String toString(Role r) { |
---|
| 145 | StringBuffer buff = new StringBuffer(toString(r.getBase())); |
---|
| 146 | buff.append(".").append(r.getName().toString()); |
---|
| 147 | return buff.toString(); |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | public static String toString(LinkedRole r) { |
---|
| 151 | StringBuffer buff = new StringBuffer(toString(r.getFirstRole())); |
---|
| 152 | buff.append(".").append(r.getSecondRoleName().toString()); |
---|
| 153 | return buff.toString(); |
---|
| 154 | |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | public static String toString(EntityExpression expr) { |
---|
| 158 | if(expr instanceof Entity) { |
---|
| 159 | return toString((Entity)expr); |
---|
| 160 | } |
---|
| 161 | if(expr instanceof Role) { |
---|
| 162 | return toString((Role)expr); |
---|
| 163 | } |
---|
| 164 | if(expr instanceof LinkedRole) { |
---|
| 165 | return toString((LinkedRole)expr); |
---|
| 166 | } |
---|
| 167 | //we don't handle instersections since they should already be |
---|
| 168 | //decomposed into expression parts, but just in case . . . |
---|
| 169 | return expr.toString(); |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | } |
---|