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