org.apache.directory.api.ldap.model.message.AddRequestImpl Java Examples

The following examples show how to use org.apache.directory.api.ldap.model.message.AddRequestImpl. 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: ApiDsmlParserOsgiTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
@Override
protected void useBundleClasses() throws Exception
{
    new Dsmlv2Grammar();

    new AddRequestDsml( null );
    new SearchRequestDsml( null );

    ParserUtils.base64Encode( "abc" );
    new Dn( "cn=foo" );
    new LdapUrl( "ldap://example.com/" );
    ResultCodeEnum.TOO_LATE.getMessage();
    ParserUtils.needsBase64Encoding( null );
    ParserUtils.parseAndVerifyRequestID( "5", null );
    new BaseElement( "foo" );
    context.getService( context.getServiceReference( LdapApiService.class.getName() ) );
    new AddRequestImpl();
    new ReferralImpl();
    new LdapResultImpl();

    new SearchResponse();
    new LdapResultDsml( null, null, null );
    new SearchResultEntryDsml( null );
    new SearchResultDoneDsml( null );
}
 
Example #2
Source File: LdapNetworkConnection.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void add( Entry entry ) throws LdapException
{
    if ( entry == null )
    {
        String msg = I18n.err( I18n.ERR_04123_CANNOT_ADD_EMPTY_ENTRY );
        
        if ( LOG.isDebugEnabled() )
        {
            LOG.debug( msg );
        }
        
        throw new IllegalArgumentException( msg );
    }

    AddRequest addRequest = new AddRequestImpl();
    addRequest.setEntry( entry );

    AddResponse addResponse = add( addRequest );

    processResponse( addResponse );
}
 
Example #3
Source File: LdapNetworkConnection.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public AddFuture addAsync( Entry entry ) throws LdapException
{
    if ( entry == null )
    {
        String msg = I18n.err( I18n.ERR_04125_CANNOT_ADD_NULL_ENTRY );
        
        if ( LOG.isDebugEnabled() )
        {
            LOG.debug( msg );
        }
        
        throw new IllegalArgumentException( msg );
    }

    AddRequest addRequest = new AddRequestImpl();
    addRequest.setEntry( entry );

    return addAsync( addRequest );
}
 
Example #4
Source File: InitAddRequest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void action( LdapMessageContainer<AddRequest> container ) throws DecoderException
{
    // Now, we can allocate the AddRequest Object
    int messageId = container.getMessageId();
    AddRequest addRequest = new AddRequestImpl();
    addRequest.setMessageId( messageId );
    container.setMessage( addRequest );

    // We will check that the request is not null
    TLV tlv = container.getCurrentTLV();

    if ( tlv.getLength() == 0 )
    {
        String msg = I18n.err( I18n.ERR_05145_NULL_ADD_REQUEST );
        LOG.error( msg );

        // Will generate a PROTOCOL_ERROR
        throw new DecoderException( msg );
    }
}
 
Example #5
Source File: ApacheLdapProviderImpl.java    From ldapchai with GNU Lesser General Public License v2.1 5 votes vote down vote up
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() );
    }
}
 
Example #6
Source File: Dsmlv2Grammar.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void action( Dsmlv2Container container ) throws XmlPullParserException
{
    AddRequestDsml addRequest = new AddRequestDsml( codec, new AddRequestImpl() );
    container.getBatchRequest().addRequest( addRequest );

    XmlPullParser xpp = container.getParser();

    // Checking and adding the request's attributes
    String attributeValue;
    // requestID
    attributeValue = xpp.getAttributeValue( "", REQUEST_ID );

    if ( attributeValue != null )
    {
        addRequest.setMessageId( ParserUtils.parseAndVerifyRequestID( attributeValue, xpp ) );
    }
    else
    {
        if ( ParserUtils.isRequestIdNeeded( container ) )
        {
            throw new XmlPullParserException( I18n.err( I18n.ERR_03000_REQUEST_ID_REQUIRED ), xpp, null );
        }
    }

    // dn
    attributeValue = xpp.getAttributeValue( "", "dn" );

    if ( attributeValue != null )
    {
        try
        {
            addRequest.setEntryDn( new Dn( attributeValue ) );
        }
        catch ( LdapInvalidDnException lide )
        {
            throw new XmlPullParserException( I18n.err( I18n.ERR_03039_PARSING_ERROR, lide.getMessage() ), xpp, lide );
        }
    }
    else
    {
        throw new XmlPullParserException( I18n.err( I18n.ERR_03001_DN_ATTRIBUTE_REQUIRED ), xpp, null );
    }
}
 
