directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akaras...@apache.org
Subject svn commit: rev 20745 - in incubator/directory/snickers/trunk/ldap-ber-provider/src: java/org/apache/snickers/ldap test/org/apache/snickers/ldap
Date Wed, 02 Jun 2004 00:41:43 GMT
Author: akarasulu
Date: Tue Jun  1 17:41:42 2004
New Revision: 20745

Added:
   incubator/directory/snickers/trunk/ldap-ber-provider/src/java/org/apache/snickers/ldap/ModificationItemRule.java
  (contents, props changed)
   incubator/directory/snickers/trunk/ldap-ber-provider/src/java/org/apache/snickers/ldap/ModifyRequestAttributeValueRule.java
  (contents, props changed)
   incubator/directory/snickers/trunk/ldap-ber-provider/src/java/org/apache/snickers/ldap/ModifyRequestEntryDnRule.java
  (contents, props changed)
   incubator/directory/snickers/trunk/ldap-ber-provider/src/java/org/apache/snickers/ldap/ModifyRequestRule.java
  (contents, props changed)
   incubator/directory/snickers/trunk/ldap-ber-provider/src/test/org/apache/snickers/ldap/ModifyRequestTest.java
  (contents, props changed)
Modified:
   incubator/directory/snickers/trunk/ldap-ber-provider/src/java/org/apache/snickers/ldap/LdapDigesterFactory.java
Log:
Added ModifyRequest processing rules to digester.


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
Tue Jun  1 17:41:42 2004
@@ -94,12 +94,62 @@
         addExtendedResponseRules( digester ) ;
         addModifyDnRequestRules( digester ) ;
         addModifyDnResponseRules( digester ) ;
+        addModifyRequestRules( digester ) ;
         addModifyResponseRules( digester ) ;
         addSearchResponseDoneRules( digester ) ;
         addSearchResponseReferenceRules( digester ) ;
         addSearchResponseEntryRules( digester ) ;
 
         return digester ;
+    }
+
+
+    private void addModifyRequestRules( BERDigester digester )
+    {
+        int[] pattern = new int[2] ;
+
+        // set pattern and addRule for the AddRequest
+        pattern[0] = UniversalTag.SEQUENCE_SEQUENCE_OF.getPrimitiveTag() ;
+        pattern[1] = LdapTag.MODIFY_REQUEST.getPrimitiveTag() ;
+        digester.addRule( pattern, new ModifyRequestRule() ) ;
+
+        // setup pattern to set the dn of the entry added
+        pattern = new int[3] ;
+        pattern[0] = UniversalTag.SEQUENCE_SEQUENCE_OF.getPrimitiveTag() ;
+        pattern[1] = LdapTag.MODIFY_REQUEST.getPrimitiveTag() ;
+        pattern[2] = UniversalTag.OCTET_STRING.getPrimitiveTag() ;
+        digester.addRule( pattern, new ModifyRequestEntryDnRule() ) ;
+
+        pattern = new int[5] ;
+        pattern[0] = UniversalTag.SEQUENCE_SEQUENCE_OF.getPrimitiveTag() ;
+        pattern[1] = LdapTag.MODIFY_REQUEST.getPrimitiveTag() ;
+        pattern[2] = UniversalTag.SEQUENCE_SEQUENCE_OF.getPrimitiveTag() ;
+        pattern[3] = UniversalTag.SEQUENCE_SEQUENCE_OF.getPrimitiveTag() ;
+        pattern[4] = UniversalTag.ENUMERATED.getPrimitiveTag() ;
+        digester.addRule( pattern,
+                new PrimitiveIntDecodeRule( UniversalTag.ENUMERATED ) ) ;
+
+        pattern[4] = UniversalTag.SEQUENCE_SEQUENCE_OF.getPrimitiveTag() ;
+        digester.addRule( pattern, new PopOnFinish() ) ;
+
+        pattern = new int[6] ;
+        pattern[0] = UniversalTag.SEQUENCE_SEQUENCE_OF.getPrimitiveTag() ;
+        pattern[1] = LdapTag.MODIFY_REQUEST.getPrimitiveTag() ;
+        pattern[2] = UniversalTag.SEQUENCE_SEQUENCE_OF.getPrimitiveTag() ;
+        pattern[3] = UniversalTag.SEQUENCE_SEQUENCE_OF.getPrimitiveTag() ;
+        pattern[4] = UniversalTag.SEQUENCE_SEQUENCE_OF.getPrimitiveTag() ;
+        pattern[5] = UniversalTag.OCTET_STRING.getPrimitiveTag() ;
+        digester.addRule( pattern, new ModificationItemRule() ) ;
+
+        pattern = new int[7] ;
+        pattern[0] = UniversalTag.SEQUENCE_SEQUENCE_OF.getPrimitiveTag() ;
+        pattern[1] = LdapTag.MODIFY_REQUEST.getPrimitiveTag() ;
+        pattern[2] = UniversalTag.SEQUENCE_SEQUENCE_OF.getPrimitiveTag() ;
+        pattern[3] = UniversalTag.SEQUENCE_SEQUENCE_OF.getPrimitiveTag() ;
+        pattern[4] = UniversalTag.SEQUENCE_SEQUENCE_OF.getPrimitiveTag() ;
+        pattern[5] = UniversalTag.SET_SET_OF.getPrimitiveTag() ;
+        pattern[6] = UniversalTag.OCTET_STRING.getPrimitiveTag() ;
+        digester.addRule( pattern, new ModifyRequestAttributeValueRule() ) ;
     }
 
 

