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  import java.util.regex.Matcher;
21  import java.util.regex.Pattern;
22  
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  import edu.internet2.middleware.shibboleth.common.attribute.BaseAttribute;
27  import edu.internet2.middleware.shibboleth.common.attribute.provider.BasicAttribute;
28  import edu.internet2.middleware.shibboleth.common.attribute.resolver.AttributeResolutionException;
29  import edu.internet2.middleware.shibboleth.common.attribute.resolver.provider.ShibbolethResolutionContext;
30  
31  /** An attribute definition that splits the source attribute's values by means of a regex. */
32  public class RegexSplitAttributeDefinition extends BaseAttributeDefinition {
33  
34      /** Class logger. */
35      private final Logger log = LoggerFactory.getLogger(RegexSplitAttributeDefinition.class);
36  
37      /** Regular expression used to split values. */
38      private Pattern regex;
39      
40      /**
41       * Constructor.
42       * 
43       * @param regularExpression expression used to split attribute values
44       * @param caseSensitive whether the regular expression is case sensitive
45       */
46      public RegexSplitAttributeDefinition(String regularExpression, boolean caseSensitive) {
47          if(!caseSensitive){
48              regex = Pattern.compile(regularExpression, Pattern.CASE_INSENSITIVE);
49          }else{
50              regex = Pattern.compile(regularExpression);
51          }
52      }
53  
54      /** {@inheritDoc} */
55      protected BaseAttribute<?> doResolve(ShibbolethResolutionContext resolutionContext)
56              throws AttributeResolutionException {
57          BasicAttribute<Object> attribute = new BasicAttribute<Object>();
58          attribute.setId(getId());
59  
60          Collection<?> values = getValuesFromAllDependencies(resolutionContext);
61          if (values == null || values.isEmpty()) {
62              return attribute;
63          }
64  
65          Matcher matcher;
66          for (Object value : values) {
67              if (value instanceof String) {
68                  matcher = regex.matcher((String) value);
69                  if(matcher.matches()){
70                      attribute.getValues().add(matcher.group(1));
71                  } else {
72                      log.debug("Value {} did not result in any values when split by regular expression {}", value, regex
73                              .toString());
74                  }
75              } else {
76                  log.debug("Ignoring non-string attribute value");
77              }
78          }
79  
80          return attribute;
81      }
82  
83      /** {@inheritDoc} */
84      public void validate() throws AttributeResolutionException {
85          if(getSourceAttributeID() == null){
86              throw new AttributeResolutionException("Source attribute ID is required but none was given.");
87          }
88      }
89  }