001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *  
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *  
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License. 
018 *  
019 */
020package org.apache.directory.server.core.authz.support;
021
022
023import java.util.Collection;
024import java.util.Iterator;
025
026import org.apache.directory.api.ldap.aci.ACITuple;
027import org.apache.directory.api.ldap.model.entry.Entry;
028import org.apache.directory.api.ldap.model.exception.LdapException;
029
030
031/**
032 * An {@link ACITupleFilter} that discards all tuples having a precedence less
033 * than the highest remaining precedence. (18.8.4.1, X.501)
034 *
035 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
036 */
037public class HighestPrecedenceFilter implements ACITupleFilter
038{
039    /**
040     * {@inheritDoc}
041     */
042    @Override
043    public Collection<ACITuple> filter( AciContext aciContext, OperationScope scope, Entry userEntry )
044        throws LdapException
045    {
046        ACI_LOG.debug( "Filtering HighestPrecedence..." );
047
048        if ( aciContext.getAciTuples().size() <= 1 )
049        {
050            ACI_LOG.debug( "HighestPrecedence : nothing to do" );
051            return aciContext.getAciTuples();
052        }
053
054        int maxPrecedence = -1;
055
056        // Find the maximum precedence for all tuples.
057        for ( ACITuple tuple : aciContext.getAciTuples() )
058        {
059            if ( ( tuple.getPrecedence() != null ) && ( tuple.getPrecedence() > maxPrecedence ) )
060            {
061                maxPrecedence = tuple.getPrecedence();
062            }
063        }
064
065        // Remove all tuples whose precedences are not the maximum one.
066        for ( Iterator<ACITuple> i = aciContext.getAciTuples().iterator(); i.hasNext(); )
067        {
068            ACITuple tuple = i.next();
069
070            if ( ( tuple.getPrecedence() != null ) && ( tuple.getPrecedence() != maxPrecedence ) )
071            {
072                i.remove();
073            }
074        }
075
076        return aciContext.getAciTuples();
077    }
078}