Added: incubator/directory/snickers/trunk/ldap-ber-provider/src/java/org/apache/snickers/ldap/ModificationItemRule.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/trunk/ldap-ber-provider/src/java/org/apache/snickers/ldap/ModificationItemRule.java
Tue Jun  1 17:41:42 2004
@@ -0,0 +1,113 @@
+/*
+ *   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 ;
+
+
+import java.nio.ByteBuffer ;
+import javax.naming.directory.ModificationItem ;
+import javax.naming.directory.DirContext;
+
+import org.apache.ldap.common.message.LockableAttributeImpl ;
+import org.apache.ldap.common.message.ModifyRequest;
+
+import org.apache.snickers.ber.primitives.UniversalTag ;
+import org.apache.snickers.ber.digester.rules.PrimitiveOctetStringRule ;
+
+
+/**
+ * A BERDigester rule to create and push ModifyRequest's attribute.
+ * 
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory
+ * Project</a>
+ * @version $Rev$
+ */
+public class ModificationItemRule extends PrimitiveOctetStringRule
+{
+    public ModificationItemRule()
+    {
+        super( UniversalTag.OCTET_STRING ) ;
+    }
+
+
+    /**
+     * Allows the super method to push a ByteBuffer onto the top of the stack
+     * which contains the drained contents of the superclass' ByteAccumulator.
+     * This ByteBuffer is popped first then used to populate the credentials.
+     * There is no need to copy this buffer since it will not be used again
+     * by the ByteAccumulator of the superclass so we should be able to use
+     * the byte[] based backing store if one is present.  However it might
+     * have to be copied even then.  Situations requiring a copy are when the
+     * buffer has a limit less than the capacity or when there is no
+     * accessible array to the buffer.
+     *
+     * @see org.apache.snickers.ber.digester.Rule#finish()
+     */
+    public void finish()
+    {
+        if ( isConstructed() )
+        {
+            return ;
+        }
+
+        // drain the accumulator
+        ByteBuffer buf = ( ByteBuffer ) getAccumulator().drain( 0 ) ;
+
+        byte[] octets = null ;
+        if ( buf.limit() == buf.capacity() && buf.hasArray() )
+        {
+            // use the backing store
+            octets = buf.array() ;
+        }
+        else
+        {
+            // copy because we don't have accessible array or data < array
+            octets = new byte[buf.remaining()] ;
+            buf.get( octets ) ;
+        }
+
+
+        int op = getDigester().popInt() ;
+        int modCode = -1 ;
+
+        switch( op )
+        {
+            case( 0 ):
+                modCode = DirContext.ADD_ATTRIBUTE ;
+                break ;
+            case( 1 ):
+                modCode = DirContext.REMOVE_ATTRIBUTE ;
+                break ;
+            case( 2 ):
+                modCode = DirContext.REPLACE_ATTRIBUTE ;
+                break ;
+            default:
+                throw new IllegalStateException(
+                    "Expecting 0, 1, 2 int value for add, delete, or replace"
+                    + " operation on entry attribute but got a " + op ) ;
+        }
+
+        LockableAttributeImpl attr =
+                new LockableAttributeImpl( new String( octets ) ) ;
+        ModifyRequest req = ( ModifyRequest ) getDigester().peek() ;
+        ModificationItem mods = new ModificationItem( modCode, attr ) ;
+        req.addModification( mods ) ;
+        getDigester().push( mods ) ;
+
+        // clean up
+        setConstructed( false ) ;
+    }
+}

