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