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  package org.opensaml.saml1.core.impl;
18  
19  import org.joda.time.DateTime;
20  import org.joda.time.chrono.ISOChronology;
21  import org.opensaml.common.SAMLVersion;
22  import org.opensaml.common.impl.AbstractSAMLObjectUnmarshaller;
23  import org.opensaml.saml1.core.Advice;
24  import org.opensaml.saml1.core.Assertion;
25  import org.opensaml.saml1.core.Conditions;
26  import org.opensaml.saml1.core.Statement;
27  import org.opensaml.xml.XMLObject;
28  import org.opensaml.xml.io.UnmarshallingException;
29  import org.opensaml.xml.signature.Signature;
30  import org.opensaml.xml.util.DatatypeHelper;
31  import org.w3c.dom.Attr;
32  import org.w3c.dom.Element;
33  
34  /**
35   * A thread-safe Unmarshaller for {@link org.opensaml.saml1.core.Assertion} objects.
36   */
37  public class AssertionUnmarshaller extends AbstractSAMLObjectUnmarshaller {
38  
39      /** {@inheritDoc} */
40      public XMLObject unmarshall(Element domElement) throws UnmarshallingException {
41          // After regular unmarshalling, check the minor version and set ID-ness if not SAML 1.0
42          Assertion assertion = (Assertion) super.unmarshall(domElement);
43          if (assertion.getMinorVersion() != 0 && !DatatypeHelper.isEmpty(assertion.getID())) {
44              domElement.setIdAttributeNS(null, Assertion.ID_ATTRIB_NAME, true);
45          }
46          return assertion;
47      }
48  
49      /** {@inheritDoc} */
50      protected void processChildElement(XMLObject parentSAMLObject, XMLObject childSAMLObject)
51              throws UnmarshallingException {
52  
53          Assertion assertion = (Assertion) parentSAMLObject;
54  
55          if (childSAMLObject instanceof Signature) {
56              assertion.setSignature((Signature) childSAMLObject);
57          } else if (childSAMLObject instanceof Conditions) {
58              assertion.setConditions((Conditions) childSAMLObject);
59          } else if (childSAMLObject instanceof Advice) {
60              assertion.setAdvice((Advice) childSAMLObject);
61          } else if (childSAMLObject instanceof Statement) {
62              assertion.getStatements().add((Statement) childSAMLObject);
63          } else {
64              super.processChildElement(parentSAMLObject, childSAMLObject);
65          }
66      }
67  
68      /** {@inheritDoc} */
69      protected void processAttribute(XMLObject samlObject, Attr attribute) throws UnmarshallingException {
70  
71          Assertion assertion = (Assertion) samlObject;
72  
73          if (Assertion.ID_ATTRIB_NAME.equals(attribute.getLocalName())) {
74              assertion.setID(attribute.getValue());
75          } else if (Assertion.ISSUER_ATTRIB_NAME.equals(attribute.getLocalName())) {
76              assertion.setIssuer(attribute.getValue());
77          } else if (Assertion.ISSUEINSTANT_ATTRIB_NAME.equals(attribute.getLocalName())
78                  && !DatatypeHelper.isEmpty(attribute.getValue())) {
79              assertion.setIssueInstant(new DateTime(attribute.getValue(), ISOChronology.getInstanceUTC()));
80          } else if (Assertion.MINORVERSION_ATTRIB_NAME.equals(attribute.getLocalName())) {
81              if (attribute.getValue().equals("0")) {
82                  assertion.setVersion(SAMLVersion.VERSION_10);
83              } else {
84                  assertion.setVersion(SAMLVersion.VERSION_11);
85              }
86          } else {
87              super.processAttribute(samlObject, attribute);
88          }
89      }
90  
91  }