View Javadoc

1   /*
2    * Copyright [2006] [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.resolver.provider.attributeDefinition;
18  
19  import java.util.Collection;
20  
21  import org.opensaml.xml.util.DatatypeHelper;
22  
23  import edu.internet2.middleware.shibboleth.common.attribute.BaseAttribute;
24  import edu.internet2.middleware.shibboleth.common.attribute.provider.BasicAttribute;
25  import edu.internet2.middleware.shibboleth.common.attribute.provider.ScopedAttributeValue;
26  import edu.internet2.middleware.shibboleth.common.attribute.resolver.AttributeResolutionException;
27  import edu.internet2.middleware.shibboleth.common.attribute.resolver.provider.ShibbolethResolutionContext;
28  
29  /**
30   * An attribute definition that creates {@link ScopedAttributeValue}s by taking a source attribute value and applying a
31   * static scope to each.
32   */
33  public class ScopedAttributeDefinition extends BaseAttributeDefinition {
34  
35      /** Scope value. */
36      private String scope;
37  
38      /**
39       * Constructor.
40       * 
41       * @param newScope scope of the attribute
42       */
43      public ScopedAttributeDefinition(String newScope) {
44          this.scope = newScope;
45      }
46  
47      /** {@inheritDoc} */
48      public BaseAttribute<ScopedAttributeValue> doResolve(ShibbolethResolutionContext resolutionContext)
49              throws AttributeResolutionException {
50          BasicAttribute<ScopedAttributeValue> attribute = new BasicAttribute<ScopedAttributeValue>();
51          attribute.setId(getId());
52  
53          Collection<?> values = getValuesFromAllDependencies(resolutionContext);
54          if (values != null && !values.isEmpty()) {
55              for (Object value : values) {
56                  if (value != null) {
57                      String strValue = DatatypeHelper.safeTrimOrNullString(value.toString());
58                      if (strValue != null) {
59                          attribute.getValues().add(new ScopedAttributeValue(strValue.toString(), scope));
60                      }
61                  }
62              }
63          }
64  
65          return attribute;
66      }
67  
68      /**
69       * Get scope value.
70       * 
71       * @return Returns the scope.
72       */
73      public String getScope() {
74          return scope;
75      }
76  
77      /** {@inheritDoc} */
78      public void validate() throws AttributeResolutionException {
79          // do nothing
80      }
81  }