Search in sources :

Example 1 with UpdateControls

use of org.apache.directory.api.ldap.extras.extended.endTransaction.UpdateControls in project directory-ldap-api by apache.

the class EndTransactionResponseTest method testEndTransactionResponseUpdateControls.

/**
 * Test the decoding of a EndTransactionResponse with updateControls
 */
@Test
public void testEndTransactionResponseUpdateControls() throws DecoderException, EncoderException {
    Asn1Decoder decoder = new Asn1Decoder();
    ByteBuffer bb = ByteBuffer.allocate(0xAC);
    bb.put(new byte[] { // EndTransactionResponse ::= SEQUENCE {
    0x30, // EndTransactionResponse ::= SEQUENCE {
    (byte) 0x81, // EndTransactionResponse ::= SEQUENCE {
    (byte) 0xA9, // UpdateControls
    0x30, // UpdateControls
    (byte) 0x81, // UpdateControls
    (byte) 0xA6, // updateControl
    0x30, // updateControl
    0x5F, // messageID
    0x02, // messageID
    0x01, // messageID
    0x01, // controls
    0x30, // controls
    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', // updateControl
    0x30, // updateControl
    0x43, // messageID
    0x02, // messageID
    0x01, // messageID
    0x02, // controls
    0x30, // controls
    0x3E, // 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' });
    String decodedPdu = Strings.dumpBytes(bb.array());
    bb.flip();
    EndTransactionResponseContainer container = new EndTransactionResponseContainer();
    try {
        decoder.decode(bb, container);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    EndTransactionResponse endTransactionResponse = container.getEndTransactionResponse();
    assertEquals(-1, endTransactionResponse.getFailedMessageId());
    assertEquals(2, endTransactionResponse.getUpdateControls().size());
    UpdateControls updateControls1 = endTransactionResponse.getUpdateControls().get(0);
    assertEquals(1, updateControls1.getMessageId());
    assertNotNull(updateControls1.getControls());
    assertEquals(4, updateControls1.getControls().size());
    for (Control control : updateControls1.getControls()) {
        switch(control.getOid()) {
            case "1.3.6.1.5.5.1":
                assertTrue(control.isCritical());
                assertEquals("abcdef", Strings.utf8ToString(((CodecControl<?>) control).getValue()));
                break;
            case "1.3.6.1.5.5.2":
                assertFalse(control.isCritical());
                assertEquals("ghijkl", Strings.utf8ToString(((CodecControl<?>) control).getValue()));
                break;
            case "1.3.6.1.5.5.3":
                assertTrue(control.isCritical());
                assertNull(((CodecControl<?>) control).getValue());
                break;
            case "1.3.6.1.5.5.4":
                assertFalse(control.isCritical());
                assertNull(((CodecControl<?>) control).getValue());
                break;
            default:
                fail();
                break;
        }
    }
    UpdateControls updateControls2 = endTransactionResponse.getUpdateControls().get(1);
    assertEquals(2, updateControls2.getMessageId());
    assertNotNull(updateControls2.getControls());
    assertEquals(3, updateControls2.getControls().size());
    for (Control control : updateControls2.getControls()) {
        switch(control.getOid()) {
            case "1.3.6.1.5.5.2":
                assertFalse(control.isCritical());
                assertEquals("ghijkl", Strings.utf8ToString(((CodecControl<?>) control).getValue()));
                break;
            case "1.3.6.1.5.5.3":
                assertTrue(control.isCritical());
                assertNull(((CodecControl<?>) control).getValue());
                break;
            case "1.3.6.1.5.5.4":
                assertFalse(control.isCritical());
                assertNull(((CodecControl<?>) control).getValue());
                break;
            default:
                fail();
                break;
        }
    }
    // Check the length
    assertEquals(0xAC, ((EndTransactionResponseDecorator) endTransactionResponse).computeLengthInternal());
    // Check the encoding
    ByteBuffer bb1 = ((EndTransactionResponseDecorator) endTransactionResponse).encodeInternal();
    String encodedPdu = Strings.dumpBytes(bb1.array());
    assertEquals(encodedPdu, decodedPdu);
}
Also used : DecoderException(org.apache.directory.api.asn1.DecoderException) Control(org.apache.directory.api.ldap.model.message.Control) CodecControl(org.apache.directory.api.ldap.codec.api.CodecControl) UpdateControls(org.apache.directory.api.ldap.extras.extended.endTransaction.UpdateControls) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) CodecControl(org.apache.directory.api.ldap.codec.api.CodecControl) EndTransactionResponse(org.apache.directory.api.ldap.extras.extended.endTransaction.EndTransactionResponse) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 2 with UpdateControls

use of org.apache.directory.api.ldap.extras.extended.endTransaction.UpdateControls in project directory-ldap-api by apache.

the class EndTransactionResponseDecorator method computeLengthInternal.

/**
 * Compute the EndTransactionResponse extended operation length
 * <pre>
 * 0x30 L1
 *   |
 *  [+-- 0x02 L2 failed message ID]
 *  [+-- 0x30 L3 updateControls SEQUENCE OF
 *         |
 *         +-- 0x30 L4 updateControls SEQUENCE
 *               |
 *               +-- 0x02 L5 messageID
 *               +-- <controls>]
 *
 * </pre>
 */
/* No qualifier */
int computeLengthInternal() {
    globalSequenceLength = 0;
    if (endTransactionResponse.getFailedMessageId() >= 0) {
        // We have had a failure, there is no updateControls just the failed MessageID
        globalSequenceLength = 1 + 1 + BerValue.getNbBytes(endTransactionResponse.getFailedMessageId());
        // The message ID length is always below 128, so we only need 1 byte for the global length
        return 1 + 1 + globalSequenceLength;
    } else {
        // If it's a success, we won't have a messageId, just update controls (if any)
        int updateControlsSize = getUpdateControls().size();
        if (updateControlsSize > 0) {
            updateControlsLength = new int[updateControlsSize];
            controlsLengths = new int[updateControlsSize];
            controlLengths = new int[updateControlsSize][];
            int messageControlsCount = 0;
            updateSequenceLength = 0;
            // Ok, process the updateControls
            for (UpdateControls updateControls : getUpdateControls()) {
                // The message ID, 0x02 LL and the ID
                updateControlsLength[messageControlsCount] = 1 + 1 + BerValue.getNbBytes(updateControls.getMessageId());
                // The controls
                int controlNumber = updateControls.getControls().size();
                if (controlNumber > 0) {
                    int controlCount = 0;
                    controlLengths[messageControlsCount] = new int[controlNumber];
                    for (Control control : updateControls.getControls()) {
                        controlLengths[messageControlsCount][controlCount] = LdapEncoder.computeControlLength(control);
                        controlsLengths[messageControlsCount] += 1 + TLV.getNbBytes(controlLengths[messageControlsCount][controlCount]) + controlLengths[messageControlsCount][controlCount];
                        controlCount++;
                    }
                    int controlsLength = controlsLengths[messageControlsCount];
                    updateControlsLength[messageControlsCount] += 1 + TLV.getNbBytes(controlsLength) + controlsLength;
                }
                updateSequenceLength += 1 + TLV.getNbBytes(updateControlsLength[messageControlsCount]) + updateControlsLength[messageControlsCount];
                messageControlsCount++;
            }
            globalSequenceLength = 1 + TLV.getNbBytes(updateSequenceLength) + updateSequenceLength;
            return 1 + TLV.getNbBytes(globalSequenceLength) + globalSequenceLength;
        } else {
            // No update control, return immediately
            return 0;
        }
    }
}
Also used : Control(org.apache.directory.api.ldap.model.message.Control) CodecControl(org.apache.directory.api.ldap.codec.api.CodecControl) UpdateControls(org.apache.directory.api.ldap.extras.extended.endTransaction.UpdateControls)

Example 3 with UpdateControls

use of org.apache.directory.api.ldap.extras.extended.endTransaction.UpdateControls in project directory-ldap-api by apache.

the class EndTransactionResponseDecorator method encodeInternal.

/**
 * Encodes the EndTransactionResponse extended operation.
 *
 * @return A ByteBuffer that contains the encoded PDU
 * @throws org.apache.directory.api.asn1.EncoderException If anything goes wrong.
 */
/* No qualifier */
ByteBuffer encodeInternal() throws EncoderException {
    ByteBuffer bb = ByteBuffer.allocate(computeLengthInternal());
    bb.put(UniversalTag.SEQUENCE.getValue());
    bb.put(TLV.getBytes(globalSequenceLength));
    // The failed message id, if any
    if (getFailedMessageId() >= 0) {
        // We have had an error, just encode the messageId
        BerValue.encode(bb, getFailedMessageId());
    } else {
        // No error, just updateControls
        bb.put(UniversalTag.SEQUENCE.getValue());
        bb.put(TLV.getBytes(updateSequenceLength));
        int updateControlsNb = 0;
        for (UpdateControls updateControls : getUpdateControls()) {
            // The updateControls length
            bb.put(UniversalTag.SEQUENCE.getValue());
            bb.put(TLV.getBytes(updateControlsLength[updateControlsNb]));
            // The message ID
            BerValue.encode(bb, updateControls.getMessageId());
            // The controls sequence
            bb.put(UniversalTag.SEQUENCE.getValue());
            bb.put(TLV.getBytes(controlsLengths[updateControlsNb]));
            // The controls
            int controlNb = 0;
            for (Control control : updateControls.getControls()) {
                // The control SEQUENCE
                bb.put(UniversalTag.SEQUENCE.getValue());
                bb.put(TLV.getBytes(controlLengths[updateControlsNb][controlNb]));
                // The control OID
                BerValue.encode(bb, control.getOid());
                // The criticality, if true
                if (control.isCritical()) {
                    BerValue.encode(bb, true);
                }
                // compute the value length
                int valueLength = ((CodecControl<?>) control).computeLength();
                if (valueLength > 0) {
                    bb.put(UniversalTag.OCTET_STRING.getValue());
                    bb.put(TLV.getBytes(valueLength));
                    ((CodecControl<?>) control).encode(bb);
                }
                controlNb++;
            }
            updateControlsNb++;
        }
    }
    return bb;
}
Also used : Control(org.apache.directory.api.ldap.model.message.Control) CodecControl(org.apache.directory.api.ldap.codec.api.CodecControl) UpdateControls(org.apache.directory.api.ldap.extras.extended.endTransaction.UpdateControls) CodecControl(org.apache.directory.api.ldap.codec.api.CodecControl) ByteBuffer(java.nio.ByteBuffer)

Aggregations

CodecControl (org.apache.directory.api.ldap.codec.api.CodecControl)3 UpdateControls (org.apache.directory.api.ldap.extras.extended.endTransaction.UpdateControls)3 Control (org.apache.directory.api.ldap.model.message.Control)3 ByteBuffer (java.nio.ByteBuffer)2 DecoderException (org.apache.directory.api.asn1.DecoderException)1 Asn1Decoder (org.apache.directory.api.asn1.ber.Asn1Decoder)1 EndTransactionResponse (org.apache.directory.api.ldap.extras.extended.endTransaction.EndTransactionResponse)1 Test (org.junit.Test)1