View Javadoc

1   /*
2    * Copyright [2007] [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.XSString;
27  import org.opensaml.xml.schema.impl.XSStringBuilder;
28  import org.opensaml.xml.util.Base64;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  import edu.internet2.middleware.shibboleth.common.attribute.BaseAttribute;
33  
34  /**
35   * Implementation of SAML 2.0 attribute encoder. * This attribute encoder only operates of {@link BaseAttribute}s with
36   * value of type <code>byte[]</code>.
37   */
38  public class SAML2Base64AttributeEncoder extends AbstractSAML2AttributeEncoder {
39  
40      /** Class logger. */
41      private final Logger log = LoggerFactory.getLogger(SAML2Base64AttributeEncoder.class);
42  
43      /** XSString factory. */
44      private final XMLObjectBuilder<XSString> stringBuilder;
45  
46      /** Constructor. */
47      public SAML2Base64AttributeEncoder() {
48          super();
49          stringBuilder = (XSStringBuilder) Configuration.getBuilderFactory().getBuilder(XSString.TYPE_NAME);
50      }
51  
52      /** {@inheritDoc} */
53      public Attribute encode(BaseAttribute attribute) {
54          Attribute samlAttribute = attributeBuilder.buildObject();
55          populateAttribute(samlAttribute);
56  
57          byte[] attributeValue;
58          XSString samlAttributeValue;
59          for (Object o : attribute.getValues()) {
60              if (o == null || !(o instanceof byte[])) {
61                  log.debug("Skipping attribute value because it is either null or not a byte[]");
62                  continue;
63              }
64  
65              attributeValue = (byte[]) o;
66              samlAttributeValue = stringBuilder.buildObject(AttributeValue.DEFAULT_ELEMENT_NAME, XSString.TYPE_NAME);
67              samlAttributeValue.setValue(Base64.encodeBytes(attributeValue));
68              samlAttribute.getAttributeValues().add(samlAttributeValue);
69          }
70  
71          List<XMLObject> attributeValues = samlAttribute.getAttributeValues();
72          if (attributeValues == null || attributeValues.isEmpty()) {
73              log.debug("Unable to encode {} attribute.  It does not contain any values", attribute.getId());
74              return null;
75          }
76  
77          return samlAttribute;
78      }
79  
80  }