Search in sources :

Example 31 with Attribute

use of org.apache.directory.api.ldap.model.entry.Attribute in project directory-ldap-api by apache.

the class ModifyRequestTest method testDecodeModifyRequestAddOperationModificationTypeEmptyValsWithControls.

/**
 * Test the decoding of a ModifyRequest with an add operation, and a
 * modification with a type and an empty vals wuth controls
 */
@Test
public void testDecodeModifyRequestAddOperationModificationTypeEmptyValsWithControls() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x54);
    stream.put(new byte[] { 0x30, // LdapMessage
    0x52, 0x02, 0x01, // Message ID : 49
    0x31, 0x66, // ModifyRequest
    0x30, 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', 0x30, 0x0C, 0x30, 0x0A, 0x0A, 0x01, 0x00, 0x30, 0x05, 0x04, 0x01, 'l', 0x31, 0x00, (byte) 0xA0, // A control
    0x1B, 0x30, 0x19, 0x04, 0x17, 0x32, 0x2E, 0x31, 0x36, 0x2E, 0x38, 0x34, 0x30, 0x2E, 0x31, 0x2E, 0x31, 0x31, 0x33, 0x37, 0x33, 0x30, 0x2E, 0x33, 0x2E, 0x34, 0x2E, 0x32 });
    String decodedPdu = Strings.dumpBytes(stream.array());
    stream.flip();
    // Allocate a LdapMessage Container
    LdapMessageContainer<ModifyRequestDecorator> ldapMessageContainer = new LdapMessageContainer<ModifyRequestDecorator>(codec);
    // Decode a ModifyRequest PDU
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    // Check the decoded PDU
    ModifyRequest modifyRequest = ldapMessageContainer.getMessage();
    assertEquals(49, modifyRequest.getMessageId());
    assertEquals("cn=testModify,ou=users,ou=system", modifyRequest.getName().toString());
    Object[] modifications = modifyRequest.getModifications().toArray();
    assertEquals(1, modifications.length);
    Modification modification = (Modification) modifications[0];
    Attribute attributeValue = modification.getAttribute();
    assertEquals("l", Strings.toLowerCaseAscii(attributeValue.getUpId()));
    assertEquals(0, attributeValue.size());
    // Check the Control
    Map<String, Control> controls = modifyRequest.getControls();
    assertEquals(1, controls.size());
    @SuppressWarnings("unchecked") CodecControl<Control> control = (org.apache.directory.api.ldap.codec.api.CodecControl<Control>) modifyRequest.getControl("2.16.840.1.113730.3.4.2");
    assertEquals("2.16.840.1.113730.3.4.2", control.getOid());
    assertEquals("", Strings.dumpBytes((byte[]) control.getValue()));
    // Check the encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(modifyRequest);
        // Check the length
        assertEquals(0x54, bb.limit());
        String encodedPdu = Strings.dumpBytes(bb.array());
        assertEquals(encodedPdu, decodedPdu);
    } catch (EncoderException ee) {
        ee.printStackTrace();
        fail(ee.getMessage());
    }
}
Also used : ModifyRequestDecorator(org.apache.directory.api.ldap.codec.decorators.ModifyRequestDecorator) LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) Modification(org.apache.directory.api.ldap.model.entry.Modification) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) ModifyRequest(org.apache.directory.api.ldap.model.message.ModifyRequest) ByteBuffer(java.nio.ByteBuffer) DecoderException(org.apache.directory.api.asn1.DecoderException) EncoderException(org.apache.directory.api.asn1.EncoderException) Control(org.apache.directory.api.ldap.model.message.Control) CodecControl(org.apache.directory.api.ldap.codec.api.CodecControl) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) CodecControl(org.apache.directory.api.ldap.codec.api.CodecControl) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 32 with Attribute

use of org.apache.directory.api.ldap.model.entry.Attribute in project directory-ldap-api by apache.

the class ModifyRequestTest method testDecodeModifyRequestAddOperationModificationTypeEmptyVals.

/**
 * Test the decoding of a ModifyRequest with an add operation, and a
 * modification with a type and an empty vals
 */
