Search in sources :

Example 81 with EncoderException

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

the class AbandonRequestDecorator method encode.

// -------------------------------------------------------------------------
// The Decorator methods
// -------------------------------------------------------------------------
/**
 * Encode the Abandon protocolOp part
 */
@Override
public ByteBuffer encode(ByteBuffer buffer) throws EncoderException {
    try {
        // The tag
        buffer.put(LdapCodecConstants.ABANDON_REQUEST_TAG);
        // The length. It has to be evaluated depending on
        // the abandoned messageId value.
        buffer.put((byte) BerValue.getNbBytes(getAbandoned()));
        // The abandoned messageId
        buffer.put(BerValue.getBytes(getAbandoned()));
    } catch (BufferOverflowException boe) {
        String msg = I18n.err(I18n.ERR_04005);
        throw new EncoderException(msg, boe);
    }
    return buffer;
}
Also used : EncoderException(org.apache.directory.api.asn1.EncoderException) BufferOverflowException(java.nio.BufferOverflowException)

Example 82 with EncoderException

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

the class ValueTest method testEncodeBitString.

@Test
public void testEncodeBitString() {
    BitString bs = new BitString(10);
    bs.setBit(9);
    ByteBuffer buffer = ByteBuffer.allocate(5);
    try {
        BerValue.encode(buffer, bs);
    } catch (EncoderException ee) {
        fail();
    }
    assertEquals("0x03 0x03 0x06 0x00 0x40 ", Asn1StringUtils.dumpBytes(buffer.array()));
}
Also used : EncoderException(org.apache.directory.api.asn1.EncoderException) BitString(org.apache.directory.api.asn1.util.BitString) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 83 with EncoderException

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

the class LdapEncoder method encodeMessage.

/**
 * Generate the PDU which contains the encoded object.
 *
 * The generation is done in two phases :
 * - first, we compute the length of each part and the
 * global PDU length
 * - second, we produce the PDU.
 *
 * <pre>
 * 0x30 L1
 *   |
 *   +--&gt; 0x02 L2 MessageId
 *   +--&gt; ProtocolOp
 *   +--&gt; Controls
 *
 * L2 = Length(MessageId)
 * L1 = Length(0x02) + Length(L2) + L2 + Length(ProtocolOp) + Length(Controls)
 * LdapMessageLength = Length(0x30) + Length(L1) + L1
 * </pre>
 *
 * @param message The message to encode
 * @return A ByteBuffer that contains the PDU
 * @throws EncoderException If anything goes wrong.
 */
public ByteBuffer encodeMessage(Message message) throws EncoderException {
    MessageDecorator<? extends Message> decorator = MessageDecorator.getDecorator(codec, message);
    int length = computeMessageLength(decorator);
    ByteBuffer buffer = ByteBuffer.allocate(length);
    try {
        try {
            // The LdapMessage Sequence
            buffer.put(UniversalTag.SEQUENCE.getValue());
            // The length has been calculated by the computeLength method
            buffer.put(TLV.getBytes(decorator.getMessageLength()));
        } catch (BufferOverflowException boe) {
            throw new EncoderException(I18n.err(I18n.ERR_04005), boe);
        }
        // The message Id
        BerValue.encode(buffer, message.getMessageId());
        // Add the protocolOp part
        decorator.encode(buffer);
        // Do the same thing for Controls, if any.
        Map<String, Control> controls = decorator.getControls();
        if ((controls != null) && (controls.size() > 0)) {
            // Encode the controls
            buffer.put((byte) LdapCodecConstants.CONTROLS_TAG);
            buffer.put(TLV.getBytes(decorator.getControlsLength()));
            // Encode each control
            for (Control control : controls.values()) {
                encodeControl(buffer, control);
                // The OctetString tag if the value is not null
                int controlValueLength = ((CodecControl<?>) control).computeLength();
                if (controlValueLength > 0) {
                    buffer.put(UniversalTag.OCTET_STRING.getValue());
                    buffer.put(TLV.getBytes(controlValueLength));
                    // And now, the value
                    ((org.apache.directory.api.ldap.codec.api.CodecControl<?>) control).encode(buffer);
                }
            }
        }
    } catch (EncoderException ee) {
        throw new MessageEncoderException(message.getMessageId(), ee.getMessage(), ee);
    }
    buffer.flip();
    return buffer;
}
Also used : ByteBuffer(java.nio.ByteBuffer) EncoderException(org.apache.directory.api.asn1.EncoderException) Control(org.apache.directory.api.ldap.model.message.Control) BufferOverflowException(java.nio.BufferOverflowException)

Example 84 with EncoderException

use of org.apache.directory.api.asn1.EncoderException 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 85 with EncoderException

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

the class CancelRequestTest method testDecodeCancel.

/**
 * Test the normal Cancel message
 */
@Test
public void testDecodeCancel() {
    Asn1Decoder cancelDecoder = new CancelDecoder();
    ByteBuffer stream = ByteBuffer.allocate(0x05);
    stream.put(new byte[] { 0x30, 0x03, 0x02, 0x01, 0x01 }).flip();
    String decodedPdu = Strings.dumpBytes(stream.array());
    // Allocate a Cancel Container
    Asn1Container cancelContainer = new CancelContainer();
    // Decode a Cancel message
    try {
        cancelDecoder.decode(stream, cancelContainer);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    CancelRequestDecorator cancel = ((CancelContainer) cancelContainer).getCancel();
    assertEquals(1, cancel.getCancelId());
    // Check the encoding
    try {
        ByteBuffer bb = cancel.encodeInternal();
        String encodedPdu = Strings.dumpBytes(bb.array());
        assertEquals(encodedPdu, decodedPdu);
    } catch (EncoderException ee) {
        ee.printStackTrace();
        fail(ee.getMessage());
    }
}
Also used : DecoderException(org.apache.directory.api.asn1.DecoderException) EncoderException(org.apache.directory.api.asn1.EncoderException) Asn1Container(org.apache.directory.api.asn1.ber.Asn1Container) CancelDecoder(org.apache.directory.api.ldap.extras.extended.ads_impl.cancel.CancelDecoder) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) CancelRequestDecorator(org.apache.directory.api.ldap.extras.extended.ads_impl.cancel.CancelRequestDecorator) CancelContainer(org.apache.directory.api.ldap.extras.extended.ads_impl.cancel.CancelContainer) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

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