Java Code Examples for org.apache.directory.api.ldap.model.message.AddRequest#getControls()

The following examples show how to use org.apache.directory.api.ldap.model.message.AddRequest#getControls() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AddRequestTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test parsing of a request with a (optional) Control element
 */
@Test
public void testRequestWith1Control()
{
    Dsmlv2Parser parser = null;
    try
    {
        parser = newParser();

        parser.setInput( AddRequestTest.class.getResource( "request_with_1_control.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( 1, addRequest.getControls().size() );

    Control control = controls.get( "1.2.840.113556.1.4.643" );

    assertNotNull( control );
    assertTrue( control.isCritical() );
    assertEquals( "1.2.840.113556.1.4.643", control.getOid() );
    assertEquals( "Some text", Strings.utf8ToString( ( ( DsmlControl<?> ) control ).getValue() ) );
}
 
Example 2
Source File: AddRequestTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test parsing of a request with a (optional) Control element with Base64 value
 */
@Test
public void testRequestWith1ControlBase64Value()
{
    Dsmlv2Parser parser = null;
    try
    {
        parser = newParser();

        parser.setInput(
            AddRequestTest.class.getResource( "request_with_1_control_base64_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( 1, addRequest.getControls().size() );

    Control control = controls.get( "1.2.840.113556.1.4.643" );

    assertNotNull( control );
    assertTrue( control.isCritical() );
    assertEquals( "1.2.840.113556.1.4.643", control.getOid() );
    assertEquals( "DSMLv2.0 rocks!!", Strings.utf8ToString( ( ( DsmlControl<?> ) control ).getValue() ) );
}
 
Example 3
Source File: AddRequestTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test parsing of a request with a (optional) Control element with empty value
 */
@Test
public void testRequestWith1ControlEmptyValue()
{
    Dsmlv2Parser parser = null;
    try
    {
        parser = newParser();

        parser.setInput( AddRequestTest.class.getResource( "request_with_1_control_empty_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( 1, addRequest.getControls().size() );

    Control control = controls.get( "1.2.840.113556.1.4.643" );

    assertNotNull( control );
    assertTrue( control.isCritical() );
    assertEquals( "1.2.840.113556.1.4.643", control.getOid() );
    assertFalse( ( ( DsmlControl<?> ) control ).hasValue() );
}
 
Example 4
Source File: AddRequestTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test parsing of a request with 2 (optional) Control elements
 */
@Test
public void testRequestWith2Controls()
{
    Dsmlv2Parser parser = null;
    try
    {
        parser = newParser();

        parser.setInput( AddRequestTest.class.getResource( "request_with_2_controls.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( 2, addRequest.getControls().size() );

    Control control = controls.get( "1.2.840.113556.1.4.789" );

    assertNotNull( control );
    assertFalse( control.isCritical() );
    assertEquals( "1.2.840.113556.1.4.789", control.getOid() );
    assertEquals( "Some other text", Strings.utf8ToString( ( ( DsmlControl<?> ) control ).getValue() ) );
}
 
Example 5
Source File: AddRequestTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * 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() );
}
 
Example 6
Source File: AddRequestTest.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Test the decoding of a AddRequest with a empty attributeList and a
 * control
 */
@Test
public void testDecodeAddRequestEmptyAttributeValueWithControl() throws  DecoderException, EncoderException
{
    ByteBuffer stream = ByteBuffer.allocate( 0x51 );

    stream.put( new byte[]
        {
            0x30, 0x4F, // LDAPMessage ::= SEQUENCE {
              0x02, 0x01, 0x01, // messageID MessageID
              0x68, 0x2D, // CHOICE { ..., addRequest AddRequest, ...
                // AddRequest ::= [APPLICATION 8] SEQUENCE {
                // 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, 0x09, // AttributeList ::= SEQUENCE OF SEQUENCE {
                  0x30, 0x07, // attribute 1
                    0x04, 0x01,
                      'l', // type AttributeDescription,
                  0x31, 0x02,
                    0x04, 0x00,
                ( byte ) 0xA0, 0x1B, // A control (ManageDsaIT)
                  0x30, 0x19,
                    0x04, 0x17,
                      '2', '.', '1', '6', '.', '8', '4', '0', '.', '1',  '.', '1', '1', '3', '7', '3', '0',
                      '.', '3', '.', '4', '.', '2'
        } );

    stream.flip();

    // Allocate a LdapMessage Container
    LdapMessageContainer<AddRequest> container = new LdapMessageContainer<>( codec );

    // Decode a AddRequest message
    Asn1Decoder.decode( stream, container );

    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.getString() );
    }

    // Check the Control
    Map<String, Control> controls = addRequest.getControls();

    assertEquals( 1, controls.size() );

    assertTrue( addRequest.hasControl( "2.16.840.1.113730.3.4.2" ) );

    Control control = controls.get( "2.16.840.1.113730.3.4.2" );
    assertTrue( control instanceof ManageDsaIT );
    assertEquals( "2.16.840.1.113730.3.4.2", control.getOid() );

    // Check encode reverse
    Asn1Buffer buffer = new Asn1Buffer();

    AddRequest request = new AddRequestImpl();
    request.setEntry( addRequest.getEntry() );
    request.setMessageId( addRequest.getMessageId() );
    request.addControl( new ManageDsaITImpl() );

    LdapEncoder.encodeMessage( buffer, codec, request );

    assertArrayEquals( stream.array(), buffer.getBytes().array() );
    
    // Check encode reverse
    Asn1Buffer asn1Buffer = new Asn1Buffer();

    AddRequest request2 = new AddRequestImpl();
    request2.setEntry( addRequest.getEntry() );
    request2.setMessageId( addRequest.getMessageId() );
    request2.addControl( new ManageDsaITImpl() );

    LdapEncoder.encodeMessage( asn1Buffer, codec, request2 );

    assertArrayEquals( stream.array(), asn1Buffer.getBytes().array() );
}