Search in sources :

Example 1 with AddRequest

use of org.apache.directory.api.ldap.model.message.AddRequest in project ldapchai by ldapchai.

the class ApacheLdapProviderImpl method createEntry.

public void createEntry(final String entryDN, final Set<String> baseObjectClasses, final Map<String, String> stringAttributes) throws ChaiOperationException, ChaiUnavailableException, IllegalStateException {
    activityPreCheck();
    getInputValidator().createEntry(entryDN, baseObjectClasses, stringAttributes);
    try {
        final AddRequest addRequest = new AddRequestImpl();
        final Entry entry = new DefaultEntry();
        entry.setDn(entryDN);
        for (final String baseObjectClass : baseObjectClasses) {
            entry.add(ChaiConstant.ATTR_LDAP_OBJECTCLASS, baseObjectClass);
        }
        for (final Map.Entry<String, String> entryIter : stringAttributes.entrySet()) {
            final String name = entryIter.getKey();
            final String value = entryIter.getValue();
            entry.add(name, value);
        }
        final AddResponse response = connection.add(addRequest);
        processResponse(response);
    } catch (LdapException e) {
        throw ChaiOperationException.forErrorMessage(e.getMessage());
    }
}
Also used : AddRequest(org.apache.directory.api.ldap.model.message.AddRequest) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) AddRequestImpl(org.apache.directory.api.ldap.model.message.AddRequestImpl) AddResponse(org.apache.directory.api.ldap.model.message.AddResponse) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 2 with AddRequest

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

the class AddRequestTest method testDecodeAddRequestEmptyAttributeValueWithControl.

/**
 * Test the decoding of a AddRequest with a empty attributeList and a
 * control
 */
@Test
public void testDecodeAddRequestEmptyAttributeValueWithControl() throws NamingException {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x51);
    stream.put(new byte[] { 0x30, // LDAPMessage ::= SEQUENCE {
    0x4F, 0x02, 0x01, // messageID MessageID
    0x01, 0x68, // CHOICE { ..., addRequest AddRequest, ...
    0x2D, // 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 {
    0x09, 0x30, // attribute 1
    0x07, 0x04, 0x01, // type AttributeDescription,
    'l', 0x31, 0x02, 0x04, 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<AddRequestDecorator> container = new LdapMessageContainer<AddRequestDecorator>(codec);
    // Decode a AddRequest message
    try {
        ldapDecoder.decode(stream, container);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    AddRequest addRequest = container.getMessage();
    // Check the decoded message
    assertEquals(1, addRequest.getMessageId());
    assertEquals("cn=testModify,ou=users,ou=system", addRequest.getEntryDn().toString());
    Entry entry = addRequest.getEntry();
    assertEquals(1, entry.size());
    Attribute attribute = entry.get("l");
    assertEquals("l", Strings.toLowerCaseAscii(attribute.getId()));
    for (Value value : attribute) {
        assertEquals("", value.getValue());
    }
    // Check the Control
    Map<String, Control> controls = addRequest.getControls();
    assertEquals(1, controls.size());
    assertTrue(addRequest.hasControl("2.16.840.1.113730.3.4.2"));
    @SuppressWarnings("unchecked") CodecControl<Control> control = (org.apache.directory.api.ldap.codec.api.CodecControl<Control>) controls.get("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(addRequest);
        // Check the length
        assertEquals(0x51, bb.limit());
        String encodedPdu = Strings.dumpBytes(bb.array());
        assertEquals(encodedPdu, decodedPdu);
    } catch (EncoderException ee) {
        ee.printStackTrace();
        fail(ee.getMessage());
    }
}
Also used : LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) ByteBuffer(java.nio.ByteBuffer) AddRequest(org.apache.directory.api.ldap.model.message.AddRequest) DecoderException(org.apache.directory.api.asn1.DecoderException) EncoderException(org.apache.directory.api.asn1.EncoderException) Entry(org.apache.directory.api.ldap.model.entry.Entry) 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) Value(org.apache.directory.api.ldap.model.entry.Value) CodecControl(org.apache.directory.api.ldap.codec.api.CodecControl) AddRequestDecorator(org.apache.directory.api.ldap.codec.decorators.AddRequestDecorator) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 3 with AddRequest

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

the class AddRequestTest method testRequestWith1AttrWithBase64Value.

/**
 * Test parsing of a request with an Attr elements with value
 */
@Test
public void testRequestWith1AttrWithBase64Value() {
    Dsmlv2Parser parser = null;
    try {
        parser = newParser();
        parser.setInput(AddRequestTest.class.getResource("request_with_1_attr_with_base64_value.xml").openStream(), "UTF-8");
        parser.parse();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    AddRequest addRequest = (AddRequest) parser.getBatchRequest().getCurrentRequest();
    Entry entry = addRequest.getEntry();
    assertEquals(1, entry.size());
    // Getting the Attribute
    Iterator<Attribute> attributeIterator = entry.iterator();
    Attribute attribute = attributeIterator.next();
    assertEquals("objectclass", attribute.getUpId());
    // Getting the Value
    Iterator<Value> valueIterator = attribute.iterator();
    assertTrue(valueIterator.hasNext());
    Value value = valueIterator.next();
    assertFalse(value.isHumanReadable());
    assertEquals("DSMLv2.0 rocks!!", value.getValue());
}
Also used : AddRequest(org.apache.directory.api.ldap.model.message.AddRequest) Entry(org.apache.directory.api.ldap.model.entry.Entry) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) Value(org.apache.directory.api.ldap.model.entry.Value) Dsmlv2Parser(org.apache.directory.api.dsmlv2.Dsmlv2Parser) Test(org.junit.Test) AbstractTest(org.apache.directory.api.dsmlv2.AbstractTest)

