package com.nailabs.abac.credential; import java.util.Iterator; import java.util.StringTokenizer; import edu.stanford.peer.rbtm.credential.Entity; import edu.stanford.peer.rbtm.credential.EntityExpression; import edu.stanford.peer.rbtm.credential.StaticCredential; import edu.stanford.peer.rbtm.credential.Role; import edu.stanford.peer.rbtm.credential.LinkedRole; import edu.stanford.peer.rbtm.credential.Intersection; import edu.stanford.rt.credential.*; /** * An enitty expression factory which generates the appropriate entity * expression from various pieces of an RTML credential domain. */ public class RtmlExpression implements EntityExpression { public static RtmlEntity convert(Entity expr) throws Exception { StringTokenizer st = new StringTokenizer(((Entity)expr).getName(), "!"); String hashCode = st.nextToken(); if(st.hasMoreTokens()) { String shortName = st.nextToken(); return new RtmlEntity(shortName, hashCode); } else { return new RtmlEntity(hashCode); } } public static Role convert(Role r) throws Exception { return new Role(convert(r.getBase()), r.getName()); } public static LinkedRole convert(LinkedRole lr) throws Exception { return new LinkedRole(convert(lr.getFirstRole()), lr.getSecondRoleName()); } public static Intersection convert(Intersection i) throws Exception { Intersection intersection = new Intersection(); Iterator parts = intersection.getParts(); while(parts.hasNext()) { intersection.and(convert((EntityExpression)parts.next())); } return intersection; } public static EntityExpression convert(EntityExpression expr) throws Exception{ if(expr instanceof Entity) { return convert((Entity)expr); } if(expr instanceof Role) { return convert((Role)expr); } if(expr instanceof LinkedRole) { return convert((LinkedRole)expr); } if(expr instanceof Intersection) { return convert((Intersection)expr); } return null; } public static EntityExpression getEntityExpression(String text) throws Exception { return convert(StaticCredential.getEntityExpression(text)); } /** converts a principal into an entity */ public static RtmlEntity convert(Principal p, CredentialDomain domain) { return new RtmlEntity(domain.getPrincipal(p), p); } /** converts an rtml role into and rbtm role */ public static edu.stanford.peer.rbtm.credential.Role convert (edu.stanford.rt.credential.Role rtmlRole, CredentialDomain domain) { RtmlEntity entity = convert(rtmlRole.getPrincipal(), domain); //System.out.print(entity.getName() + "."); String roleName = rtmlRole.getName(); return new Role(entity, roleName); } /** converts an rtml linked role into an rbtm linked role */ public static edu.stanford.peer.rbtm.credential.LinkedRole convert (edu.stanford.rt.credential.LinkedRole rtml, CredentialDomain domain) { RtmlEntity entity = convert(domain.getIssuer(), domain); String primaryRole = rtml.getRoleTermAt(0).getRoleDeclaration().getName(); String secondaryRole = rtml.getRoleTermAt(1).getRoleDeclaration().getName(); return new LinkedRole(entity, primaryRole, secondaryRole); } /** converts a role intersection into an rbtm intersection */ public static edu.stanford.peer.rbtm.credential.Intersection convert (edu.stanford.rt.credential.RoleIntersection rtml, CredentialDomain domain) { System.out.println("RTML Intersection:"); RtmlEntity issuer = convert(domain.getIssuer(), domain); Iterator parts = rtml.getParts().iterator(); Intersection intersect = new Intersection(); int pCount = 0; while(parts.hasNext()) { RoleTerm term = (RoleTerm)parts.next(); edu.stanford.peer.rbtm.credential.Role role = new Role(issuer, term.getRoleDeclaration().getName()); System.out.println("Part number " + ++pCount); System.out.println(role); intersect.and(role); } return intersect; } /** * a polymorphic conversion method, which acts as an entity expression * factory. */ public static EntityExpression convert(PrincipalExpression expr, CredentialDomain domain) { if(expr instanceof edu.stanford.rt.credential.Principal) { return convert((edu.stanford.rt.credential.Principal)expr, domain); } if(expr instanceof edu.stanford.rt.credential.Role) { return convert((edu.stanford.rt.credential.Role)expr, domain); } if(expr instanceof edu.stanford.rt.credential.LinkedRole) { return convert((edu.stanford.rt.credential.LinkedRole)expr, domain); } if(expr instanceof edu.stanford.rt.credential.RoleIntersection) { return convert((edu.stanford.rt.credential.RoleIntersection)expr, domain); } System.out.println("$$$ Skipping over " + expr); return null; } public static String toString(Entity e) { if(e instanceof RtmlEntity) { return ((RtmlEntity)e).getHash(); } return e.toString(); } public static String toString(Role r) { StringBuffer buff = new StringBuffer(toString(r.getBase())); buff.append(".").append(r.getName().toString()); return buff.toString(); } public static String toString(LinkedRole r) { StringBuffer buff = new StringBuffer(toString(r.getFirstRole())); buff.append(".").append(r.getSecondRoleName().toString()); return buff.toString(); } public static String toString(EntityExpression expr) { if(expr instanceof Entity) { return toString((Entity)expr); } if(expr instanceof Role) { return toString((Role)expr); } if(expr instanceof LinkedRole) { return toString((LinkedRole)expr); } //we don't handle instersections since they should already be //decomposed into expression parts, but just in case . . . return expr.toString(); } }