[8780cbec] | 1 | /* |
---|
| 2 | * StaticCredential.java |
---|
| 3 | */ |
---|
| 4 | |
---|
| 5 | package edu.stanford.peer.rbtm.credential; |
---|
| 6 | |
---|
| 7 | import edu.stanford.peer.rbtm.util.*; |
---|
| 8 | import java.util.*; |
---|
| 9 | |
---|
| 10 | /** |
---|
| 11 | * StaticCredential |
---|
| 12 | */ |
---|
| 13 | public class StaticCredential extends Credential |
---|
| 14 | { |
---|
| 15 | private Role role; |
---|
| 16 | private EntityExpression subject; |
---|
| 17 | |
---|
| 18 | public StaticCredential(Role r, EntityExpression s) { |
---|
| 19 | role = r; |
---|
| 20 | subject = s; |
---|
| 21 | } |
---|
| 22 | public StaticCredential(String str) |
---|
| 23 | throws CredentialParsingException { |
---|
| 24 | StringTokenizer st = new StringTokenizer(str, "<-"); |
---|
| 25 | //Debug.debugInfo("StaticCredential: cred string " + str + |
---|
| 26 | // " contains " + st.countTokens() + " tokens"); |
---|
| 27 | String lhs = st.nextToken().trim(); |
---|
| 28 | String rhs = st.nextToken().trim(); |
---|
| 29 | //Debug.debugInfo("StaticCredential: lhs = " + lhs); |
---|
| 30 | //Debug.debugInfo("StaticCredential: rhs = " + rhs); |
---|
| 31 | role = (Role)getRole(lhs); |
---|
| 32 | subject = getEntityExpression(rhs); |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | public StaticCredential(Entity issuer, RoleName n, EntityExpression s) { |
---|
| 36 | role = new Role(issuer, n); |
---|
| 37 | subject = s; |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | public StaticCredential(String e, String rn, String es) |
---|
| 41 | throws CredentialParsingException |
---|
| 42 | { |
---|
| 43 | role = new Role(new SimpleEntity(e), new SimpleRoleName(rn)); |
---|
| 44 | subject = StaticCredential.getEntityExpression(es); |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | public Entity getIssuer() { return role.getBase(); } |
---|
| 48 | |
---|
| 49 | //public Role getRole() { return role; } |
---|
| 50 | |
---|
| 51 | public Role getDefinedRole() { return role; } |
---|
| 52 | |
---|
| 53 | public EntityExpression getSubject() { return subject; } |
---|
| 54 | |
---|
| 55 | public String toString() { |
---|
| 56 | return role.toString() + "<-" + subject.toString(); |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | public String toXML() { |
---|
| 60 | StringBuffer buff = new StringBuffer("<Credential "); |
---|
| 61 | buff.append(" definedRole=\"").append(role).append("\""); |
---|
| 62 | buff.append(" subject=\"").append(subject).append("\""); |
---|
| 63 | buff.append("/>\n"); |
---|
| 64 | return buff.toString(); |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | public int hashCode() { |
---|
| 68 | return role.hashCode() * 2 + subject.hashCode(); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | public boolean equals(Object o) |
---|
| 72 | { |
---|
| 73 | return (o instanceof StaticCredential) && |
---|
| 74 | role.equals(((StaticCredential)o).role) && |
---|
| 75 | subject.equals(((StaticCredential)o).subject); |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | public static EntityExpression getRole(String str) |
---|
| 79 | throws CredentialParsingException { |
---|
| 80 | EntityExpression expr = null; |
---|
| 81 | StringTokenizer st = new StringTokenizer(str.trim(), "."); |
---|
| 82 | int count = st.countTokens(); |
---|
| 83 | String error = null; |
---|
| 84 | |
---|
| 85 | if(count <= 0) { |
---|
| 86 | error = "Role expression contained one or less parts!"; |
---|
| 87 | } |
---|
| 88 | else if(count == 2) { |
---|
| 89 | expr = (EntityExpression) new Role(st.nextToken(), st.nextToken()); |
---|
| 90 | } |
---|
| 91 | else if(count == 3) { |
---|
| 92 | expr = (EntityExpression) |
---|
| 93 | new LinkedRole(st.nextToken(),st.nextToken(), st.nextToken()); |
---|
| 94 | } |
---|
| 95 | else { |
---|
| 96 | error = "Role " + str + " expression contained too many role names"; |
---|
| 97 | } |
---|
| 98 | if(expr == null) { |
---|
| 99 | throw new CredentialParsingException(error); |
---|
| 100 | } |
---|
| 101 | return expr; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | /** creates a new Intersection from a string */ |
---|
| 105 | public static Intersection getIntersection(String str) |
---|
| 106 | throws CredentialParsingException { |
---|
| 107 | StringTokenizer st = new StringTokenizer(str, "^"); |
---|
| 108 | int count = st.countTokens(); |
---|
| 109 | Intersection i = new Intersection(); |
---|
| 110 | |
---|
| 111 | for(int j = 0; j < count; j++) { |
---|
| 112 | i.and(getEntityExpression(st.nextToken())); |
---|
| 113 | } |
---|
| 114 | return i; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | /** creates a new SimpleEntity instance from a string */ |
---|
| 118 | public static Entity getEntity(String str) |
---|
| 119 | throws CredentialParsingException { |
---|
| 120 | return new SimpleEntity(str.trim()); |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | /** |
---|
| 124 | * converts a String into an EntityExpression, deals with Entity, Role, |
---|
| 125 | * LinkedRole, and Intersection. This method uses helper functions for |
---|
| 126 | * parsiing entities, roles, and intersections, so these methods may be |
---|
| 127 | * overridden in subclassing implementations. |
---|
| 128 | */ |
---|
| 129 | public static EntityExpression getEntityExpression(String str) |
---|
| 130 | throws CredentialParsingException { |
---|
| 131 | if(str.indexOf("^") > 0) |
---|
| 132 | return (EntityExpression)getIntersection(str); |
---|
| 133 | if(str.indexOf(".") > 0) |
---|
| 134 | return getRole(str); |
---|
| 135 | return (EntityExpression)getEntity(str); |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | } |
---|