Added: incubator/directory/snickers/trunk/ldap-ber-provider/src/java/org/apache/snickers/ldap/ModifyRequestAttributeValueRule.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/trunk/ldap-ber-provider/src/java/org/apache/snickers/ldap/ModifyRequestAttributeValueRule.java
Tue Jun  1 17:41:42 2004
@@ -0,0 +1,87 @@
+/*
+ *   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 ;
+
+
+import java.nio.ByteBuffer ;
+import javax.naming.directory.ModificationItem ;
+
+import org.apache.snickers.ber.primitives.UniversalTag ;
+import org.apache.snickers.ber.digester.rules.PrimitiveOctetStringRule ;
+
+
+/**
+ * A BERDigester rule to set the vales of the ModifyRequest's attribute.
+ * 
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory
+ * Project</a>
+ * @version $Rev$
+ */
+public class ModifyRequestAttributeValueRule extends PrimitiveOctetStringRule
+{
+    public ModifyRequestAttributeValueRule()
+    {
+        super( UniversalTag.OCTET_STRING ) ;
+    }
+
+    
+    /**
+     * Allows the super method to push a ByteBuffer onto the top of the stack
+     * which contains the drained contents of the superclass' ByteAccumulator.
+     * This ByteBuffer is popped first then used to populate the credentials.
+     * There is no need to copy this buffer since it will not be used again
+     * by the ByteAccumulator of the superclass so we should be able to use
+     * the byte[] based backing store if one is present.  However it might
+     * have to be copied even then.  Situations requiring a copy are when the
+     * buffer has a limit less than the capacity or when there is no
+     * accessible array to the buffer.
+     *
+     * @see org.apache.snickers.ber.digester.Rule#finish()
+     */
+    public void finish()
+    {
+        if ( isConstructed() )
+        {
+            return ;
+        }
+
+        // drain the accumulator
+        ByteBuffer buf = ( ByteBuffer ) getAccumulator().drain( 0 ) ;
+
+        // peek at the ModificationItem object underneath which we add values to
+        ModificationItem mods = ( ModificationItem ) getDigester().peek() ;
+
+        byte[] octets = null ;
+        if ( buf.limit() == buf.capacity() && buf.hasArray() )
+        {
+            // use the backing store
+            octets = buf.array() ;
+        }
+        else
+        {
+            // copy because we don't have accessible array or data < array
+            octets = new byte[buf.remaining()] ;
+            buf.get( octets ) ;
+        }
+
+        // Add the attribute
+        mods.getAttribute().add( new String( octets ) ) ;
+
+        // clean up
+        setConstructed( false ) ;
+    }
+}

