Search in sources :

Example 66 with EncoderException

use of org.apache.directory.api.asn1.EncoderException in project directory-ldap-api by apache.

the class LdapControlTest method testDecodeRequestWithEmptyControls.

/**
 * Test the decoding of a Request with an empty list of controls
 */
@Test
public void testDecodeRequestWithEmptyControls() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x0A);
    stream.put(new byte[] { // LDAPMessage ::=SEQUENCE {
    0x30, // LDAPMessage ::=SEQUENCE {
    0x08, // messageID MessageID
    0x02, // messageID MessageID
    0x01, // messageID MessageID
    0x03, // CHOICE { ..., abandonRequest
    0x50, // CHOICE { ..., abandonRequest
    0x01, // CHOICE { ..., abandonRequest
    0x02, // controls [0] Controls OPTIONAL }
    (byte) 0xA0, // controls [0] Controls OPTIONAL }
    0x00 });
    stream.flip();
    // Allocate a LdapMessageContainer Container
    LdapMessageContainer<AbandonRequestDecorator> ldapMessageContainer = new LdapMessageContainer<AbandonRequestDecorator>(codec);
    // Decode the PDU
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    // Check that everything is OK
    AbandonRequestDecorator abandonRequest = ldapMessageContainer.getMessage();
    // Copy the message
    AbandonRequest internalAbandonRequest = new AbandonRequestImpl(abandonRequest.getAbandoned());
    internalAbandonRequest.setMessageId(abandonRequest.getMessageId());
    assertEquals(3, abandonRequest.getMessageId());
    assertEquals(2, abandonRequest.getAbandoned());
    // Check the Controls
    Map<String, Control> controls = abandonRequest.getControls();
    assertEquals(0, controls.size());
    // Check the encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(internalAbandonRequest);
        // Check the length, which should be 2 bytes shorter, as we don't encode teh empty control
        assertEquals(0x08, bb.limit());
        // So we decode the generated PDU, and we compare it with the initial message
        try {
            ldapDecoder.decode(bb, ldapMessageContainer);
        } catch (DecoderException de) {
            de.printStackTrace();
            fail(de.getMessage());
        }
        AbandonRequest abandonRequest2 = ldapMessageContainer.getMessage();
        assertEquals(abandonRequest, abandonRequest2);
    } catch (EncoderException ee) {
        ee.printStackTrace();
        fail(ee.getMessage());
    }
}
Also used : LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) DecoderException(org.apache.directory.api.asn1.DecoderException) EncoderException(org.apache.directory.api.asn1.EncoderException) Control(org.apache.directory.api.ldap.model.message.Control) CodecControl(org.apache.directory.api.ldap.codec.api.CodecControl) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) AbandonRequest(org.apache.directory.api.ldap.model.message.AbandonRequest) ByteBuffer(java.nio.ByteBuffer) AbandonRequestDecorator(org.apache.directory.api.ldap.codec.decorators.AbandonRequestDecorator) AbandonRequestImpl(org.apache.directory.api.ldap.model.message.AbandonRequestImpl) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 67 with EncoderException

use of org.apache.directory.api.asn1.EncoderException in project directory-ldap-api by apache.

the class LdapMessageTest method testDecodeUnBindRequestNoControls.

/**
 * Test the decoding of a LdapMessage with a large MessageId
 */
