Package org.apache.directory.ldap.client.api

Examples of org.apache.directory.ldap.client.api.LdapConnection



    @Test
    public void testModifyReplaceWithNullValues() throws LdapException, IOException
    {
        LdapConnection connection = new LdapNetworkConnection( "localhost", getLdapServer().getPort() );
        connection.setTimeOut( 0L );

        // Use the client API
        connection.bind( "uid=admin,ou=system", "secret" );

        // Add a new entry with some null values
        Entry entry = new DefaultEntry( "uid=12345,ou=system",
            "ObjectClass: top",
            "ObjectClass: person",
            "ObjectClass: person",
            "ObjectClass: OrganizationalPerson",
            "ObjectClass: inetOrgPerson",
            "uid: 12345",
            "cn: test",
            "sn: Test",
            "userPassword: 12345" );

        connection.add( entry );

        // Now modify the entry : we should replace the password with a null value
        // and add a mail Attribute with a null value
        connection.modify( new Dn( "uid=12345,ou=system" ),
            new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, "userPassword", Strings.EMPTY_BYTES ),
            new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, "mail", ( String ) null )
            );

        // Get back the entry
        Entry found = connection.lookup( "uid=12345,ou=system" );

        assertNotNull( found );
        assertNotNull( found.get( "mail" ) );
        assertNotNull( found.get( "userPassword" ) );
        assertEquals( 1, found.get( "mail" ).size() );
        assertEquals( 1, found.get( "userPassword" ).size() );
        assertTrue( found.contains( "mail", Strings.EMPTY_BYTES ) );
        assertTrue( found.contains( "userPassword", "" ) );

        // Now, do a replace with no value. We should not anymore have a mail
        connection.modify( new Dn( "uid=12345,ou=system" ),
            new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, "mail" )
            );

        // Get back the entry
        found = connection.lookup( "uid=12345,ou=system" );

        assertNotNull( found );
        assertNull( found.get( "mail" ) );
        assertNotNull( found.get( "userPassword" ) );
        assertEquals( 1, found.get( "userPassword" ).size() );
        assertTrue( found.contains( "userPassword", "" ) );

        connection.close();
    }
View Full Code Here


     * @return the pooled admin connection
     * @throws Exception the exception
     */
    public static LdapConnection getPooledAdminConnection( LdapServer ldapServer ) throws Exception
    {
        LdapConnection ldapConnection = getAdminPool( ldapServer ).getConnection();

        ldapConnection.setBinaryAttributeDetector(
            new SchemaBinaryAttributeDetector(
                ldapServer.getDirectoryService().getSchemaManager() ) );

        return ldapConnection;
    }
View Full Code Here

     * @return A LdapNetworkConnection instance
     * @exception If the connection could not be established.
     */
    public static LdapConnection getAnonymousNetworkConnection( String host, int port ) throws Exception
    {
        LdapConnection connection = new LdapNetworkConnection( host, port );
        connection.bind();

        return connection;
    }
View Full Code Here

     * @return A LdapNetworkConnection instance
     * @exception If the connection could not be established.
     */
    public static LdapConnection getAnonymousNetworkConnection( LdapServer ldapServer ) throws Exception
    {
        LdapConnection connection = new LdapNetworkConnection( "localHost", ldapServer.getPort() );
        connection.setTimeOut( 0L );
        connection.bind();

        return connection;
    }
