View Javadoc

1   /*
2    *   @(#) $Id: HighestPrecedenceFilter.java 326050 2005-10-18 08:19:14Z akarasulu $
3    *   
4    *   Copyright 2004 The Apache Software Foundation
5    *
6    *   Licensed under the Apache License, Version 2.0 (the "License");
7    *   you may not use this file except in compliance with the License.
8    *   You may obtain a copy of the License at
9    *
10   *       http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing, software
13   *   distributed under the License is distributed on an "AS IS" BASIS,
14   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *   See the License for the specific language governing permissions and
16   *   limitations under the License.
17   *
18   */
19  package org.apache.ldap.server.authz.support;
20  
21  import java.util.Collection;
22  import java.util.Iterator;
23  
24  import javax.naming.Name;
25  import javax.naming.NamingException;
26  import javax.naming.directory.Attributes;
27  
28  import org.apache.ldap.common.aci.ACITuple;
29  import org.apache.ldap.common.aci.AuthenticationLevel;
30  import org.apache.ldap.server.partition.DirectoryPartitionNexusProxy;
31  
32  
33  /***
34   * An {@link ACITupleFilter} that discards all tuples having a precedence less
35   * than the highest remaining precedence. (18.8.4.1, X.501)
36   *
37   * @author The Apache Directory Project
38   * @version $Rev: 326050 $, $Date: 2005-10-18 04:19:14 -0400 (Tue, 18 Oct 2005) $
39   */
40  public class HighestPrecedenceFilter implements ACITupleFilter
41  {
42      public Collection filter( Collection tuples, OperationScope scope, DirectoryPartitionNexusProxy proxy, Collection userGroupNames, Name userName, Attributes userEntry, AuthenticationLevel authenticationLevel, Name entryName, String attrId, Object attrValue, Attributes entry, Collection microOperations ) throws NamingException
43      {
44          if( tuples.size() <= 1 )
45          {
46              return tuples;
47          }
48  
49          int maxPrecedence = -1;
50  
51          // Find the maximum precedence for all tuples.
52          for( Iterator i = tuples.iterator(); i.hasNext(); )
53          {
54              ACITuple tuple = ( ACITuple ) i.next();
55              if( tuple.getPrecedence() > maxPrecedence )
56              {
57                  maxPrecedence = tuple.getPrecedence();
58              }
59          }
60  
61          // Remove all tuples whose precedences are not the maximum one.
62          for( Iterator i = tuples.iterator(); i.hasNext(); )
63          {
64              ACITuple tuple = ( ACITuple ) i.next();
65              if( tuple.getPrecedence() != maxPrecedence )
66              {
67                  i.remove();
68              }
69          }
70  
71          return tuples;
72      }
73  }