You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@directory.apache.org by Marina <pp...@yahoo.com> on 2012/05/09 21:58:52 UTC

How to import LDIF file programmatically


Hello,
I'm trying to import my LDIF file (with all users/groups/orgs defined) into a newly created partition of my my embedded LDAP server. 

I found APIs and examples for how to create a partition programmatically:

JdbmPartition partition = new JdbmPartition();
        partition.setId( partitionId );
        partition.setPartitionDir( new File( service.getWorkingDirectory(), partitionId ) );
        partition.setSuffix( partitionDn );
        service.addPartition( partition );

Now, I would like to add users, etc. from my LDIF file - but I cannot find APIs or examples for how to do that. I see how I could create each user/org programmatically, but that's not what I want - I already have the whole hierarchy defined in the LDIF, I don't want to repeat all that in the code.

If somebody could point me to the APIs that could do that, or provide some examples - it would be great!

Thanks!
Marina

Re: How to import LDIF file programmatically

Posted by Göktürk Gezer <go...@gmail.com>.
Hi,

On Thu, May 10, 2012 at 8:06 PM, Marina <pp...@yahoo.com> wrote:

>
>
> Hi, Gokturk,
> This example code does not compile - I get a few errors about
> missing/incorrect classes and method signatures....
> For example:
>

It is about your version of ApacheDS, as i said there may be changes based
on your version. Example is for 2.0.0 series.


>  if ( entry.isLdifContent() ) --> The method isLdifContent() is undefined
> for the type LdifEntry
>  ModifyDnRequest modDnRequest = new ModifyDnRequestImpl();  -->
> ModifyDnRequest class does not exist, and The constructor
> ModifyDnRequestImpl() is undefined
> and others....
>
But if you want to stick with 1.5.7,

if(entry.isLdifContext()) can be changed with  if(entry.getChangeType() ==
ChangeType.None)

For the other, errors, just find your best way based on which version
you're on and which mechanism you're using to issue operations.

>
> Which version of ApacheDS is it for? I'm using 1.5.7.
> Or, maybe I'm missing some libraries? I have apacheds-all-1.5.7.jar.
>
Yes you're missing definitions. But this is also possible in 1.5.7 nothing
special to 2.0.0. Just different.

>
> Are there some published APIS/ documentation for this kind of tasks?
>
As i understand your ldif entries have no change type, i mean they're for
addition only. If this is the case, then just add them without going
through modify/rename checks. But issue operations through CoreSession
reference so that your entry will contain necessary system attributes.

>
> thanks!
> Marina
>

Regards,

> ________________________________
> From: Göktürk Gezer <go...@gmail.com>
> To: users@directory.apache.org
> Sent: Wednesday, May 9, 2012 4:23 PM
> Subject: Re: How to import LDIF file programmatically
>
> You can use below code as your base. You might wanna slightly change it
> based on which version of ApacheDS you're using.
>
> And make sure your ldif file is constructed in correct order from the top
> to the bottom. I'd preffer to use CoreSession to issue operations to make
> sure they're checked, but you can issue operations on the Jdbm partition
> reference you've created as well..
>
> LdifReader ldifReader = new LdifReader(new
> FileInputStream("yourfile.ldif"));
> CoreSession conn; // Obtain it from embedded DirectoryService reference.
>
> for ( LdifEntry entry : ldifReader )
> {
>     if ( entry.isLdifContent() )
>     {
>         conn.add( entry.getEntry() );
>         continue;
>     }
>
>     switch ( entry.getChangeType() )
>     {
>         case ModRdn:
>         case ModDn:
>             ModifyDnRequest modDnRequest = new ModifyDnRequestImpl();
>             modDnRequest.setName( entry.getDn() );
>             modDnRequest.setDeleteOldRdn( entry.isDeleteOldRdn() );
>
>             if ( entry.getNewSuperior() != null )
>             {
>                 modDnRequest.setNewSuperior( new Dn( entry.getNewSuperior()
> ) );
>             }
>
>             if ( entry.getNewRdn() != null )
>             {
>                 modDnRequest.setNewRdn( new Rdn( entry.getNewRdn() ) );
>             }
>             conn.modifyDn( modDnRequest );
>             break;
>         case Delete:
>             conn.delete( entry.getDn() );
>             break;
>         case Modify:
>             conn.modify( entry.getDn(), entry.getModificationArray() );
>             break;
>         default:
>             throw new IllegalArgumentException( "Unknown change type in
> LdifEntry: "
>                 + entry.getChangeType().toString() );
>     }
> }
>

Re: How to import LDIF file programmatically

Posted by Marina <pp...@yahoo.com>.

Hi, Gokturk,
This example code does not compile - I get a few errors about missing/incorrect classes and method signatures....
For example: 

 if ( entry.isLdifContent() ) --> The method isLdifContent() is undefined for the type LdifEntry
 ModifyDnRequest modDnRequest = new ModifyDnRequestImpl();  --> ModifyDnRequest class does not exist, and The constructor ModifyDnRequestImpl() is undefined
and others....

Which version of ApacheDS is it for? I'm using 1.5.7.
Or, maybe I'm missing some libraries? I have apacheds-all-1.5.7.jar.

Are there some published APIS/ documentation for this kind of tasks?

thanks!
Marina
________________________________
From: Göktürk Gezer <go...@gmail.com>
To: users@directory.apache.org 
Sent: Wednesday, May 9, 2012 4:23 PM
Subject: Re: How to import LDIF file programmatically

