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 edu.internet2.middleware.shibboleth.common.attribute.BaseAttribute;
20  import edu.internet2.middleware.shibboleth.common.attribute.filtering.provider.FilterProcessingException;
21  import edu.internet2.middleware.shibboleth.common.attribute.filtering.provider.ShibbolethFilteringContext;
22  
23  /**
24   * A match functor that checks if the given attribute has more than the minimum number of values but less than the
25   * maximum.
26   */
27  public class NumOfAttributeValuesMatchFunctor extends AbstractMatchFunctor {
28  
29      /** ID of the attribute that will be checked. */
30      private String attributeId;
31  
32      /** Minimum allowed number of attribute values. */
33      private int minimumValues;
34  
35      /** Maximum allowed number of attribute values. */
36      private int maximumValues;
37  
38      /**
39       * Constructor.
40       * 
41       * @param id ID of the attribute to be checked
42       * @param min minimum number of values allowed
43       * @param max maximum number of values allowed
44       */
45      public NumOfAttributeValuesMatchFunctor(String id, int min, int max) {
46          attributeId = id;
47          minimumValues = min;
48          maximumValues = max;
49      }
50  
51      /** {@inheritDoc} */
52      protected boolean doEvaluatePolicyRequirement(ShibbolethFilteringContext filterContext)
53              throws FilterProcessingException {
54          return isWithinRange(filterContext.getUnfilteredAttributes().get(attributeId));
55      }
56  
57      /** {@inheritDoc} */
58      protected boolean doEvaluateValue(ShibbolethFilteringContext filterContext, String id, Object value)
59              throws FilterProcessingException {
60          return isWithinRange(filterContext.getUnfilteredAttributes().get(attributeId));
61      }
62  
63      /**
64       * Checks that the number of values for the given attribute is within the given range.
65       * 
66       * @param attribute attribute to check
67       * 
68       * @return true if the attribute has more than the minimum number of values and less than the maximum.
69       */
70      protected boolean isWithinRange(BaseAttribute attribute) {
71          if (attribute == null) {
72              return false;
73          }
74  
75          int numOfValues = attribute.getValues().size();
76  
77          return numOfValues >= minimumValues && numOfValues <= maximumValues;
78      }
79  }