Skip to content
Permalink
Browse files
o Removed some dead code from LdapEncoder
o Renamed some methods
  • Loading branch information
elecharny committed Jan 2, 2019
1 parent e861d2c commit f5a7338260fd311a74ba9ea24df440165ff2a2cf
Show file tree
Hide file tree
Showing 30 changed files with 136 additions and 281 deletions.
@@ -20,15 +20,12 @@
package org.apache.directory.api.ldap.codec.api;


import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

import org.apache.directory.api.asn1.EncoderException;
import org.apache.directory.api.asn1.ber.tlv.BerValue;
import org.apache.directory.api.asn1.ber.tlv.TLV;
import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
import org.apache.directory.api.asn1.util.Asn1Buffer;
import org.apache.directory.api.i18n.I18n;
@@ -55,9 +52,7 @@
import org.apache.directory.api.ldap.codec.factory.UnbindRequestFactory;
import org.apache.directory.api.ldap.model.message.Control;
import org.apache.directory.api.ldap.model.message.Message;
import org.apache.directory.api.ldap.model.message.Referral;
import org.apache.directory.api.ldap.model.message.Request;
import org.apache.directory.api.util.Strings;


/**
@@ -75,79 +70,6 @@ private LdapEncoder()
// Nothing to do
}

/**
* Compute the control's encoded length
*
* @param control The control to compute
* @return the encoded control length
*/
public static int computeControlLength( Control control )
{
// First, compute the control's value length
int controlValueLength = ( ( CodecControl<?> ) control ).computeLength();

// Now, compute the envelop length
// The OID
int oidLengh = Strings.getBytesUtf8( control.getOid() ).length;
int controlLength = 1 + TLV.getNbBytes( oidLengh ) + oidLengh;

// The criticality, only if true
if ( control.isCritical() )
{
// Always 3 for a boolean
controlLength += 1 + 1 + 1;
}

if ( controlValueLength != 0 )
{
controlLength += 1 + TLV.getNbBytes( controlValueLength ) + controlValueLength;
}

return controlLength;
}


/**
* Encode a control to a byte[]
*
* @param buffer The buffer that will contain the encoded control
* @param control The control to encode
* @return The control encoded in a byte[]
* @throws EncoderException If the encoding failed
*/
public static ByteBuffer encodeControl( ByteBuffer buffer, Control control ) throws EncoderException
{
if ( buffer == null )
{
throw new EncoderException( I18n.err( I18n.ERR_08000_CANNOT_PUT_A_PDU_IN_NULL_BUFFER ) );
}

try
{
// The LdapMessage Sequence
buffer.put( UniversalTag.SEQUENCE.getValue() );

// The length has been calculated by the computeLength method
int controlLength = computeControlLength( control );
buffer.put( TLV.getBytes( controlLength ) );
}
catch ( BufferOverflowException boe )
{
throw new EncoderException( I18n.err( I18n.ERR_08212_PDU_BUFFER_TOO_SMALL ), boe );
}

// The control type
BerValue.encode( buffer, Strings.getBytesUtf8( control.getOid() ) );

// The control criticality, if true
if ( control.isCritical() )
{
BerValue.encode( buffer, control.isCritical() );
}

return buffer;
}


