View Javadoc

1   /*
2    * Copyright 2008 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 edu.internet2.middleware.shibboleth.common.attribute.encoding.provider;
18  
19  import java.util.List;
20  
21  import org.opensaml.Configuration;
22  import org.opensaml.saml2.core.Attribute;
23  import org.opensaml.saml2.core.AttributeValue;
24  import org.opensaml.xml.XMLObject;
25  import org.opensaml.xml.XMLObjectBuilder;
26  import org.opensaml.xml.schema.XSAny;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  import edu.internet2.middleware.shibboleth.common.attribute.BaseAttribute;
31  import edu.internet2.middleware.shibboleth.common.attribute.encoding.AttributeEncodingException;
32  
33  /** A SAML 2 encoder that uses {@link XMLObject} as the value for attribute values. */
34  public class SAML2XMLObjectAttributeEncoder extends AbstractSAML2AttributeEncoder {
35  
36      /** Class logger. */
37      private final Logger log = LoggerFactory.getLogger(SAML2XMLObjectAttributeEncoder.class);
38  
39      /** Builder of AttributeValue XMLObjects. */
40      private final XMLObjectBuilder<XSAny> attributeValueBuilder;
41  
42      /** Constructor. */
43      public SAML2XMLObjectAttributeEncoder() {
44          super();
45          attributeValueBuilder = Configuration.getBuilderFactory().getBuilder(XSAny.TYPE_NAME);
46      }
47  
48      /** {@inheritDoc} */
49      public Attribute encode(BaseAttribute attribute) throws AttributeEncodingException {
50          Attribute samlAttribute = attributeBuilder.buildObject();
51          populateAttribute(samlAttribute);
52  
53          XSAny samlAttributeValue;
54          for (Object o : attribute.getValues()) {
55              if (o == null || !(o instanceof XMLObject)) {
56                  continue;
57              }
58  
59              samlAttributeValue = attributeValueBuilder.buildObject(AttributeValue.DEFAULT_ELEMENT_NAME);
60              samlAttributeValue.getUnknownXMLObjects().add((XMLObject) o);
61              samlAttribute.getAttributeValues().add(samlAttributeValue);
62          }
63  
64          List<XMLObject> attributeValues = samlAttribute.getAttributeValues();
65          if (attributeValues == null || attributeValues.isEmpty()) {
66              log.debug("Unable to encode {} attribute.  It does not contain any values", attribute.getId());
67              return null;
68          }
69  
70          return samlAttribute;
71      }
72  
73  }