⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 intersectionvalidityperiod.java

📁 一个完整的XACML工程,学习XACML技术的好例子!
💻 JAVA
字号:
/*
* Copyright (c) 2000-2005, University of Salford
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without 
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this 
* list of conditions and the following disclaimer.
* 
* Redistributions in binary form must reproduce the above copyright notice, 
* this list of conditions and the following disclaimer in the documentation 
* and/or other materials provided with the distribution. 
*
* Neither the name of the University of Salford nor the names of its 
* contributors may be used to endorse or promote products derived from this 
* software without specific prior written permission. 
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
* POSSIBILITY OF SUCH DAMAGE.
*/

package issrg.pba.rbac;

import java.util.Date;

/**
 * This class is an intersection of ValidityPeriods. It is used when the 
 * intersection cannot be precomputed, e.g. when a AdjustedValidityPeriod is 
 * intersected with another ValidityPeriod, or when RelativeValidityPeriods are
 * intersected - the results of both intersections will depend on the current 
 * time.
 */
public class IntersectionValidityPeriod extends DefaultValidityPeriodBehaviour {
  ValidityPeriod vp1;
  ValidityPeriod vp2;

  protected IntersectionValidityPeriod() {
  }

  /**
   * This constructor builds an intersection of two ValidityPeriods. They are
   * treated equally, and any of them can be null. If either of them is null,
   * then the result of getNotBefore and getNotAfter is provided by the 
   * non-null ValidityPeriod.
   *
   * @param v1 - one ValidityPeriod
   * @param v2 - another ValidityPeriod
   */
  public IntersectionValidityPeriod(ValidityPeriod v1, ValidityPeriod v2){
    vp1=v1;
    vp2=v2;

    // the following check allows to simplify the operations
    // so that it is sufficient to test whether v1 is an AdjustedValidityPeriod,
    // and then it is not necessary to test if v2 is, see the other methods.
    if (v2 instanceof AdjustedValidityPeriod){  // we will check for vp1 being adjustedvalidityperiod only
          // if they both are adjustedvalidityperiod, the result will be "now or never"
          // (the adjusted validity period would return now for both NA and NB dates.
          // the other adjustedvalidityperiod can only change it to "never")
      vp1=v2;
      vp2=v1;
    }
  }

  /**
   * Generally, this method returns the latest of the notBefore times of the
   * ValidityPeriods.
   *
   * <p>If either of the ValidityPeriods is a 
   * AdjustedValidityPeriod, the result is the notBefore time of the periods
   * adjusted against each other. If neither of the ValidityPeriods is an 
   * AdjustedValidityPeriod, then the latest notBefore time of the two is 
   * returned.
   *
   * @return the Date specifying the latest notBefore time of the two periods,
   *   or null, if it is not constrained ("valid from time immemorial")
   */
  public java.util.Date getNotBefore(){
    Date notBefore=null;
    if (vp1!=null){
      if (vp1 instanceof AdjustedValidityPeriod){
        return ((AdjustedValidityPeriod)vp1).adjust(vp2).getNotBefore();
      }

      notBefore = vp1.getNotBefore();
    }

    if (vp2!=null){
      Date d2=vp2.getNotBefore();
      if (notBefore==null || (d2!=null && d2.after(notBefore))){  // choose the latest
                                              // if d2 = null, then notBefore is the latest (or if it is also null, null will be reassigned)
        notBefore=d2;
      }
    }

    return notBefore;
  }

  /**
   * Generally this method returns the earliest notAfter time of the two 
   * ValidityPeriods. 
   *
   * <p>If either of the ValidityPeriods is a AdjustedValidityPeriod, then the
   * notAfter time of the result of adjusting one period against the other is
   * returned. If neither of them is an AdjustedValidityPeriod, then the 
   * earliest notAfter time of the two ValidityPeriods is returned.
   *
   * @return the Date specifying the earliest notAfter time of the two periods,
   *   or null, if it is not constrained ("valid forever")
   */
  public java.util.Date getNotAfter(){
    Date notAfter=null;
    if (vp1!=null){
      if (vp1 instanceof AdjustedValidityPeriod){
        return ((AdjustedValidityPeriod)vp1).adjust(vp2).getNotAfter();
      }

      notAfter=vp1.getNotAfter();
    }

    if (vp2!=null){
      Date d2=vp2.getNotAfter();

      if (notAfter==null || (d2!=null && d2.before(notAfter))){ // choose the earliest
        notAfter=d2;
      }
    }

    return notAfter;
  }

  /**
   * Generally, this method tests that both ValidityPeriods contain the given
   * ValidityPeriod (the condition that the intersection of sets contains the
   * given value). 
   *
   * <p>This method overrides the inherited behaviour of the contains method to
   * treat the AdjustedValidityPeriods differently. If neither of the Validity
   * Periods in the intersection is an AdjustedValidityPeriod, or the given
   * Credentials is not a ValidityPeriod, the inherited method determines 
   * whether the given Credentials is contained in this ValidityPeriod.
   *
   * <p>If either of the ValidityPeriods in the intersection is an 
   * AdjustedValidityPeriod, such
   * validity period is adjusted against the given ValidityPeriod. The result
   * of adjustment must still contain the given ValidityPeriod.
   *
   * @param c - the Credentials that must be contained in the intersection of 
   *   the ValidityPeriods (e.g. another ValidityPeriod or a 
   *   SetOfSubsetsCredentials containing multiple ValidityPeriods)
   *
   * @return true, if the intersection of the validity periods contains the 
   *   given Credentials
   */
  public boolean contains(issrg.pba.Credentials c){
    if (!(vp1 instanceof AdjustedValidityPeriod || vp2 instanceof AdjustedValidityPeriod) || !(c instanceof ValidityPeriod)){
      return super.contains(c);
    }

    ValidityPeriod vp = (ValidityPeriod)c;
    if (vp1 instanceof AdjustedValidityPeriod){
      vp=((AdjustedValidityPeriod)vp1).adjust(vp);
    }else{
      if (!vp1.contains(vp)){
        return false;
      }
    }

    if (vp2 instanceof AdjustedValidityPeriod){
      vp=((AdjustedValidityPeriod)vp2).adjust(vp);
    }else{
      if (!vp2.contains(vp)){
        return false;
      }
    }

    return vp.contains(c);
  }

  /**
   * This method tells the caller if this intersection of validity periods is 
   * null or not.
   */
  public boolean isNull(){
    Date d=getNotBefore();
    Date d2=getNotAfter();

    return d!=null && d2!=null && d.after(d2);
  }

  public Object clone(){
    return new IntersectionValidityPeriod(vp1==null?null:(ValidityPeriod)(vp1.clone()), vp2==null?null:(ValidityPeriod)(vp2.clone()));
  }

  public String toString(){
    return "intersection of "+vp1+" "+vp2;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -