View Javadoc

1   /*
2    * Copyright [2006] [University Corporation for Advanced Internet Development, Inc.]
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.opensaml.saml2.common;
18  
19  import java.util.List;
20  
21  import org.joda.time.DateTime;
22  import org.opensaml.xml.XMLObject;
23  
24  public class SAML2Helper {
25  
26      /**
27       * Checks to see if the given XMLObject is still valid. An XMLObject is valid if, and only if, every descendant
28       * {@link TimeBoundSAMLObject} is valid.
29       * 
30       * @param xmlObject the XML object tree to check
31       * 
32       * @return true of the tree is valid, false if not
33       */
34      public static boolean isValid(XMLObject xmlObject) {
35          if (xmlObject instanceof TimeBoundSAMLObject) {
36              TimeBoundSAMLObject timeBoundObject = (TimeBoundSAMLObject) xmlObject;
37              if (!timeBoundObject.isValid()) {
38                  return false;
39              }
40          }
41  
42          List<XMLObject> children = xmlObject.getOrderedChildren();
43          if (children != null && !children.isEmpty()) {
44              for (XMLObject child : children) {
45                  if (!isValid(child)) {
46                      return false;
47                  }
48              }
49          }
50  
51          return true;
52      }
53  
54      /**
55       * Gets the earliest expiration instant for a XMLObject. This method traverses the tree of SAMLObject rooted at the
56       * given object and calculates the earliest expiration as the earliest of the following two items:
57       * <ul>
58       * <li>the earliest validUntil time on a {@link TimeBoundSAMLObject}</li>
59       * <li>the shortest duration on a {@link CacheableSAMLObject} added to the current time</li>
60       * </ul>
61       * 
62       * @param xmlObject the XML object tree to get the earliest expiration time from
63       * 
64       * @return the earliest expiration time
65       */
66      public static DateTime getEarliestExpiration(XMLObject xmlObject) {
67          DateTime now = new DateTime();
68          return getEarliestExpiration(xmlObject, null, now);
69      }
70  
71      /**
72       * Gets the earliest expiration instant within a metadata tree.
73       * 
74       * @param xmlObject the metadata
75       * @param earliestExpiration the earliest expiration instant
76       * @param now when this method was called
77       * 
78       * @return the earliest expiration instant within a metadata tree
79       */
80      public static DateTime getEarliestExpiration(XMLObject xmlObject, DateTime earliestExpiration, DateTime now) {
81  
82          // expiration time for a specific element
83          DateTime elementExpirationTime;
84  
85          // Test duration based times
86          if (xmlObject instanceof CacheableSAMLObject) {
87              CacheableSAMLObject cacheInfo = (CacheableSAMLObject) xmlObject;
88  
89              if (cacheInfo.getCacheDuration() != null && cacheInfo.getCacheDuration().longValue() > 0) {
90                  elementExpirationTime = now.plus(cacheInfo.getCacheDuration().longValue());
91                  if (earliestExpiration == null) {
92                      earliestExpiration = elementExpirationTime;
93                  } else {
94                      if (elementExpirationTime != null && elementExpirationTime.isBefore(earliestExpiration)) {
95                          earliestExpiration = elementExpirationTime;
96                      }
97                  }
98              }
99          }
100 
101         // Test instant based times
102         if (xmlObject instanceof TimeBoundSAMLObject) {
103             TimeBoundSAMLObject timeBoundObject = (TimeBoundSAMLObject) xmlObject;
104             elementExpirationTime = timeBoundObject.getValidUntil();
105             if (earliestExpiration == null) {
106                 earliestExpiration = elementExpirationTime;
107             } else {
108                 if (elementExpirationTime != null && elementExpirationTime.isBefore(earliestExpiration)) {
109                     earliestExpiration = elementExpirationTime;
110                 }
111             }
112         }
113 
114         // Inspect children
115         List<XMLObject> children = xmlObject.getOrderedChildren();
116         if (children != null) {
117             for (XMLObject child : xmlObject.getOrderedChildren()) {
118                 if (child != null) {
119                     earliestExpiration = getEarliestExpiration(child, earliestExpiration, now);
120                 }
121             }
122         }
123 
124         return earliestExpiration;
125     }
126 }