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.saml2.core.Attribute;
24  import org.opensaml.saml2.core.AttributeValue;
25  import org.opensaml.xml.XMLObject;
26  import org.opensaml.xml.XMLObjectBuilder;
27  import org.opensaml.xml.schema.XSString;
28  import org.opensaml.xml.schema.impl.XSStringBuilder;
29  import org.opensaml.xml.util.DatatypeHelper;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  import edu.internet2.middleware.shibboleth.common.attribute.BaseAttribute;
34  
35  /**
36   * Implementation of SAML 2.0 attribute encoder.
37   */
38  public class SAML2StringAttributeEncoder extends AbstractSAML2AttributeEncoder {
39  
40      /** Class logger. */
41      private final Logger log = LoggerFactory.getLogger(SAML2StringAttributeEncoder.class);
42  
43      /** XSString factory. */
44      private final XMLObjectBuilder<XSString> stringBuilder;
45  
46      /** Constructor. */
47      public SAML2StringAttributeEncoder() {
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          String attributeValue;
58          XSString samlAttributeValue;
59          for (Object o : attribute.getValues()) {
60              if (o == null) {
61                  continue;
62              }
63  
64              attributeValue = o.toString();
65              if (!(DatatypeHelper.isEmpty(attributeValue))) {
66                  samlAttributeValue = stringBuilder.buildObject(AttributeValue.DEFAULT_ELEMENT_NAME, XSString.TYPE_NAME);
67                  samlAttributeValue.setValue(attributeValue);
68                  samlAttribute.getAttributeValues().add(samlAttributeValue);
69              }
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  }