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.RequestAbstractType;
28  import org.opensaml.saml1.core.RespondWith;
29  import org.opensaml.xml.XMLObject;
30  import org.opensaml.xml.io.UnmarshallingException;
31  import org.opensaml.xml.signature.Signature;
32  import org.opensaml.xml.util.DatatypeHelper;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  import org.w3c.dom.Attr;
36  import org.w3c.dom.Element;
37  
38  /**
39   * A thread safe Unmarshaller for {@link org.opensaml.saml1.core.RequestAbstractType} objects.
40   */
41  public abstract class RequestAbstractTypeUnmarshaller extends AbstractSAMLObjectUnmarshaller {
42  
43      /** Logger. */
44      private final Logger log = LoggerFactory.getLogger(RequestAbstractType.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          RequestAbstractType request = (RequestAbstractType) super.unmarshall(domElement);
50          if (request.getMinorVersion() != 0 && !DatatypeHelper.isEmpty(request.getID())) {
51              domElement.setIdAttributeNS(null, RequestAbstractType.ID_ATTRIB_NAME, true);
52          }
53          return request;
54      }
55  
56      /** {@inheritDoc} */
57      protected void processChildElement(XMLObject parentSAMLObject, XMLObject childSAMLObject)
58              throws UnmarshallingException {
59          RequestAbstractType request = (RequestAbstractType) parentSAMLObject;
60  
61          if (childSAMLObject instanceof Signature) {
62              request.setSignature((Signature) childSAMLObject);
63          } else if (childSAMLObject instanceof RespondWith) {
64              request.getRespondWiths().add((RespondWith) childSAMLObject);
65          } else {
66              super.processChildElement(parentSAMLObject, childSAMLObject);
67          }
68      }
69  
70      /** {@inheritDoc} */
71      protected void processAttribute(XMLObject samlElement, Attr attribute) throws UnmarshallingException {
72          RequestAbstractType request = (RequestAbstractType) samlElement;
73  
74          if (RequestAbstractType.ID_ATTRIB_NAME.equals(attribute.getLocalName())) {
75              request.setID(attribute.getValue());
76          } else if (RequestAbstractType.ISSUEINSTANT_ATTRIB_NAME.equals(attribute.getLocalName())
77                  && !DatatypeHelper.isEmpty(attribute.getValue())) {
78              DateTime cal = new DateTime(attribute.getValue(), ISOChronology.getInstanceUTC());
79              request.setIssueInstant(cal);
80          } else if (RequestAbstractType.MINORVERSION_ATTRIB_NAME.equals(attribute.getLocalName())) {
81              int minor;
82              try {
83                  minor = Integer.parseInt(attribute.getValue());
84              } catch (NumberFormatException n) {
85                  log.error("Unable to parse minor version string", n);
86                  throw new UnmarshallingException(n);
87              }
88              if (minor == 0) {
89                  request.setVersion(SAMLVersion.VERSION_10);
90              } else if (minor == 1) {
91                  request.setVersion(SAMLVersion.VERSION_11);
92              }
93          } else {
94              super.processAttribute(samlElement, attribute);
95          }
96      }
97  
98  }