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