Search in sources :

Example 1 with AddResponseImpl

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

the class AddRequestTest method testDecodeAddRequestbadDN.

/**
 * Test the decoding of a AddRequest
 */
@Test
public void testDecodeAddRequestbadDN() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x59);
    stream.put(new byte[] { 0x30, // LDAPMessage ::= SEQUENCE {
    0x57, 0x02, 0x01, // messageID MessageID
    0x01, 0x68, // CHOICE { ..., addRequest AddRequest, ...
    0x52, // 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', // attributes AttributeList }
    0x30, // AttributeList ::= SEQUENCE OF SEQUENCE {
    0x2E, 0x30, // attribute 1
    0x0c, 0x04, 0x01, // type AttributeDescription,
    'l', 0x31, // vals SET OF AttributeValue }
    0x07, 0x04, 0x05, 'P', 'a', 'r', 'i', 's', 0x30, // attribute 2
    0x1E, // type AttributeDescription,
    0x04, 0x05, 'a', 't', 't', 'r', 's', 0x31, // vals SET
    0x15, // }
    0x04, 0x05, 't', 'e', 's', 't', '1', 0x04, 0x05, 't', 'e', 's', 't', '2', 0x04, 0x05, 't', 'e', 's', 't', '3' });
    stream.flip();
    // Allocate a LdapMessage Container
    Asn1Container ldapMessageContainer = new LdapMessageContainer<MessageDecorator<? extends Message>>(codec);
    // Decode a AddRequest message
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        assertTrue(de instanceof ResponseCarryingException);
        Message response = ((ResponseCarryingException) de).getResponse();
        assertTrue(response instanceof AddResponseImpl);
        assertEquals(ResultCodeEnum.INVALID_DN_SYNTAX, ((AddResponseImpl) 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) Asn1Container(org.apache.directory.api.asn1.ber.Asn1Container) Message(org.apache.directory.api.ldap.model.message.Message) AddResponseImpl(org.apache.directory.api.ldap.model.message.AddResponseImpl) 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 2 with AddResponseImpl

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

the class AddRequestTest method testDecodeAddRequestNullEntry.

/**
 * Test the decoding of a AddRequest with a null entry
 */
@Test
public void testDecodeAddRequestNullEntry() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x39);
    stream.put(new byte[] { 0x30, // LDAPMessage ::= SEQUENCE {
    0x37, 0x02, 0x01, // messageID MessageID
    0x01, 0x68, // CHOICE { ..., addRequest AddRequest, ...
    0x26, // entry LDAPDN,
    0x04, 0x00, // attributes AttributeList }
    0x30, // AttributeList ::= SEQUENCE OF SEQUENCE {
    0x2E, 0x30, // attribute 1
    0x0c, 0x04, 0x01, // type AttributeDescription,
    'l', 0x31, // vals SET OF AttributeValue }
    0x07, 0x04, 0x05, 'P', 'a', 'r', 'i', 's', 0x30, // attribute 2
    0x1E, // type AttributeDescription,
    0x04, 0x05, 'a', 't', 't', 'r', 's', 0x31, // vals SET
    0x15, // }
    0x04, 0x05, 't', 'e', 's', 't', '1', 0x04, 0x05, 't', 'e', 's', 't', '2', 0x04, 0x05, 't', 'e', 's', 't', '3' });
    stream.flip();
    // Allocate a LdapMessage Container
    Asn1Container ldapMessageContainer = new LdapMessageContainer<MessageDecorator<? extends Message>>(codec);
    // Decode a AddRequest message
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        assertTrue(de instanceof ResponseCarryingException);
        Message response = ((ResponseCarryingException) de).getResponse();
        assertTrue(response instanceof AddResponseImpl);
        assertEquals(ResultCodeEnum.NAMING_VIOLATION, ((AddResponseImpl) 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) Asn1Container(org.apache.directory.api.asn1.ber.Asn1Container) Message(org.apache.directory.api.ldap.model.message.Message) AddResponseImpl(org.apache.directory.api.ldap.model.message.AddResponseImpl) 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 3 with AddResponseImpl

use of org.apache.directory.api.ldap.model.message.AddResponseImpl 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 4 with AddResponseImpl

use of org.apache.directory.api.ldap.model.message.AddResponseImpl 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 5 with AddResponseImpl

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

the class InitAddResponse method action.

/**
 * {@inheritDoc}
 */
public void action(LdapMessageContainer<AddResponseDecorator> container) throws DecoderException {
    // Now, we can allocate the AddResponse Object
    AddResponseDecorator addResponse = new AddResponseDecorator(container.getLdapCodecService(), new AddResponseImpl(container.getMessageId()));
    container.setMessage(addResponse);
    // We will check that the request is not null
    TLV tlv = container.getCurrentTLV();
    int expectedLength = tlv.getLength();
    if (expectedLength == 0) {
        String msg = I18n.err(I18n.ERR_04088);
        LOG.error(msg);
        throw new DecoderException(msg);
    }
    LOG.debug("Add Response");
}
Also used : DecoderException(org.apache.directory.api.asn1.DecoderException) AddResponseImpl(org.apache.directory.api.ldap.model.message.AddResponseImpl) AddResponseDecorator(org.apache.directory.api.ldap.codec.decorators.AddResponseDecorator) TLV(org.apache.directory.api.asn1.ber.tlv.TLV)

Aggregations

AddResponseImpl (org.apache.directory.api.ldap.model.message.AddResponseImpl)6 ResponseCarryingException (org.apache.directory.api.ldap.codec.api.ResponseCarryingException)5 DecoderException (org.apache.directory.api.asn1.DecoderException)4 ByteBuffer (java.nio.ByteBuffer)3 Asn1Container (org.apache.directory.api.asn1.ber.Asn1Container)3 Asn1Decoder (org.apache.directory.api.asn1.ber.Asn1Decoder)3 TLV (org.apache.directory.api.asn1.ber.tlv.TLV)3 LdapMessageContainer (org.apache.directory.api.ldap.codec.api.LdapMessageContainer)3 AbstractCodecServiceTest (org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)3 Message (org.apache.directory.api.ldap.model.message.Message)3 Test (org.junit.Test)3 AddRequestDecorator (org.apache.directory.api.ldap.codec.decorators.AddRequestDecorator)2 AddResponseDecorator (org.apache.directory.api.ldap.codec.decorators.AddResponseDecorator)1 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)1 LdapInvalidDnException (org.apache.directory.api.ldap.model.exception.LdapInvalidDnException)1 Dn (org.apache.directory.api.ldap.model.name.Dn)1