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