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.common.SAMLVersion;
24  import org.opensaml.saml2.core.RequestAbstractType;
25  import org.opensaml.xml.util.DatatypeHelper;
26  import org.opensaml.xml.validation.ValidationException;
27  import org.opensaml.xml.validation.Validator;
28  
29  /**
30   * Checks {@link org.opensaml.saml2.core.RequestAbstractType} for Schema compliance.
31   * 
32   * @param <RequestType> request type that will be validated
33   */
34  public abstract class RequestAbstractTypeSchemaValidator<RequestType extends RequestAbstractType> implements
35          Validator<RequestType> {
36  
37      /**
38       * Constructor.
39       */
40      public RequestAbstractTypeSchemaValidator() {
41      }
42  
43      /** {@inheritDoc} */
44      public void validate(RequestType request) throws ValidationException {
45          validateID(request);
46          validateVersion(request);
47          validateIssueInstant(request);
48  
49      }
50  
51      /**
52       * Validates the ID attribute.
53       * 
54       * @param request request to validate
55       * @throws ValidationException if invalid
56       */
57      protected void validateID(RequestAbstractType request) throws ValidationException {
58          if (DatatypeHelper.isEmpty(request.getID())) {
59              throw new ValidationException("ID attribute must not be empty");
60          }
61      }
62  
63      /**
64       * Validates the Version attribute.
65       * 
66       * @param request request to validate
67       * @throws ValidationException if invalid
68       */
69      protected void validateVersion(RequestAbstractType request) throws ValidationException {
70          if (request.getVersion() == null) {
71              throw new ValidationException("Version attribute must not be null");
72          }
73          if (request.getVersion().toString() != SAMLVersion.VERSION_20.toString()) {
74              throw new ValidationException("Wrong SAML Version");
75          }
76      }
77  
78      /**
79       * Validates the IsssueInstant attribute.
80       * 
81       * @param request request to validate
82       * @throws ValidationException if invalid
83       */
84      protected void validateIssueInstant(RequestAbstractType request) throws ValidationException {
85          if (request.getIssueInstant() == null) {
86              throw new ValidationException("IssueInstant attribute must not be null");
87          }
88      }
89  }