You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ak...@apache.org on 2004/08/22 04:18:13 UTC

svn commit: rev 36684 - in incubator/directory/snickers/branches/encoder-redesign: ber-codec/src/java/org/apache/snickers/ber ldap-ber-provider/src/java/org/apache/snickers/ldap/encoder ldap-ber-provider/src/java/org/apache/snickers/ldap/encoder/bind ldap-ber-provider/src/test/org/apache/snickers/ldap/encoder ldap-ber-provider/src/test/org/apache/snickers/ldap/encoder/bind

Author: akarasulu
Date: Sat Aug 21 19:18:12 2004
New Revision: 36684

Added:
   incubator/directory/snickers/branches/encoder-redesign/ber-codec/src/java/org/apache/snickers/ber/DeterminateLengthVisitor.java
   incubator/directory/snickers/branches/encoder-redesign/ldap-ber-provider/src/java/org/apache/snickers/ldap/encoder/bind/BindResponseEncoder.java
   incubator/directory/snickers/branches/encoder-redesign/ldap-ber-provider/src/test/org/apache/snickers/ldap/encoder/EncoderUtilsTest.java
   incubator/directory/snickers/branches/encoder-redesign/ldap-ber-provider/src/test/org/apache/snickers/ldap/encoder/bind/BindResponseEncoderTest.java
Modified:
   incubator/directory/snickers/branches/encoder-redesign/ber-codec/src/java/org/apache/snickers/ber/DefaultMutableTupleNode.java
   incubator/directory/snickers/branches/encoder-redesign/ldap-ber-provider/src/java/org/apache/snickers/ldap/encoder/LdapResultEncoder.java
Log:
Commit changes:

 o corrected accept() method for tuple node impl
 o fixed null pointer on LdapResultEncoder
 o added incomplete vistor to alter indeterminate encodings to determinate form
 o added some test cases although still incomplete
 


Modified: incubator/directory/snickers/branches/encoder-redesign/ber-codec/src/java/org/apache/snickers/ber/DefaultMutableTupleNode.java
==============================================================================
--- incubator/directory/snickers/branches/encoder-redesign/ber-codec/src/java/org/apache/snickers/ber/DefaultMutableTupleNode.java	(original)
+++ incubator/directory/snickers/branches/encoder-redesign/ber-codec/src/java/org/apache/snickers/ber/DefaultMutableTupleNode.java	Sat Aug 21 19:18:12 2004
@@ -496,13 +496,43 @@
 
 
     /**
+     * Recursively descends the tree at this node based on the order of the
+     * visitor.
+     *
      * @see TupleNode#accept(TupleNodeVisitor)
      */
     public void accept( TupleNodeVisitor visitor )
     {
         if ( visitor.canVisit( this ) )
         {
-            visitor.visit( this ) ;
+            if ( visitor.isPrefix() )
+            {
+                ArrayList l_children = visitor.getOrder( this, children ) ;
+
+                if ( visitor.canVisit( this ) )
+                {
+                    visitor.visit( this ) ;
+                }
+
+                for ( int ii = 0; ii < l_children.size(); ii++ )
+                {
+                    ( ( TupleNode ) l_children.get( ii ) ).accept( visitor ) ;
+                }
+            }
+            else
+            {
+                ArrayList l_children = visitor.getOrder( this, children ) ;
+
+                for ( int ii = 0; ii < l_children.size(); ii++ )
+                {
+                    ( ( TupleNode ) l_children.get( ii ) ).accept( visitor ) ;
+                }
+
+                if ( visitor.canVisit( this ) )
+                {
+                    visitor.visit( this ) ;
+                }
+            }
         }
     }
 }

Added: incubator/directory/snickers/branches/encoder-redesign/ber-codec/src/java/org/apache/snickers/ber/DeterminateLengthVisitor.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/branches/encoder-redesign/ber-codec/src/java/org/apache/snickers/ber/DeterminateLengthVisitor.java	Sat Aug 21 19:18:12 2004
@@ -0,0 +1,79 @@
+/*
+ *   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.ber;
+
+
+import java.util.ArrayList;
+
+
+/**
+ * A visitor used to transform a TLV tuple tree by altering tuples to use
+ * determinate length encodings rather than the indeterminate form.
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org"> Apache Directory
+ *         Project</a> $Rev$
+ */
+public class DeterminateLengthVisitor implements TupleNodeVisitor
+{
+    /**
+     * Visits a tree of tuple nodes using a specific visitation order.
+     *
+     * @param node the node to visit
+     */
+    public void visit( TupleNode node )
+    {
+        System.out.println( node );
+    }
+
+
+    /**
+     * Checks to see if a node can be visited.
+     *
+     * @param node the node to be visited
+     * @return whether or node the node should be visited
+     */
+    public boolean canVisit( TupleNode node )
+    {
+        return true;
+    }
+
+
+    /**
+     * Determines whether the visitation order is prefix or postfix.
+     *
+     * @return true if the visitation is in prefix order, false otherwise.
+     */
+    public boolean isPrefix()
+    {
+        return false;
+    }
+
+
+    /**
+     * Get the array of children to visit sequentially to determine the order of
+     * child visitations.  Some children may not be returned at all if
+     * canVisit() returns false on them.
+     *
+     * @param node     the parent branch node
+     * @param children the child node array
+     * @return the new reordered array of children
+     */
+    public ArrayList getOrder( TupleNode node, ArrayList children )
+    {
+        return children;
+    }
+}

