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