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