AdjustedValidityPeriod | Line # 51 | 50 | 13 | 81% |
0.8101266
|
No Tests | |||
1 | /* | |
2 | * Copyright (c) 2000-2005, University of Salford | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions are met: | |
7 | * | |
8 | * Redistributions of source code must retain the above copyright notice, this | |
9 | * list of conditions and the following disclaimer. | |
10 | * | |
11 | * Redistributions in binary form must reproduce the above copyright notice, | |
12 | * this list of conditions and the following disclaimer in the documentation | |
13 | * and/or other materials provided with the distribution. | |
14 | * | |
15 | * Neither the name of the University of Salford nor the names of its | |
16 | * contributors may be used to endorse or promote products derived from this | |
17 | * software without specific prior written permission. | |
18 | * | |
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
29 | * POSSIBILITY OF SUCH DAMAGE. | |
30 | */ | |
31 | ||
32 | package issrg.pba.rbac; | |
33 | ||
34 | /** | |
35 | * This class represents a validity period with run-time rules. It | |
36 | * builds the margins updated according to the rules and the given | |
37 | * ValidityPeriod. | |
38 | * | |
39 | * <p>If the given validity period has NotAfter time between MinSpan and | |
40 | * MaxSpan, and | |
41 | * NotBefore time later than Age, then the unchanged validity period is | |
42 | * returned. Otherwise | |
43 | * an Invalid Period is returned (null length). | |
44 | * | |
45 | * <p>If no validity period is provided, it returns "now" for both notBefore | |
46 | * and notAfter times. | |
47 | * | |
48 | * @author A.Otenko | |
49 | */ | |
50 | ||
51 | public class AdjustedValidityPeriod extends RelativeValidityPeriod { | |
52 | ||
53 | protected RelativeDate maxSpan; | |
54 | protected RelativeDate minSpan; | |
55 | protected RelativeDate age; | |
56 | ||
57 | private java.util.GregorianCalendar gc=new java.util.GregorianCalendar(); | |
58 | ||
59 | // this variable contains a reference to an invalid validity | |
60 | // period: intersecting withit will always result in a null period | |
61 | private final static RelativeValidityPeriod INVALID_PERIOD=null;//new RelativeValidityPeriod(0,0,0,0,0,0,-1,0,0,0,0,0); | |
62 | ||
63 | 5 | protected AdjustedValidityPeriod() { |
64 | 5 | super(0,0,0,0,0,0, |
65 | 0,0,0,0,0,0); | |
66 | } | |
67 | ||
68 | /** | |
69 | * This constructor builds a AdjustedValidityPeriod given the Age, Min life | |
70 | * span, and Max life span. Note that if Min life span is longer than Max | |
71 | * life span, any ValidityPeriod will be invalid. Any or all of these | |
72 | * parameters can be null, | |
73 | * meaning that the ValidityPeriods will not be constrained in that respect. | |
74 | * | |
75 | * @param age - the maximum age of the ValidityPeriod; a RelativeDate showing | |
76 | * the earliest point in the past when the ValidityPeriod can start; if it | |
77 | * starts earlier, it is fully invalid | |
78 | * @param minSpan - the minimum life span required of the ValidityPeriod; a | |
79 | * RelativeDate showing the earliest point in the future when the | |
80 | * ValidityPeriod can end; if it ends earlier, it is fully invalid | |
81 | * @param maxSpan - the maximum life span required of the ValidityPeriod; a | |
82 | * RelativeDate showing the latest point in the future when the | |
83 | * ValidityPeriod can end; if it ends later, it is fully invalid | |
84 | */ | |
85 | 5 | public AdjustedValidityPeriod(RelativeDate age, RelativeDate minSpan, RelativeDate maxSpan){ |
86 | 5 | super(0,0,0,0,0,0, |
87 | 0,0,0,0,0,0); | |
88 | ||
89 | 5 | this.maxSpan=maxSpan; |
90 | 5 | this.minSpan=minSpan; |
91 | 5 | this.age=age; |
92 | } | |
93 | ||
94 | /** | |
95 | * This method returns the given ValidityPeriod adjusted according to the | |
96 | * constraints provided at construction time. See description of the | |
97 | * logic of the constraints in the constructor. | |
98 | * | |
99 | * @param vp - the ValidityPeriod to be constrained | |
100 | */ | |
101 | 244 | public ValidityPeriod adjust(ValidityPeriod vp){ |
102 | 0 | if (vp==null) return this; // if there is nothing on input, return this, which will work as "now" |
103 | ||
104 | 244 | synchronized(gc){ |
105 | 244 | if (age!=null){ // vp is invalid, if it contains age - been valid for too long |
106 | 2 | if (vp.getNotBefore()==null){ |
107 | 0 | return INVALID_PERIOD; |
108 | } | |
109 | ||
110 | 2 | gc.setTime(getNotBefore()); // this will get now |
111 | ||
112 | 2 | gc.add(gc.YEAR, -age.years); |
113 | 2 | gc.add(gc.MONTH, -age.months); |
114 | 2 | gc.add(gc.DATE, -age.days); |
115 | 2 | gc.add(gc.HOUR, -age.hours); |
116 | 2 | gc.add(gc.MINUTE, -age.minutes); |
117 | 2 | gc.add(gc.SECOND, -age.seconds); |
118 | // now gc is set to the maximum acceptable age. the older must be discarded | |
119 | ||
120 | 2 | if (gc.getTime().after(vp.getNotBefore())){ // the margin of age is after the notBefore of the given VP |
121 | 1 | return INVALID_PERIOD; |
122 | } | |
123 | } | |
124 | ||
125 | 243 | if (minSpan!=null){ // vp is invalid, if it does not contain minSpan - valid for too little time |
126 | 120 | if (vp.getNotAfter()!=null){ // if vp.getNotAfter()==null, then vp is infinitely valid => it contains minSpan |
127 | ||
128 | 120 | gc.setTime(getNotAfter()); // this will get now |
129 | ||
130 | 120 | gc.add(gc.YEAR, minSpan.years); |
131 | 120 | gc.add(gc.MONTH, minSpan.months); |
132 | 120 | gc.add(gc.DATE, minSpan.days); |
133 | 120 | gc.add(gc.HOUR, minSpan.hours); |
134 | 120 | gc.add(gc.MINUTE, minSpan.minutes); |
135 | 120 | gc.add(gc.SECOND, minSpan.seconds); |
136 | // now gc is set to the minimum acceptable lifespan. the shorter must be discarded | |
137 | ||
138 | 120 | if (gc.getTime().after(vp.getNotAfter())){ // the margin of lifespan is after the notAfter of the given VP |
139 | 0 | return INVALID_PERIOD; |
140 | } | |
141 | } | |
142 | } | |
143 | ||
144 | 243 | if (maxSpan!=null){ // vp is invalid, if it contains maxSpan - valid for too long |
145 | 122 | if (vp.getNotAfter()==null){ |
146 | 0 | return INVALID_PERIOD; |
147 | } | |
148 | ||
149 | 122 | gc.setTime(getNotAfter()); // this will get now |
150 | ||
151 | 122 | gc.add(gc.YEAR, maxSpan.years); |
152 | 122 | gc.add(gc.MONTH, maxSpan.months); |
153 | 122 | gc.add(gc.DATE, maxSpan.days); |
154 | 122 | gc.add(gc.HOUR, maxSpan.hours); |
155 | 122 | gc.add(gc.MINUTE, maxSpan.minutes); |
156 | 122 | gc.add(gc.SECOND, maxSpan.seconds); |
157 | // now gc is set to the maximum acceptable lifespan. the longer must be discarded | |
158 | ||
159 | 122 | if (gc.getTime().before(vp.getNotAfter())){ // the margin of lifespan is before the notAfter of the given VP |
160 | 121 | return INVALID_PERIOD; |
161 | } | |
162 | } | |
163 | ||
164 | 122 | return vp; |
165 | } | |
166 | } | |
167 | ||
168 | /** | |
169 | * This method redefines how intersection rule works. Effectively, it applies | |
170 | * the adjust method to compute intersections, if the given Credentials is a | |
171 | * ValidityPeriod. | |
172 | * | |
173 | * @param c - a Credentials to intersect with; makes sense only if it is a | |
174 | * ValidityPeriod, but supports intersections with other Credentials | |
175 | */ | |
176 | 244 | public issrg.pba.Credentials intersection(issrg.pba.Credentials c){ |
177 | 244 | if (!(c instanceof ValidityPeriod)){ |
178 | 0 | return super.intersection(c); |
179 | } | |
180 | ||
181 | 244 | if (c instanceof AdjustedValidityPeriod){ |
182 | 0 | return new AdjustedPeriodCollection(this, (ValidityPeriod)c); |
183 | } | |
184 | ||
185 | 244 | return adjust((ValidityPeriod)c); |
186 | } | |
187 | ||
188 | 0 | public String toString(){ |
189 | 0 | return "{rule age="+age+", max="+maxSpan+", min="+minSpan+"}"; |
190 | } | |
191 | } |
|