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