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     */
020    package org.apache.directory.server.core.subtree;
021    
022    
023    import java.util.Iterator;
024    
025    import org.apache.directory.server.i18n.I18n;
026    import org.apache.directory.shared.ldap.constants.SchemaConstants;
027    import org.apache.directory.shared.ldap.entry.EntryAttribute;
028    import org.apache.directory.shared.ldap.exception.LdapException;
029    import org.apache.directory.shared.ldap.filter.EqualityNode;
030    import org.apache.directory.shared.ldap.filter.SimpleNode;
031    import org.apache.directory.shared.ldap.schema.registries.OidRegistry;
032    
033    
034    /**
035     * A refinement leaf node evaluator.  This evaluator checks to see if the
036     * objectClass attribute of a candidate entry is matched by a leaf node in
037     * a refinement filter expression tree.
038     *
039     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
040     * @version $Rev: 927839 $
041     */
042    public class RefinementLeafEvaluator
043    {
044        /** an OID to name and vice versa registry */
045        private final OidRegistry registry;
046    
047    
048        /**
049         * Creates a refinement filter's leaf node evaluator.
050         *
051         * @param registry the OID registry used to lookup names for objectClass OIDs
052         */
053        public RefinementLeafEvaluator(OidRegistry registry)
054        {
055            this.registry = registry;
056        }
057    
058    
059        /**
060         * Evaluates whether or not a simple leaf node of a refinement filter selects an
061         * entry based on the entry's objectClass attribute values.
062         *
063         * @param node the leaf node of the refinement filter
064         * @param objectClasses the objectClass attribute's values
065         * @return true if the leaf node selects the entry based on objectClass values, false
066         * if it rejects the entry
067         * @throws LdapException
068         */
069        public boolean evaluate( SimpleNode node, EntryAttribute objectClasses ) throws LdapException
070        {
071            if ( node == null )
072            {
073                throw new IllegalArgumentException( I18n.err( I18n.ERR_295 ) );
074            }
075            
076            if ( !( node instanceof EqualityNode ) )
077            {
078                throw new LdapException( I18n.err( I18n.ERR_301, node ) );
079            }
080            
081            if ( !node.getAttribute().equalsIgnoreCase( SchemaConstants.OBJECT_CLASS_AT ) )
082            {
083                throw new LdapException( I18n.err( I18n.ERR_302, node.getAttribute() ) );
084            }
085    
086            if ( null == objectClasses )
087            {
088                throw new IllegalArgumentException( I18n.err( I18n.ERR_303 ) );
089            }
090            
091            if ( !objectClasses.instanceOf( SchemaConstants.OBJECT_CLASS_AT ) )
092            {
093                throw new IllegalArgumentException( I18n.err( I18n.ERR_304 ) );
094            }
095    
096            // check if AVA value exists in attribute
097            // If the filter value for the objectClass is an OID we need to resolve a name
098            String value = node.getValue().getString();
099    
100            if ( objectClasses.contains( value ) )
101            {
102                return true;
103            }
104            
105            if ( Character.isDigit( value.charAt( 0 ) ) )
106            {
107                Iterator<String> list = registry.getNameSet( value ).iterator();
108                
109                while ( list.hasNext() )
110                {
111                    String objectClass = list.next();
112                    
113                    if ( objectClasses.contains( objectClass ) )
114                    {
115                        return true;
116                    }
117                }
118            }
119    
120            // no match so return false
121            return false;
122        }
123    }