View Javadoc

1   /*
2    * Copyright [2005] [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  /**
18   * 
19   */
20  
21  package org.opensaml.saml2.core.validator;
22  
23  import org.opensaml.saml2.core.Conditions;
24  import org.opensaml.saml2.core.OneTimeUse;
25  import org.opensaml.saml2.core.ProxyRestriction;
26  import org.opensaml.xml.validation.ValidationException;
27  import org.opensaml.xml.validation.Validator;
28  
29  /**
30   * Checks the {@link org.opensaml.saml2.core.Conditions} for Spec compliance.
31   */
32  public class ConditionsSpecValidator implements Validator<Conditions> {
33  
34      /** Constructor */
35      public ConditionsSpecValidator() {
36  
37      }
38  
39      /** {@inheritDoc} */
40      public void validate(Conditions conditions) throws ValidationException {
41          validateOneTimeUseCondition(conditions);
42          validateProxyRestrictionCondition(conditions);
43      }
44  
45      /**
46       * Checks that there is at most one OneTimeUse condition.
47       * 
48       * @param conditions
49       * @throws ValidationException
50       */
51      protected void validateOneTimeUseCondition(Conditions conditions) throws ValidationException {
52          int oneTimeUseCount = 0;
53          for (int i = 0; i < conditions.getConditions().size(); i++) {
54              if (conditions.getConditions().get(i) instanceof OneTimeUse) {
55                  oneTimeUseCount++;
56              }
57          }
58          
59          if (oneTimeUseCount > 1) {
60              throw new ValidationException("At most one instance of OneTimeUse allowed");
61          }
62      }
63  
64      protected void validateProxyRestrictionCondition(Conditions conditions) throws ValidationException {
65          int proxyRestrictionCount = 0;
66          for (int i = 0; i < conditions.getConditions().size(); i++) {
67              if (conditions.getConditions().get(i) instanceof ProxyRestriction) {
68                  proxyRestrictionCount++;
69              }
70          }
71          
72          if (proxyRestrictionCount > 1) {
73              throw new ValidationException("At most one instance of ProxyRestriction allowed");
74          }
75      }
76  }