KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > security > NodePermission


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/security/NodePermission.java,v 1.18 2004/08/08 22:15:20 masonjm Exp $
3  * $Revision: 1.18 $
4  * $Date: 2004/08/08 22:15:20 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.security;
25
26 import java.io.Serializable JavaDoc;
27
28 import org.apache.slide.common.ObjectValidationFailedException;
29 import org.apache.slide.content.NodeRevisionNumber;
30 import org.apache.slide.structure.ActionNode;
31 import org.apache.slide.structure.ObjectNode;
32 import org.apache.slide.structure.SubjectNode;
33 import org.apache.slide.util.Messages;
34
35 /**
36  * Permission object.
37  *
38  * @version $Revision: 1.18 $
39  */

40 public final class NodePermission implements Serializable JavaDoc, Cloneable JavaDoc {
41     
42     
43     // ----------------------------------------------------------- Constructors
44

45     
46     /**
47      * Constructor 1.
48      *
49      * @param objectUri Object Uri
50      * @param subjectUri Subject Uri
51      * @param actionUri Action Uri
52      */

53     public NodePermission(String JavaDoc objectUri, String JavaDoc subjectUri,
54                           String JavaDoc actionUri) {
55         if (objectUri != null) {
56             this.objectUri = objectUri;
57         }
58         if (subjectUri != null) {
59             setSubject(subjectUri);
60         }
61         if (actionUri != null) {
62             setAction(actionUri);
63         }
64         inheritable = true;
65         negative = false;
66     }
67     
68     
69     /**
70      * Constructor 2.
71      *
72      * @param objectUri Object Uri
73      * @param subjectUri Subject Uri
74      * @param actionUri Action Uri
75      * @param inheritable Inheritance flag
76      */

77     public NodePermission(String JavaDoc objectUri, String JavaDoc subjectUri,
78                           String JavaDoc actionUri, boolean inheritable) {
79         this(objectUri, subjectUri, actionUri);
80         this.inheritable = inheritable;
81     }
82     
83     
84     /**
85      * Constructor 3.
86      *
87      * @param objectUri Object Uri
88      * @param subjectUri Subject Uri
89      * @param actionUri Action Uri
90      * @param inheritable Inheritance flag
91      * @param negative Negative flag
92      */

93     public NodePermission(String JavaDoc objectUri, String JavaDoc subjectUri,
94                           String JavaDoc actionUri, boolean inheritable,
95                           boolean negative) {
96         this(objectUri, subjectUri, actionUri);
97         this.inheritable = inheritable;
98         this.negative = negative;
99     }
100     
101     
102     /**
103      * Constructor 4.
104      *
105      * @param object Object
106      * @param subject Subject
107      * @param action Action
108      */

109     public NodePermission(ObjectNode object, SubjectNode subject,
110                           ActionNode action) {
111         this(object.getUri(), subject.getUri(), action.getUri());
112     }
113     
114     
115     /**
116      * Constructor 5.
117      *
118      * @param object Object
119      * @param subject Subject
120      * @param action Action
121      * @param inheritable Inheritance flag
122      */

123     public NodePermission(ObjectNode object, SubjectNode subject,
124                           ActionNode action, boolean inheritable) {
125         this(object, subject, action);
126         this.inheritable = inheritable;
127     }
128     
129     
130     /**
131      * Constructor 6.
132      *
133      * @param object Object
134      * @param subject Subject
135      * @param action Action
136      * @param inheritable Inheritance flag
137      * @param negative Negative flag
138      */

139     public NodePermission(ObjectNode object, SubjectNode subject,
140                           ActionNode action, boolean inheritable,
141                           boolean negative) {
142         this(object, subject, action);
143         this.inheritable = inheritable;
144         this.negative = negative;
145     }
146     
147     
148     /**
149      * Constructor 7.
150      *
151      * @param object Object
152      * @param revisionNumber Revision number
153      * @param subject Subject
154      * @param action Action
155      * @param inheritable Inheritance flag
156      * @param negative Negative flag
157      */

158     public NodePermission(ObjectNode object, NodeRevisionNumber revisionNumber,
159                           SubjectNode subject, ActionNode action,
160                           boolean inheritable, boolean negative) {
161         this(object, subject, action);
162         this.inheritable = inheritable;
163         if (revisionNumber != null)
164             this.inheritable = false;
165         this.negative = negative;
166         this.revisionNumber = revisionNumber;
167     }
168     
169     
170     /**
171      * Constructor 8.
172      *
173      * @param objectUri Object Uri
174      * @param subjectUri Subject Uri
175      * @param actionUri Action Uri
176      * @param inheritable Inheritance flag
177      * @param negative Negative flag
178      */

179     public NodePermission(String JavaDoc objectUri, String JavaDoc revisionNumber,
180                           String JavaDoc subjectUri, String JavaDoc actionUri,
181                           boolean inheritable, boolean negative) {
182         this(objectUri, subjectUri, actionUri);
183         this.inheritable = inheritable;
184         if (revisionNumber != null) {
185             this.revisionNumber = new NodeRevisionNumber(revisionNumber);
186             this.inheritable = false;
187         }
188         this.negative = negative;
189     }
190     
191     
192     // ----------------------------------------------------- Instance Variables
193

194     
195     /**
196      * Object Uri.
197      */

198     protected String JavaDoc objectUri;
199     
200     
201     /**
202      * Revision number.
203      */

204     protected NodeRevisionNumber revisionNumber;
205     
206     
207     /**
208      * Subject Uri.
209      */

210     protected String JavaDoc subjectUri;
211     
212     
213     /**
214      * Action Uri.
215      */

216     protected String JavaDoc actionUri;
217     
218     
219     /**
220      * Inheritance flag. True if permission is inheritable.
221      */

222     protected boolean inheritable;
223     
224     /**
225      * URI of the resource this permission was inherited from
226      * NOT TO BE MADE PERSISTENT
227      */

228     protected transient String JavaDoc inheritedFrom;
229     
230     
231     /**
232      * Negative permission flag.
233      */

234     protected boolean negative;
235     
236     
237     /**
238      * Invert permission flag.
239      */

240     protected boolean invert;
241     
242     /**
243      * Protected flag
244      */

245     protected boolean protect = false;
246     
247     
248     // ------------------------------------------------------------- Properties
249

250     
251     /**
252      * Inheritance flag mutator.
253      *
254      * @param inheritable New flag value
255      */

256     public void setInheritable(boolean inheritable) {
257         this.inheritable = inheritable;
258     }
259     
260     
261     /**
262      * Inheritance flag accessor.
263      *
264      * @return boolean True if permission is inheritable
265      */

266     public boolean isInheritable() {
267         return inheritable;
268     }
269     
270     /**
271      * Method setInheritedFrom
272      *
273      * @param uri the URI this permission will be inherited from
274      *
275      */

276     public void setInheritedFrom(String JavaDoc uri) {
277         this.inheritedFrom = uri;
278     }
279     
280     /**
281      * Method getInheritedFrom
282      *
283      * @return the URI this permission was inherited from
284      *
285      */

286     public String JavaDoc getInheritedFrom() {
287         return inheritedFrom;
288     }
289     
290     
291     /**
292      * Negative flag mutator.
293      *
294      * @param negative New negative value
295      */

296     public void setNegative(boolean negative) {
297         this.negative = negative;
298     }
299     
300     
301     /**
302      * Negative flag accessor.
303      *
304      * @return boolean True if permission is negative
305      */

306     public boolean isNegative() {
307         return negative;
308     }
309     
310     
311     /**
312      * Method setInvert
313      *
314      * @param invert a boolean
315      */

316     public void setInvert(boolean invert) {
317         this.invert = invert;
318     }
319     
320     /**
321      * Method isInvert
322      *
323      * @return a boolean
324      */

325     public boolean isInvert() {
326         return invert;
327     }
328     
329     /**
330      * Method setProtected
331      *
332      * @param protect a boolean
333      *
334      */

335     public void setProtected(boolean protect) {
336         this.protect = protect;
337     }
338     
339     /**
340      * Method isProtected
341      *
342      * @return true, if this permission cannot be removed
343      *
344      */

345     public boolean isProtected() {
346         return protect;
347     }
348     
349     
350     /**
351      * Object Uri accessor.
352      *
353      * @return String Object Uri
354      */

355     public String JavaDoc getObjectUri() {
356         return objectUri;
357     }
358     
359     
360     /**
361      * Object Uri mutator.
362      *
363      * @param object New object
364      */

365     void setObject(ObjectNode object) {
366         if (object != null) {
367             this.objectUri = object.getUri();
368         }
369     }
370     
371     
372     /**
373      * Object Uri mutator
374      *
375      * @param objectUri New object Uri
376      */

377     public void setObject(String JavaDoc objectUri) {
378         this.objectUri = objectUri;
379     }
380     
381     
382     /**
383      * Subject Uri mutator.
384      *
385      * @return String Subject Uri
386      */

387     public String JavaDoc getSubjectUri() {
388         return subjectUri;
389     }
390     
391     
392     /**
393      * Subject Uri mutator.
394      *
395      * @param subject New subject
396      */

397     void setSubject(SubjectNode subject) {
398         if (subject != null) {
399             setSubject(subject.getUri());
400         }
401     }
402     
403     
404     /**
405      * Subject Uri mutator.
406      *
407      * @param subjectUri New subject Uri
408      */

409     void setSubject(String JavaDoc subjectUri) {
410         this.subjectUri = SubjectNode.getSubjectNode(subjectUri).getUri();
411     }
412     
413     SubjectNode getSubjectNode() {
414         return SubjectNode.getSubjectNode(subjectUri);
415     }
416     
417     /**
418      * Revision number accessor.
419      *
420      * @return Revision number to which this permission applies
421      */

422     public NodeRevisionNumber getRevisionNumber() {
423         return revisionNumber;
424     }
425     
426     
427     /**
428      * Revision number mutator.
429      *
430      * @param revisionNumber Revision number
431      */

432     void setRevisionNumber(NodeRevisionNumber revisionNumber) {
433         this.revisionNumber = revisionNumber;
434     }
435     
436     
437     /**
438      * Action Uri accessor.
439      *
440      * @return String Action Uri
441      */

442     public String JavaDoc getActionUri() {
443         return actionUri;
444     }
445     
446     ActionNode getActionNode() {
447         return ActionNode.getActionNode(actionUri);
448     }
449     
450     
451     /**
452      * Action Uri mutator.
453      *
454      * @param action New action
455      */

456     void setAction(ActionNode action) {
457         if (action != null) {
458             setAction(action.getUri());
459         }
460     }
461     
462     
463     /**
464      * Action Uri mutator.
465      *
466      * @param actionUri New action Uri
467      */

468     void setAction(String JavaDoc actionUri) {
469         this.actionUri = actionUri;
470     }
471     
472     
473     // --------------------------------------------------------- Object Methods
474

475     
476     /**
477      * Equals.
478      *
479      * @param obj Object to test
480      * @return boolean True if the two object are equal :
481      * <li>obj is of type SlidePermission and is not null</li>
482      * <li>All three Uris are equal</li>
483      */

484     public boolean equals(Object JavaDoc obj) {
485         boolean result = false;
486         if ((obj != null) && (obj instanceof NodePermission)) {
487             NodePermission permission = (NodePermission) obj;
488             result = (this.objectUri.equals(permission.getObjectUri()))
489                 && (this.subjectUri.equals(permission.getSubjectUri()))
490                 && (this.actionUri.equals(permission.getActionUri()))
491                 && (this.isNegative() == permission.isNegative());
492         }
493         return result;
494     }
495     
496     
497     /**
498      * Hash Code.
499      *
500      * @return int Hash code value
501      */

502     public int hashCode() {
503         return toString().hashCode();
504     }
505     
506     
507     /**
508      * String representation of the permission.
509      * <p/>
510      * Format : ObjectUri-SubjectUri-ActionUri-InheritanceFlag
511      *
512      * @return String String representation
513      */

514     public String JavaDoc toString() {
515         // return objectUri + "-" + subjectUri + "-" + actionUri + "-"
516
// + inheritable;
517
return "[object="+objectUri+", subject="+subjectUri+", action="+actionUri+", ->"+(negative?"DENY":"GRANT")+"]";
518     }
519     
520     
521     /**
522      * Clone.
523      *
524      * @return Object clone
525      */

526     public NodePermission cloneObject() {
527         NodePermission result = null;
528         try {
529             result = (NodePermission) super.clone();
530         } catch(CloneNotSupportedException JavaDoc e) {
531         }
532         return result;
533     }
534     
535     
536     /**
537      * Validate.
538      *
539      * @param expectedUri Uri
540      */

541     public void validate(String JavaDoc expectedUri) {
542         
543         if (objectUri == null)
544             throw new ObjectValidationFailedException
545                 (expectedUri, Messages.message
546                      (NodePermission.class.getName() + ".nullObjectUri"));
547         
548         if (!objectUri.equals(expectedUri)) {
549             // extra test to make "/files/x" == "/files/x/"
550
String JavaDoc tmpObjectUri=objectUri;
551             if (tmpObjectUri.endsWith("/"))
552                 tmpObjectUri=tmpObjectUri.substring(0,tmpObjectUri.length()-1);
553             String JavaDoc tmpExpectedUri=expectedUri;
554             if (tmpExpectedUri.endsWith("/"))
555                 tmpExpectedUri=tmpExpectedUri.substring(0,tmpExpectedUri.length()-1);
556             
557             if (!tmpObjectUri.equals(tmpExpectedUri))
558                 throw new ObjectValidationFailedException
559                     (expectedUri, Messages.message
560                          (NodePermission.class.getName() + ".incorrectObjectUri"));
561         }
562         
563         if (subjectUri == null)
564             throw new ObjectValidationFailedException
565                 (expectedUri, Messages.message
566                      (NodePermission.class.getName() + ".nullSubjectUri"));
567         
568         if (actionUri == null)
569             throw new ObjectValidationFailedException
570                 (expectedUri, Messages.message
571                      (NodePermission.class.getName() + ".nullActionUri"));
572         
573     }
574     
575     
576 }
577
Popular Tags