directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akaras...@apache.org
Subject svn commit: rev 21187 - in incubator/directory/snickers/trunk/ldap-ber-provider/src: java/org/apache/snickers/ldap java/org/apache/snickers/ldap/search test/org/apache/snickers/ldap/search
Date Mon, 14 Jun 2004 04:41:12 GMT
Author: akarasulu
Date: Sun Jun 13 21:41:11 2004
New Revision: 21187

Added:
   incubator/directory/snickers/trunk/ldap-ber-provider/src/java/org/apache/snickers/ldap/search/NotRule.java
  (contents, props changed)
Modified:
   incubator/directory/snickers/trunk/ldap-ber-provider/src/java/org/apache/snickers/ldap/LdapDigesterFactory.java
   incubator/directory/snickers/trunk/ldap-ber-provider/src/test/org/apache/snickers/ldap/search/SearchRequestTest.java
Log:
Added rules to handle NOT filter expression creation


Modified: incubator/directory/snickers/trunk/ldap-ber-provider/src/java/org/apache/snickers/ldap/LdapDigesterFactory.java
==============================================================================
--- incubator/directory/snickers/trunk/ldap-ber-provider/src/java/org/apache/snickers/ldap/LdapDigesterFactory.java
(original)
+++ incubator/directory/snickers/trunk/ldap-ber-provider/src/java/org/apache/snickers/ldap/LdapDigesterFactory.java
Sun Jun 13 21:41:11 2004
@@ -229,6 +229,16 @@
 
         pattern[2] = LdapTag.CONTEXT_SPECIFIC_TAG_4.getPrimitiveTag();
         digester.addRule( pattern, new ExtensibleMatchDnAttributesRule() );
+
+        //
+        // End Extensible match filter rules
+        //
+
+        // NOT filter expresion rules
+        pattern = new int[2];
+        pattern[0] = TagTree.WILDCARD;
+        pattern[1] = LdapTag.CONTEXT_SPECIFIC_TAG_2.getPrimitiveTag();
+        digester.addRule( pattern, new NotRule() );
     }
 
 

Added: incubator/directory/snickers/trunk/ldap-ber-provider/src/java/org/apache/snickers/ldap/search/NotRule.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/trunk/ldap-ber-provider/src/java/org/apache/snickers/ldap/search/NotRule.java
Sun Jun 13 21:41:11 2004
@@ -0,0 +1,137 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.snickers.ldap.search;
+
+
+import java.nio.ByteBuffer;
+
+import org.apache.snickers.ber.digester.AbstractRule;
+import org.apache.snickers.ber.TypeClass;
+import org.apache.snickers.ldap.LdapTag;
+import org.apache.ldap.common.filter.BranchNode;
+import org.apache.ldap.common.filter.ExprNode;
+
+
+/**
+ * A rule used to build filter expressions that are negated using the '!'
+ * NOT operator.  This rule pops the stack for its sole argument which it
+ * checks to see is a filter expression tree node of the type ExprNode.  It
+ * uses that expression to construct a negated expression with it, and pushs
+ * that node onto the stack.  This rule also makes sure that it is enabled only
+ * under a search request.
+ * 
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory
+ *         Project</a>
+ * @version $Rev$
+ */
+public class NotRule extends AbstractRule
+{
+    private boolean isEnabled = true;
+
+
+    /* (non-Javadoc)
+     * @see org.apache.snickers.ber.Rule#tag(int, boolean,
+     * org.apache.snickers.ber.TypeClass)
+     */
+    public void tag( int id, boolean isPrimitive, TypeClass typeClass )
+    {
+        // check to see we are within limits - have the right number of tags
+        int tagCount = getDigester().getTagCount();
+        if ( tagCount < 3 )
+        {
+            this.isEnabled = false;
+            return;
+        }
+
+        /*
+         * check to see that we're dealing within a search request - this is
+         * done by making sure the tag right above the bottom tag is equal
+         * to the SEARCH_REQUEST tag. If not we must disable this rule.
+         */
+        if ( getDigester().getTag( tagCount - 2 ) !=
+                LdapTag.SEARCH_REQUEST.getPrimitiveTag() )
+        {
+            this.isEnabled = false;
+            return;
+        }
+
+        /*
+         * This rule is registered using [*, 0x82000000] which matches all
+         * context sensitive 2 tags in the search request.  This makes it
+         * cross react with extensible match expression and substring match
+         * expression components.  We need to check if the next tags in stack
+         * are equal to extensible match or substring match context specific
+         * tags to see if we need to disable this rule.
+         */
+        if
+        (
+            getDigester().getTag( 2 ) ==
+                LdapTag.CONTEXT_SPECIFIC_TAG_4.getPrimitiveTag() ||
+            getDigester().getTag( 1 ) ==
+                LdapTag.CONTEXT_SPECIFIC_TAG_9.getPrimitiveTag()
+        )
+        {
+            this.isEnabled = false ;
+            return ;
+        }
+
+        super.tag( id, isPrimitive, typeClass );
+    }
+
+
+    /* (non-Javadoc)
+     * @see org.apache.snickers.ber.Rule#length(int)
+     */
+    public void length( int length )
+    {
+        if ( isEnabled )
+        {
+            super.length( length );
+        }
+    }
+
+
+    /* (non-Javadoc)
+     * @see org.apache.snickers.ber.Rule#value(java.nio.ByteBuffer)
+     */
+    public void value( ByteBuffer buf )
+    {
+        if ( isEnabled )
+        {
+            super.value( buf );
+        }
+    }
+
+
+    /* (non-Javadoc)
+     * @see org.apache.snickers.ber.Rule#finish()
+     */
+    public void finish()
+    {
+        if ( isEnabled )
+        {
+            super.finish();
+
+            BranchNode node;
+            node = new BranchNode( BranchNode.NOT );
+            node.addNode( ( ExprNode ) getDigester().pop() );
+            getDigester().push( node );
+        }
+
+        this.isEnabled = true;
+    }
+}

