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.Assertion;
24  import org.opensaml.xml.util.DatatypeHelper;
25  import org.opensaml.xml.validation.ValidationException;
26  import org.opensaml.xml.validation.Validator;
27  
28  /**
29   * Checks {@link org.opensaml.saml2.core.Assertion} for Schema compliance.
30   */
31  public class AssertionSchemaValidator implements Validator<Assertion> {
32  
33      /** Constructor */
34      public AssertionSchemaValidator() {
35  
36      }
37  
38      /** {@inheritDoc} */
39      public void validate(Assertion assertion) throws ValidationException {
40          validateIssuer(assertion);
41          validateVersion(assertion);
42          validateID(assertion);
43          validateIssueInstant(assertion);
44      }
45  
46      /**
47       * Checks that Issuer element is present.
48       * 
49       * @param assertion
50       * @throws ValidationException
51       */
52      protected void validateIssuer(Assertion assertion) throws ValidationException {
53          if (assertion.getIssuer() == null) {
54              throw new ValidationException("Issuer is required element");
55          }
56      }
57  
58      /**
59       * Checks that the Version attribute is present.
60       * 
61       * @param assertion
62       * @throws ValidationException
63       */
64      protected void validateVersion(Assertion assertion) throws ValidationException {
65          if (assertion.getVersion() == null) {
66              throw new ValidationException("Version is required attribute");
67          }
68      }
69  
70      /**
71       * Checks that the ID attribute is present.
72       * 
73       * @param assertion
74       * @throws ValidationException
75       */
76      protected void validateID(Assertion assertion) throws ValidationException {
77          if (DatatypeHelper.isEmpty(assertion.getID())) {
78              throw new ValidationException("ID is required attribute");
79          }
80      }
81  
82      /**
83       * Checks that the IssueInstant attribute is present.
84       * 
85       * @param assertion
86       * @throws ValidationException
87       */
88      protected void validateIssueInstant(Assertion assertion) throws ValidationException {
89          if (assertion.getIssueInstant() == null) {
90              throw new ValidationException("IssueInstant is required attribute");
91          }
92      }
93  }