View Javadoc

1   /*
2    * Copyright [2007] [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.filtering.provider.match.basic;
18  
19  import java.util.Collection;
20  
21  import edu.internet2.middleware.shibboleth.common.attribute.BaseAttribute;
22  import edu.internet2.middleware.shibboleth.common.attribute.filtering.provider.FilterProcessingException;
23  import edu.internet2.middleware.shibboleth.common.attribute.filtering.provider.ShibbolethFilteringContext;
24  import edu.internet2.middleware.shibboleth.common.attribute.provider.ScopedAttributeValue;
25  
26  /**
27   * Match functor that checks if an attribute's scoped values are equal to a given regular expression.
28   * 
29   * Attribute values evaluated by this functor <strong>must</strong> be of type {@link ScopedAttributeValue}.
30   */
31  public class AttributeScopeRegexMatchFunctor extends AbstractAttributeTargetedRegexMatchFunctor {
32  
33      /**
34       * Checks if the given attribute value's scope matchs the given regular expression.
35       * 
36       * {@inheritDoc}
37       */
38      protected boolean doEvaluateValue(ShibbolethFilteringContext filterContext, String attributeId,
39              Object attributeValue) throws FilterProcessingException {
40          return isMatch(((ScopedAttributeValue) attributeValue).getScope());
41      }
42  
43      /**
44       * Checks if any of the scopes for the values of the given attribute match the given regular expression.
45       * 
46       * {@inheritDoc}
47       */
48      protected boolean doEvaluatePolicyRequirement(ShibbolethFilteringContext filterContext)
49              throws FilterProcessingException {
50          BaseAttribute attribute = filterContext.getUnfilteredAttributes().get(getAttributeId());
51  
52          if (attribute != null) {
53              ScopedAttributeValue value;
54              Collection values = attribute.getValues();
55  
56              //
57              // Let's make some sense of this. If there are values, then we look at every member.
58              // If the member is a scopedAttribute we will look at the scope and see if it fits.
59              // Otherwise keep on going - we may find something which fits. If we get to the end
60              // and nothing has fit, say false.
61              //
62  
63              if (values != null) {
64                  for (Object object : values) {
65                      if (object instanceof ScopedAttributeValue) {
66                          value = (ScopedAttributeValue) object;
67                          if (isMatch(value.getScope())) {
68                              return true;
69  
70                          }
71                      }
72                  }
73              }
74          }
75  
76          return false;
77      }
78  }