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 java.util.StringTokenizer;
20  
21  import javax.xml.namespace.QName;
22  
23  import org.joda.time.DateTime;
24  import org.joda.time.chrono.ISOChronology;
25  import org.opensaml.common.impl.AbstractSAMLObjectUnmarshaller;
26  import org.opensaml.saml2.common.CacheableSAMLObject;
27  import org.opensaml.saml2.common.Extensions;
28  import org.opensaml.saml2.common.TimeBoundSAMLObject;
29  import org.opensaml.saml2.metadata.ContactPerson;
30  import org.opensaml.saml2.metadata.KeyDescriptor;
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.RoleDescriptor} objects.
42   */
43  public abstract class RoleDescriptorUnmarshaller extends AbstractSAMLObjectUnmarshaller {
44  
45      /** {@inheritDoc} */
46      protected void processChildElement(XMLObject parentSAMLObject, XMLObject childSAMLObject)
47              throws UnmarshallingException {
48          RoleDescriptor roleDescriptor = (RoleDescriptor) parentSAMLObject;
49  
50          if (childSAMLObject instanceof Extensions) {
51              roleDescriptor.setExtensions((Extensions) childSAMLObject);
52          } else if (childSAMLObject instanceof Signature) {
53              roleDescriptor.setSignature((Signature) childSAMLObject);
54          } else if (childSAMLObject instanceof KeyDescriptor) {
55              roleDescriptor.getKeyDescriptors().add((KeyDescriptor) childSAMLObject);
56          } else if (childSAMLObject instanceof Organization) {
57              roleDescriptor.setOrganization((Organization) childSAMLObject);
58          } else if (childSAMLObject instanceof ContactPerson) {
59              roleDescriptor.getContactPersons().add((ContactPerson) childSAMLObject);
60          } else {
61              super.processChildElement(parentSAMLObject, childSAMLObject);
62          }
63      }
64  
65      /** {@inheritDoc} */
66      protected void processAttribute(XMLObject samlObject, Attr attribute) throws UnmarshallingException {
67          RoleDescriptor roleDescriptor = (RoleDescriptor) samlObject;
68  
69          if (attribute.getLocalName().equals(RoleDescriptor.ID_ATTRIB_NAME)) {
70              roleDescriptor.setID(attribute.getValue());
71              attribute.getOwnerElement().setIdAttributeNode(attribute, true);
72          } else if (attribute.getLocalName().equals(TimeBoundSAMLObject.VALID_UNTIL_ATTRIB_NAME)
73                  && !DatatypeHelper.isEmpty(attribute.getValue())) {
74              roleDescriptor.setValidUntil(new DateTime(attribute.getValue(), ISOChronology.getInstanceUTC()));
75          } else if (attribute.getLocalName().equals(CacheableSAMLObject.CACHE_DURATION_ATTRIB_NAME)) {
76              roleDescriptor.setCacheDuration(XMLHelper.durationToLong(attribute.getValue()));
77          } else if (attribute.getLocalName().equals(RoleDescriptor.PROTOCOL_ENUMERATION_ATTRIB_NAME)) {
78              StringTokenizer protocolTokenizer = new StringTokenizer(attribute.getValue(), " ");
79              while (protocolTokenizer.hasMoreTokens()) {
80                  roleDescriptor.addSupportedProtocol(protocolTokenizer.nextToken());
81              }
82          } else if (attribute.getLocalName().equals(RoleDescriptor.ERROR_URL_ATTRIB_NAME)) {
83              roleDescriptor.setErrorURL(attribute.getValue());
84          } else {
85              QName attribQName = XMLHelper.getNodeQName(attribute);
86              if (attribute.isId()) {
87                  roleDescriptor.getUnknownAttributes().registerID(attribQName);
88              }
89              roleDescriptor.getUnknownAttributes().put(attribQName, attribute.getValue());
90          }
91      }
92  }