View Full Code Here

    })
    public void testMethodDs()
    {
        assertEquals( createLdapServerRule.getLdapServer(), classCreateLdapServerRule.getLdapServer() );
   
        LdapConnection ldapConnection = null;
        try
        {
            LdapServer ldapServer = createLdapServerRule.getLdapServer();
            ldapServer.getPort();
            ldapConnection = new LdapNetworkConnection( "localhost", ldapServer.getPort() );
            ldapConnection.connect();
            ldapConnection.bind( "uid=admin,ou=system", "secret" );
   
            Dn dn = new Dn( "cn=methodDs,ou=system" );
            Entry entry = ldapConnection.lookup( dn );
            assertNotNull( entry );
            assertEquals( "methodDs", entry.get( "cn" ).get().getValue() );
   
            try
            {
                dn = new Dn( "cn=class,ou=system" );
                entry = ldapConnection.lookup( dn );
                assertNull( entry );
            }
            catch ( LdapNoSuchObjectException e )
            {
                // expected
            }
        }
        catch ( LdapException e )
        {
            fail( e.getMessage() );
        }
        finally
        {
            if ( ldapConnection != null )
            {
                try
                {
                    ldapConnection.close();
                }
                catch ( IOException e )
                {
                    // who cares!
                }
View Full Code Here

    {
        assertNotEquals( createLdapServerRule.getLdapServer(), classCreateLdapServerRule.getLdapServer() );
        assertNotEquals( createLdapServerRule.getLdapServer().getPort(),
            classCreateLdapServerRule.getLdapServer().getPort() );
   
        LdapConnection ldapConnection = null;
        try
        {
            LdapServer ldapServer = createLdapServerRule.getLdapServer();
            ldapServer.getPort();
            ldapConnection = new LdapNetworkConnection( "localhost", ldapServer.getPort() );
            ldapConnection.connect();
            ldapConnection.bind( "uid=admin,ou=system", "secret" );
   
            Dn dn = new Dn( "cn=class,ou=system" );
            Entry entry = ldapConnection.lookup( dn );
            assertNotNull( entry );
            assertEquals( "class", entry.get( "cn" ).get().getValue() );
        }
        catch ( LdapException e )
        {
            fail( e.getMessage() );
        }
        finally
        {
            if ( ldapConnection != null )
            {
                try
                {
                    ldapConnection.close();
                }
                catch ( IOException e )
                {
                    // who cares!
                }
View Full Code Here

        assertEquals( classCreateLdapServerRule.getLdapServer(), createLdapServerRule.getLdapServer() );
        LdapServer ldapServer = createLdapServerRule.getLdapServer();
        DirectoryService directoryService = ldapServer.getDirectoryService();
        assertEquals( classCreateLdapServerRule.getDirectoryService(), directoryService );
   
        LdapConnection ldapConnection = null;
        try
        {
            Dn dn = new Dn( "cn=class,ou=system" );
            Entry entry = directoryService.getAdminSession().lookup( dn );
            assertNotNull( entry );
            assertEquals( "class", entry.get( "cn" ).get().getValue() );
   
            LOG.debug( "getting network connection" );
            ldapServer.getPort();
            ldapConnection = new LdapNetworkConnection( "localhost", ldapServer.getPort() );
            ldapConnection.connect();
            ldapConnection.bind( "uid=admin,ou=system", "secret" );
   
            entry = ldapConnection.lookup( dn );
            assertNotNull( entry );
            assertEquals( "class", entry.get( "cn" ).get().getValue() );
        }
        catch ( LdapException e )
        {
            fail( e.getMessage() );
        }
        finally
        {
            if ( ldapConnection != null )
            {
                try
                {
                    ldapConnection.close();
                }
                catch ( IOException e )
                {
                    // who cares!
                }
View Full Code Here

    public void testClassLdapConnectionPool()
    {
        assertEquals( createLdapConnectionPoolRule.getLdapConnectionPool(),
            classCreateLdapConnectionPoolRule.getLdapConnectionPool() );
   
        LdapConnection ldapConnection = null;
        try
        {
            ldapConnection = createLdapConnectionPoolRule.getLdapConnectionPool()
                .getConnection();
   
            Dn dn = new Dn( "cn=class,ou=system" );
            Entry entry = ldapConnection.lookup( dn );
            assertNotNull( entry );
            assertEquals( "class", entry.get( "cn" ).get().getValue() );
        }
        catch ( LdapException e )
        {
View Full Code Here

    * A basic search for one single entry
    */
    @Test
    public void testSearchPerfObjectScope() throws Exception
    {
        LdapConnection connection = IntegrationUtils.getAdminConnection( getService() );

        EntryCursor cursor = connection.search( "uid=admin,ou=system", "(ObjectClass=*)",
            SearchScope.OBJECT, "*" );

        int i = 0;

        while ( cursor.next() )
        {
            cursor.get();
            ++i;
        }

        cursor.close();

        assertEquals( 1, i );

        int nbIterations = 1500000;

        Dn dn = new Dn( getService().getSchemaManager(), "uid=admin,ou=system" );

        SearchRequest searchRequest = new SearchRequestImpl();

        searchRequest.setBase( dn );
        searchRequest.setFilter( "(ObjectClass=*)" );
        searchRequest.setScope( SearchScope.OBJECT );
        searchRequest.addAttributes( "*" );
        searchRequest.setDerefAliases( AliasDerefMode.DEREF_ALWAYS );

        long t0 = System.currentTimeMillis();
        long t00 = 0L;
        long tt0 = System.currentTimeMillis();
        int count = 0;

        for ( i = 0; i < nbIterations; i++ )
        {
            if ( i % 100000 == 0 )
            {
                long tt1 = System.currentTimeMillis();

                System.out.println( i + ", " + ( tt1 - tt0 ) );
                tt0 = tt1;
            }

            if ( i == 500000 )
            {
                t00 = System.currentTimeMillis();
            }

            cursor = new EntryCursorImpl( connection.search( searchRequest ) );

            while ( cursor.next() )
            {
                cursor.get();
                count++;
            }

            cursor.close();
        }

        long t1 = System.currentTimeMillis();

        Long deltaWarmed = ( t1 - t00 );
        System.out.println( "OBJECT level - Delta : " + deltaWarmed + "( "
            + ( ( ( nbIterations - 500000 ) * 1000 ) / deltaWarmed )
            + " per s ) /" + ( t1 - t0 ) + ", count : " + count );
        connection.close();
    }
View Full Code Here

    * A basic search for one single entry
    */
    @Test
    public void testSearchPerfOneLevelScope() throws Exception
    {
        LdapConnection connection = IntegrationUtils.getAdminConnection( getService() );

        EntryCursor cursor = connection.search( "ou=system", "(ObjectClass=*)",
            SearchScope.ONELEVEL, "*" );

        int i = 0;

        while ( cursor.next() )
        {
            cursor.get();
            ++i;
        }

        cursor.close();

        assertEquals( 5, i );

        int nbIterations = 150000;
        Dn dn = new Dn( getService().getSchemaManager(), "ou=system" );
        SearchRequest searchRequest = new SearchRequestImpl();

        searchRequest.setBase( dn );
        searchRequest.setFilter( "(ObjectClass=*)" );
        searchRequest.setScope( SearchScope.ONELEVEL );
        searchRequest.addAttributes( "*" );
        searchRequest.setDerefAliases( AliasDerefMode.DEREF_ALWAYS );

        long t0 = System.currentTimeMillis();
        long t00 = 0L;
        long tt0 = System.currentTimeMillis();
        int count = 0;

        for ( i = 0; i < nbIterations; i++ )
        {
            if ( i % 10000 == 0 )
            {
                long tt1 = System.currentTimeMillis();

                System.out.println( i + ", " + ( tt1 - tt0 ) );
                tt0 = tt1;
            }

            if ( i == 50000 )
            {
                t00 = System.currentTimeMillis();
            }

            cursor = new EntryCursorImpl( connection.search( searchRequest ) );

            while ( cursor.next() )
            {
                cursor.get();
                count++;
            }

            cursor.close();
        }

        long t1 = System.currentTimeMillis();

        Long deltaWarmed = ( t1 - t00 );
        System.out.println( "ONE level - Delta : " + deltaWarmed + "( "
            + ( ( ( nbIterations - 50000 ) * 1000 ) / deltaWarmed ) * 5
            + " per s ) /" + ( t1 - t0 ) + ", count : " + count );
        connection.close();
    }
View Full Code Here

TOP

Related Classes of org.apache.directory.ldap.client.api.LdapConnection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.