Search in sources :

Example 1 with ResponseCarryingMessageException

use of org.apache.directory.api.ldap.model.exception.ResponseCarryingMessageException in project directory-ldap-api by apache.

the class LdapProtocolDecoder method decode.

/**
 * Decode an incoming buffer into LDAP messages. The result can be 0, 1 or many
 * LDAP messages, which will be stored into the array the caller has created.
 *
 * @param buffer The incoming byte buffer
 * @param messageContainer The LdapMessageContainer which will be used to store the
 * message being decoded. If the message is not fully decoded, the ucrrent state
 * is stored into this container
 * @param decodedMessages The list of decoded messages
 * @throws Exception If the decoding failed
 */
private void decode(ByteBuffer buffer, LdapMessageContainer<MessageDecorator<? extends Message>> messageContainer, List<Message> decodedMessages) throws DecoderException {
    buffer.mark();
    while (buffer.hasRemaining()) {
        try {
            if (IS_DEBUG) {
                CODEC_LOG.debug(I18n.msg(I18n.MSG_14000_DECODING_PDU));
                int size = buffer.limit();
                int position = buffer.position();
                int pduLength = size - position;
                byte[] array = new byte[pduLength];
                System.arraycopy(buffer.array(), position, array, 0, pduLength);
                if (array.length == 0) {
                    CODEC_LOG.debug(I18n.msg(I18n.MSG_14001_NULL_BUFFER));
                } else {
                    CODEC_LOG.debug(Strings.dumpBytes(array));
                }
            }
            asn1Decoder.decode(buffer, messageContainer);
            if (messageContainer.getState() == TLVStateEnum.PDU_DECODED) {
                if (IS_DEBUG) {
                    CODEC_LOG.debug(I18n.msg(I18n.MSG_14002_DECODED_LDAP_MESSAGE, messageContainer.getMessage()));
                }
                Message message = messageContainer.getMessage();
                decodedMessages.add(message);
                messageContainer.clean();
            }
        } catch (ResponseCarryingException rce) {
            buffer.clear();
            messageContainer.clean();
            // Transform the DecoderException message to a MessageException
            ResponseCarryingMessageException rcme = new ResponseCarryingMessageException(rce.getMessage(), rce);
            rcme.setResponse(rce.getResponse());
            throw rcme;
        } catch (DecoderException de) {
            buffer.clear();
            messageContainer.clean();
            // TODO : This is certainly not the way we should handle such an exception !
            throw new ResponseCarryingException(de.getMessage(), de);
        }
    }
}
Also used : DecoderException(org.apache.directory.api.asn1.DecoderException) ResponseCarryingException(org.apache.directory.api.ldap.codec.api.ResponseCarryingException) Message(org.apache.directory.api.ldap.model.message.Message) ResponseCarryingMessageException(org.apache.directory.api.ldap.model.exception.ResponseCarryingMessageException)

Example 2 with ResponseCarryingMessageException

use of org.apache.directory.api.ldap.model.exception.ResponseCarryingMessageException in project directory-ldap-api by apache.

the class LdapDecoderTest method decode.

/**
 * Decode an incoming buffer into LDAP messages. The result can be 0, 1 or many
 * LDAP messages, which will be stored into the array the caller has created.
 *
 * @param buffer The incoming byte buffer
 * @param messageContainer The LdapMessageContainer which will be used to store the
 * message being decoded. If the message is not fully decoded, the ucrrent state
 * is stored into this container
 * @param decodedMessages The list of decoded messages
 * @throws Exception If the decoding failed
 */
private void decode(ByteBuffer buffer, LdapMessageContainer<MessageDecorator<? extends Message>> messageContainer, List<Message> decodedMessages) throws DecoderException {
    buffer.mark();
    while (buffer.hasRemaining()) {
        try {
            asn1Decoder.decode(buffer, messageContainer);
            if (messageContainer.getState() == TLVStateEnum.PDU_DECODED) {
                Message message = messageContainer.getMessage();
                decodedMessages.add(message);
                messageContainer.clean();
            }
        } catch (DecoderException de) {
            buffer.clear();
            messageContainer.clean();
            if (de instanceof ResponseCarryingException) {
                // Transform the DecoderException message to a MessageException
                ResponseCarryingMessageException rcme = new ResponseCarryingMessageException(de.getMessage());
                rcme.setResponse(((ResponseCarryingException) de).getResponse());
                throw rcme;
            } else {
                // TODO : This is certainly not the way we should handle such an exception !
                throw new ResponseCarryingException(de.getMessage());
            }
        }
    }
}
Also used : DecoderException(org.apache.directory.api.asn1.DecoderException) ResponseCarryingException(org.apache.directory.api.ldap.codec.api.ResponseCarryingException) Message(org.apache.directory.api.ldap.model.message.Message) ResponseCarryingMessageException(org.apache.directory.api.ldap.model.exception.ResponseCarryingMessageException)

Aggregations

DecoderException (org.apache.directory.api.asn1.DecoderException)2 ResponseCarryingException (org.apache.directory.api.ldap.codec.api.ResponseCarryingException)2 ResponseCarryingMessageException (org.apache.directory.api.ldap.model.exception.ResponseCarryingMessageException)2 Message (org.apache.directory.api.ldap.model.message.Message)2