You can use below code as your base. You might wanna slightly change it
based on which version of ApacheDS you're using.

And make sure your ldif file is constructed in correct order from the top
to the bottom. I'd preffer to use CoreSession to issue operations to make
sure they're checked, but you can issue operations on the Jdbm partition
reference you've created as well..

LdifReader ldifReader = new LdifReader(new
FileInputStream("yourfile.ldif"));
CoreSession conn; // Obtain it from embedded DirectoryService reference.

for ( LdifEntry entry : ldifReader )
{
    if ( entry.isLdifContent() )
    {
        conn.add( entry.getEntry() );
        continue;
    }

    switch ( entry.getChangeType() )
    {
        case ModRdn:
        case ModDn:
            ModifyDnRequest modDnRequest = new ModifyDnRequestImpl();
            modDnRequest.setName( entry.getDn() );
            modDnRequest.setDeleteOldRdn( entry.isDeleteOldRdn() );

            if ( entry.getNewSuperior() != null )
            {
                modDnRequest.setNewSuperior( new Dn( entry.getNewSuperior()
) );
            }

            if ( entry.getNewRdn() != null )
            {
                modDnRequest.setNewRdn( new Rdn( entry.getNewRdn() ) );
            }
            conn.modifyDn( modDnRequest );
            break;
        case Delete:
            conn.delete( entry.getDn() );
            break;
        case Modify:
            conn.modify( entry.getDn(), entry.getModificationArray() );
            break;
        default:
            throw new IllegalArgumentException( "Unknown change type in
LdifEntry: "
                + entry.getChangeType().toString() );
    }
}

Re: How to import LDIF file programmatically

Posted by Marina <pp...@yahoo.com>.
Awesome, thank you, Gokturk, I will try that!
Marina




________________________________
From: Göktürk Gezer <go...@gmail.com>
To: users@directory.apache.org 
Sent: Wednesday, May 9, 2012 4:23 PM
Subject: Re: How to import LDIF file programmatically

You can use below code as your base. You might wanna slightly change it
based on which version of ApacheDS you're using.

And make sure your ldif file is constructed in correct order from the top
to the bottom. I'd preffer to use CoreSession to issue operations to make
sure they're checked, but you can issue operations on the Jdbm partition
reference you've created as well..

LdifReader ldifReader = new LdifReader(new
FileInputStream("yourfile.ldif"));
CoreSession conn; // Obtain it from embedded DirectoryService reference.

for ( LdifEntry entry : ldifReader )
{
    if ( entry.isLdifContent() )
    {
        conn.add( entry.getEntry() );
        continue;
    }

    switch ( entry.getChangeType() )
    {
        case ModRdn:
        case ModDn:
            ModifyDnRequest modDnRequest = new ModifyDnRequestImpl();
            modDnRequest.setName( entry.getDn() );
            modDnRequest.setDeleteOldRdn( entry.isDeleteOldRdn() );

            if ( entry.getNewSuperior() != null )
            {
                modDnRequest.setNewSuperior( new Dn( entry.getNewSuperior()
) );
            }

            if ( entry.getNewRdn() != null )
            {
                modDnRequest.setNewRdn( new Rdn( entry.getNewRdn() ) );
            }
            conn.modifyDn( modDnRequest );
            break;
        case Delete:
            conn.delete( entry.getDn() );
            break;
        case Modify:
            conn.modify( entry.getDn(), entry.getModificationArray() );
            break;
        default:
            throw new IllegalArgumentException( "Unknown change type in
LdifEntry: "
                + entry.getChangeType().toString() );
    }
}

Re: How to import LDIF file programmatically

Posted by Göktürk Gezer <go...@gmail.com>.
You can use below code as your base. You might wanna slightly change it
based on which version of ApacheDS you're using.

And make sure your ldif file is constructed in correct order from the top
to the bottom. I'd preffer to use CoreSession to issue operations to make
sure they're checked, but you can issue operations on the Jdbm partition
reference you've created as well..

LdifReader ldifReader = new LdifReader(new
FileInputStream("yourfile.ldif"));
CoreSession conn; // Obtain it from embedded DirectoryService reference.

for ( LdifEntry entry : ldifReader )
{
    if ( entry.isLdifContent() )
    {
        conn.add( entry.getEntry() );
        continue;
    }

    switch ( entry.getChangeType() )
    {
        case ModRdn:
        case ModDn:
            ModifyDnRequest modDnRequest = new ModifyDnRequestImpl();
            modDnRequest.setName( entry.getDn() );
            modDnRequest.setDeleteOldRdn( entry.isDeleteOldRdn() );

            if ( entry.getNewSuperior() != null )
            {
                modDnRequest.setNewSuperior( new Dn( entry.getNewSuperior()
) );
            }

            if ( entry.getNewRdn() != null )
            {
                modDnRequest.setNewRdn( new Rdn( entry.getNewRdn() ) );
            }
            conn.modifyDn( modDnRequest );
            break;
        case Delete:
            conn.delete( entry.getDn() );
            break;
        case Modify:
            conn.modify( entry.getDn(), entry.getModificationArray() );
            break;
        default:
            throw new IllegalArgumentException( "Unknown change type in
LdifEntry: "
                + entry.getChangeType().toString() );
    }
}