Modified: incubator/directory/snickers/trunk/ldap-ber-provider/src/test/org/apache/snickers/ldap/search/SearchRequestTest.java
==============================================================================
--- incubator/directory/snickers/trunk/ldap-ber-provider/src/test/org/apache/snickers/ldap/search/SearchRequestTest.java
(original)
+++ incubator/directory/snickers/trunk/ldap-ber-provider/src/test/org/apache/snickers/ldap/search/SearchRequestTest.java
Sun Jun 13 21:41:11 2004
@@ -455,4 +455,65 @@
         assertEquals(buf0.toString(), buf1.toString()) ;
         System.out.println( buf1.toString() );
     }
+
+
+    /**
+     * Tests an search request decode with a simple equality match filter.
+     */
+    public void testNotFilter() throws Exception
+    {
+        SearchRequestImpl req = new SearchRequestImpl( 33 );
+        req.setBase( "dc=example,dc=com" );
+        req.setDerefAliases( DerefAliasesEnum.DEREFFINDINGBASEOBJ );
+        req.setScope( ScopeEnum.BASEOBJECT );
+        req.setSizeLimit( 2 );
+        req.setTimeLimit( 3 );
+        req.setTypesOnly( true );
+
+        req.addAttribute( "attr0" );
+        req.addAttribute( "attr1" );
+        req.addAttribute( "attr2" );
+
+
+        FilterParserImpl parser = new FilterParserImpl();
+        ExprNode node = null ;
+        node = parser.parse(
+                "( ! ( ou = Human Resources ) )" ) ;
+        req.setFilter( node );
+
+        System.out.println( "Generated SearchRequest for test:" );
+        System.out.println( TestUtils.printTupleTree( req ) );
+
+        SearchRequest decoded = ( SearchRequest )
+                snickersDecode( snaccEncode( req ) );
+        assertNotNull( decoded );
+
+        // test that we have all the properties set
+        assertEquals( req.getBase(), decoded.getBase() );
+        assertEquals( req.getScope(), decoded.getScope() );
+        assertEquals( req.getTypesOnly(), decoded.getTypesOnly() );
+        assertEquals( req.getTimeLimit(), decoded.getTimeLimit() );
+        assertEquals( req.getSizeLimit(), decoded.getSizeLimit() );
+        assertEquals( req.getDerefAliases(), decoded.getDerefAliases() );
+
+        // test that we have all the attributes
+        Iterator list = req.getAttributes().iterator();
+        Collection attributes = decoded.getAttributes();
+        while( list.hasNext() )
+        {
+            assertTrue( attributes.contains( list.next() ) );
+        }
+
+        // control test should not exist
+        assertFalse( attributes.contains( "(*&#$&#$*@#" ) );
+
+        // filter tests
+        node = req.getFilter();
+        StringBuffer buf0 = new StringBuffer();
+        node.printToBuffer( buf0 );
+        node = decoded.getFilter();
+        StringBuffer buf1 = new StringBuffer();
+        node.printToBuffer( buf1 );
+        assertEquals(buf0.toString(), buf1.toString()) ;
+    }
 }

Mime
View raw message