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.saml1.core.validator;
22  
23  import java.util.List;
24  
25  import org.opensaml.saml1.core.Assertion;
26  import org.opensaml.saml1.core.Statement;
27  import org.opensaml.xml.util.DatatypeHelper;
28  import org.opensaml.xml.validation.ValidationException;
29  import org.opensaml.xml.validation.Validator;
30  
31  /**
32   * Checks {@link org.opensaml.saml1.core.Assertion} for Schema compliance.
33   */
34  public class AssertionSchemaValidator implements Validator<Assertion> {
35  
36      /** {@inheritDoc} */
37      public void validate(Assertion assertion) throws ValidationException {
38           validateVersion(assertion);
39           validateId(assertion);
40           validateIssuer(assertion);
41           validateIssueInstant(assertion);
42           validateStatements(assertion);
43      }
44      
45      /**
46       * Test that the version is SAML1.1 or 1.0
47       * @param assertion what to test
48       * @throws ValidationException
49       */
50      protected void validateVersion(Assertion assertion) throws ValidationException {
51           if ((assertion.getMajorVersion() != 1) &&
52               (assertion.getMinorVersion() != 0 || assertion.getMinorVersion() != 1)) {
53               throw new ValidationException("Invalid Version");
54           }
55      }    
56      
57      /**
58       * Test that the ID is present
59       * @param assertion
60       * @throws ValidationException
61       */
62      protected void validateId(Assertion assertion) throws ValidationException {
63           if (DatatypeHelper.isEmpty(assertion.getID())) {
64               throw new ValidationException("ID not present");
65           }
66      }
67           
68      /**
69       * Test that the issuer is present
70       * @param assertion
71       * @throws ValidationException
72       */
73      protected void validateIssuer(Assertion assertion) throws ValidationException {
74          if (DatatypeHelper.isEmpty(assertion.getIssuer())) {
75               throw new ValidationException("Issuer not present");
76           }
77      }
78      
79      /**
80       * Test that the IssueInstant is present
81       * @param assertion
82       * @throws ValidationException
83       */
84      protected void validateIssueInstant(Assertion assertion) throws ValidationException {
85           if (assertion.getIssueInstant() == null) {
86               throw new ValidationException("IssueInstant not present");
87           }
88      }
89           
90      /**
91       * Test that the provided assertion has some statements in 
92       * @param assertion
93       * @throws ValidationException
94       */
95      protected void validateStatements(Assertion assertion) throws ValidationException {
96          List <Statement> list = assertion.getStatements();
97           if (list == null || list.size() == 0) {
98               throw new ValidationException("No Statements present");
99           }
100     }
101 }