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.saml2.metadata.impl;
22  
23  import java.util.Map.Entry;
24  
25  import javax.xml.namespace.QName;
26  
27  import org.opensaml.Configuration;
28  import org.opensaml.common.impl.AbstractSAMLObjectMarshaller;
29  import org.opensaml.saml2.common.CacheableSAMLObject;
30  import org.opensaml.saml2.common.TimeBoundSAMLObject;
31  import org.opensaml.saml2.metadata.AffiliationDescriptor;
32  import org.opensaml.xml.XMLObject;
33  import org.opensaml.xml.io.MarshallingException;
34  import org.opensaml.xml.util.XMLHelper;
35  import org.slf4j.Logger;
36  import org.slf4j.LoggerFactory;
37  import org.w3c.dom.Attr;
38  import org.w3c.dom.Element;
39  
40  /**
41   * A thread safe Marshaller for {@link org.opensaml.saml2.metadata.AffiliationDescriptor} objects.
42   */
43  public class AffiliationDescriptorMarshaller extends AbstractSAMLObjectMarshaller {
44  
45      /** Class logger. */
46      private final Logger log = LoggerFactory.getLogger(AffiliationDescriptorMarshaller.class);
47  
48      /** {@inheritDoc} */
49      protected void marshallAttributes(XMLObject samlElement, Element domElement) throws MarshallingException {
50          AffiliationDescriptor descriptor = (AffiliationDescriptor) samlElement;
51  
52          // Set affiliationOwnerID
53          if (descriptor.getOwnerID() != null) {
54              domElement.setAttributeNS(null, AffiliationDescriptor.OWNER_ID_ATTRIB_NAME, descriptor.getOwnerID());
55          }
56  
57          // Set ID
58          if (descriptor.getID() != null) {
59              domElement.setAttributeNS(null, AffiliationDescriptor.ID_ATTRIB_NAME, descriptor.getID());
60              domElement.setIdAttributeNS(null, AffiliationDescriptor.ID_ATTRIB_NAME, true);
61          }
62  
63          // Set the validUntil attribute
64          if (descriptor.getValidUntil() != null) {
65              log.debug("Writting validUntil attribute to AffiliationDescriptor DOM element");
66              String validUntilStr = Configuration.getSAMLDateFormatter().print(descriptor.getValidUntil());
67              domElement.setAttributeNS(null, TimeBoundSAMLObject.VALID_UNTIL_ATTRIB_NAME, validUntilStr);
68          }
69  
70          // Set the cacheDuration attribute
71          if (descriptor.getCacheDuration() != null) {
72              log.debug("Writting cacheDuration attribute to AffiliationDescriptor DOM element");
73              String cacheDuration = XMLHelper.longToDuration(descriptor.getCacheDuration());
74              domElement.setAttributeNS(null, CacheableSAMLObject.CACHE_DURATION_ATTRIB_NAME, cacheDuration);
75          }
76  
77          Attr attribute;
78          for (Entry<QName, String> entry : descriptor.getUnknownAttributes().entrySet()) {
79              attribute = XMLHelper.constructAttribute(domElement.getOwnerDocument(), entry.getKey());
80              attribute.setValue(entry.getValue());
81              domElement.setAttributeNodeNS(attribute);
82              if (Configuration.isIDAttribute(entry.getKey())
83                      || descriptor.getUnknownAttributes().isIDAttribute(entry.getKey())) {
84                  attribute.getOwnerElement().setIdAttributeNode(attribute, true);
85              }
86          }
87      }
88  }