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 string.
28   * 
29   * Attribute values evaluated by this functor <strong>must</strong> be of type {@link ScopedAttributeValue}.
30   */
31  public class AttributeScopeStringMatchFunctor extends AbstractAttributeTargetedStringMatchFunctor {
32  
33      /**
34       * Checks if the given attribute value's scope matchs the given string.
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 string.
45       * 
46       * {@inheritDoc}
47       */
48      protected boolean doEvaluatePolicyRequirement(ShibbolethFilteringContext filterContext)
49              throws FilterProcessingException {
50          BaseAttribute attribute = filterContext.getUnfilteredAttributes().get(getAttributeId());
51          if (attribute == null) {
52              return false;
53          }
54          
55          Collection values = attribute.getValues();
56          ScopedAttributeValue scoped;
57    
58          //
59          // Let's make some sense of this.  If there are values, then we look at every member.
60          // If the member is a scopedAttribute we will look at the scope and see if it fits.
61          // Otherwise keep on going - we may find something which fits.  If we get to the end
62          // and nothing has fit, say false.
63          //
64  
65          if (values != null) {
66              for (Object value : values) {
67                  if (value instanceof ScopedAttributeValue) {
68                      scoped = (ScopedAttributeValue) value;
69                      if (isMatch(scoped.getScope())) {
70                          return true;
71                      }                  
72                  } 
73              }
74          }
75          
76          return false;
77      }
78  }