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 org.opensaml.Configuration;
21  import org.opensaml.common.SAMLObjectBuilder;
22  import org.opensaml.saml2.core.NameID;
23  import org.opensaml.xml.util.DatatypeHelper;
24  
25  import edu.internet2.middleware.shibboleth.common.attribute.BaseAttribute;
26  import edu.internet2.middleware.shibboleth.common.attribute.encoding.AttributeEncodingException;
27  import edu.internet2.middleware.shibboleth.common.attribute.encoding.SAML2NameIDEncoder;
28  
29  /**
30   * An attribute encoder that takes the first value of an attribute and creates a {@link NameID} of it. Attribute values
31   * are turned into the values for the NameID by invoking the values {@link Object#toString()} method.
32   */
33  public class SAML2StringNameIDEncoder extends AbstractAttributeEncoder<NameID> implements SAML2NameIDEncoder {
34  
35      /** NameID builder object. */
36      private SAMLObjectBuilder<NameID> nameIdBuilder;
37  
38      /** Format of the NameID. */
39      private String nameFormat;
40  
41      /** Name qualifier for the NameID. */
42      private String nameQualifier;
43  
44      /** Constructor. */
45      public SAML2StringNameIDEncoder() {
46          nameIdBuilder = (SAMLObjectBuilder<NameID>) Configuration.getBuilderFactory().getBuilder(
47                  NameID.DEFAULT_ELEMENT_NAME);
48      }
49  
50      /** {@inheritDoc} */
51      public NameID encode(BaseAttribute attribute) throws AttributeEncodingException {
52          NameID nameId = nameIdBuilder.buildObject();
53  
54          if (attribute.getValues() == null || attribute.getValues().isEmpty()) {
55              throw new AttributeEncodingException(attribute.getId() + " attribute does not contain any values to encode");
56          }
57          nameId.setValue(attribute.getValues().iterator().next().toString());
58  
59          if (nameFormat != null) {
60              nameId.setFormat(nameFormat);
61          }
62  
63          if (nameQualifier != null) {
64              nameId.setNameQualifier(nameQualifier);
65          }
66  
67          return nameId;
68      }
69  
70      /** {@inheritDoc} */
71      public String getNameFormat() {
72          return nameFormat;
73      }
74  
75      /** {@inheritDoc} */
76      public void setNameFormat(String format) {
77          nameFormat = DatatypeHelper.safeTrimOrNullString(format);
78      }
79  
80      /** {@inheritDoc} */
81      public String getNameQualifier() {
82          return nameQualifier;
83      }
84  
85      /** {@inheritDoc} */
86      public void setNameQualifier(String qualifier) {
87          nameQualifier = DatatypeHelper.safeTrimOrNullString(qualifier);
88      }
89  }