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.api.event;
021
022
023import java.util.regex.Pattern;
024import java.util.regex.PatternSyntaxException;
025
026import org.apache.directory.api.ldap.model.entry.Attribute;
027import org.apache.directory.api.ldap.model.entry.Entry;
028import org.apache.directory.api.ldap.model.entry.Value;
029import org.apache.directory.api.ldap.model.exception.LdapException;
030import org.apache.directory.api.ldap.model.exception.LdapInvalidSearchFilterException;
031import org.apache.directory.api.ldap.model.filter.ExprNode;
032import org.apache.directory.api.ldap.model.filter.SubstringNode;
033import org.apache.directory.api.ldap.model.name.Dn;
034import org.apache.directory.api.ldap.model.schema.AttributeType;
035import org.apache.directory.api.ldap.model.schema.MatchingRule;
036import org.apache.directory.api.ldap.model.schema.Normalizer;
037import org.apache.directory.server.i18n.I18n;
038
039
040/**
041 * Evaluates substring filter assertions on an entry.
042 * 
043 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
044 */
045public class SubstringEvaluator implements Evaluator
046{
047    /**
048     * Creates a new SubstringEvaluator for substring expressions.
049     */
050    public SubstringEvaluator()
051    {
052    }
053
054
055    /**
056     * {@inheritDoc}
057     */
058    public boolean evaluate( ExprNode node, Dn dn, Entry entry ) throws LdapException
059    {
060        Pattern regex = null;
061        SubstringNode snode = ( SubstringNode ) node;
062        AttributeType attributeType = snode.getAttributeType();
063        MatchingRule matchingRule = attributeType.getSubstring();
064
065        if ( matchingRule == null )
066        {
067            matchingRule = attributeType.getEquality();
068        }
069
070        Normalizer normalizer = matchingRule.getNormalizer();
071
072        // get the attribute
073        Attribute attr = entry.get( snode.getAttribute() );
074
075        // if the attribute does not exist just return false
076        if ( null == attr )
077        {
078            return false;
079        }
080
081        // compile the regular expression to search for a matching attribute
082        try
083        {
084            regex = snode.getRegex( normalizer );
085        }
086        catch ( PatternSyntaxException pse )
087        {
088            LdapInvalidSearchFilterException ne = new LdapInvalidSearchFilterException( I18n.err( I18n.ERR_248, node ) );
089            ne.initCause( pse );
090            throw ne;
091        }
092
093        /*
094         * Cycle through the attribute values testing normalized version 
095         * obtained from using the substring matching rule's normalizer.
096         * The test uses the comparator obtained from the appropriate 
097         * substring matching rule.
098         */
099
100        for ( Value value : attr )
101        {
102            String normValue = normalizer.normalize( value.getString() );
103
104            // Once match is found cleanup and return true
105
106            if ( regex.matcher( normValue ).matches() )
107            {
108                return true;
109            }
110        }
111
112        // we fell through so a match was not found - assertion was false.
113        return false;
114    }
115}