@Test
public void testDecodeUnBindRequestNoControls() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x08);
    stream.put(new byte[] { // LDAPMessage ::=SEQUENCE {
    0x30, // LDAPMessage ::=SEQUENCE {
    0x06, 0x02, 0x02, 0x01, // messageID MessageID (500)
    (byte) 0xF4, 0x42, // CHOICE { ..., unbindRequest UnbindRequest,...
    0x00 // UnbindRequest ::= [APPLICATION 2] NULL
    });
    String decodedPdu = Strings.dumpBytes(stream.array());
    stream.flip();
    // Allocate a BindRequest Container
    LdapMessageContainer<UnbindRequestDecorator> container = new LdapMessageContainer<UnbindRequestDecorator>(codec);
    try {
        ldapDecoder.decode(stream, container);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    Message message = container.getMessage();
    assertEquals(500, message.getMessageId());
    // Check the length
    UnbindRequest internalUnbindRequest = new UnbindRequestImpl();
    internalUnbindRequest.setMessageId(message.getMessageId());
    try {
        ByteBuffer bb = encoder.encodeMessage(internalUnbindRequest);
        // Check the length
        assertEquals(0x08, bb.limit());
        String encodedPdu = Strings.dumpBytes(bb.array());
        assertEquals(encodedPdu, decodedPdu);
    } catch (EncoderException ee) {
        ee.printStackTrace();
        fail(ee.getMessage());
    }
}
Also used : LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) DecoderException(org.apache.directory.api.asn1.DecoderException) EncoderException(org.apache.directory.api.asn1.EncoderException) Message(org.apache.directory.api.ldap.model.message.Message) UnbindRequestImpl(org.apache.directory.api.ldap.model.message.UnbindRequestImpl) UnbindRequestDecorator(org.apache.directory.api.ldap.codec.decorators.UnbindRequestDecorator) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) ByteBuffer(java.nio.ByteBuffer) UnbindRequest(org.apache.directory.api.ldap.model.message.UnbindRequest) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 68 with EncoderException

use of org.apache.directory.api.asn1.EncoderException in project directory-ldap-api by apache.

the class IntermediateResponseDecorator method encode.

/**
 * Encode the IntermediateResponse message to a PDU.
 * <br>
 * IntermediateResponse :
 * <pre>
 *   0x79 LL
 *     [0x80 LL response name]
 *     [0x81 LL responseValue]
 * </pre>
 *
 * @param buffer The buffer where to put the PDU
 */
@Override
public ByteBuffer encode(ByteBuffer buffer) throws EncoderException {
    try {
        // The ExtendedResponse Tag
        buffer.put(LdapCodecConstants.INTERMEDIATE_RESPONSE_TAG);
        buffer.put(TLV.getBytes(intermediateResponseLength));
        // The responseName, if any
        if ((responseNameBytes != null) && (responseNameBytes.length != 0)) {
            buffer.put((byte) LdapCodecConstants.INTERMEDIATE_RESPONSE_NAME_TAG);
            buffer.put(TLV.getBytes(responseNameBytes.length));
            buffer.put(responseNameBytes);
        }
        // The encodedValue, if any
        if (encodedValueBytes != null) {
            buffer.put((byte) LdapCodecConstants.INTERMEDIATE_RESPONSE_VALUE_TAG);
            buffer.put(TLV.getBytes(encodedValueBytes.length));
            if (encodedValueBytes.length != 0) {
                buffer.put(encodedValueBytes);
            }
        }
    } catch (BufferOverflowException boe) {
        throw new EncoderException(I18n.err(I18n.ERR_04005), boe);
    }
    return buffer;
}
Also used : EncoderException(org.apache.directory.api.asn1.EncoderException) BufferOverflowException(java.nio.BufferOverflowException)

Example 69 with EncoderException

use of org.apache.directory.api.asn1.EncoderException in project directory-ldap-api by apache.

the class LdapResultDecorator method encode.

/**
 * Encode the LdapResult message to a PDU.
 *
 * @param buffer The buffer where to put the PDU
 * @return The PDU.
 */
@Override
public ByteBuffer encode(ByteBuffer buffer) throws EncoderException {
    if (buffer == null) {
        throw new EncoderException(I18n.err(I18n.ERR_04023));
    }
    if (decoratedLdapResult.isDefaultSuccess()) {
        // The length of a default success PDU : 0xA0 0x01 0x00 0x04 0x00 0x04 0x00
        buffer.put(DEFAULT_SUCCESS);
        return buffer;
    }
    try {
        // The result code
        BerValue.encodeEnumerated(buffer, getResultCode().getValue());
    } catch (BufferOverflowException boe) {
        throw new EncoderException(I18n.err(I18n.ERR_04005), boe);
    }
    // The matchedDN
    BerValue.encode(buffer, matchedDnBytes);
    // The error message
    BerValue.encode(buffer, errorMessageBytes);
    // The referrals, if any
    Referral referral = getReferral();
    if (referral != null) {
        LdapEncoder.encodeReferral(buffer, referral);
    }
    return buffer;
}
Also used : EncoderException(org.apache.directory.api.asn1.EncoderException) Referral(org.apache.directory.api.ldap.model.message.Referral) BufferOverflowException(java.nio.BufferOverflowException)