Modified: incubator/directory/snickers/branches/encoder-redesign/ldap-ber-provider/src/java/org/apache/snickers/ldap/encoder/LdapResultEncoder.java
==============================================================================
--- incubator/directory/snickers/branches/encoder-redesign/ldap-ber-provider/src/java/org/apache/snickers/ldap/encoder/LdapResultEncoder.java	(original)
+++ incubator/directory/snickers/branches/encoder-redesign/ldap-ber-provider/src/java/org/apache/snickers/ldap/encoder/LdapResultEncoder.java	Sat Aug 21 19:18:12 2004
@@ -49,8 +49,12 @@
                 UniversalTag.ENUMERATED, result.getResultCode().getValue() ) );
         top.addLast( ( DefaultMutableTupleNode ) EncoderUtils.encode(
                 result.getMatchedDn() ) );
-        top.addLast( ( DefaultMutableTupleNode ) EncoderUtils.encode(
-                result.getErrorMessage() ) );
+
+        if ( result.getErrorMessage() != null )
+        {
+            top.addLast( ( DefaultMutableTupleNode ) EncoderUtils.encode(
+                    result.getErrorMessage() ) );
+        }
 
         if ( result.getReferral() != null &&
                 result.getReferral().getLdapUrls().size() > 0 )

Added: incubator/directory/snickers/branches/encoder-redesign/ldap-ber-provider/src/java/org/apache/snickers/ldap/encoder/bind/BindResponseEncoder.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/branches/encoder-redesign/ldap-ber-provider/src/java/org/apache/snickers/ldap/encoder/bind/BindResponseEncoder.java	Sat Aug 21 19:18:12 2004
@@ -0,0 +1,84 @@
+/*
+ *   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.encoder.bind;
+
+
+import org.apache.snickers.ber.Tuple;
+import org.apache.snickers.ber.Length;
+import org.apache.snickers.ldap.LdapTag;
+import org.apache.snickers.ber.TupleNode;
+import org.apache.snickers.ldap.encoder.EncoderUtils;
+import org.apache.snickers.ber.DefaultMutableTupleNode;
+import org.apache.snickers.ber.primitives.UniversalTag;
+import org.apache.snickers.ldap.encoder.LdapResultEncoder;
+
+import org.apache.ldap.common.message.BindResponse;
+
+import org.apache.commons.lang.NotImplementedException;
+
+
+/**
+ * A thread safe BindResponse PDU encoder that produces a TupleNode tree
+ * representing the TLV nesting heirarchy.
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org"> Apache Directory
+ *         Project</a> $Rev$
+ */
+public class BindResponseEncoder
+{
+    /** An instance of this encoder */
+    public static final BindResponseEncoder INSTANCE =
+            new BindResponseEncoder();
+
+
+    /**
+     * Encodes a BindResponse into a TupleTree representing the TLV nesting
+     * heirarchy.
+     *
+     * @param response the BindResponse to encode
+     * @return the root TLV tuple for the encoded BindResponse PDU
+     */
+    public TupleNode encode( BindResponse response )
+    {
+        /// Create the top level BindResponse PDU node
+        DefaultMutableTupleNode top =
+                new DefaultMutableTupleNode( new Tuple() );
+        top.getTuple().setTag( UniversalTag.SEQUENCE_SEQUENCE_OF, false );
+        top.getTuple().setLength( Length.INDEFINATE );
+
+        // Create and add the message id to response PDU
+        top.addLast( ( DefaultMutableTupleNode ) EncoderUtils.encode(
+                response.getMessageId() ) );
+
+        // Create the bind response sequence of TLV tuple
+        DefaultMutableTupleNode bindresp =
+                new DefaultMutableTupleNode( new Tuple() );
+        bindresp.getTuple().setTag( LdapTag.BIND_RESPONSE, false );
+        bindresp.getTuple().setLength( Length.INDEFINATE );
+
+        // Stuff sequence of TLV tuple with the Components of the LDAPResult
+        LdapResultEncoder.INSTANCE.encode( bindresp, response.getLdapResult() );
+
+        // Conditionally add the sasl credentials if this is a SASL bind
+        if ( response.getServerSaslCreds() != null )
+        {
+            throw new NotImplementedException( "SASL binds not implemented" ) ;
+        }
+
+        return top;
+    }
+}

