Search in sources :

Example 1 with AbandonRequestDecorator

use of org.apache.directory.api.ldap.codec.decorators.AbandonRequestDecorator in project directory-ldap-api by apache.

the class AbandonRequestTest method testDecodeAbandonRequestNoControlsHighMessageId.

/**
 * Test the decoding of a AbandonRequest with no controls
 */
@Test
public void testDecodeAbandonRequestNoControlsHighMessageId() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x0A);
    stream.put(new byte[] { // LDAPMessage ::=SEQUENCE {
    0x30, // LDAPMessage ::=SEQUENCE {
    0x08, // messageID MessageID
    0x02, 0x03, 0x00, (byte) 0x80, 0x13, 0x50, 0x01, // CHOICE { ..., abandonRequest
    0x02 // AbandonRequest,...
    // AbandonRequest ::= [APPLICATION 16] MessageID
    });
    String decodedPdu = Strings.dumpBytes(stream.array());
    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
    AbandonRequest abandonRequest = ldapMessageContainer.getMessage();
    assertEquals(32787, abandonRequest.getMessageId());
    assertEquals(2, abandonRequest.getAbandoned());
    // Check the length
    AbandonRequest internalAbandonRequest = new AbandonRequestImpl(abandonRequest.getAbandoned());
    internalAbandonRequest.setMessageId(abandonRequest.getMessageId());
    // Check the encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(internalAbandonRequest);
        // Check the length
        assertEquals(0x0A, 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) 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 2 with AbandonRequestDecorator

use of org.apache.directory.api.ldap.codec.decorators.AbandonRequestDecorator in project directory-ldap-api by apache.

the class AbandonRequestTest method testDecodeAbandonRequestWithControls.

/**
 * Test the decoding of a AbandonRequest with controls
 */
@Test
public void testDecodeAbandonRequestWithControls() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x64);
    stream.put(new byte[] { // LDAPMessage ::=SEQUENCE {
    0x30, // LDAPMessage ::=SEQUENCE {
    0x62, 0x02, 0x01, // messageID MessageID
    0x03, 0x50, 0x01, // CHOICE { ..., abandonRequest
    0x02, // AbandonRequest,...
    (byte) 0xA0, // controls [0] Controls OPTIONAL }
    0x5A, 0x30, // Control ::= SEQUENCE {
    0x1A, // controlType LDAPOID,
    0x04, 0x0D, '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '1', // criticality BOOLEAN DEFAULT FALSE,
    0x01, 0x01, (byte) 0xFF, // controlValue OCTET STRING OPTIONAL }
    0x04, 0x06, 'a', 'b', 'c', 'd', 'e', 'f', 0x30, // Control ::= SEQUENCE {
    0x17, // controlType LDAPOID,
    0x04, 0x0D, '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '2', // controlValue OCTET STRING OPTIONAL }
    0x04, 0x06, 'g', 'h', 'i', 'j', 'k', 'l', 0x30, // Control ::= SEQUENCE {
    0x12, // controlType LDAPOID,
    0x04, 0x0D, '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '3', // criticality BOOLEAN DEFAULT FALSE }
    0x01, 0x01, (byte) 0xFF, 0x30, // Control ::= SEQUENCE {
    0x0F, // controlType LDAPOID}
    0x04, 0x0D, '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '4' });
    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(4, controls.size());
    CodecControl<? extends Control> control = (org.apache.directory.api.ldap.codec.api.CodecControl<?>) controls.get("1.3.6.1.5.5.1");
    assertEquals("1.3.6.1.5.5.1", control.getOid());
    assertEquals("0x61 0x62 0x63 0x64 0x65 0x66 ", Strings.dumpBytes((byte[]) control.getValue()));
    assertTrue(control.isCritical());
    internalAbandonRequest.addControl(control);
    control = (org.apache.directory.api.ldap.codec.api.CodecControl<?>) controls.get("1.3.6.1.5.5.2");
    assertEquals("1.3.6.1.5.5.2", control.getOid());
    assertEquals("0x67 0x68 0x69 0x6A 0x6B 0x6C ", Strings.dumpBytes((byte[]) control.getValue()));
    assertFalse(control.isCritical());
    internalAbandonRequest.addControl(control);
    control = (org.apache.directory.api.ldap.codec.api.CodecControl<?>) controls.get("1.3.6.1.5.5.3");
    assertEquals("1.3.6.1.5.5.3", control.getOid());
    assertEquals("", Strings.dumpBytes((byte[]) control.getValue()));
    assertTrue(control.isCritical());
    internalAbandonRequest.addControl(control);
    control = (org.apache.directory.api.ldap.codec.api.CodecControl<?>) controls.get("1.3.6.1.5.5.4");
    assertEquals("1.3.6.1.5.5.4", control.getOid());
    assertEquals("", Strings.dumpBytes((byte[]) control.getValue()));
    assertFalse(control.isCritical());
    internalAbandonRequest.addControl(control);
    // Check the encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(internalAbandonRequest);
        // Check the length
        assertEquals(0x64, 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) 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) 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) CodecControl(org.apache.directory.api.ldap.codec.api.CodecControl) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 3 with AbandonRequestDecorator

