Search in sources :

Example 1 with SortKey

use of org.apache.directory.api.ldap.model.message.controls.SortKey in project directory-ldap-api by apache.

the class SortRequestControlTest method testDecodeControl.

@Test
public void testDecodeControl() throws Exception {
    ByteBuffer buffer = ByteBuffer.allocate(16);
    buffer.put(new byte[] { 0x30, 0x0E, 0x30, 0x0C, 0x04, 0x02, 'c', 'n', (byte) 0x80, 0x03, 'o', 'i', 'd', (byte) 0x81, 0x01, 0x00 });
    buffer.flip();
    SortRequestDecorator decorator = new SortRequestDecorator(codec);
    SortRequest control = (SortRequest) decorator.decode(buffer.array());
    assertEquals(1, control.getSortKeys().size());
    SortKey sk = control.getSortKeys().get(0);
    assertEquals("cn", sk.getAttributeTypeDesc());
    assertEquals("oid", sk.getMatchingRuleId());
    assertFalse(sk.isReverseOrder());
    // default value of false reverseOrder will not be encoded
    int skipBytes = 3;
    ByteBuffer encoded = ByteBuffer.allocate(buffer.capacity() - skipBytes);
    decorator.computeLength();
    decorator.encode(encoded);
    assertFalse(Arrays.equals(buffer.array(), encoded.array()));
    assertEquals(buffer.array().length - skipBytes, encoded.array().length);
}
Also used : SortRequest(org.apache.directory.api.ldap.model.message.controls.SortRequest) SortKey(org.apache.directory.api.ldap.model.message.controls.SortKey) ByteBuffer(java.nio.ByteBuffer) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest) Test(org.junit.Test)

Example 2 with SortKey

use of org.apache.directory.api.ldap.model.message.controls.SortKey in project directory-ldap-api by apache.

the class SortRequestControlTest method testDecodeControlWithoutMrOid.

@Test
public void testDecodeControlWithoutMrOid() throws Exception {
    ByteBuffer buffer = ByteBuffer.allocate(11);
    buffer.put(new byte[] { 0x30, 0x09, 0x30, 0x07, 0x04, 0x02, 'c', 'n', (byte) 0x81, 0x01, (byte) 0xFF });
    buffer.flip();
    SortRequestDecorator decorator = new SortRequestDecorator(codec);
    SortRequest control = (SortRequest) decorator.decode(buffer.array());
    assertEquals(1, control.getSortKeys().size());
    SortKey sk = control.getSortKeys().get(0);
    assertEquals("cn", sk.getAttributeTypeDesc());
    assertNull(sk.getMatchingRuleId());
    assertTrue(sk.isReverseOrder());
    ByteBuffer encoded = ByteBuffer.allocate(buffer.capacity());
    decorator.computeLength();
    decorator.encode(encoded);
    assertTrue(Arrays.equals(buffer.array(), encoded.array()));
}
Also used : SortRequest(org.apache.directory.api.ldap.model.message.controls.SortRequest) SortKey(org.apache.directory.api.ldap.model.message.controls.SortKey) ByteBuffer(java.nio.ByteBuffer) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest) Test(org.junit.Test)

Example 3 with SortKey

use of org.apache.directory.api.ldap.model.message.controls.SortKey in project directory-ldap-api by apache.

the class SortRequestControlTest method testDecodeControlWithAtDescOnly.

