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.saml2.metadata.impl;
18  
19  import javax.xml.namespace.QName;
20  
21  import org.joda.time.DateTime;
22  import org.joda.time.chrono.ISOChronology;
23  import org.opensaml.common.impl.AbstractSAMLObjectUnmarshaller;
24  import org.opensaml.saml2.common.CacheableSAMLObject;
25  import org.opensaml.saml2.common.Extensions;
26  import org.opensaml.saml2.common.TimeBoundSAMLObject;
27  import org.opensaml.saml2.metadata.AdditionalMetadataLocation;
28  import org.opensaml.saml2.metadata.AffiliationDescriptor;
29  import org.opensaml.saml2.metadata.ContactPerson;
30  import org.opensaml.saml2.metadata.EntityDescriptor;
31  import org.opensaml.saml2.metadata.Organization;
32  import org.opensaml.saml2.metadata.RoleDescriptor;
33  import org.opensaml.xml.XMLObject;
34  import org.opensaml.xml.io.UnmarshallingException;
35  import org.opensaml.xml.signature.Signature;
36  import org.opensaml.xml.util.DatatypeHelper;
37  import org.opensaml.xml.util.XMLHelper;
38  import org.w3c.dom.Attr;
39  
40  /**
41   * A thread safe Unmarshaller for {@link org.opensaml.saml2.metadata.EntityDescriptor}s.
42   */
43  public class EntityDescriptorUnmarshaller extends AbstractSAMLObjectUnmarshaller {
44  
45      /** {@inheritDoc} */
46      protected void processChildElement(XMLObject parentSAMLObject, XMLObject childSAMLObject)
47              throws UnmarshallingException {
48          EntityDescriptor entityDescriptor = (EntityDescriptor) parentSAMLObject;
49  
50          if (childSAMLObject instanceof Extensions) {
51              entityDescriptor.setExtensions((Extensions) childSAMLObject);
52          } else if (childSAMLObject instanceof Signature) {
53              entityDescriptor.setSignature((Signature) childSAMLObject);
54          } else if (childSAMLObject instanceof RoleDescriptor) {
55              entityDescriptor.getRoleDescriptors().add((RoleDescriptor) childSAMLObject);
56          } else if (childSAMLObject instanceof AffiliationDescriptor) {
57              entityDescriptor.setAffiliationDescriptor((AffiliationDescriptor) childSAMLObject);
58          } else if (childSAMLObject instanceof Organization) {
59              entityDescriptor.setOrganization((Organization) childSAMLObject);
60          } else if (childSAMLObject instanceof ContactPerson) {
61              entityDescriptor.getContactPersons().add((ContactPerson) childSAMLObject);
62          } else if (childSAMLObject instanceof AdditionalMetadataLocation) {
63              entityDescriptor.getAdditionalMetadataLocations().add((AdditionalMetadataLocation) childSAMLObject);
64          } else {
65              super.processChildElement(parentSAMLObject, childSAMLObject);
66          }
67      }
68  
69      /** {@inheritDoc} */
70      protected void processAttribute(XMLObject samlObject, Attr attribute) throws UnmarshallingException {
71          EntityDescriptor entityDescriptor = (EntityDescriptor) samlObject;
72  
73          if (attribute.getLocalName().equals(EntityDescriptor.ENTITY_ID_ATTRIB_NAME)) {
74              entityDescriptor.setEntityID(attribute.getValue());
75          } else if (attribute.getLocalName().equals(EntityDescriptor.ID_ATTRIB_NAME)) {
76              entityDescriptor.setID(attribute.getValue());
77              attribute.getOwnerElement().setIdAttributeNode(attribute, true);
78          } else if (attribute.getLocalName().equals(TimeBoundSAMLObject.VALID_UNTIL_ATTRIB_NAME)
79                  && !DatatypeHelper.isEmpty(attribute.getValue())) {
80              entityDescriptor.setValidUntil(new DateTime(attribute.getValue(), ISOChronology.getInstanceUTC()));
81          } else if (attribute.getLocalName().equals(CacheableSAMLObject.CACHE_DURATION_ATTRIB_NAME)) {
82              entityDescriptor.setCacheDuration(XMLHelper.durationToLong(attribute.getValue()));
83          } else {
84              QName attribQName = XMLHelper.getNodeQName(attribute);
85              if (attribute.isId()) {
86                  entityDescriptor.getUnknownAttributes().registerID(attribQName);
87              }
88              entityDescriptor.getUnknownAttributes().put(attribQName, attribute.getValue());
89          }
90      }
91  }