View Javadoc

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