Added: incubator/directory/snickers/trunk/ldap-ber-provider/src/java/org/apache/snickers/ldap/ModifyRequestEntryDnRule.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/trunk/ldap-ber-provider/src/java/org/apache/snickers/ldap/ModifyRequestEntryDnRule.java
Tue Jun  1 17:41:42 2004
@@ -0,0 +1,81 @@
+/*
+ *   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 ;
+
+
+import java.nio.ByteBuffer ;
+
+import org.apache.ldap.common.message.ModifyRequest ;
+
+import org.apache.snickers.ber.primitives.UniversalTag ;
+import org.apache.snickers.ber.digester.rules.PrimitiveOctetStringRule ;
+
+
+/**
+ * A BERDigester rule to set the dn of the entry changed with a ModifyRequest.
+ * 
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory
+ * Project</a>
+ * @version $Rev$
+ */
+public class ModifyRequestEntryDnRule extends PrimitiveOctetStringRule
+{
+    public ModifyRequestEntryDnRule()
+    {
+        super( UniversalTag.OCTET_STRING ) ;
+    }
+
+    
+    /**
+     * Allows the super method to push a ByteBuffer onto the top of the stack
+     * which contains the drained contents of the superclass' ByteAccumulator.
+     * This ByteBuffer is popped first then used to populate the credentials.
+     * There is no need to copy this buffer since it will not be used again
+     * by the ByteAccumulator of the superclass so we should be able to use
+     * the byte[] based backing store if one is present.  However it might
+     * have to be copied even then.  Situations requiring a copy are when the
+     * buffer has a limit less than the capacity or when there is no
+     * accessible array to the buffer.
+     *
+     * @see org.apache.snickers.ber.digester.Rule#finish()
+     */
+    public void finish()
+    {
+        // pushes a ByteBuffer onto the stack
+        super.finish() ;
+
+        // pop the ByteBuffer the super method pushed
+        ByteBuffer buf = ( ByteBuffer ) getDigester().pop() ;
+        // peek at the ModifyRequest underneath whose octets we set
+        ModifyRequest req = ( ModifyRequest ) getDigester().peek() ;
+
+        byte[] octets = null ;
+        if ( buf.limit() == buf.capacity() && buf.hasArray() )
+        {
+            // use the backing store
+            octets = buf.array() ;
+        }
+        else
+        {
+            // copy because we don't have accessible array or data < array
+            octets = new byte[buf.remaining()] ;
+            buf.get( octets ) ;
+        }
+
+        req.setName( new String( octets ) ) ;
+    }
+}

Added: incubator/directory/snickers/trunk/ldap-ber-provider/src/java/org/apache/snickers/ldap/ModifyRequestRule.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/trunk/ldap-ber-provider/src/java/org/apache/snickers/ldap/ModifyRequestRule.java
Tue Jun  1 17:41:42 2004
@@ -0,0 +1,63 @@
+/*
+ *   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 ;
+
+
+import org.apache.snickers.ber.digester.AbstractRule ;
+import org.apache.snickers.ber.TypeClass;
+import org.apache.ldap.common.message.ModifyRequestImpl;
+
+
+/**
+ * Document this class.
+ * 
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory
+ *         Project</a>
+ * @version $Rev$
+ */
+public class ModifyRequestRule extends AbstractRule
+{
+    /* (non-Javadoc)
+     * @see org.apache.snickers.ber.digester.Rule#tag(int, boolean,
+     * org.apache.snickers.ber.TypeClass)
+     */
+    public void tag( int id, boolean isPrimitive, TypeClass typeClass )
+    {
+        super.tag( id, isPrimitive, typeClass ) ;
+
+        LdapTag tag = LdapTag.getLdapTagById( id ) ;
+
+        if ( LdapTag.MODIFY_REQUEST != tag )
+        {
+            throw new IllegalArgumentException(
+                    "Expected a MODIFY_REQUEST tag id but got a " + tag ) ;
+        }
+
+        Object req = new ModifyRequestImpl( getDigester().popInt() ) ;
+        getDigester().push( req ) ;
+    }
+
+
+    /* (non-Javadoc)
+     * @see org.apache.snickers.ber.digester.Rule#finish()
+     */
+    public void finish()
+    {
+        super.finish() ;
+        getDigester().pop() ;
+    }
+}