Added: incubator/directory/snickers/branches/encoder-redesign/ldap-ber-provider/src/test/org/apache/snickers/ldap/encoder/EncoderUtilsTest.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/branches/encoder-redesign/ldap-ber-provider/src/test/org/apache/snickers/ldap/encoder/EncoderUtilsTest.java	Sat Aug 21 19:18:12 2004
@@ -0,0 +1,75 @@
+/*
+ *   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.encoder;
+
+
+import junit.framework.TestCase;
+import org.apache.snickers.ber.TupleNode;
+import org.apache.snickers.ber.primitives.UniversalTag;
+import org.apache.snickers.ldap.BufferUtils;
+import org.apache.snickers.ldap.LdapTag;
+import org.apache.commons.lang.ArrayUtils;
+
+
+/**
+ * Tests the EncoderUtils class.
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org"> Apache Directory
+ *         Project</a> $Rev$
+ */
+public class EncoderUtilsTest extends TestCase
+{
+    /**
+     * Tests an overload of the EncoderUtils.encode() method for encoding
+     * Strings.
+     *
+     * @see EncoderUtils#encode(org.apache.snickers.ber.TagEnum, String)
+     */
+    public void testEncodeTagEnumString()
+    {
+        final String str = "hello world";
+        TupleNode node = EncoderUtils.encode( LdapTag.CONTEXT_SPECIFIC_TAG_7,
+                 str );
+        assertEquals( node.getTuple().getRawPrimitiveTag(),
+                LdapTag.CONTEXT_SPECIFIC_TAG_7.getPrimitiveTag() );
+        assertEquals( node.getTuple().getLength(), str.getBytes().length );
+        assertTrue( node.getTuple().isPrimitive() );
+        byte[] bites = BufferUtils.getArray(
+                node.getTuple().getLastValueChunk() );
+        assertTrue( ArrayUtils.isEquals( str.getBytes(), bites ) );
+    }
+
+
+    /**
+     * Tests an overload of the EncoderUtils.encode() method for encoding
+     * Strings.
+     *
+     * @see EncoderUtils#encode(String)
+     */
+    public void testEncodeString()
+    {
+        final String str = "hello world";
+        TupleNode node = EncoderUtils.encode( str );
+        assertEquals( node.getTuple().getRawTag(),
+                UniversalTag.OCTET_STRING.getPrimitiveTag() );
+        assertEquals( node.getTuple().getLength(), str.getBytes().length );
+        assertTrue( node.getTuple().isPrimitive() );
+        byte[] bites = BufferUtils.getArray(
+                node.getTuple().getLastValueChunk() );
+        assertTrue( ArrayUtils.isEquals( str.getBytes(), bites ) );
+    }
+}

Added: incubator/directory/snickers/branches/encoder-redesign/ldap-ber-provider/src/test/org/apache/snickers/ldap/encoder/bind/BindResponseEncoderTest.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/branches/encoder-redesign/ldap-ber-provider/src/test/org/apache/snickers/ldap/encoder/bind/BindResponseEncoderTest.java	Sat Aug 21 19:18:12 2004
@@ -0,0 +1,48 @@
+/*
+ *   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.encoder.bind;
+
+import junit.framework.TestCase;
+import org.apache.ldap.common.message.BindResponseImpl;
+import org.apache.ldap.common.message.LdapResultImpl;
+import org.apache.ldap.common.message.ResultCodeEnum;
+import org.apache.snickers.ber.TupleNode;
+import org.apache.snickers.ber.DeterminateLengthVisitor;
+
+
+/**
+ * Tests the BindResponse encoder.
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org"> Apache Directory
+ *         Project</a> $Rev$
+ */
+public class BindResponseEncoderTest extends TestCase
+{
+
+
+    public void testEncode()
+    {
+        BindResponseImpl response = new BindResponseImpl( 5 );
+        LdapResultImpl result = new LdapResultImpl( response );
+        response.setLdapResult( result );
+        result.setMatchedDn( "dc=example,dc=com" );
+        result.setResultCode( ResultCodeEnum.SUCCESS );
+        TupleNode node = BindResponseEncoder.INSTANCE.encode( response );
+        DeterminateLengthVisitor visitor = new DeterminateLengthVisitor();
+        node.accept( visitor );
+    }
+}