use of org.apache.directory.api.ldap.codec.decorators.AbandonRequestDecorator in project directory-ldap-api by apache.

the class LdapControlTest method testDecodeRequestWithControls.

/**
 * Test the decoding of a Request with controls
 */
@SuppressWarnings("unchecked")
@Test
public void testDecodeRequestWithControls() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x64);
    stream.put(new byte[] { // LDAPMessage ::=SEQUENCE {
    0x30, // LDAPMessage ::=SEQUENCE {
    0x62, // 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 }
    0x5A, // Control ::= SEQUENCE {
    0x30, // Control ::= SEQUENCE {
    0x1A, // controlType LDAPOID,
    0x04, // controlType LDAPOID,
    0x0D, '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '1', // criticality BOOLEAN DEFAULT FALSE,
    0x01, // criticality BOOLEAN DEFAULT FALSE,
    0x01, // criticality BOOLEAN DEFAULT FALSE,
    (byte) 0xFF, // controlValue OCTET STRING OPTIONAL }
    0x04, // controlValue OCTET STRING OPTIONAL }
    0x06, 'a', 'b', 'c', 'd', 'e', 'f', // Control ::= SEQUENCE {
    0x30, // Control ::= SEQUENCE {
    0x17, // controlType LDAPOID,
    0x04, // controlType LDAPOID,
    0x0D, '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '2', // controlValue OCTET STRING OPTIONAL }
    0x04, // controlValue OCTET STRING OPTIONAL }
    0x06, 'g', 'h', 'i', 'j', 'k', 'l', // Control ::= SEQUENCE {
    0x30, // Control ::= SEQUENCE {
    0x12, // controlType LDAPOID,
    0x04, // controlType LDAPOID,
    0x0D, '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '3', // criticality BOOLEAN DEFAULT FALSE}
    0x01, // criticality BOOLEAN DEFAULT FALSE}
    0x01, // criticality BOOLEAN DEFAULT FALSE}
    (byte) 0xFF, // Control ::= SEQUENCE {
    0x30, // Control ::= SEQUENCE {
    0x0F, // controlType LDAPOID}
    0x04, // controlType LDAPOID}
    0x0D, '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '4' });
    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(4, controls.size());
    CodecControl<Control> control = (org.apache.directory.api.ldap.codec.api.CodecControl<Control>) controls.get("1.3.6.1.5.5.1");
    assertEquals("1.3.6.1.5.5.1", control.getOid());
    assertEquals("0x61 0x62 0x63 0x64 0x65 0x66 ", Strings.dumpBytes((byte[]) control.getValue()));
    assertTrue(control.isCritical());
    internalAbandonRequest.addControl(control);
    control = (org.apache.directory.api.ldap.codec.api.CodecControl<Control>) controls.get("1.3.6.1.5.5.2");
    assertEquals("1.3.6.1.5.5.2", control.getOid());
    assertEquals("0x67 0x68 0x69 0x6A 0x6B 0x6C ", Strings.dumpBytes((byte[]) control.getValue()));
    assertFalse(control.isCritical());
    internalAbandonRequest.addControl(control);
    control = (org.apache.directory.api.ldap.codec.api.CodecControl<Control>) controls.get("1.3.6.1.5.5.3");
    assertEquals("1.3.6.1.5.5.3", control.getOid());
    assertEquals("", Strings.dumpBytes((byte[]) control.getValue()));
    assertTrue(control.isCritical());
    internalAbandonRequest.addControl(control);
    control = (org.apache.directory.api.ldap.codec.api.CodecControl<Control>) controls.get("1.3.6.1.5.5.4");
    assertEquals("1.3.6.1.5.5.4", control.getOid());
    assertEquals("", Strings.dumpBytes((byte[]) control.getValue()));
    assertFalse(control.isCritical());
    internalAbandonRequest.addControl(control);
    // Check the encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(internalAbandonRequest);
        // Check the length
        assertEquals(0x64, 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) 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) 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) CodecControl(org.apache.directory.api.ldap.codec.api.CodecControl) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 4 with AbandonRequestDecorator

