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 2011/01/25 00:23:46 UTC
svn commit: r1063059 - in
/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/message:
AbstractMessage.java Encodeable.java EncodeableDecorator.java
LdapEncoder.java
Author: akarasulu
Date: Mon Jan 24 23:23:46 2011
New Revision: 1063059
URL: http://svn.apache.org/viewvc?rev=1063059&view=rev
Log:
EncodeableDecorator idea in action: will rollback if need be - just to eval for now
Added:
directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/message/Encodeable.java
directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/message/EncodeableDecorator.java (contents, props changed)
- copied, changed from r1062996, directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/CodecControlDecorator.java
Modified:
directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/message/AbstractMessage.java
directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/message/LdapEncoder.java
Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/message/AbstractMessage.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/message/AbstractMessage.java?rev=1063059&r1=1063058&r2=1063059&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/message/AbstractMessage.java (original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/message/AbstractMessage.java Mon Jan 24 23:23:46 2011
@@ -286,40 +286,40 @@ public abstract class AbstractMessage im
}
- /**
- * {@inheritDoc}
- */
- /* No qualifier*/void setControlsLength( int controlsLength )
- {
- this.controlsLength = controlsLength;
- }
-
-
- /**
- * {@inheritDoc}
- */
- /* No qualifier*/int getControlsLength()
- {
- return controlsLength;
- }
-
-
- /**
- * {@inheritDoc}
- */
- /* No qualifier*/void setMessageLength( int messageLength )
- {
- this.messageLength = messageLength;
- }
-
-
- /**
- * {@inheritDoc}
- */
- /* No qualifier*/int getMessageLength()
- {
- return messageLength;
- }
+// /**
+// * {@inheritDoc}
+// */
+// /* No qualifier*/void setControlsLength( int controlsLength )
+// {
+// this.controlsLength = controlsLength;
+// }
+//
+//
+// /**
+// * {@inheritDoc}
+// */
+// /* No qualifier*/int getControlsLength()
+// {
+// return controlsLength;
+// }
+//
+//
+// /**
+// * {@inheritDoc}
+// */
+// /* No qualifier*/void setMessageLength( int messageLength )
+// {
+// this.messageLength = messageLength;
+// }
+//
+//
+// /**
+// * {@inheritDoc}
+// */
+// /* No qualifier*/int getMessageLength()
+// {
+// return messageLength;
+// }
/**
Added: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/message/Encodeable.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/message/Encodeable.java?rev=1063059&view=auto
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/message/Encodeable.java (added)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/message/Encodeable.java Mon Jan 24 23:23:46 2011
@@ -0,0 +1,46 @@
+/*
+ * 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.message;
+
+
+import org.apache.directory.shared.ldap.model.message.Message;
+
+
+/**
+ * Objects that can be encoded by this LdapEncoder must implement this interface.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public interface Encodeable
+{
+ void setControlsLength( int controlsLength );
+
+
+ int getControlsLength();
+
+
+ void setMessageLength( int messageLength );
+
+
+ int getMessageLength();
+
+
+ Message getMessage();
+}
Copied: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/message/EncodeableDecorator.java (from r1062996, directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/CodecControlDecorator.java)
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/message/EncodeableDecorator.java?p2=directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/message/EncodeableDecorator.java&p1=directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/CodecControlDecorator.java&r1=1062996&r2=1063059&rev=1063059&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/CodecControlDecorator.java (original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/message/EncodeableDecorator.java Mon Jan 24 23:23:46 2011
@@ -17,20 +17,10 @@
* under the License.
*
*/
-package org.apache.directory.shared.ldap.codec.controls;
+package org.apache.directory.shared.ldap.message;
-import org.apache.directory.shared.asn1.AbstractAsn1Object;
-import org.apache.directory.shared.asn1.EncoderException;
-import org.apache.directory.shared.asn1.ber.tlv.TLV;
-import org.apache.directory.shared.asn1.ber.tlv.UniversalTag;
-import org.apache.directory.shared.asn1.ber.tlv.Value;
-import org.apache.directory.shared.i18n.I18n;
-import org.apache.directory.shared.ldap.model.message.Control;
-import org.apache.directory.shared.util.Strings;
-
-import java.nio.BufferOverflowException;
-import java.nio.ByteBuffer;
+import org.apache.directory.shared.ldap.model.message.Message;
/**
@@ -41,239 +31,56 @@ import java.nio.ByteBuffer;
*
* @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
*/
-public class CodecControlDecorator extends AbstractAsn1Object implements Control, CodecControl
+public class EncodeableDecorator implements Encodeable
{
// ~ Instance fields
// ----------------------------------------------------------------------------
/** The decorated Control */
- private final Control decoratedComponent;
-
- /** The encoded value length */
- protected int valueLength;
+ private final Message decoratedMessage;
- /** The control length */
- private int controlLength;
+ /** The encoded Message length */
+ protected int messageLength;
- /** The control decoder */
- protected ControlDecoder decoder;
+ /** The length of the controls */
+ private int controlsLength;
/**
- * Default constructor.
+ * Makes a Message an Encodeable object.
*/
- public CodecControlDecorator( Control decoratedComponent)
+ public EncodeableDecorator(Message decoratedMessage)
{
- this.decoratedComponent = decoratedComponent;
+ this.decoratedMessage = decoratedMessage;
}
- /**
- * Get the OID
- *
- * @return A string which represent the control oid
- */
- public String getOid()
+ public Message getMessage()
{
- return decoratedComponent.getOid();
+ return decoratedMessage;
}
- /**
- * Get the control value
- *
- * @return The control value
- */
- public byte[] getValue()
+ public void setControlsLength( int controlsLength )
{
- return decoratedComponent.getValue();
+ this.controlsLength = controlsLength;
}
- /**
- * Set the encoded control value
- *
- * @param value The encoded control value to store
- */
- public void setValue( byte[] value )
- {
- if ( value != null )
- {
- byte[] copy = new byte[ value.length ];
- System.arraycopy( value, 0, copy, 0, value.length );
- decoratedComponent.setValue( copy );
- }
- else
- {
- decoratedComponent.setValue( null );
- }
- }
-
-
- /**
- * Get the criticality
- *
- * @return <code>true</code> if the criticality flag is true.
- */
- public boolean isCritical()
+ public int getControlsLength()
{
- return decoratedComponent.isCritical();
+ return controlsLength;
}
- /**
- * Set the criticality
- *
- * @param criticality The criticality value
- */
- public void setCritical( boolean criticality )
+ public void setMessageLength( int messageLength )
{
- decoratedComponent.setCritical( criticality );
+ this.messageLength = messageLength;
}
-
- /**
- * {@inheritDoc}
- */
- public int computeLength()
- {
- return 0;
- }
-
- /**
- * {@inheritDoc}
- */
- public int computeLength( int valueLength )
+ public int getMessageLength()
{
- // The OID
- int oidLengh = Strings.getBytesUtf8( getOid() ).length;
- controlLength = 1 + TLV.getNbBytes( oidLengh ) + oidLengh;
-
- // The criticality, only if true
- if ( isCritical() )
- {
- controlLength += 1 + 1 + 1; // Always 3 for a boolean
- }
-
- this.valueLength = valueLength;
-
- if ( valueLength != 0 )
- {
- controlLength += 1 + TLV.getNbBytes( valueLength ) + valueLength;
- }
-
- return 1 + TLV.getNbBytes( controlLength ) + controlLength;
- }
-
-
- /**
- * {@inheritDoc}
- */
- public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
- {
- if ( buffer == null )
- {
- throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
- }
-
- try
- {
- // The LdapMessage Sequence
- buffer.put( UniversalTag.SEQUENCE.getValue() );
-
- // The length has been calculated by the computeLength method
- buffer.put( TLV.getBytes( controlLength ) );
- }
- catch ( BufferOverflowException boe )
- {
- throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
- }
-
- // The control type
- Value.encode( buffer, getOid().getBytes() );
-
- // The control criticality, if true
- if ( isCritical() )
- {
- Value.encode( buffer, isCritical() );
- }
-
- return buffer;
- }
-
-
- /**
- * {@inheritDoc}
- */
- public boolean hasValue()
- {
- return decoratedComponent.hasValue();
- }
-
-
- /**
- * {@inheritDoc}
- */
- public ControlDecoder getDecoder()
- {
- return decoder;
- }
-
-
- /**
- * @see Object#equals(Object)
- */
- public boolean equals( Object o )
- {
- if ( o == this )
- {
- return true;
- }
-
- if ( o == null )
- {
- return false;
- }
-
- if ( !( o instanceof Control) )
- {
- return false;
- }
-
- Control otherControl = ( Control ) o;
-
- if ( !getOid().equalsIgnoreCase( otherControl.getOid() ) )
- {
- return false;
- }
-
- if ( isCritical() != otherControl.isCritical() )
- {
- return false;
- }
-
- return hasValue() == otherControl.hasValue();
- }
-
-
- /**
- * Return a String representing a Control
- */
- public String toString()
- {
- StringBuffer sb = new StringBuffer();
-
- sb.append( " Control\n" );
- sb.append( " Control oid : '" ).append( getOid() ).append( "'\n" );
- sb.append( " Criticality : '" ).append( isCritical() ).append( "'\n" );
-
- if ( getValue() != null )
- {
- sb.append( " Control value : '" ).append( Strings.dumpBytes( getValue() ) )
- .append( "'\n" );
- }
-
- return sb.toString();
+ return messageLength;
}
}
Propchange: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/message/EncodeableDecorator.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/message/LdapEncoder.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/message/LdapEncoder.java?rev=1063059&r1=1063058&r2=1063059&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/message/LdapEncoder.java (original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/message/LdapEncoder.java Mon Jan 24 23:23:46 2011
@@ -81,7 +81,8 @@ public class LdapEncoder
*/
public ByteBuffer encodeMessage( Message message ) throws EncoderException
{
- int length = computeMessageLength( message );
+ Encodeable encodeable = new EncodeableDecorator( message );
+ int length = computeMessageLength( encodeable );
ByteBuffer buffer = ByteBuffer.allocate( length );
try
@@ -92,7 +93,7 @@ public class LdapEncoder
buffer.put( UniversalTag.SEQUENCE.getValue() );
// The length has been calculated by the computeLength method
- buffer.put( TLV.getBytes( ( ( AbstractMessage ) message ).getMessageLength() ) );
+ buffer.put( TLV.getBytes( encodeable.getMessageLength() ) );
}
catch ( BufferOverflowException boe )
{
@@ -112,7 +113,7 @@ public class LdapEncoder
{
// Encode the controls
buffer.put( ( byte ) LdapConstants.CONTROLS_TAG );
- buffer.put( TLV.getBytes( ( ( AbstractMessage ) message ).getControlsLength() ) );
+ buffer.put( TLV.getBytes( encodeable.getControlsLength() ) );
// Encode each control
for ( Control control : controls.values() )
@@ -146,20 +147,20 @@ public class LdapEncoder
* L1 = length(ProtocolOp)
* LdapMessage length = Length(0x30) + Length(L1) + MessageId length + L1
*
- * @param message the Message who's length is to be encoded
+ * @param encodeable the decorated Message who's length is to be encoded
*/
- private int computeMessageLength( Message message )
+ private int computeMessageLength( Encodeable encodeable )
{
// 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
- int ldapMessageLength = 1 + 1 + Value.getNbBytes( message.getMessageId() );
+ int ldapMessageLength = 1 + 1 + Value.getNbBytes( encodeable.getMessage().getMessageId() );
// Get the protocolOp length
- ldapMessageLength += computeProtocolOpLength( message );
+ ldapMessageLength += computeProtocolOpLength( encodeable.getMessage() );
- Map<String, Control> controls = message.getControls();
+ Map<String, Control> controls = encodeable.getMessage().getControls();
// Do the same thing for Controls, if any.
if ( controls.size() > 0 )
@@ -193,14 +194,14 @@ public class LdapEncoder
// Computes the controls length
// 1 + Length.getNbBytes( controlsSequenceLength ) + controlsSequenceLength;
- ( ( AbstractMessage ) message ).setControlsLength( controlsSequenceLength );
+ encodeable.setControlsLength( controlsSequenceLength );
// Now, add the tag and the length of the controls length
ldapMessageLength += 1 + TLV.getNbBytes( controlsSequenceLength ) + controlsSequenceLength;
}
// Store the messageLength
- ( ( AbstractMessage ) message ).setMessageLength( ldapMessageLength );
+ encodeable.setMessageLength( ldapMessageLength );
// finally, calculate the global message size :
// length(Tag) + Length(length) + length