Example #7
Source File: ModelFactoryImpl.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public AddRequest newAddRequest( Entry entry )
{
    return new AddRequestImpl().setEntry( entry );
}
 
Example #8
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
 */
@Test
public void testDecodeAddRequestEmptyAttributeValue() throws DecoderException, EncoderException
{
    ByteBuffer stream = ByteBuffer.allocate( 0x34 );

    stream.put( new byte[]
        {
            0x30, 0x32,                     // LDAPMessage ::= SEQUENCE {
              0x02, 0x01, 0x01,             // messageID MessageID
              0x68, 0x2D,                   // CHOICE { ..., addRequest AddRequest, ...
                                            // AddRequest ::= [APPLICATION 8] SEQUENCE {
                0x04, 0x20,                 // entry LDAPDN,
                  '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
        } );

    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 encode reverse
    Asn1Buffer buffer = new Asn1Buffer();

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

    LdapEncoder.encodeMessage( buffer, codec, request );

    assertArrayEquals( stream.array(), buffer.getBytes().array() );
}
 
Example #9
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() );
}
 
Example #10
Source File: LdapDirectoryServerConnectionTest.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
public void testUserCreation() {
        LdapConnection connection = new LdapNetworkConnection( "localhost", 10389 );
        try {
            connection.bind( "uid=admin,ou=system", "secret" );

            connection.add(new DefaultEntry(
                    "ou=acsadmins,ou=users,ou=system",
            "objectClass: organizationalUnit",
// might also need to be           objectClass: top
            "ou: acsadmins"
            ));
            connection.add(new DefaultEntry(
                    "uid=dahn,ou=acsadmins,ou=users,ou=system",
                    "objectClass: inetOrgPerson",
                    "objectClass: top",
                    "cn: dahn",
                    "sn: Hoogland",
                    "givenName: Daan",
                    "mail: [email protected]",
                    "uid: dahn"
            ));

            connection.add(
                    new DefaultEntry(
                            "cn=JuniorAdmins,ou=groups,ou=system", // The Dn
                            "objectClass: groupOfUniqueNames",
                            "ObjectClass: top",
                            "cn: JuniorAdmins",
                            "uniqueMember: uid=dahn,ou=acsadmins,ou=system,ou=users") );

            assertTrue( connection.exists( "cn=JuniorAdmins,ou=groups,ou=system" ) );
            assertTrue( connection.exists( "uid=dahn,ou=acsadmins,ou=users,ou=system" ) );

            Entry ourUser = connection.lookup("uid=dahn,ou=acsadmins,ou=users,ou=system");
            ourUser.add("memberOf", "cn=JuniorAdmins,ou=groups,ou=system");
            AddRequest addRequest = new AddRequestImpl();
            addRequest.setEntry( ourUser );
            AddResponse response = connection.add( addRequest );
            assertNotNull( response );
            // We would need to either
//            assertEquals( ResultCodeEnum.SUCCESS, response.getLdapResult().getResultCode() );
            // or have the automatic virtual attribute

            List<LdapUser> usahs = ldapManager.getUsers(1L);
            assertEquals("now an admin and a normal user should be present",2, usahs.size());

        } catch (LdapException | NoLdapUserMatchingQueryException e) {
            fail(e.getLocalizedMessage());
        }
    }
 
Example #11
Source File: AddRequestDsml.java    From directory-ldap-api with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new getDecoratedMessage() of AddRequestDsml.
 * 
 * @param codec The LDAP Service to use
 */
public AddRequestDsml( LdapApiService codec )
{
    super( codec, new AddRequestImpl() );
}