View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  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,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.server.core.event;
21  
22  
23  import java.util.regex.Pattern;
24  import java.util.regex.PatternSyntaxException;
25  
26  import javax.naming.NamingException;
27  
28  import org.apache.directory.server.core.entry.ServerEntry;
29  import org.apache.directory.server.schema.registries.AttributeTypeRegistry;
30  import org.apache.directory.server.schema.registries.OidRegistry;
31  import org.apache.directory.shared.ldap.entry.EntryAttribute;
32  import org.apache.directory.shared.ldap.entry.Value;
33  import org.apache.directory.shared.ldap.filter.ExprNode;
34  import org.apache.directory.shared.ldap.filter.SubstringNode;
35  import org.apache.directory.shared.ldap.schema.AttributeType;
36  import org.apache.directory.shared.ldap.schema.MatchingRule;
37  import org.apache.directory.shared.ldap.schema.Normalizer;
38  
39  
40  /**
41   * Evaluates substring filter assertions on an entry.
42   * 
43   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
44   * @version $Rev: 648932 $
45   */
46  public class SubstringEvaluator implements Evaluator
47  {
48      /** Oid Registry used to translate attributeIds to OIDs */
49      private OidRegistry oidRegistry;
50      /** AttributeType registry needed for normalizing and comparing values */
51      private AttributeTypeRegistry attributeTypeRegistry;
52  
53  
54      /**
55       * Creates a new SubstringEvaluator for substring expressions.
56       *
57       * @param oidRegistry the OID registry for name to OID mapping
58       * @param attributeTypeRegistry the attributeType registry
59       */
60      public SubstringEvaluator(OidRegistry oidRegistry, AttributeTypeRegistry attributeTypeRegistry)
61      {
62          this.oidRegistry = oidRegistry;
63          this.attributeTypeRegistry = attributeTypeRegistry;
64      }
65  
66  
67      /**
68       * @see Evaluator#evaluate( ExprNode, String, ServerEntry )
69       */
70      public boolean evaluate( ExprNode node, String dn, ServerEntry entry ) throws NamingException
71      {
72          Pattern regex = null;
73          SubstringNode snode = (SubstringNode)node;
74          String oid = oidRegistry.getOid( snode.getAttribute() );
75          AttributeType type = attributeTypeRegistry.lookup( oid );
76          MatchingRule matchingRule = type.getSubstr();
77          
78          if ( matchingRule == null )
79          {
80              matchingRule = type.getEquality();
81          }
82          
83          Normalizer normalizer = matchingRule.getNormalizer();
84          
85  
86          // get the attribute
87          EntryAttribute attr = entry.get( snode.getAttribute() );
88  
89          // if the attribute does not exist just return false
90          if ( null == attr )
91          {
92              return false;
93          }
94  
95          // compile the regular expression to search for a matching attribute
96          try
97          {
98              regex = snode.getRegex( normalizer );
99          }
100         catch ( PatternSyntaxException pse )
101         {
102             NamingException ne = new NamingException( "SubstringNode '" + node + "' had " + "incorrect syntax" );
103             ne.setRootCause( pse );
104             throw ne;
105         }
106 
107         /*
108          * Cycle through the attribute values testing normalized version 
109          * obtained from using the substring matching rule's normalizer.
110          * The test uses the comparator obtained from the appropriate 
111          * substring matching rule.
112          */
113 
114         for ( Value<?> value: attr )
115         {
116             String normValue = ( String ) normalizer.normalize( value );
117 
118             // Once match is found cleanup and return true
119 
120             if ( regex.matcher( normValue ).matches() )
121             {
122                 return true;
123             }
124         }
125 
126         // we fell through so a match was not found - assertion was false.
127         return false;
128     }
129 }