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