/**
* Encode a control to a byte[]. The controls are encoded recursively, to start with the last
@@ -167,7 +89,7 @@ public static ByteBuffer encodeControl( ByteBuffer buffer, Control control ) thr
* @return The control encoded in a byte[]
* @throws EncoderException If the encoding failed
*/
private static void encodeControlsReverse( Asn1Buffer buffer, LdapApiService codec,
private static void encodeControls( Asn1Buffer buffer, LdapApiService codec,
Map<String, Control> controls, Iterator<String> iterator, boolean isRequest ) throws EncoderException
{
if ( iterator.hasNext() )
@@ -176,7 +98,7 @@ private static void encodeControlsReverse( Asn1Buffer buffer, LdapApiService cod
Control control = controls.get( iterator.next() );

// Encode the remaining controls recursively
encodeControlsReverse( buffer, codec, controls, iterator, isRequest );
encodeControls( buffer, codec, controls, iterator, isRequest );

// Fetch the control's factory from the LdapApiService
ControlFactory<?> controlFactory;
@@ -347,7 +269,7 @@ private static void encodeProtocolOp( Asn1Buffer buffer, LdapApiService codec, M
* @return A ByteBuffer that contains the PDU
* @throws EncoderException If anything goes wrong.
*/
public static ByteBuffer encodeMessageReverse( Asn1Buffer buffer, LdapApiService codec, Message message ) throws EncoderException
public static ByteBuffer encodeMessage( Asn1Buffer buffer, LdapApiService codec, Message message ) throws EncoderException
{
int start = buffer.getPos();

@@ -356,7 +278,7 @@ public static ByteBuffer encodeMessageReverse( Asn1Buffer buffer, LdapApiService

if ( ( controls != null ) && ( controls.size() > 0 ) )
{
encodeControlsReverse( buffer, codec, controls, controls.keySet().iterator(), message instanceof Request );
encodeControls( buffer, codec, controls, controls.keySet().iterator(), message instanceof Request );

// The controls tag
BerValue.encodeSequence( buffer, ( byte ) LdapCodecConstants.CONTROLS_TAG, start );
@@ -373,71 +295,4 @@ public static ByteBuffer encodeMessageReverse( Asn1Buffer buffer, LdapApiService

return buffer.getBytes();
}


/**
* Encode the Referral message to a PDU.
*
* @param buffer The buffer where to put the PDU
* @param referral The referral to encode
* @exception EncoderException If the encoding failed
*/
public static void encodeReferral( ByteBuffer buffer, Referral referral ) throws EncoderException
{
Collection<byte[]> ldapUrlsBytes = referral.getLdapUrlsBytes();

if ( ( ldapUrlsBytes != null ) && ( !ldapUrlsBytes.isEmpty() ) )
{
// Encode the referrals sequence
// The referrals length MUST have been computed before !
buffer.put( ( byte ) LdapCodecConstants.LDAP_RESULT_REFERRAL_SEQUENCE_TAG );
buffer.put( TLV.getBytes( referral.getReferralLength() ) );

// Each referral
for ( byte[] ldapUrlBytes : ldapUrlsBytes )
{
// Encode the current referral
BerValue.encode( buffer, ldapUrlBytes );
}
}
}


/**
* Compute the referral's encoded length
* @param referral The referral to encode
* @return The length of the encoded PDU
*/
public static int computeReferralLength( Referral referral )
{
if ( referral != null )
{
Collection<String> ldapUrls = referral.getLdapUrls();

if ( ( ldapUrls != null ) && ( !ldapUrls.isEmpty() ) )
{
int referralLength = 0;

// Each referral
for ( String ldapUrl : ldapUrls )
{
byte[] ldapUrlBytes = Strings.getBytesUtf8( ldapUrl );
referralLength += 1 + TLV.getNbBytes( ldapUrlBytes.length ) + ldapUrlBytes.length;
referral.addLdapUrlBytes( ldapUrlBytes );
}

referral.setReferralLength( referralLength );

return referralLength;
}
else
{
return 0;
}
}
else
{
return 0;
}
}
}
@@ -162,7 +162,7 @@ public void testDecodeRequestWithControls() throws DecoderException, EncoderExce

// Check the encoding
Asn1Buffer buffer = new Asn1Buffer();
ByteBuffer bb = LdapEncoder.encodeMessageReverse( buffer, codec, internalAbandonRequest );
ByteBuffer bb = LdapEncoder.encodeMessage( buffer, codec, internalAbandonRequest );

// Check the length
assertEquals( 0x9C, bb.limit() );
@@ -221,7 +221,7 @@ public void testDecodeRequestWithEmptyControls() throws DecoderException, Encode

// Check the encoding
Asn1Buffer buffer = new Asn1Buffer();
ByteBuffer bb = LdapEncoder.encodeMessageReverse( buffer, codec, internalAbandonRequest );
ByteBuffer bb = LdapEncoder.encodeMessage( buffer, codec, internalAbandonRequest );

// Check the length, which should be 2 bytes shorter, as we don't encode teh empty control
assertEquals( 0x08, bb.limit() );
@@ -173,7 +173,7 @@ public void testDecodeFull() throws DecoderException, EncoderException
// Check the revert encoder
Asn1Buffer buffer = new Asn1Buffer();

LdapEncoder.encodeMessageReverse( buffer, codec, bindRequest );
LdapEncoder.encodeMessage( buffer, codec, bindRequest );

assertArrayEquals( stream.array(), buffer.getBytes().array() );
}
@@ -353,7 +353,7 @@ public void testDecodeSplittedPDU() throws DecoderException, EncoderException
// Check the revert encoder
Asn1Buffer buffer = new Asn1Buffer();

LdapEncoder.encodeMessageReverse( buffer, codec, bindRequest );
LdapEncoder.encodeMessage( buffer, codec, bindRequest );

assertArrayEquals(
new byte[]
@@ -272,7 +272,7 @@ public void testDecodeUnBindRequestNoControls() throws DecoderException, Encoder
// Check the reverse encoding
Asn1Buffer buffer = new Asn1Buffer();

ByteBuffer result = LdapEncoder.encodeMessageReverse( buffer, codec, unbindRequest );
ByteBuffer result = LdapEncoder.encodeMessage( buffer, codec, unbindRequest );

assertArrayEquals( stream.array(), result.array() );
}
@@ -251,7 +251,7 @@ public void testDecodeAddResponseEmptyResultCodeOK() throws DecoderException, En
AddResponse response = new AddResponseImpl( addResponse.getMessageId() );
response.getLdapResult().setResultCode( ResultCodeEnum.SUCCESS );

ByteBuffer result = LdapEncoder.encodeMessageReverse( buffer, codec, response );
ByteBuffer result = LdapEncoder.encodeMessage( buffer, codec, response );

assertArrayEquals( stream.array(), result.array() );
}
@@ -314,7 +314,7 @@ public void testDecodeAddResponseEmptyResultCodeOKReferral() throws DecoderExcep
response.getLdapResult().setReferral( referralResult );
response.getLdapResult().setResultCode( ResultCodeEnum.REFERRAL );

ByteBuffer result = LdapEncoder.encodeMessageReverse( buffer, codec, response );
ByteBuffer result = LdapEncoder.encodeMessage( buffer, codec, response );

assertArrayEquals( stream.array(), result.array() );
}
@@ -385,7 +385,7 @@ public void testDecodeAddResponseEmptyResultCodeOKReferrals() throws DecoderExce
response.getLdapResult().setReferral( referralResult );
response.getLdapResult().setResultCode( ResultCodeEnum.REFERRAL );

ByteBuffer result = LdapEncoder.encodeMessageReverse( buffer, codec, response );
ByteBuffer result = LdapEncoder.encodeMessage( buffer, codec, response );

assertArrayEquals( stream.array(), result.array() );
}
@@ -461,7 +461,7 @@ public void testDecodeAddResponseEmptyResultCodeEmptyReferral() throws DecoderEx
response.getLdapResult().setReferral( referralResult );
response.getLdapResult().setResultCode( ResultCodeEnum.REFERRAL );

ByteBuffer result = LdapEncoder.encodeMessageReverse( buffer, codec, response );
ByteBuffer result = LdapEncoder.encodeMessage( buffer, codec, response );

assertArrayEquals( stream.array(), result.array() );
}
@@ -165,7 +165,7 @@ public void testDecodeAbandonRequestWithControls() throws DecoderException, Enco

// Check reverse encoding
Asn1Buffer buffer = new Asn1Buffer();
LdapEncoder.encodeMessageReverse( buffer, codec, internalAbandonRequest );
LdapEncoder.encodeMessage( buffer, codec, internalAbandonRequest );

// Check that the decoded messages are equals
LdapMessageContainer<AbandonRequest> ldapMessageContainer2 =
@@ -220,7 +220,7 @@ public void testDecodeAbandonRequestNoControlsHighMessageId() throws DecoderExce

// Check the reverse encoding
Asn1Buffer buffer = new Asn1Buffer();
LdapEncoder.encodeMessageReverse( buffer, codec, internalAbandonRequest );
LdapEncoder.encodeMessage( buffer, codec, internalAbandonRequest );

assertArrayEquals( stream.array(), buffer.getBytes().array() );
}
@@ -177,7 +177,7 @@ public void testDecodeAddRequestSuccess() throws DecoderException, EncoderExcept
request.setEntry( addRequest.getEntry() );
request.setMessageId( addRequest.getMessageId() );

