1   
2   package jsdsi;
3   
4   import java.util.ArrayList;
5   import java.util.Iterator;
6   import java.util.List;
7   
8   import jsdsi.sexp.SexpList;
9   import jsdsi.sexp.SexpParseException;
10  import jsdsi.sexp.SexpUtil;
11  
12  /***
13   * An ACL entry that specifies a permission (the Tag) and a set of
14   * principals (the Subject) that may access the object protected by this
15   * entry's ACL.
16   * 
17   * @see Acl
18   * 
19   * @author Sameer Ajmani
20   * @author Sean Radford
21   * @version $Revision: 1.4.2.1 $ $Date: 2005/11/08 03:12:52 $
22   */
23  public class AclEntry extends Obj {
24      
25      private static final long serialVersionUID = -3020366729004164789L;
26      
27      /***
28       * The subject of this <code>AclEntry</code>.
29       */
30      private transient final Subject subject;
31      
32      /***
33       * The permission of this ACL entry.
34       */
35      private transient final Auth auth;
36      
37      /***
38       * The validity of this <code>AclEntry</code>.
39       */
40      private transient final Validity validity;
41      
42      /***
43       * The comment of this <code>AclEntry</code>.
44       */
45      private transient final String comment;
46      
47      /***
48       * Creates a new <code>AclEntry</code> from a given subject, tag,
49       * delegation bit, validity, and comment.
50       * 
51       * @param  s subject of this <code>AclEntry</code>.
52       * @param  t tag of this <code>AclEntry</code>.
53       * @param  p delegation bit of this <code>AclEntry</code>.
54       * @param  v validity of this <code>AclEntry</code>.
55       * @param  c comment of this <code>AclEntry</code>.
56       */
57      public AclEntry(Subject s, Tag t, boolean p, Validity v, String c) {
58          assert(s != null) : "null subject";
59          assert(t != null) : "null tag";
60          subject = s;
61          auth = new Auth(t, p);
62          validity = v; // may be null
63          comment = c; // may be null
64      }
65      
66      /***
67       * Returns the subject of this <code>AclEntry</code>.
68       * 
69       * @return the subject of this <code>AclEntry</code>.
70       */
71      public Subject getSubject() {
72          return subject;
73      }
74      
75      /***
76       * Returns the tag of this <code>AclEntry</code>'s auth.
77       * 
78       * @return the tag of this <code>AclEntry</code>'s auth.
79       */
80      public Tag getTag() {
81          return auth.getTag();
82      }
83      
84      /***
85       * Returns the delegation bit of this <code>AclEntry</code>'s auth.
86       * 
87       * @return the delegation bit of this <code>AclEntry</code>' auth.
88       */
89      public boolean getPropagate() {
90          return auth.getPropagate();
91      }
92      
93      /***
94       * Returns the validity of this <code>AclEntry</code>
95       * (may be <code>null</code>).
96       * 
97       * @return the validity of this <code>AclEntry</code>.
98       */
99      public Validity getValidity() {
100         return validity;
101     }
102     
103     /***
104      * Returns the comment of this <code>AclEntry</code> 
105      * (may be <code>null</code>).
106      * 
107      * @return the comment of this <code>AclEntry</code>.
108      */
109     public String getComment() {
110         return comment;
111     }
112     
113     /***
114      * @see java.lang.Object#equals(Object)
115      */
116     public boolean equals(Object o) {
117         if (o instanceof AclEntry) {
118             AclEntry e = (AclEntry) o;
119             return subject.equals(e.subject)
120             && auth.equals(e.auth)
121             && Util.equals(validity, e.validity)
122             && Util.equals(comment, e.comment);
123         }
124         return false;
125     }
126     
127     /***
128      * @see java.lang.Object#hashCode()
129      */
130     public int hashCode() {
131         return subject.hashCode()
132         ^ auth.hashCode()
133         ^ Util.hashCode(validity)
134         ^ Util.hashCode(comment);
135     }
136     
137     /***
138      * Returns an <code>SexpList</code> that represents this
139      * <code>AclEntry</code>.
140      */
141     public SexpList toSexp() {
142         List l = new ArrayList(5);
143         l.add(getSubject().toSexp());
144         if (getPropagate()) {
145             l.add(SexpUtil.toSexpList("propagate"));
146         }
147         l.add(getTag().toSexp());
148         if (getValidity() != null) {
149             l.add(getValidity().toSexp());
150         }
151         if (getComment() != null) {
152             l.add(SexpUtil.toSexpComment(getComment()));
153         }
154         return SexpUtil.toSexp("entry", l);
155     }
156     
157     /***
158      * Parses an <code>AclEntry</code> from a given <code>SexpList</code>.
159      * 
160      * @param l the <code>SexpList</code> to parse.
161      * @return the <code>AclEntry</code> contained in <code>l</code>. 
162      * @throws SexpParseException
163      */
164     static AclEntry parseAclEntry(SexpList l) throws SexpParseException {
165         Iterator ebody = SexpUtil.getBody(l);
166         // FIXME: same as Cert parsing!
167         // <sub-obj>
168         Subject subject = Subject.Default.parseSubject(
169             SexpUtil.getNextList(ebody,
170                                  "acl entry subject"));
171         // <deleg>? <tag>
172         boolean propagate = false;
173         SexpList propOrTag =
174             SexpUtil.getNextList(ebody, "cert propagate or tag");
175         String type = propOrTag.getType();
176         if (type.equals("propagate")) {
177             propagate = true;
178             SexpUtil.check(propOrTag.size() == 1, "extra fields in propagate");
179             propOrTag = SexpUtil.getNextList(ebody, "tag", "cert tag");
180         }
181         Tag tag = Tag.parseTag(propOrTag);
182         // <valid>? <comment>?
183         Validity validity = null;
184         String comment = null;
185         if (ebody.hasNext()) {
186             SexpList validOrComment =
187                 SexpUtil.getNextList(ebody, "cert valid or comment");
188             type = validOrComment.getType();
189             if (type.equals("valid")) {
190                 validity = Validity.parseValidity(validOrComment);
191                 if (ebody.hasNext()) {
192                     validOrComment =
193                         SexpUtil.getNextList(ebody, "comment", "cert comment");
194                     type = "comment"; // FIXME: ugly!
195                 }
196             }
197             if (type.equals("comment")) {
198                 Iterator combody = SexpUtil.getBody(validOrComment);
199                 comment = SexpUtil.getNextString(combody, "comment body");
200                 SexpUtil.checkDone(combody, "comment");
201             }
202         }
203         return new AclEntry(subject, tag, propagate, validity, comment);
204     }
205 }
This page was automatically generated by Maven