Search in sources :

Example 16 with ResponseCarryingException

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

the class StoreCompareRequestAttributeDesc method action.

/**
 * {@inheritDoc}
 */
public void action(LdapMessageContainer<CompareRequestDecorator> container) throws DecoderException {
    // Get the CompareRequest Object
    CompareRequest compareRequest = container.getMessage();
    // Get the Value and store it in the CompareRequest
    TLV tlv = container.getCurrentTLV();
    // Dn
    if (tlv.getLength() == 0) {
        String msg = I18n.err(I18n.ERR_04093);
        LOG.error(msg);
        CompareResponseImpl response = new CompareResponseImpl(compareRequest.getMessageId());
        throw new ResponseCarryingException(msg, response, ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, compareRequest.getName(), null);
    }
    String type = Strings.utf8ToString(tlv.getValue().getData());
    compareRequest.setAttributeId(type);
    if (IS_DEBUG) {
        LOG.debug("Comparing attribute description {}", compareRequest.getAttributeId());
    }
}
Also used : CompareRequest(org.apache.directory.api.ldap.model.message.CompareRequest) ResponseCarryingException(org.apache.directory.api.ldap.codec.api.ResponseCarryingException) CompareResponseImpl(org.apache.directory.api.ldap.model.message.CompareResponseImpl) TLV(org.apache.directory.api.asn1.ber.tlv.TLV)

Example 17 with ResponseCarryingException

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

the class StoreCompareRequestEntryName method action.

/**
 * {@inheritDoc}
 */
public void action(LdapMessageContainer<CompareRequestDecorator> container) throws DecoderException {
    CompareRequest compareRequest = container.getMessage();
    // Get the Value and store it in the CompareRequest
    TLV tlv = container.getCurrentTLV();
    Dn entry;
    // Dn
    if (tlv.getLength() == 0) {
        // This will generate a PROTOCOL_ERROR
        throw new DecoderException(I18n.err(I18n.ERR_04089));
    } else {
        byte[] dnBytes = tlv.getValue().getData();
        String dnStr = Strings.utf8ToString(dnBytes);
        try {
            entry = new Dn(dnStr);
        } catch (LdapInvalidDnException ine) {
            String msg = "Invalid Dn given : " + dnStr + " (" + Strings.dumpBytes(dnBytes) + ") is invalid";
            LOG.error("{} : {}", msg, ine.getMessage());
            CompareResponseImpl response = new CompareResponseImpl(compareRequest.getMessageId());
            throw new ResponseCarryingException(msg, response, ResultCodeEnum.INVALID_DN_SYNTAX, Dn.EMPTY_DN, ine);
        }
        compareRequest.setName(entry);
    }
    if (IS_DEBUG) {
        LOG.debug("Comparing Dn {}", entry);
    }
}
Also used : DecoderException(org.apache.directory.api.asn1.DecoderException) CompareRequest(org.apache.directory.api.ldap.model.message.CompareRequest) ResponseCarryingException(org.apache.directory.api.ldap.codec.api.ResponseCarryingException) CompareResponseImpl(org.apache.directory.api.ldap.model.message.CompareResponseImpl) Dn(org.apache.directory.api.ldap.model.name.Dn) TLV(org.apache.directory.api.asn1.ber.tlv.TLV) LdapInvalidDnException(org.apache.directory.api.ldap.model.exception.LdapInvalidDnException)

Example 18 with ResponseCarryingException

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

the class StoreModifyDnRequestNewSuperior method action.

/**
 * {@inheritDoc}
 */