Added: incubator/directory/snickers/trunk/ldap-ber-provider/src/test/org/apache/snickers/ldap/ModifyRequestTest.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/trunk/ldap-ber-provider/src/test/org/apache/snickers/ldap/ModifyRequestTest.java
Tue Jun  1 17:41:42 2004
@@ -0,0 +1,126 @@
+/*
+ *   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 ;
+
+
+import java.util.HashSet ;
+import java.util.Iterator ;
+import java.util.Collection ;
+import javax.naming.directory.Attribute ;
+import javax.naming.directory.DirContext ;
+import javax.naming.directory.ModificationItem ;
+
+import org.apache.ldap.common.message.* ;
+
+import org.apache.snickers.ldap.testutils.TestUtils ;
+import org.apache.snickers.ldap.testutils.RuleTestCase ;
+
+
+/**
+ * Tests the capability to end to end decode a ModifyRequest.
+ * 
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory
+ * Project</a>
+ * @version $Rev$
+ */
+public class ModifyRequestTest extends RuleTestCase
+{
+    /**
+     * Tests an modify request decode.
+     */
+    public void testModifyRequest() throws Exception
+    {
+        ModifyRequestImpl req = new ModifyRequestImpl( 33 ) ;
+        req.setName( "dc=example,dc=com" ) ;
+
+        Attribute attr = new LockableAttributeImpl( "dc" ) ;
+        attr.add( "opensoftware" ) ;
+        ModificationItem mod = new ModificationItem(
+                DirContext.REPLACE_ATTRIBUTE, attr ) ;
+        req.addModification( mod ) ;
+
+        attr = new LockableAttributeImpl( "bogus" ) ;
+        attr.add( "somevalue-to-remove" ) ;
+        mod = new ModificationItem(
+                DirContext.REMOVE_ATTRIBUTE, attr ) ;
+        req.addModification( mod ) ;
+
+        attr = new LockableAttributeImpl( "objectClass" ) ;
+        attr.add( "organizationalUnit" ) ;
+        mod = new ModificationItem( DirContext.ADD_ATTRIBUTE, attr ) ;
+        req.addModification( mod ) ;
+
+        attr = new LockableAttributeImpl( "ou" ) ;
+        attr.add( "OSS Site" ) ;
+        attr.add( "Main Site" ) ;
+        mod = new ModificationItem( DirContext.ADD_ATTRIBUTE, attr ) ;
+        req.addModification( mod ) ;
+
+        System.out.println( "Generated ModifyRequest for test:" ) ;
+        System.out.println( TestUtils.printTupleTree( req ) ) ;
+
+        ModifyRequest decoded = ( ModifyRequest )
+                snickersDecode( snaccEncode( req ) ) ;
+        assertNotNull( decoded ) ;
+        assertEquals( req.getName(), decoded.getName() ) ;
+
+        Collection items = decoded.getModificationItems() ;
+        assertEquals( 4, items.size() ) ;
+        Iterator list = items.iterator() ;
+        while( list.hasNext() )
+        {
+            ModificationItem item = ( ModificationItem ) list.next() ;
+
+            if ( item.getAttribute().getID().equalsIgnoreCase( "dc" ) )
+            {
+                assertEquals( DirContext.REPLACE_ATTRIBUTE,
+                        item.getModificationOp() ) ;
+                assertEquals( "opensoftware", item.getAttribute().get() ) ;
+            }
+
+            if ( item.getAttribute().getID().equalsIgnoreCase( "bogus" ) )
+            {
+                assertEquals( DirContext.REMOVE_ATTRIBUTE,
+                        item.getModificationOp() ) ;
+                assertEquals( "somevalue-to-remove",
+                        item.getAttribute().get() ) ;
+            }
+
+            if ( item.getAttribute().getID().equalsIgnoreCase( "objectClass" ) )
+            {
+                assertEquals( DirContext.ADD_ATTRIBUTE,
+                        item.getModificationOp() ) ;
+                assertEquals( "organizationalUnit",
+                        item.getAttribute().get() ) ;
+            }
+
+            if ( item.getAttribute().getID().equalsIgnoreCase( "ou" ) )
+            {
+                assertEquals( DirContext.ADD_ATTRIBUTE,
+                        item.getModificationOp() ) ;
+
+                HashSet values = new HashSet() ;
+                for( int ii = 0; ii < item.getAttribute().size(); ii++ )
+                {
+                    values.add( item.getAttribute().get(ii) ) ;
+                }
+                assertTrue( values.contains( "OSS Site" ) ) ;
+                assertTrue( values.contains( "Main Site" ) ) ;
+            }
+        }
+    }
+}
\ No newline at end of file

Mime
View raw message