use of org.apache.directory.api.ldap.codec.decorators.AbandonRequestDecorator 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 5 with AbandonRequestDecorator

use of org.apache.directory.api.ldap.codec.decorators.AbandonRequestDecorator in project directory-ldap-api by apache.

the class InitAbandonRequest method action.

/**
 * {@inheritDoc}
 */
public void action(LdapMessageContainer<AbandonRequestDecorator> container) throws DecoderException {
    // Create the AbandonRequest LdapMessage instance and store it in the container
    AbandonRequest internalAbandonRequest = new AbandonRequestImpl();
    internalAbandonRequest.setMessageId(container.getMessageId());
    AbandonRequestDecorator abandonRequest = new AbandonRequestDecorator(container.getLdapCodecService(), internalAbandonRequest);
    container.setMessage(abandonRequest);
    // The current TLV should be a integer
    // We get it and store it in MessageId
    TLV tlv = container.getCurrentTLV();
    BerValue value = tlv.getValue();
    if ((value == null) || (value.getData() == null)) {
        String msg = I18n.err(I18n.ERR_04075);
        LOG.error(msg);
        // This will generate a PROTOCOL_ERROR
        throw new DecoderException(msg);
    }
    try {
        int abandonnedMessageId = IntegerDecoder.parse(value, 0, Integer.MAX_VALUE);
        abandonRequest.setAbandoned(abandonnedMessageId);
        if (IS_DEBUG) {
            LOG.debug("AbandonMessage Id has been decoded : {}", Integer.valueOf(abandonnedMessageId));
        }
        container.setGrammarEndAllowed(true);
        return;
    } catch (IntegerDecoderException ide) {
        LOG.error(I18n.err(I18n.ERR_04076, Strings.dumpBytes(value.getData()), ide.getMessage()));
        // This will generate a PROTOCOL_ERROR
        throw new DecoderException(ide.getMessage(), ide);
    }
}
Also used : IntegerDecoderException(org.apache.directory.api.asn1.ber.tlv.IntegerDecoderException) DecoderException(org.apache.directory.api.asn1.DecoderException) BerValue(org.apache.directory.api.asn1.ber.tlv.BerValue) IntegerDecoderException(org.apache.directory.api.asn1.ber.tlv.IntegerDecoderException) AbandonRequest(org.apache.directory.api.ldap.model.message.AbandonRequest) AbandonRequestImpl(org.apache.directory.api.ldap.model.message.AbandonRequestImpl) AbandonRequestDecorator(org.apache.directory.api.ldap.codec.decorators.AbandonRequestDecorator) TLV(org.apache.directory.api.asn1.ber.tlv.TLV)

Aggregations

AbandonRequestDecorator (org.apache.directory.api.ldap.codec.decorators.AbandonRequestDecorator)6 AbandonRequest (org.apache.directory.api.ldap.model.message.AbandonRequest)6 DecoderException (org.apache.directory.api.asn1.DecoderException)5 AbandonRequestImpl (org.apache.directory.api.ldap.model.message.AbandonRequestImpl)5 ByteBuffer (java.nio.ByteBuffer)4 EncoderException (org.apache.directory.api.asn1.EncoderException)4 Asn1Decoder (org.apache.directory.api.asn1.ber.Asn1Decoder)4 LdapMessageContainer (org.apache.directory.api.ldap.codec.api.LdapMessageContainer)4 AbstractCodecServiceTest (org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)4 Control (org.apache.directory.api.ldap.model.message.Control)4 Test (org.junit.Test)4 CodecControl (org.apache.directory.api.ldap.codec.api.CodecControl)3 BerValue (org.apache.directory.api.asn1.ber.tlv.BerValue)1 IntegerDecoderException (org.apache.directory.api.asn1.ber.tlv.IntegerDecoderException)1 TLV (org.apache.directory.api.asn1.ber.tlv.TLV)1 AddRequestDecorator (org.apache.directory.api.ldap.codec.decorators.AddRequestDecorator)1 AddResponseDecorator (org.apache.directory.api.ldap.codec.decorators.AddResponseDecorator)1 BindRequestDecorator (org.apache.directory.api.ldap.codec.decorators.BindRequestDecorator)1 BindResponseDecorator (org.apache.directory.api.ldap.codec.decorators.BindResponseDecorator)1 CompareRequestDecorator (org.apache.directory.api.ldap.codec.decorators.CompareRequestDecorator)1