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.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  
25  import edu.internet2.middleware.shibboleth.common.attribute.BaseAttribute;
26  import edu.internet2.middleware.shibboleth.common.attribute.provider.BasicAttribute;
27  import edu.internet2.middleware.shibboleth.common.attribute.provider.ScopedAttributeValue;
28  import edu.internet2.middleware.shibboleth.common.attribute.resolver.AttributeResolutionException;
29  import edu.internet2.middleware.shibboleth.common.attribute.resolver.provider.ShibbolethResolutionContext;
30  
31  /**
32   * An attribute definition that creates {@link ScopedAttributeValue}s by taking a source attribute value splitting it
33   * at a delimiter. The first atom becomes the attribute value and the second value becomes the scope.
34   */
35  public class PrescopedAttributeDefinition extends BaseAttributeDefinition {
36  
37      /** Class logger. */
38      private final Logger log = LoggerFactory.getLogger(PrescopedAttributeDefinition.class);
39  
40      /** Delimiter between value and scope. */
41      private String scopeDelimiter;
42  
43      /**
44       * Constructor.
45       * 
46       * @param delimiter scope of the attribute
47       */
48      public PrescopedAttributeDefinition(String delimiter) {
49          scopeDelimiter = delimiter;
50      }
51  
52      /** {@inheritDoc} */
53      public BaseAttribute<ScopedAttributeValue> doResolve(ShibbolethResolutionContext resolutionContext)
54              throws AttributeResolutionException {
55          BasicAttribute<ScopedAttributeValue> attribute = new BasicAttribute<ScopedAttributeValue>();
56          attribute.setId(getId());
57  
58          Collection<?> values = getValuesFromAllDependencies(resolutionContext);
59          if (values == null || values.isEmpty()) {
60              return attribute;
61          }
62  
63          String[] stringValues;
64          for (Object value : values) {
65              if (!(value instanceof String)) {
66                  continue;
67              }
68  
69              stringValues = ((String) value).split(scopeDelimiter);
70              if (stringValues.length < 2) {
71                  log.error("Input attribute value {} does not contain delimited {} and can not be split", value,
72                          scopeDelimiter);
73                  throw new AttributeResolutionException("Input attribute value can not be split.");
74              }
75              attribute.getValues().add(new ScopedAttributeValue(stringValues[0], stringValues[1]));
76          }
77  
78          return attribute;
79      }
80  
81      /**
82       * Get delimiter between value and scope.
83       * 
84       * @return delimiter between value and scope
85       */
86      public String getScopeDelimited() {
87          return scopeDelimiter;
88      }
89  
90      /** {@inheritDoc} */
91      public void validate() throws AttributeResolutionException {
92          // do nothing
93      }
94  }