@Test
public void testDecodeModifyRequestAddOperationModificationTypeEmptyVals() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x37);
    stream.put(new byte[] { 0x30, // LdapMessage
    0x35, 0x02, 0x01, // Message ID : 49
    0x31, 0x66, // ModifyRequest
    0x30, 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', 0x30, 0x0C, 0x30, 0x0A, 0x0A, 0x01, 0x00, 0x30, 0x05, 0x04, 0x01, 'l', 0x31, 0x00 });
    String decodedPdu = Strings.dumpBytes(stream.array());
    stream.flip();
    // Allocate a LdapMessage Container
    LdapMessageContainer<ModifyRequestDecorator> ldapMessageContainer = new LdapMessageContainer<ModifyRequestDecorator>(codec);
    // Decode a ModifyRequest PDU
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    // Check the decoded PDU
    ModifyRequest modifyRequest = ldapMessageContainer.getMessage();
    assertEquals(49, modifyRequest.getMessageId());
    assertEquals("cn=testModify,ou=users,ou=system", modifyRequest.getName().toString());
    Object[] modifications = modifyRequest.getModifications().toArray();
    assertEquals(1, modifications.length);
    Modification modification = (Modification) modifications[0];
    Attribute attributeValue = modification.getAttribute();
    assertEquals("l", Strings.toLowerCaseAscii(attributeValue.getUpId()));
    assertEquals(0, attributeValue.size());
    // Check the encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(modifyRequest);
        // Check the length
        assertEquals(0x37, bb.limit());
        String encodedPdu = Strings.dumpBytes(bb.array());
        assertEquals(encodedPdu, decodedPdu);
    } catch (EncoderException ee) {
        ee.printStackTrace();
        fail(ee.getMessage());
    }
}
Also used : ModifyRequestDecorator(org.apache.directory.api.ldap.codec.decorators.ModifyRequestDecorator) LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) Modification(org.apache.directory.api.ldap.model.entry.Modification) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) ModifyRequest(org.apache.directory.api.ldap.model.message.ModifyRequest) ByteBuffer(java.nio.ByteBuffer) DecoderException(org.apache.directory.api.asn1.DecoderException) EncoderException(org.apache.directory.api.asn1.EncoderException) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 33 with Attribute

use of org.apache.directory.api.ldap.model.entry.Attribute in project directory-ldap-api by apache.

the class SearchResultEntryDecorator method encode.

/**
 * Encode the SearchResultEntry message to a PDU.
 * <br>
 * SearchResultEntry :
 * <pre>
 * 0x64 LL
 *   0x04 LL objectName
 *   0x30 LL attributes
 *     0x30 LL partialAttributeList
 *       0x04 LL type
 *       0x31 LL vals
 *         0x04 LL attributeValue
 *         ...
 *         0x04 LL attributeValue
 *     ...
 *     0x30 LL partialAttributeList
 *       0x04 LL type
 *       0x31 LL vals
 *         0x04 LL attributeValue
 *         ...
 *         0x04 LL attributeValue
 * </pre>
 *
 * @param buffer The buffer where to put the PDU
 * @return The PDU.
 */
@Override
public ByteBuffer encode(ByteBuffer buffer) throws EncoderException {
    try {
        // The SearchResultEntry Tag
        buffer.put(LdapCodecConstants.SEARCH_RESULT_ENTRY_TAG);
        buffer.put(TLV.getBytes(searchResultEntryLength));
        // The objectName
        BerValue.encode(buffer, objectNameBytes);
        // The attributes sequence
        buffer.put(UniversalTag.SEQUENCE.getValue());
        buffer.put(TLV.getBytes(attributesLength));
        // The partial attribute list
        Entry entry = getEntry();
        if ((entry != null) && (entry.size() != 0)) {
            int attributeNumber = 0;
            // Compute the attributes length
            for (Attribute attribute : entry) {
                // The partial attribute list sequence
                buffer.put(UniversalTag.SEQUENCE.getValue());
                int localAttributeLength = attributeLength.get(attributeNumber);
                buffer.put(TLV.getBytes(localAttributeLength));
                // The attribute type
                BerValue.encode(buffer, attributeIds.get(attributeNumber));
                // The values
                buffer.put(UniversalTag.SET.getValue());
                int localValuesLength = valuesLength.get(attributeNumber);
                buffer.put(TLV.getBytes(localValuesLength));
                if (attribute.size() > 0) {
                    for (Value value : attribute) {
                        BerValue.encode(buffer, value.getBytes());
                    }
                }
                // Go to the next attribute number
                attributeNumber++;
            }
        }
    } catch (BufferOverflowException boe) {
        throw new EncoderException(I18n.err(I18n.ERR_04005), boe);
    }
    return buffer;
}
Also used : EncoderException(org.apache.directory.api.asn1.EncoderException) SearchResultEntry(org.apache.directory.api.ldap.model.message.SearchResultEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) Value(org.apache.directory.api.ldap.model.entry.Value) BerValue(org.apache.directory.api.asn1.ber.tlv.BerValue) BufferOverflowException(java.nio.BufferOverflowException)

Example 34 with Attribute

use of org.apache.directory.api.ldap.model.entry.Attribute in project directory-ldap-api by apache.

the class ModifyRequestTest method testRequestWith1ModificationBase64Value.

/**
 * Test parsing of a request with a Modification element with Base64 Value
 * @throws NamingException
 */
