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