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.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.schema.impl.XSStringBuilder;
28  import org.opensaml.xml.util.DatatypeHelper;
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 1.X attribute encoder.
36   */
37  public class SAML1StringAttributeEncoder extends AbstractSAML1AttributeEncoder {
38  
39      /** Class logger. */
40      private final Logger log = LoggerFactory.getLogger(SAML1StringAttributeEncoder.class);
41  
42      /** XSString factory. */
43      private final XMLObjectBuilder<XSString> stringBuilder;
44  
45      /** Constructor. */
46      public SAML1StringAttributeEncoder() {
47          super();
48          stringBuilder = new XSStringBuilder();
49      }
50  
51      /** {@inheritDoc} */
52      public Attribute encode(BaseAttribute attribute) {
53          Attribute samlAttribute = attributeBuilder.buildObject();
54          populateAttribute(samlAttribute);
55  
56          String attributeValue;
57          XSString samlAttributeValue;
58          for (Object o : attribute.getValues()) {
59              if (o == null) {
60                  continue;
61              }
62  
63              attributeValue = o.toString();
64              if (!DatatypeHelper.isEmpty(attributeValue)) {
65                  samlAttributeValue = stringBuilder.buildObject(AttributeValue.DEFAULT_ELEMENT_NAME, XSString.TYPE_NAME);
66                  samlAttributeValue.setValue(attributeValue);
67                  samlAttribute.getAttributeValues().add(samlAttributeValue);
68              }
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  }