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