|
|
Author: elecharny
Date: Fri Sep 8 10:50:25 2006
New Revision: 441591
URL: http://svn.apache.org/viewvc?view=rev&rev=441591
Log:
Added the encode method
Modified:
directory/sandbox/akarasulu/apacheds-2.0/shared/ldap/src/main/java/org/apache/directory/shared/ldap/codec/asn1ber/messages/MessageAsn1Ber.java
Modified:
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=diff&rev=441591&r1=441590&r2=441591
==============================================================================
---
directory/sandbox/akarasulu/apacheds-2.0/shared/ldap/src/main/java/org/apache/directory/shared/ldap/codec/asn1ber/messages/MessageAsn1Ber.java
(original)
+++
directory/sandbox/akarasulu/apacheds-2.0/shared/ldap/src/main/java/org/apache/directory/shared/ldap/codec/asn1ber/messages/MessageAsn1Ber.java
Fri Sep 8 10:50:25 2006
@@ -19,22 +19,19 @@
*/
package org.apache.directory.shared.ldap.codec.asn1ber.messages;
+import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
-import java.util.Iterator;
-
-import javax.naming.ldap.Control;
+import org.apache.directory.shared.asn1.ber.UniversalTags;
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;
@@ -59,9 +56,6 @@
/** The controls sequence length */
private transient int controlsSequenceLength;
- /** The controls length */
- private transient int controlsLength;
-
/**
*
* Creates a new instance of BindRequestAsn1Ber.
@@ -71,8 +65,6 @@
public MessageAsn1Ber( Message message )
{
super( message );
-
-
}
/**
@@ -83,12 +75,15 @@
* 0x30 L1
* |
* +--> 0x02 0x0(1-4) [0..2^31-1] (MessageId)
- * +--> protocolOp
+ * +--> (protocolOp)
* [+--> Controls]
*
* MessageId length = Length(0x02) + length(MessageId) + MessageId.length
* L1 = length(ProtocolOp)
* LdapMessage length = Length(0x30) + Length(L1) + MessageId length + L1
+ *
+ * Here, the protocolOp length is not calculated. We just return the length
+ * of all other parts (messageId and controls.
*/
public int computeLength()
{
@@ -98,14 +93,8 @@
// - 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 )
+ // Compute Control lengths, if any.
+ if ( ( getControls() != null ) && ( getControls().size() != 0 ) )
{
// Controls :
// 0xA0 L3
@@ -116,7 +105,7 @@
// +--> 0x30 Li
// +--> ...
// +--> 0x30 Ln
- //
+ //getControlsLength
// L3 = Length(0x30) + Length(L5) + L5
// + Length(0x30) + Length(L6) + L6
// + ...
@@ -128,41 +117,94 @@
// + Length(L3) + L3
controlsSequenceLength = 0;
- Iterator<Control> controlIterator =
getControls().values().iterator();
-
- // We may have more than one control. ControlsLength is L4.
- while ( controlIterator.hasNext() )
+ for ( ControlDecorator controlDecorator:controlDecorators )
{
- Control control = ( ( Control ) controlIterator.next() );
-// ControlDecorator controlDecorator = new ControlAsn1Ber(
control );
-// controlsSequenceLength += controlDecorator.computeLength();
+ 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 );
+ log.debug( "Message length : {}", ldapMessageLength );
}
- return length;
+ return ldapMessageLength;
}
+ /**
+ * Generate the PDU which contains the encoded object. getControlsLength
+ *
+ * The generation is done in two phases :
+ * - first, we compute the length of each part and the
+ * global PDU length
+ * - second, we produce the PDU.
+ *
+ * 0x30 L1
+ * |
+ * +--> 0x02 L2 MessageId
+ * +--> ProtocolOp
+ * +--> Controls
+ *
+ * L2 = Length(MessageId)
+ * L1 = Length(0x02) + Length(L2) + L2 + Length(ProtocolOp) +
Length(Controls)
+ * LdapMessageLength = Length(0x30) + Length(L1) + L1
+ *
+ * @param buffer The encoded PDU
+ * @return A ByteBuffer that contaons the PDU
+ * @throws EncoderException If anything goes wrong.
+ */
public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException,
ValueException
{
+ // The buffer should already contains the protocolOp.
+ // We just have to add the header, the messageId and the controls,
+ // if any
+ if ( buffer == null )
+ {
+ throw new EncoderException( "Cannot put a PDU in a null buffer !"
);
+ }
+
+ try
+ {
+ // The LdapMessage Sequence
+ buffer.put(0, UniversalTags.SEQUENCE_SEQUENCE_OF.getValue() );
+
+ // The length has been calculated by the computeLength method
+ buffer.put( Length.getBytes( ldapMessageLength ) );
+
+ // The message Id
+ Value.encode( buffer, getMessageId() );
+
+ // Now, we have to move to the end of the protocolOp,
+ // but only if we have controls
+ if ( ( getControls() != null ) && ( getControls().size() != 0 ) )
+ {
+ // Encode the controls
+ buffer.put( ( byte ) LdapBerTags.CONTROLS_TAG.getValue() );
+ buffer.put( Length.getBytes( controlsSequenceLength ) );
+
+ // iterate through all the controls
+ for ( ControlDecorator controlDecorator:controlDecorators )
+ {
+ controlDecorator.encode( buffer );
+ }
+ }
+ }
+ catch ( BufferOverflowException boe )
+ {
+ throw new EncoderException( "The PDU buffer size is too small !" );
+ }
+
return buffer;
+ }
+
+ /**
+ * @return The length of all controls, if any (zero otherwise)
+ */
+ public int getControlsSequenceLength()
+ {
+ return controlsSequenceLength;
}
}
|
|