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.impl;
22  
23  import org.joda.time.DateTime;
24  import org.joda.time.chrono.ISOChronology;
25  import org.opensaml.common.SAMLVersion;
26  import org.opensaml.common.impl.AbstractSAMLObjectUnmarshaller;
27  import org.opensaml.saml1.core.ResponseAbstractType;
28  import org.opensaml.xml.XMLObject;
29  import org.opensaml.xml.io.UnmarshallingException;
30  import org.opensaml.xml.signature.Signature;
31  import org.opensaml.xml.util.DatatypeHelper;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  import org.w3c.dom.Attr;
35  import org.w3c.dom.Element;
36  
37  /**
38   * A thread-safe {@link org.opensaml.xml.io.Unmarshaller} for {@link org.opensaml.saml1.core.ResponseAbstractType}
39   * objects.
40   */
41  public abstract class ResponseAbstractTypeUnmarshaller extends AbstractSAMLObjectUnmarshaller {
42  
43      /** Logger. */
44      private final Logger log = LoggerFactory.getLogger(ResponseUnmarshaller.class);
45  
46      /** {@inheritDoc} */
47      public XMLObject unmarshall(Element domElement) throws UnmarshallingException {
48          // After regular unmarshalling, check the minor version and set ID-ness if not SAML 1.0
49          ResponseAbstractType response = (ResponseAbstractType) super.unmarshall(domElement);
50          if (response.getMinorVersion() != 0 && !DatatypeHelper.isEmpty(response.getID())) {
51              domElement.setIdAttributeNS(null, ResponseAbstractType.ID_ATTRIB_NAME, true);
52          }
53          return response;
54      }
55  
56      /** {@inheritDoc} */
57      protected void processChildElement(XMLObject parentSAMLObject, XMLObject childSAMLObject)
58              throws UnmarshallingException {
59          ResponseAbstractType response = (ResponseAbstractType) parentSAMLObject;
60  
61          if (childSAMLObject instanceof Signature) {
62              response.setSignature((Signature) childSAMLObject);
63          } else {
64              super.processChildElement(parentSAMLObject, childSAMLObject);
65          }
66      }
67  
68      /** {@inheritDoc} */
69      protected void processAttribute(XMLObject samlObject, Attr attribute) throws UnmarshallingException {
70          ResponseAbstractType response = (ResponseAbstractType) samlObject;
71  
72          if (attribute.getLocalName().equals(ResponseAbstractType.ID_ATTRIB_NAME)) {
73              response.setID(attribute.getValue());
74          } else if (attribute.getLocalName().equals(ResponseAbstractType.INRESPONSETO_ATTRIB_NAME)) {
75              response.setInResponseTo(attribute.getValue());
76          } else if (attribute.getLocalName().equals(ResponseAbstractType.ISSUEINSTANT_ATTRIB_NAME)
77                  && !DatatypeHelper.isEmpty(attribute.getValue())) {
78              response.setIssueInstant(new DateTime(attribute.getValue(), ISOChronology.getInstanceUTC()));
79          } else if (attribute.getLocalName().equals(ResponseAbstractType.MINORVERSION_ATTRIB_NAME)) {
80              int minor;
81              try {
82                  minor = Integer.parseInt(attribute.getValue());
83              } catch (NumberFormatException n) {
84                  log.error("Parsing minor version ", n);
85                  throw new UnmarshallingException(n);
86              }
87              if (minor == 0) {
88                  response.setVersion(SAMLVersion.VERSION_10);
89              } else if (minor == 1) {
90                  response.setVersion(SAMLVersion.VERSION_11);
91              }
92          } else if (attribute.getLocalName().equals(ResponseAbstractType.RECIPIENT_ATTRIB_NAME)) {
93              response.setRecipient(attribute.getValue());
94          } else {
95              super.processAttribute(samlObject, attribute);
96          }
97      }
98  
99  }