View Javadoc

1   /*
2    * Licensed to the University Corporation for Advanced Internet Development, 
3    * Inc. (UCAID) under one or more contributor license agreements.  See the 
4    * NOTICE file distributed with this work for additional information regarding
5    * copyright ownership. The UCAID licenses this file to You under the Apache 
6    * License, Version 2.0 (the "License"); you may not use this file except in 
7    * compliance with the License.  You may obtain a copy of the License at
8    *
9    *    http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package edu.internet2.middleware.shibboleth.common.attribute.encoding.provider;
19  
20  import java.util.List;
21  
22  import org.opensaml.Configuration;
23  import org.opensaml.saml1.core.Attribute;
24  import org.opensaml.saml1.core.AttributeValue;
25  import org.opensaml.xml.XMLObject;
26  import org.opensaml.xml.XMLObjectBuilder;
27  import org.opensaml.xml.schema.XSAny;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  import edu.internet2.middleware.shibboleth.common.attribute.BaseAttribute;
32  import edu.internet2.middleware.shibboleth.common.attribute.encoding.AttributeEncodingException;
33  
34  /** A SAML 1 encoder that uses {@link XMLObject} as the value for attribute values. */
35  public class SAML1XMLObjectAttributeEncoder extends AbstractSAML1AttributeEncoder {
36  
37      /** Class logger. */
38      private final Logger log = LoggerFactory.getLogger(SAML1XMLObjectAttributeEncoder.class);
39  
40      /** Builder of AttributeValue XMLObjects. */
41      private final XMLObjectBuilder<XSAny> attributeValueBuilder;
42  
43      /** Constructor. */
44      public SAML1XMLObjectAttributeEncoder() {
45          super();
46          attributeValueBuilder = Configuration.getBuilderFactory().getBuilder(XSAny.TYPE_NAME);
47      }
48  
49      /** {@inheritDoc} */
50      public Attribute encode(BaseAttribute attribute) throws AttributeEncodingException {
51          Attribute samlAttribute = attributeBuilder.buildObject();
52          populateAttribute(samlAttribute);
53  
54          XSAny samlAttributeValue;
55          for (Object o : attribute.getValues()) {
56              if (o == null || !(o instanceof XMLObject)) {
57                  continue;
58              }
59  
60              samlAttributeValue = attributeValueBuilder.buildObject(AttributeValue.DEFAULT_ELEMENT_NAME);
61              samlAttributeValue.getUnknownXMLObjects().add((XMLObject) o);
62              samlAttribute.getAttributeValues().add(samlAttributeValue);
63          }
64  
65          List<XMLObject> attributeValues = samlAttribute.getAttributeValues();
66          if (attributeValues == null || attributeValues.isEmpty()) {
67              log.debug("Unable to encode {} attribute.  It does not contain any values", attribute.getId());
68              return null;
69          }
70  
71          return samlAttribute;
72      }
73  
74  }