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