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.List;
20  import java.util.Map.Entry;
21  
22  import javax.xml.namespace.QName;
23  
24  import org.opensaml.Configuration;
25  import org.opensaml.common.impl.AbstractSAMLObjectMarshaller;
26  import org.opensaml.saml2.common.CacheableSAMLObject;
27  import org.opensaml.saml2.common.TimeBoundSAMLObject;
28  import org.opensaml.saml2.metadata.RoleDescriptor;
29  import org.opensaml.xml.XMLObject;
30  import org.opensaml.xml.io.MarshallingException;
31  import org.opensaml.xml.util.XMLHelper;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  import org.w3c.dom.Attr;
35  import org.w3c.dom.Element;
36  
37  /**
38   * A thread safe Marshaller for {@link org.opensaml.saml2.metadata.RoleDescriptor} objects.
39   */
40  public abstract class RoleDescriptorMarshaller extends AbstractSAMLObjectMarshaller {
41  
42      /** Class logger. */
43      private final Logger log = LoggerFactory.getLogger(RoleDescriptorMarshaller.class);
44  
45      /** {@inheritDoc} */
46      protected void marshallAttributes(XMLObject samlElement, Element domElement) throws MarshallingException {
47          RoleDescriptor roleDescriptor = (RoleDescriptor) samlElement;
48  
49          // Set the ID attribute
50          if (roleDescriptor.getID() != null) {
51              log.trace("Writing ID attribute to RoleDescriptor DOM element");
52              domElement.setAttributeNS(null, RoleDescriptor.ID_ATTRIB_NAME, roleDescriptor.getID());
53              domElement.setIdAttributeNS(null, RoleDescriptor.ID_ATTRIB_NAME, true);
54          }
55  
56          // Set the validUntil attribute
57          if (roleDescriptor.getValidUntil() != null) {
58              log.trace("Writting validUntil attribute to RoleDescriptor DOM element");
59              String validUntilStr = Configuration.getSAMLDateFormatter().print(roleDescriptor.getValidUntil());
60              domElement.setAttributeNS(null, TimeBoundSAMLObject.VALID_UNTIL_ATTRIB_NAME, validUntilStr);
61          }
62  
63          // Set the cacheDuration attribute
64          if (roleDescriptor.getCacheDuration() != null) {
65              log.trace("Writting cacheDuration attribute to EntitiesDescriptor DOM element");
66              String cacheDuration = XMLHelper.longToDuration(roleDescriptor.getCacheDuration());
67              domElement.setAttributeNS(null, CacheableSAMLObject.CACHE_DURATION_ATTRIB_NAME, cacheDuration);
68          }
69  
70          // Set the protocolSupportEnumeration attribute
71          List<String> supportedProtocols = roleDescriptor.getSupportedProtocols();
72          if (supportedProtocols != null && supportedProtocols.size() > 0) {
73              log.trace("Writting protocolSupportEnumberation attribute to RoleDescriptor DOM element");
74  
75              StringBuilder builder = new StringBuilder();
76              for (String protocol : supportedProtocols) {
77                  builder.append(protocol);
78                  builder.append(" ");
79              }
80  
81              domElement.setAttributeNS(null, RoleDescriptor.PROTOCOL_ENUMERATION_ATTRIB_NAME, builder.toString().trim());
82          }
83  
84          // Set errorURL attribute
85          if (roleDescriptor.getErrorURL() != null) {
86              log.trace("Writting errorURL attribute to RoleDescriptor DOM element");
87              domElement.setAttributeNS(null, RoleDescriptor.ERROR_URL_ATTRIB_NAME, roleDescriptor.getErrorURL());
88          }
89  
90          Attr attribute;
91          for (Entry<QName, String> entry : roleDescriptor.getUnknownAttributes().entrySet()) {
92              attribute = XMLHelper.constructAttribute(domElement.getOwnerDocument(), entry.getKey());
93              attribute.setValue(entry.getValue());
94              domElement.setAttributeNodeNS(attribute);
95              if (Configuration.isIDAttribute(entry.getKey())
96                      || roleDescriptor.getUnknownAttributes().isIDAttribute(entry.getKey())) {
97                  attribute.getOwnerElement().setIdAttributeNode(attribute, true);
98              }
99          }
100     }
101 }