Example 4 with AddRequest

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

the class AddRequestTest method testRequestWith3ControlsWithoutValue.

/**
 * Test parsing of a request with 3 (optional) Control elements without value
 */
@Test
public void testRequestWith3ControlsWithoutValue() {
    Dsmlv2Parser parser = null;
    try {
        parser = newParser();
        parser.setInput(AddRequestTest.class.getResource("request_with_3_controls_without_value.xml").openStream(), "UTF-8");
        parser.parse();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    AddRequest addRequest = (AddRequest) parser.getBatchRequest().getCurrentRequest();
    Map<String, Control> controls = addRequest.getControls();
    assertEquals(3, addRequest.getControls().size());
    Control control = controls.get("1.2.840.113556.1.4.456");
    assertNotNull(control);
    assertTrue(control.isCritical());
    assertEquals("1.2.840.113556.1.4.456", control.getOid());
    assertFalse(((DsmlControl<?>) control).hasValue());
}
Also used : AddRequest(org.apache.directory.api.ldap.model.message.AddRequest) DsmlControl(org.apache.directory.api.dsmlv2.DsmlControl) Control(org.apache.directory.api.ldap.model.message.Control) Dsmlv2Parser(org.apache.directory.api.dsmlv2.Dsmlv2Parser) Test(org.junit.Test) AbstractTest(org.apache.directory.api.dsmlv2.AbstractTest)

Example 5 with AddRequest

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

the class AddRequestTest method testRequestWith1AttrWithValue.

/**
 * Test parsing of a request with an Attr elements with value
 */
@Test
public void testRequestWith1AttrWithValue() {
    Dsmlv2Parser parser = null;
    try {
        parser = newParser();
        parser.setInput(AddRequestTest.class.getResource("request_with_1_attr_with_value.xml").openStream(), "UTF-8");
        parser.parse();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    AddRequest addRequest = (AddRequest) parser.getBatchRequest().getCurrentRequest();
    Entry entry = addRequest.getEntry();
    assertEquals(1, entry.size());
    // Getting the Attribute
    Iterator<Attribute> attributeIterator = entry.iterator();
    Attribute attribute = attributeIterator.next();
    assertEquals("objectclass", attribute.getUpId());
    // Getting the Value
    Iterator<Value> valueIterator = attribute.iterator();
    assertTrue(valueIterator.hasNext());
    Value value = valueIterator.next();
    assertEquals("top", value.getValue());
}
Also used : AddRequest(org.apache.directory.api.ldap.model.message.AddRequest) Entry(org.apache.directory.api.ldap.model.entry.Entry) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) Value(org.apache.directory.api.ldap.model.entry.Value) Dsmlv2Parser(org.apache.directory.api.dsmlv2.Dsmlv2Parser) Test(org.junit.Test) AbstractTest(org.apache.directory.api.dsmlv2.AbstractTest)

Aggregations

AddRequest (org.apache.directory.api.ldap.model.message.AddRequest)26 Test (org.junit.Test)19 AbstractTest (org.apache.directory.api.dsmlv2.AbstractTest)16 Dsmlv2Parser (org.apache.directory.api.dsmlv2.Dsmlv2Parser)16 Entry (org.apache.directory.api.ldap.model.entry.Entry)12 Value (org.apache.directory.api.ldap.model.entry.Value)11 Attribute (org.apache.directory.api.ldap.model.entry.Attribute)10 Control (org.apache.directory.api.ldap.model.message.Control)7 DsmlControl (org.apache.directory.api.dsmlv2.DsmlControl)5 AddRequestDecorator (org.apache.directory.api.ldap.codec.decorators.AddRequestDecorator)5 DecoderException (org.apache.directory.api.asn1.DecoderException)4 AddRequestImpl (org.apache.directory.api.ldap.model.message.AddRequestImpl)4 ByteBuffer (java.nio.ByteBuffer)3 EncoderException (org.apache.directory.api.asn1.EncoderException)3 Asn1Decoder (org.apache.directory.api.asn1.ber.Asn1Decoder)3 BatchRequestDsml (org.apache.directory.api.dsmlv2.request.BatchRequestDsml)3 LdapMessageContainer (org.apache.directory.api.ldap.codec.api.LdapMessageContainer)3 AbstractCodecServiceTest (org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)3 AbandonRequest (org.apache.directory.api.ldap.model.message.AbandonRequest)3 BindRequest (org.apache.directory.api.ldap.model.message.BindRequest)3