@Test
public void testDecodeControlWithAtDescOnly() throws Exception {
    ByteBuffer buffer = ByteBuffer.allocate(8);
    buffer.put(new byte[] { 0x30, 0x06, 0x30, 0x04, 0x04, 0x02, 'c', 'n' });
    buffer.flip();
    SortRequestDecorator decorator = new SortRequestDecorator(codec);
    SortRequest control = (SortRequest) decorator.decode(buffer.array());
    assertEquals(1, control.getSortKeys().size());
    SortKey sk = control.getSortKeys().get(0);
    assertEquals("cn", sk.getAttributeTypeDesc());
    assertNull(sk.getMatchingRuleId());
    assertFalse(sk.isReverseOrder());
    ByteBuffer encoded = ByteBuffer.allocate(buffer.capacity());
    decorator.computeLength();
    decorator.encode(encoded);
    assertTrue(Arrays.equals(buffer.array(), encoded.array()));
}
Also used : SortRequest(org.apache.directory.api.ldap.model.message.controls.SortRequest) SortKey(org.apache.directory.api.ldap.model.message.controls.SortKey) ByteBuffer(java.nio.ByteBuffer) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest) Test(org.junit.Test)

Example 4 with SortKey

use of org.apache.directory.api.ldap.model.message.controls.SortKey in project directory-ldap-api by apache.

the class SortRequestDecorator method encode.

/**
 * {@inheritDoc}
 */
@Override
public ByteBuffer encode(ByteBuffer buffer) throws EncoderException {
    if (buffer == null) {
        throw new EncoderException(I18n.err(I18n.ERR_04023));
    }
    buffer.put(UniversalTag.SEQUENCE.getValue());
    buffer.put(TLV.getBytes(sortReqLen));
    List<SortKey> lst = getSortKeys();
    for (int i = 0; i < lst.size(); i++) {
        SortKey sk = lst.get(i);
        int skLen = sortKeyLenList.get(i);
        buffer.put(UniversalTag.SEQUENCE.getValue());
        buffer.put(TLV.getBytes(skLen));
        BerValue.encode(buffer, sk.getAttributeTypeDesc());
        String mrId = sk.getMatchingRuleId();
        if (mrId != null) {
            buffer.put((byte) ORDERING_RULE_TAG);
            byte[] value = Asn1StringUtils.getBytesUtf8(mrId);
            buffer.put(TLV.getBytes(value.length));
            buffer.put(value);
        }
        if (sk.isReverseOrder()) {
            buffer.put((byte) REVERSE_ORDER_TAG);
            buffer.put((byte) 0x01);
            buffer.put(BerValue.TRUE_VALUE);
        }
    }
    return buffer;
}
Also used : EncoderException(org.apache.directory.api.asn1.EncoderException) SortKey(org.apache.directory.api.ldap.model.message.controls.SortKey)

Example 5 with SortKey

use of org.apache.directory.api.ldap.model.message.controls.SortKey in project directory-ldap-api by apache.

the class SortRequestDecorator method computeLength.

/**
 * @return the control length.
 */
@Override
public int computeLength() {
    sortReqLen = 0;
    sortKeyLenList.clear();
    valueLength = 0;
    for (SortKey sk : getSortKeys()) {
        int skLen = 0;
        byte[] atBytes = Strings.getBytesUtf8(sk.getAttributeTypeDesc());
        skLen += 1 + TLV.getNbBytes(atBytes.length) + atBytes.length;
        if (sk.getMatchingRuleId() != null) {
            byte[] mrBytes = Strings.getBytesUtf8(sk.getMatchingRuleId());
            skLen += 1 + TLV.getNbBytes(mrBytes.length) + mrBytes.length;
        }
        if (sk.isReverseOrder()) {
            // reverse order flag
            skLen += 1 + 1 + 1;
        }
        sortKeyLenList.add(skLen);
        // the sequence
        sortReqLen += 1 + TLV.getNbBytes(skLen) + skLen;
    }
    valueLength = 1 + TLV.getNbBytes(sortReqLen) + sortReqLen;
    return valueLength;
}
Also used : SortKey(org.apache.directory.api.ldap.model.message.controls.SortKey)

Aggregations

SortKey (org.apache.directory.api.ldap.model.message.controls.SortKey)7 ByteBuffer (java.nio.ByteBuffer)5 AbstractCodecServiceTest (org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)5 SortRequest (org.apache.directory.api.ldap.model.message.controls.SortRequest)5 Test (org.junit.Test)5 EncoderException (org.apache.directory.api.asn1.EncoderException)1