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