Search in sources :

Example 6 with ResponseCarryingException

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

the class ModifyDNRequestTest method testDecodeModifyDNRequestBadRDN.

/**
 * Test the decoding of a bad Rdn ModifyDNRequest
 */
@Test
public void testDecodeModifyDNRequestBadRDN() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x48);
    stream.put(new byte[] { 0x30, // LDAPMessage ::= SEQUENCE {
    0x46, 0x02, 0x01, // messageID MessageID
    0x01, 0x6C, // CHOICE { ..., modifyDNRequest ModifyDNRequest,
    0x41, // entry LDAPDN,
    0x04, 0x20, 'c', 'n', '=', 't', 'e', 's', 't', 'M', 'o', 'd', 'i', 'f', 'y', ',', 'o', 'u', '=', 'u', 's', 'e', 'r', 's', ',', 'o', 'u', '=', 's', 'y', 's', 't', 'e', 'm', // newrdn RelativeLDAPDN,
    0x04, 0x0F, 'c', 'n', ':', 't', 'e', 's', 't', 'D', 'N', 'M', 'o', 'd', 'i', 'f', 'y', 0x01, 0x01, // deleteoldrdn BOOLEAN,
    0x00, // newSuperior [0] LDAPDN OPTIONAL }
    (byte) 0x80, 0x09, 'o', 'u', '=', 's', 'y', 's', 't', 'e', 'm' });
    stream.flip();
    // Allocate a ModifyRequest Container
    LdapMessageContainer<ModifyDnRequestDecorator> ldapMessageContainer = new LdapMessageContainer<ModifyDnRequestDecorator>(codec);
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        assertTrue(de instanceof ResponseCarryingException);
        Message response = ((ResponseCarryingException) de).getResponse();
        assertTrue(response instanceof ModifyDnResponseImpl);
        assertEquals(ResultCodeEnum.INVALID_DN_SYNTAX, ((ModifyDnResponseImpl) response).getLdapResult().getResultCode());
        return;
    }
    fail("We should not reach this point");
}
Also used : LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) 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) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) ModifyDnResponseImpl(org.apache.directory.api.ldap.model.message.ModifyDnResponseImpl) ModifyDnRequestDecorator(org.apache.directory.api.ldap.codec.decorators.ModifyDnRequestDecorator) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 7 with ResponseCarryingException

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

the class DelRequestTest method testDecodeDelRequestBadDN.

/**
 * Test the decoding of a full DelRequest
 */
@Test
public void testDecodeDelRequestBadDN() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x27);
    stream.put(new byte[] { 0x30, // LDAPMessage ::= SEQUENCE {
    0x25, 0x02, 0x01, // messageID MessageID
    0x01, // DelRequest ::= [APPLICATION 10] LDAPDN;
    0x4A, 0x20, 'c', 'n', ':', 't', 'e', 's', 't', 'M', 'o', 'd', 'i', 'f', 'y', ',', 'o', 'u', '=', 'u', 's', 'e', 'r', 's', ',', 'o', 'u', '=', 's', 'y', 's', 't', 'e', 'm' });
    stream.flip();
    // Allocate a LdapMessage Container
    LdapMessageContainer<DeleteRequestDecorator> container = new LdapMessageContainer<DeleteRequestDecorator>(codec);
    // Decode a DelRequest PDU
    try {
        ldapDecoder.decode(stream, container);
    } catch (DecoderException de) {
        assertTrue(de instanceof ResponseCarryingException);
        Message response = ((ResponseCarryingException) de).getResponse();
        assertTrue(response instanceof DeleteResponseImpl);
        assertEquals(ResultCodeEnum.INVALID_DN_SYNTAX, ((DeleteResponseImpl) response).getLdapResult().getResultCode());
        return;
    }
    fail("We should not reach this point");
}
Also used : LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) DecoderException(org.apache.directory.api.asn1.DecoderException) ResponseCarryingException(org.apache.directory.api.ldap.codec.api.ResponseCarryingException) DeleteResponseImpl(org.apache.directory.api.ldap.model.message.DeleteResponseImpl) DeleteRequestDecorator(org.apache.directory.api.ldap.codec.decorators.DeleteRequestDecorator) Message(org.apache.directory.api.ldap.model.message.Message) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 8 with ResponseCarryingException

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

