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.saml1.core.Attribute;
22  import org.opensaml.saml1.core.AttributeValue;
23  import org.opensaml.xml.XMLObject;
24  import org.opensaml.xml.XMLObjectBuilder;
25  import org.opensaml.xml.schema.XSString;
26  import org.opensaml.xml.schema.impl.XSStringBuilder;
27  import org.opensaml.xml.util.DatatypeHelper;
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  public class SAML1StringAttributeEncoder extends AbstractSAML1AttributeEncoder {
37  
38      /** Class logger. */
39      private final Logger log = LoggerFactory.getLogger(SAML1StringAttributeEncoder.class);
40  
41      /** XSString factory. */
42      private final XMLObjectBuilder<XSString> stringBuilder;
43  
44      /** Constructor. */
45      public SAML1StringAttributeEncoder() {
46          super();
47          stringBuilder = new XSStringBuilder();
48      }
49  
50      /** {@inheritDoc} */
51      public Attribute encode(BaseAttribute attribute) {
52          Attribute samlAttribute = attributeBuilder.buildObject();
53          populateAttribute(samlAttribute);
54  
55          String attributeValue;
56          XSString samlAttributeValue;
57          for (Object o : attribute.getValues()) {
58              if (o == null) {
59                  continue;
60              }
61  
62              attributeValue = o.toString();
63              if (!DatatypeHelper.isEmpty(attributeValue)) {
64                  samlAttributeValue = stringBuilder.buildObject(AttributeValue.DEFAULT_ELEMENT_NAME, XSString.TYPE_NAME);
65                  samlAttributeValue.setValue(attributeValue);
66                  samlAttribute.getAttributeValues().add(samlAttributeValue);
67              }
68          }
69  
70          List<XMLObject> attributeValues = samlAttribute.getAttributeValues();
71          if (attributeValues == null || attributeValues.isEmpty()) {
72              log.debug("Unable to encode {} attribute.  It does not contain any values", attribute.getId());
73              return null;
74          }
75  
76          return samlAttribute;
77      }
78  
79  }