View Javadoc

1   /*
2    *   Copyright 2004 The Apache Software Foundation
3    *
4    *   Licensed under the Apache License, Version 2.0 (the "License");
5    *   you may not use this file except in compliance with the License.
6    *   You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *   Unless required by applicable law or agreed to in writing, software
11   *   distributed under the License is distributed on an "AS IS" BASIS,
12   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *   See the License for the specific language governing permissions and
14   *   limitations under the License.
15   *
16   */
17  package org.apache.ldap.server.event;
18  
19  
20  import javax.naming.NamingException;
21  import javax.naming.Name;
22  import javax.naming.directory.SearchControls;
23  import javax.naming.directory.Attributes;
24  
25  import org.apache.ldap.common.filter.ExprNode;
26  import org.apache.ldap.common.filter.ScopeNode;
27  import org.apache.ldap.common.name.DnParser;
28  
29  
30  /***
31   * Evaluates ScopeNode assertions on candidates using a database.
32   * 
33   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
34   * @version $Rev$
35   */
36  public class ScopeEvaluator implements Evaluator
37  {
38      private DnParser parser = null;
39  
40  
41      public ScopeEvaluator() throws NamingException
42      {
43          parser = new DnParser();
44      }
45  
46  
47      /***
48       * @see Evaluator#evaluate(ExprNode, String, Attributes)
49       */
50      public boolean evaluate( ExprNode node, String dn, Attributes record )
51          throws NamingException
52      {
53          ScopeNode snode = ( ScopeNode ) node;
54  
55          switch( snode.getScope() )
56          {
57          case( SearchControls.OBJECT_SCOPE ):
58              return dn.equals( snode.getBaseDn() );
59          case( SearchControls.ONELEVEL_SCOPE ):
60              if ( dn.endsWith( snode.getBaseDn() ) )
61              {
62                  Name candidateDn = parser.parse( dn );
63                  Name scopeDn = parser.parse( snode.getBaseDn() );
64                  return ( scopeDn.size() + 1 ) == candidateDn.size();
65              }
66          case( SearchControls.SUBTREE_SCOPE ):
67              return dn.endsWith( snode.getBaseDn() );
68          default:
69              throw new NamingException( "Unrecognized search scope!" );
70          }
71      }
72  }