the class AddAddRequestAttributeType method action.

/**
 * {@inheritDoc}
 */
public void action(LdapMessageContainer<AddRequestDecorator> container) throws DecoderException {
    AddRequestDecorator addRequest = container.getMessage();
    TLV tlv = container.getCurrentTLV();
    // Store the type. It can't be null.
    if (tlv.getLength() == 0) {
        String msg = I18n.err(I18n.ERR_04086);
        LOG.error(msg);
        AddResponseImpl response = new AddResponseImpl(addRequest.getMessageId());
        throw new ResponseCarryingException(msg, response, ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, addRequest.getEntry().getDn(), null);
    }
    String type = Strings.utf8ToString(tlv.getValue().getData());
    try {
        addRequest.addAttributeType(type);
    } catch (LdapException ne) {
        String msg = I18n.err(I18n.ERR_04087);
        LOG.error(msg);
        AddResponseImpl response = new AddResponseImpl(addRequest.getMessageId());
        throw new ResponseCarryingException(msg, response, ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, addRequest.getEntry().getDn(), ne);
    }
    if (IS_DEBUG) {
        LOG.debug("Adding type {}", type);
    }
}
Also used : ResponseCarryingException(org.apache.directory.api.ldap.codec.api.ResponseCarryingException) AddResponseImpl(org.apache.directory.api.ldap.model.message.AddResponseImpl) AddRequestDecorator(org.apache.directory.api.ldap.codec.decorators.AddRequestDecorator) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) TLV(org.apache.directory.api.asn1.ber.tlv.TLV)

Example 9 with ResponseCarryingException

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

the class StoreAddRequestEntryName method action.

/**
 * {@inheritDoc}
 */
public void action(LdapMessageContainer<AddRequestDecorator> container) throws DecoderException {
    AddRequestDecorator addRequest = container.getMessage();
    TLV tlv = container.getCurrentTLV();
    // Store the entry. It can't be null
    if (tlv.getLength() == 0) {
        String msg = I18n.err(I18n.ERR_04085);
        LOG.error(msg);
        AddResponseImpl response = new AddResponseImpl(addRequest.getMessageId());
        // Not 100% sure though ...
        throw new ResponseCarryingException(msg, response, ResultCodeEnum.NAMING_VIOLATION, Dn.EMPTY_DN, null);
    } else {
        Dn entryDn = null;
        byte[] dnBytes = tlv.getValue().getData();
        String dnStr = Strings.utf8ToString(dnBytes);
        try {
            entryDn = new Dn(dnStr);
        } catch (LdapInvalidDnException ine) {
            String msg = "Invalid Dn given : " + dnStr + " (" + Strings.dumpBytes(dnBytes) + ") is invalid";
            LOG.error("{} : {}", msg, ine.getMessage());
            AddResponseImpl response = new AddResponseImpl(addRequest.getMessageId());
            throw new ResponseCarryingException(msg, response, ResultCodeEnum.INVALID_DN_SYNTAX, Dn.EMPTY_DN, ine);
        }
        addRequest.setEntryDn(entryDn);
    }
    LOG.debug("Adding an entry with Dn : {}", addRequest.getEntry());
}
Also used : ResponseCarryingException(org.apache.directory.api.ldap.codec.api.ResponseCarryingException) AddResponseImpl(org.apache.directory.api.ldap.model.message.AddResponseImpl) Dn(org.apache.directory.api.ldap.model.name.Dn) AddRequestDecorator(org.apache.directory.api.ldap.codec.decorators.AddRequestDecorator) TLV(org.apache.directory.api.asn1.ber.tlv.TLV) LdapInvalidDnException(org.apache.directory.api.ldap.model.exception.LdapInvalidDnException)

Example 10 with ResponseCarryingException

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

the class StoreModifyDnRequestEntryName 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 entry;
    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());
            ModifyDnResponseImpl response = new ModifyDnResponseImpl(modifyDnRequest.getMessageId());
            throw new ResponseCarryingException(msg, response, ResultCodeEnum.INVALID_DN_SYNTAX, Dn.EMPTY_DN, ine);
        }
        modifyDnRequest.setName(entry);
    }
    if (IS_DEBUG) {
        LOG.debug("Modifying Dn {}", entry);
    }
}
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)

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