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.common.SAMLObjectBuilder;
23  import org.opensaml.saml1.core.Attribute;
24  import org.opensaml.saml1.core.AttributeValue;
25  import org.opensaml.xml.XMLObject;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  import edu.internet2.middleware.shibboleth.common.attribute.BaseAttribute;
30  import edu.internet2.middleware.shibboleth.common.attribute.encoding.AttributeEncodingException;
31  import edu.internet2.middleware.shibboleth.common.attribute.encoding.SAML1AttributeEncoder;
32  
33  /**
34   * Implementation of SAML 1.X scoped attribute encoder.
35   */
36  public class SAML1ScopedStringAttributeEncoder extends
37          AbstractScopedAttributeEncoder<org.opensaml.saml1.core.Attribute> implements SAML1AttributeEncoder {
38  
39      /** Class logger. */
40      private final Logger log = LoggerFactory.getLogger(SAML1ScopedStringAttributeEncoder.class);
41  
42      /** Attribute factory. */
43      private final SAMLObjectBuilder<Attribute> attributeBuilder;
44  
45      /** Namespace of attribute. */
46      private String namespace;
47  
48      /** Constructor. */
49      public SAML1ScopedStringAttributeEncoder() {
50          super();
51          attributeBuilder = (SAMLObjectBuilder<Attribute>) Configuration.getBuilderFactory().getBuilder(
52                  Attribute.DEFAULT_ELEMENT_NAME);
53      }
54  
55      /** {@inheritDoc} */
56      public String getNamespace() {
57          return namespace;
58      }
59  
60      /** {@inheritDoc} */
61      public void setNamespace(String newNamespace) {
62          namespace = newNamespace;
63      }
64  
65      /** {@inheritDoc} */
66      public Attribute encode(BaseAttribute attribute) throws AttributeEncodingException {
67          Attribute samlAttribute = attributeBuilder.buildObject();
68          samlAttribute.setAttributeName(getAttributeName());
69          samlAttribute.setAttributeNamespace(getNamespace());
70          samlAttribute.getAttributeValues()
71                  .addAll(encodeAttributeValues(AttributeValue.DEFAULT_ELEMENT_NAME, attribute));
72  
73          List<XMLObject> attributeValues = samlAttribute.getAttributeValues();
74          if (attributeValues == null || attributeValues.isEmpty()) {
75              log.debug("Unable to encode {} attribute.  It does not contain any values", attribute.getId());
76              return null;
77          }
78  
79          return samlAttribute;
80      }
81  
82  }