public void action(LdapMessageContainer<ModifyDnRequestDecorator> container) throws DecoderException {
    ModifyDnRequest modifyDnRequest = container.getMessage();
    // Get the Value and store it in the modifyDNRequest
    TLV tlv = container.getCurrentTLV();
    // We have to handle the special case of a 0 length matched
    // Dn
    Dn newSuperior = Dn.EMPTY_DN;
    if (tlv.getLength() == 0) {
        if (modifyDnRequest.getDeleteOldRdn()) {
            // This will generate a PROTOCOL_ERROR
            throw new DecoderException(I18n.err(I18n.ERR_04092));
        } else {
            LOG.warn("The new superior is null, so we will change the entry");
        }
        modifyDnRequest.setNewSuperior(newSuperior);
    } else {
        byte[] dnBytes = tlv.getValue().getData();
        String dnStr = Strings.utf8ToString(dnBytes);
        try {
            newSuperior = new Dn(dnStr);
        } catch (LdapInvalidDnException ine) {
            String msg = "Invalid new superior Dn given : " + dnStr + " (" + Strings.dumpBytes(dnBytes) + ") is invalid";
            LOG.error("{} : {}", msg, ine.getMessage());
            ModifyDnResponseImpl response = new ModifyDnResponseImpl(modifyDnRequest.getMessageId());
            throw new ResponseCarryingException(msg, response, ResultCodeEnum.INVALID_DN_SYNTAX, modifyDnRequest.getName(), ine);
        }
        modifyDnRequest.setNewSuperior(newSuperior);
    }
    // We can have an END transition
    container.setGrammarEndAllowed(true);
    if (IS_DEBUG) {
        LOG.debug("New superior Dn {}", newSuperior);
    }
}
Also used : DecoderException(org.apache.directory.api.asn1.DecoderException) ResponseCarryingException(org.apache.directory.api.ldap.codec.api.ResponseCarryingException) Dn(org.apache.directory.api.ldap.model.name.Dn) ModifyDnResponseImpl(org.apache.directory.api.ldap.model.message.ModifyDnResponseImpl) ModifyDnRequest(org.apache.directory.api.ldap.model.message.ModifyDnRequest) TLV(org.apache.directory.api.asn1.ber.tlv.TLV) LdapInvalidDnException(org.apache.directory.api.ldap.model.exception.LdapInvalidDnException)

Example 19 with ResponseCarryingException

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

the class InitSaslBind method action.

/**
 * {@inheritDoc}
 */
public void action(LdapMessageContainer<BindRequestDecorator> container) throws DecoderException {
    BindRequest bindRequestMessage = container.getMessage();
    TLV tlv = container.getCurrentTLV();
    // We will check that the sasl is not null
    if (tlv.getLength() == 0) {
        String msg = I18n.err(I18n.ERR_04079);
        LOG.error(msg);
        BindResponseImpl response = new BindResponseImpl(bindRequestMessage.getMessageId());
        throw new ResponseCarryingException(msg, response, ResultCodeEnum.INVALID_CREDENTIALS, bindRequestMessage.getDn(), null);
    }
    bindRequestMessage.setSimple(false);
    if (IS_DEBUG) {
        LOG.debug("The SaslCredential has been created");
    }
}
Also used : ResponseCarryingException(org.apache.directory.api.ldap.codec.api.ResponseCarryingException) BindRequest(org.apache.directory.api.ldap.model.message.BindRequest) BindResponseImpl(org.apache.directory.api.ldap.model.message.BindResponseImpl) TLV(org.apache.directory.api.asn1.ber.tlv.TLV)

Example 20 with ResponseCarryingException

use of org.apache.directory.api.ldap.codec.api.ResponseCarryingException 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)

Aggregations

ResponseCarryingException (org.apache.directory.api.ldap.codec.api.ResponseCarryingException)28 DecoderException (org.apache.directory.api.asn1.DecoderException)20 Message (org.apache.directory.api.ldap.model.message.Message)16 ByteBuffer (java.nio.ByteBuffer)14 Asn1Decoder (org.apache.directory.api.asn1.ber.Asn1Decoder)14 LdapMessageContainer (org.apache.directory.api.ldap.codec.api.LdapMessageContainer)14 AbstractCodecServiceTest (org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)14 Test (org.junit.Test)14 TLV (org.apache.directory.api.asn1.ber.tlv.TLV)12 LdapInvalidDnException (org.apache.directory.api.ldap.model.exception.LdapInvalidDnException)8 Dn (org.apache.directory.api.ldap.model.name.Dn)8 ModifyDnResponseImpl (org.apache.directory.api.ldap.model.message.ModifyDnResponseImpl)6 AddResponseImpl (org.apache.directory.api.ldap.model.message.AddResponseImpl)5 ModifyRequestDecorator (org.apache.directory.api.ldap.codec.decorators.ModifyRequestDecorator)4 CompareResponseImpl (org.apache.directory.api.ldap.model.message.CompareResponseImpl)4 ModifyResponseImpl (org.apache.directory.api.ldap.model.message.ModifyResponseImpl)4 Asn1Container (org.apache.directory.api.asn1.ber.Asn1Container)3 ModifyDnRequestDecorator (org.apache.directory.api.ldap.codec.decorators.ModifyDnRequestDecorator)3 BindResponseImpl (org.apache.directory.api.ldap.model.message.BindResponseImpl)3 ModifyDnRequest (org.apache.directory.api.ldap.model.message.ModifyDnRequest)3