LdapEncoder.encodeMessageReverse( buffer, codec, request );
LdapEncoder.encodeMessage( buffer, codec, request );

assertArrayEquals( stream.array(), buffer.getBytes().array() );
}
@@ -589,7 +589,7 @@ public void testDecodeAddRequestEmptyAttributeValue() throws DecoderException, E
request.setEntry( addRequest.getEntry() );
request.setMessageId( addRequest.getMessageId() );

LdapEncoder.encodeMessageReverse( buffer, codec, request );
LdapEncoder.encodeMessage( buffer, codec, request );

assertArrayEquals( stream.array(), buffer.getBytes().array() );
}
@@ -677,7 +677,7 @@ public void testDecodeAddRequestEmptyAttributeValueWithControl() throws Decoder
request.setMessageId( addRequest.getMessageId() );
request.addControl( new ManageDsaITImpl() );

LdapEncoder.encodeMessageReverse( buffer, codec, request );
LdapEncoder.encodeMessage( buffer, codec, request );

assertArrayEquals( stream.array(), buffer.getBytes().array() );

@@ -689,7 +689,7 @@ public void testDecodeAddRequestEmptyAttributeValueWithControl() throws Decoder
request2.setMessageId( addRequest.getMessageId() );
request2.addControl( new ManageDsaITImpl() );