Example 70 with EncoderException

use of org.apache.directory.api.asn1.EncoderException in project directory-ldap-api by apache.

the class ModifyDnRequestDecorator method encode.

/**
 * Encode the ModifyDNRequest message to a PDU.
 * <br>
 * ModifyDNRequest :
 * <pre>
 * 0x6C LL
 *   0x04 LL entry
 *   0x04 LL newRDN
 *   0x01 0x01 deleteOldRDN
 *   [0x80 LL newSuperior]
 * </pre>
 *
 * @param buffer The buffer where to put the PDU
 * @return The PDU.
 */
@Override
public ByteBuffer encode(ByteBuffer buffer) throws EncoderException {
    try {
        // The ModifyDNRequest Tag
        buffer.put(LdapCodecConstants.MODIFY_DN_REQUEST_TAG);
        buffer.put(TLV.getBytes(modifyDnRequestLength));
        // The entry
        BerValue.encode(buffer, dnBytes);
        // The newRDN
        BerValue.encode(buffer, getNewRdn().getName());
        // The flag deleteOldRdn
        BerValue.encode(buffer, getDeleteOldRdn());
        // The new superior, if any
        if (getNewSuperior() != null) {
            // Encode the reference
            buffer.put((byte) LdapCodecConstants.MODIFY_DN_REQUEST_NEW_SUPERIOR_TAG);
            int newSuperiorLength = newSuperiorBytes.length;
            buffer.put(TLV.getBytes(newSuperiorLength));
            if (newSuperiorLength != 0) {
                buffer.put(newSuperiorBytes);
            }
        }
    } catch (BufferOverflowException boe) {
        throw new EncoderException(I18n.err(I18n.ERR_04005), boe);
    }
    return buffer;
}
Also used : EncoderException(org.apache.directory.api.asn1.EncoderException) BufferOverflowException(java.nio.BufferOverflowException)

Aggregations

EncoderException (org.apache.directory.api.asn1.EncoderException)226 ByteBuffer (java.nio.ByteBuffer)191 Test (org.junit.Test)189 DecoderException (org.apache.directory.api.asn1.DecoderException)151 Asn1Decoder (org.apache.directory.api.asn1.ber.Asn1Decoder)150 AbstractCodecServiceTest (org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)127 LdapMessageContainer (org.apache.directory.api.ldap.codec.api.LdapMessageContainer)124 BufferOverflowException (java.nio.BufferOverflowException)40 Control (org.apache.directory.api.ldap.model.message.Control)38 CodecControl (org.apache.directory.api.ldap.codec.api.CodecControl)35 AbstractCodecServiceTest (org.apache.directory.api.ldap.extras.AbstractCodecServiceTest)35 SearchRequestDecorator (org.apache.directory.api.ldap.codec.decorators.SearchRequestDecorator)33 SearchRequest (org.apache.directory.api.ldap.model.message.SearchRequest)33 ExprNode (org.apache.directory.api.ldap.model.filter.ExprNode)28 SyncInfoValue (org.apache.directory.api.ldap.extras.intermediate.syncrepl.SyncInfoValue)20 SyncInfoValueDecorator (org.apache.directory.api.ldap.extras.intermediate.syncrepl.SyncInfoValueDecorator)20 Attribute (org.apache.directory.api.ldap.model.entry.Attribute)20 AndNode (org.apache.directory.api.ldap.model.filter.AndNode)15 Entry (org.apache.directory.api.ldap.model.entry.Entry)14 EqualityNode (org.apache.directory.api.ldap.model.filter.EqualityNode)14