@Test
public void testRequestWith1ModificationBase64Value() throws LdapException {
    Dsmlv2Parser parser = null;
    try {
        parser = newParser();
        parser.setInput(ModifyRequestTest.class.getResource("request_with_1_modification_base64_value.xml").openStream(), "UTF-8");
        parser.parse();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    ModifyRequest modifyRequest = (ModifyRequest) parser.getBatchRequest().getCurrentRequest();
    Collection<Modification> modifications = modifyRequest.getModifications();
    assertEquals(1, modifications.size());
    Modification modification = modifications.iterator().next();
    Attribute attribute = modification.getAttribute();
    assertEquals(ModificationOperation.ADD_ATTRIBUTE, modification.getOperation());
    assertEquals("directreport", attribute.getId());
    String expected = new String(new byte[] { 'c', 'n', '=', 'E', 'm', 'm', 'a', 'n', 'u', 'e', 'l', ' ', 'L', (byte) 0xc3, (byte) 0xa9, 'c', 'h', 'a', 'r', 'n', 'y', ',', ' ', 'o', 'u', '=', 'p', 'e', 'o', 'p', 'l', 'e', ',', ' ', 'd', 'c', '=', 'e', 'x', 'a', 'm', 'p', 'l', 'e', ',', ' ', 'd', 'c', '=', 'c', 'o', 'm' }, StandardCharsets.UTF_8);
    assertEquals(expected, attribute.get().getValue());
}
Also used : Modification(org.apache.directory.api.ldap.model.entry.Modification) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) Dsmlv2Parser(org.apache.directory.api.dsmlv2.Dsmlv2Parser) ModifyRequest(org.apache.directory.api.ldap.model.message.ModifyRequest) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) Test(org.junit.Test) AbstractTest(org.apache.directory.api.dsmlv2.AbstractTest)

Example 35 with Attribute

use of org.apache.directory.api.ldap.model.entry.Attribute in project directory-ldap-api by apache.

the class ModifyRequestTest method testRequestWithModificationWithEmptyValue.

/**
 * Test parsing of a request with a Modification element with an empty value
 * @throws NamingException
 */
@Test
public void testRequestWithModificationWithEmptyValue() throws LdapException {
    Dsmlv2Parser parser = null;
    try {
        parser = newParser();
        parser.setInput(ModifyRequestTest.class.getResource("request_with_modification_with_empty_value.xml").openStream(), "UTF-8");
        parser.parse();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    ModifyRequest modifyRequest = (ModifyRequest) parser.getBatchRequest().getCurrentRequest();
    Collection<Modification> modifications = modifyRequest.getModifications();
    assertEquals(1, modifications.size());
    Modification modification = modifications.iterator().next();
    assertEquals(ModificationOperation.ADD_ATTRIBUTE, modification.getOperation());
    Attribute attribute = modification.getAttribute();
    assertEquals("directreport", attribute.getId());
    assertEquals(1, attribute.size());
    assertEquals("", attribute.get().getValue());
}
Also used : Modification(org.apache.directory.api.ldap.model.entry.Modification) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) Dsmlv2Parser(org.apache.directory.api.dsmlv2.Dsmlv2Parser) ModifyRequest(org.apache.directory.api.ldap.model.message.ModifyRequest) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) Test(org.junit.Test) AbstractTest(org.apache.directory.api.dsmlv2.AbstractTest)

Aggregations

Attribute (org.apache.directory.api.ldap.model.entry.Attribute)269 Test (org.junit.Test)180 DefaultAttribute (org.apache.directory.api.ldap.model.entry.DefaultAttribute)168 Entry (org.apache.directory.api.ldap.model.entry.Entry)94 Modification (org.apache.directory.api.ldap.model.entry.Modification)56 Value (org.apache.directory.api.ldap.model.entry.Value)52 DefaultEntry (org.apache.directory.api.ldap.model.entry.DefaultEntry)46 DefaultModification (org.apache.directory.api.ldap.model.entry.DefaultModification)35 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)23 EncoderException (org.apache.directory.api.asn1.EncoderException)20 ByteBuffer (java.nio.ByteBuffer)18 DecoderException (org.apache.directory.api.asn1.DecoderException)18 Asn1Decoder (org.apache.directory.api.asn1.ber.Asn1Decoder)18 LdapMessageContainer (org.apache.directory.api.ldap.codec.api.LdapMessageContainer)18 AbstractCodecServiceTest (org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)18 SearchResultEntry (org.apache.directory.api.ldap.model.message.SearchResultEntry)18 LdapInvalidAttributeValueException (org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException)16 LdifEntry (org.apache.directory.api.ldap.model.ldif.LdifEntry)16 ModifyRequest (org.apache.directory.api.ldap.model.message.ModifyRequest)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13