LdapEncoder.encodeMessageReverse( asn1Buffer, codec, request2 );
LdapEncoder.encodeMessage( asn1Buffer, codec, request2 );

assertArrayEquals( stream.array(), asn1Buffer.getBytes().array() );
}
@@ -97,7 +97,7 @@ public void testDecodeAddResponseSuccess() throws DecoderException, EncoderExcep
// Check encode reverse
Asn1Buffer buffer = new Asn1Buffer();

LdapEncoder.encodeMessageReverse( buffer, codec, addResponse );
LdapEncoder.encodeMessage( buffer, codec, addResponse );

assertArrayEquals( stream.array(), buffer.getBytes().array() );
}
@@ -193,7 +193,7 @@ public void testDecodeAddResponseSuccessWithControl() throws DecoderException, E
// Check encode reverse
Asn1Buffer buffer = new Asn1Buffer();

LdapEncoder.encodeMessageReverse( buffer, codec, addResponse );
LdapEncoder.encodeMessage( buffer, codec, addResponse );

assertArrayEquals( stream.array(), buffer.getBytes().array() );
}
@@ -89,7 +89,7 @@ public void testEncodeBindRequestPerf() throws Exception
for ( int i = 0; i < nbLoops; i++ )
{
// Check the encoding
LdapEncoder.encodeMessageReverse( buffer, codec, bindRequest );
LdapEncoder.encodeMessage( buffer, codec, bindRequest );
buffer.clear();
}

0 comments on commit f5a7338

Please sign in to comment.