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.saml2.core.Attribute;
25  import org.opensaml.saml2.core.AttributeValue;
26  import org.opensaml.saml2.core.impl.AttributeBuilder;
27  import org.opensaml.xml.XMLObject;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  import edu.internet2.middleware.shibboleth.common.attribute.BaseAttribute;
32  import edu.internet2.middleware.shibboleth.common.attribute.encoding.AttributeEncodingException;
33  import edu.internet2.middleware.shibboleth.common.attribute.encoding.SAML2AttributeEncoder;
34  
35  /**
36   * Implementation of SAML 2.0 scoped attribute encoder.
37   */
38  public class SAML2ScopedStringAttributeEncoder extends
39          AbstractScopedAttributeEncoder<org.opensaml.saml2.core.Attribute> implements SAML2AttributeEncoder {
40  
41      /** Class logger. */
42      private final Logger log = LoggerFactory.getLogger(SAML2ScopedStringAttributeEncoder.class);
43  
44      /** Builder for SAML 2 attribute XMLObjects. */
45      private final SAMLObjectBuilder<Attribute> attributeBuilder;
46  
47      /** Format of attribute. */
48      private String format;
49  
50      /** Friendly name of attribute. */
51      private String friendlyName;
52  
53      /** Constructor. */
54      public SAML2ScopedStringAttributeEncoder() {
55          super();
56  
57          attributeBuilder = (AttributeBuilder) Configuration.getBuilderFactory().getBuilder(
58                  Attribute.DEFAULT_ELEMENT_NAME);
59      }
60  
61      /** {@inheritDoc} */
62      public String getNameFormat() {
63          return format;
64      }
65  
66      /** {@inheritDoc} */
67      public String getFriendlyName() {
68          return friendlyName;
69      }
70  
71      /** {@inheritDoc} */
72      public void setNameFormat(String newFormat) {
73          format = newFormat;
74      }
75  
76      /** {@inheritDoc} */
77      public void setFriendlyName(String name) {
78          friendlyName = name;
79      }
80  
81      /** {@inheritDoc} */
82      public Attribute encode(BaseAttribute attribute) throws AttributeEncodingException {
83          Attribute samlAttribute = attributeBuilder.buildObject();
84          samlAttribute.setName(getAttributeName());
85          samlAttribute.setNameFormat(getNameFormat());
86          samlAttribute.setFriendlyName(getFriendlyName());
87          samlAttribute.getAttributeValues()
88                  .addAll(encodeAttributeValues(AttributeValue.DEFAULT_ELEMENT_NAME, attribute));
89  
90          List<XMLObject> attributeValues = samlAttribute.getAttributeValues();
91          if (attributeValues == null || attributeValues.isEmpty()) {
92              log.debug("Unable to encode {} attribute.  It does not contain any values", attribute.getId());
93              return null;
94          }
95  
96          return samlAttribute;
97      }
98  }