directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r441416 - /directory/sandbox/akarasulu/apacheds-2.0/shared/ldap/src/main/java/org/apache/directory/shared/ldap/codec/asn1ber/messages/MessageAsn1Ber.java
Date Fri, 08 Sep 2006 07:34:13 GMT
Author: elecharny
Date: Fri Sep  8 00:34:12 2006
New Revision: 441416

URL: http://svn.apache.org/viewvc?view=rev&rev=441416
Log:
Added the skeleton of the Message Asn1Ber decorator implementation

Added:
    directory/sandbox/akarasulu/apacheds-2.0/shared/ldap/src/main/java/org/apache/directory/shared/ldap/codec/asn1ber/messages/MessageAsn1Ber.java

Added: directory/sandbox/akarasulu/apacheds-2.0/shared/ldap/src/main/java/org/apache/directory/shared/ldap/codec/asn1ber/messages/MessageAsn1Ber.java
URL: http://svn.apache.org/viewvc/directory/sandbox/akarasulu/apacheds-2.0/shared/ldap/src/main/java/org/apache/directory/shared/ldap/codec/asn1ber/messages/MessageAsn1Ber.java?view=auto&rev=441416
==============================================================================
--- directory/sandbox/akarasulu/apacheds-2.0/shared/ldap/src/main/java/org/apache/directory/shared/ldap/codec/asn1ber/messages/MessageAsn1Ber.java
(added)
+++ directory/sandbox/akarasulu/apacheds-2.0/shared/ldap/src/main/java/org/apache/directory/shared/ldap/codec/asn1ber/messages/MessageAsn1Ber.java
Fri Sep  8 00:34:12 2006
@@ -0,0 +1,168 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.directory.shared.ldap.codec.asn1ber.messages;
+
+import java.nio.ByteBuffer;
+import java.util.Iterator;
+
+import javax.naming.ldap.Control;
+
+import org.apache.directory.shared.asn1.ber.tlv.Length;
+import org.apache.directory.shared.asn1.ber.tlv.Value;
+import org.apache.directory.shared.asn1.ber.tlv.ValueException;
+import org.apache.directory.shared.ldap.codec.Decoder;
+import org.apache.directory.shared.ldap.codec.Encoder;
+import org.apache.directory.shared.ldap.codec.EncoderException;
+import org.apache.directory.shared.ldap.codec.asn1ber.messages.bind.BindRequestAsn1Ber;
+import org.apache.directory.shared.ldap.messages.ControlDecorator;
+import org.apache.directory.shared.ldap.messages.Message;
+import org.apache.directory.shared.ldap.messages.MessageDecorator;
+import org.apache.directory.shared.ldap.messages.bind.BindRequestDecorator;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * 
+ * This class is a ASN.1 BER codec decorator. It adds coding and decoding
+ * behavior to the Message object. 
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class MessageAsn1Ber extends MessageDecorator implements Encoder, Decoder
+{
+    /** The logger */
+    private static Logger log = LoggerFactory.getLogger( MessageAsn1Ber.class );
+
+    /** A speedup for logger */
+    private static final boolean IS_DEBUG = log.isDebugEnabled();
+
+    /** The LdapMessage length */
+    private transient int ldapMessageLength;
+    
+    /** The controls sequence length */
+    private transient int controlsSequenceLength;
+
+    /** The controls length */
+    private transient int controlsLength;
+
+    /**
+     * 
+     * Creates a new instance of BindRequestAsn1Ber.
+     *
+     * @param bindRequest The bindRequest object to decorate
+     */
+    public MessageAsn1Ber( Message message )
+    {
+        super( message );
+        
+        
+    }
+
+    /**
+     * Compute the LdapMessage length
+     * 
+     * LdapMessage :
+     * 
+     * 0x30 L1
+     *  |
+     *  +--> 0x02 0x0(1-4) [0..2^31-1] (MessageId)
+     *  +--> protocolOp
+     * [+--> Controls]
+     * 
+     * MessageId length = Length(0x02) + length(MessageId) + MessageId.length
+     * L1 = length(ProtocolOp)
+     * LdapMessage length = Length(0x30) + Length(L1) + MessageId length + L1
+     */
+    public int computeLength()
+    {
+        // The length of the MessageId. It's the sum of
+        // - the tag (0x02), 1 byte
+        // - the length of the Id length, 1 byte
+        // - the Id length, 1 to 4 bytes
+        ldapMessageLength = 1 + 1 + Value.getNbBytes( getMessageId() );
+
+        // Get the protocolOp length
+        int protocolOpLength = this.computeLength();
+
+        // Add the protocol length to the message length
+        ldapMessageLength += protocolOpLength;
+
+        // Do the same thing for Controls, if any.
+        if ( getControls() != null )
+        {
+            // Controls :
+            // 0xA0 L3
+            // |
+            // +--> 0x30 L4
+            // +--> 0x30 L5
+            // +--> ...
+            // +--> 0x30 Li
+            // +--> ...
+            // +--> 0x30 Ln
+            //
+            // L3 = Length(0x30) + Length(L5) + L5
+            // + Length(0x30) + Length(L6) + L6
+            // + ...
+            // + Length(0x30) + Length(Li) + Li
+            // + ...
+            // + Length(0x30) + Length(Ln) + Ln
+            //
+            // LdapMessageLength = LdapMessageLength + Length(0x90)
+            // + Length(L3) + L3
+            controlsSequenceLength = 0;
+
+            Iterator<Control> controlIterator = getControls().values().iterator();
+
+            // We may have more than one control. ControlsLength is L4.
+            while ( controlIterator.hasNext() )
+            {
+                Control control = ( ( Control ) controlIterator.next() );
+//                ControlDecorator controlDecorator = new ControlAsn1Ber( control );
+//                controlsSequenceLength += controlDecorator.computeLength();
+            }
+
+            // Computes the controls length
+            controlsLength = controlsSequenceLength; // 1 +
+                                                        // Length.getNbBytes(
+                                                        // controlsSequenceLength
+                                                        // ) +
+                                                        // controlsSequenceLength;
+
+            // Now, add the tag and the length of the controls length
+            ldapMessageLength += 1 + Length.getNbBytes( controlsSequenceLength ) + controlsSequenceLength;
+        }
+
+        // finally, calculate the global message size :
+        // length(Tag) + Length(length) + length
+        int length = 1 + ldapMessageLength + Length.getNbBytes( ldapMessageLength );
+
+        if ( IS_DEBUG )
+        {
+            log.debug( "Message length : {}", length );
+        }
+        
+        return length;
+    }
+    
+    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException, ValueException
+    {
+        return buffer;
+    }
+}



Mime
View raw message