Java Code Examples for org.apache.directory.api.ldap.model.name.Dn#getParent()

The following examples show how to use org.apache.directory.api.ldap.model.name.Dn#getParent() . 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: LdifRevertor.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Compute a reverse LDIF for a forward change which if in LDIF format
 * would represent a Move operation. Hence there is no newRdn in the
 * picture here.
 *
 * @param newSuperiorDn the new parent dn to be (must not be null)
 * @param modifiedDn the dn of the entry being moved (must not be null)
 * @return a reverse LDIF
 * @throws LdapException if something went wrong
 */
public static LdifEntry reverseMove( Dn newSuperiorDn, Dn modifiedDn ) throws LdapException
{
    LdifEntry entry = new LdifEntry();
    Dn currentParent;
    Rdn currentRdn;
    Dn newDn;

    if ( newSuperiorDn == null )
    {
        throw new IllegalArgumentException( I18n.err( I18n.ERR_13466_NEW_SUPERIOR_DN_NULL ) );
    }

    if ( modifiedDn == null )
    {
        throw new IllegalArgumentException( I18n.err( I18n.ERR_13467_NULL_MODIFIED_DN ) );
    }

    if ( modifiedDn.size() == 0 )
    {
        throw new IllegalArgumentException( I18n.err( I18n.ERR_13468_DONT_MOVE_ROOTDSE ) );
    }

    currentParent = modifiedDn;
    currentRdn = currentParent.getRdn();
    currentParent = currentParent.getParent();

    newDn = newSuperiorDn;
    newDn = newDn.add( modifiedDn.getRdn() );

    entry.setChangeType( ChangeType.ModDn );
    entry.setDn( newDn );
    entry.setNewRdn( currentRdn.getName() );
    entry.setNewSuperior( currentParent.getName() );
    entry.setDeleteOldRdn( false );
    return entry;
}
 
Example 2
Source File: LdifRevertor.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * A small helper class to compute the simple revert.
 * 
 * @param entry The entry to revert
 * @param newDn The new Dn
 * @param newSuperior The new superior, if it has changed (null otherwise)
 * @param oldRdn The old Rdn
 * @param newRdn The new RDN if the RDN has changed
 * @return The reverted entry
 * @throws LdapInvalidDnException If the Dn is invalid
 */
private static LdifEntry revertEntry( Entry entry, Dn newDn, Dn newSuperior, Rdn oldRdn, Rdn newRdn )
    throws LdapInvalidDnException
{
    LdifEntry reverted = new LdifEntry();

    // We have a composite old Rdn, something like A=a+B=b
    // It does not matter if the RDNs overlap
    reverted.setChangeType( ChangeType.ModRdn );

    if ( newSuperior != null )
    {
        Dn restoredDn = newSuperior.add( newRdn );
        reverted.setDn( restoredDn );
    }
    else
    {
        reverted.setDn( newDn );
    }

    reverted.setNewRdn( oldRdn.getName() );

    // Is the newRdn's value present in the entry ?
    // ( case 3, 4 and 5)
    // If keepOldRdn = true, we cover case 4 and 5
    boolean keepOldRdn = entry.contains( newRdn.getNormType(), newRdn.getValue() );

    reverted.setDeleteOldRdn( !keepOldRdn );

    if ( newSuperior != null )
    {
        Dn oldSuperior = entry.getDn();

        oldSuperior = oldSuperior.getParent();
        reverted.setNewSuperior( oldSuperior.getName() );
    }

    return reverted;
}
 
Example 3
Source File: LdifRevertor.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * A helper method which generates a reverted entry for a MODDN operation
 * 
 * @param newSuperior The new superior, if it has changed (null otherwise)
 * @param newRdn The new RDN if the RDN has changed
 * @param newDn The new Dn
 * @param oldRdn The old Rdn
 * @param deleteOldRdn If the old Rdn attributes must be deleted or not
 * @return The reverted entry
 * @throws LdapInvalidDnException If the DN is invalid
 */
private static LdifEntry generateReverted( Dn newSuperior, Rdn newRdn, Dn newDn, Rdn oldRdn, boolean deleteOldRdn )
    throws LdapInvalidDnException
{
    LdifEntry reverted = new LdifEntry();
    reverted.setChangeType( ChangeType.ModRdn );

    if ( newSuperior != null )
    {
        Dn restoredDn = newSuperior.add( newRdn );
        reverted.setDn( restoredDn );
    }
    else
    {
        reverted.setDn( newDn );
    }

    reverted.setNewRdn( oldRdn.getName() );

    if ( newSuperior != null )
    {
        Dn oldSuperior = newDn;

        oldSuperior = oldSuperior.getParent();
        reverted.setNewSuperior( oldSuperior.getName() );
    }

    // Delete the newRDN values
    reverted.setDeleteOldRdn( deleteOldRdn );

    return reverted;
}
 
Example 4
Source File: LdapStandaloneServer.java    From bouncr with Eclipse Public License 1.0 5 votes vote down vote up
private void checkPartition(LdifEntry ldifEntry) throws Exception {
    Dn dn = ldifEntry.getDn();
    Dn parent = dn.getParent();
    try {
        directoryService.getAdminSession().exists(parent);
    } catch (Exception e) {
        System.out.println("Creating new partition for DN=" + dn + "\n");
        AvlPartition partition = new AvlPartition(directoryService.getSchemaManager());
        partition.setId(dn.getName());
        partition.setSuffixDn(dn);
        directoryService.addPartition(partition);
    }
}
 
Example 5
Source File: LdapServer.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
private void checkPartition(LdifEntry ldifEntry) throws Exception {
   Dn dn = ldifEntry.getDn();
   Dn parent = dn.getParent();
   try {
      directoryService.getAdminSession().exists(parent);
   } catch (Exception e) {
      System.out.println("Creating new partition for DN=" + dn + "\n");
      AvlPartition partition = new AvlPartition(directoryService.getSchemaManager());
      partition.setId(dn.getName());
      partition.setSuffixDn(dn);
      directoryService.addPartition(partition);
   }
}
 
Example 6
Source File: LdapNetworkConnection.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void moveAndRename( Dn entryDn, Dn newDn, boolean deleteOldRdn ) throws LdapException
{
    // Check the parameters first
    if ( entryDn == null )
    {
        throw new IllegalArgumentException( I18n.err( I18n.ERR_04142_NULL_ENTRY_DN ) );
    }

    if ( entryDn.isRootDse() )
    {
        throw new IllegalArgumentException( I18n.err( I18n.ERR_04143_CANNOT_MOVE_ROOT_DSE ) );
    }

    if ( newDn == null )
    {
        throw new IllegalArgumentException( I18n.err( I18n.ERR_04144_NULL_NEW_DN ) );
    }

    if ( newDn.isRootDse() )
    {
        throw new IllegalArgumentException( I18n.err( I18n.ERR_04145_ROOT_DSE_CANNOT_BE_TARGET ) );
    }

    // Create the request
    ModifyDnRequest modDnRequest = new ModifyDnRequestImpl();
    modDnRequest.setName( entryDn );
    modDnRequest.setNewRdn( newDn.getRdn() );
    
    // Check if we really need to specify newSuperior.
    // newSuperior is optional [RFC4511, section 4.9]
    // Some servers (e.g. OpenDJ 2.6) require a special privilege if
    // newSuperior is specified even if it is the same as the old one. Therefore let's not
    // specify it if we do not need it. This is better interoperability. 
    Dn newDnParent = newDn.getParent();
    if ( newDnParent != null && !newDnParent.equals( entryDn.getParent() ) )
    {
        modDnRequest.setNewSuperior( newDnParent );
    }
    
    modDnRequest.setDeleteOldRdn( deleteOldRdn );

    ModifyDnResponse modifyDnResponse = modifyDn( modDnRequest );

    processResponse( modifyDnResponse );
}
 
Example 7
Source File: SearchRequestHandler.java    From MyVirtualDirectory with Apache License 2.0 4 votes vote down vote up
/**
 * Searches up the ancestry of a Dn searching for the farthest referral
 * ancestor.  This is required to properly handle referrals.  Note that
 * this function is quite costly since it attempts to lookup all the
 * ancestors up the hierarchy just to see if they represent referrals.
 * Techniques can be employed later to improve this performance hit by
 * having an intelligent referral cache.
 *
 * @return the farthest referral ancestor or null
 * @throws Exception if there are problems during this search
 */
// This will suppress PMD.EmptyCatchBlock warnings in this method
@SuppressWarnings("PMD.EmptyCatchBlock")
public static final Entry getFarthestReferralAncestor( LdapSession session, Dn target ) throws Exception
{
    Entry entry;
    Entry farthestReferralAncestor = null;
    Dn dn = target;

    dn = dn.getParent();

    while ( !dn.isEmpty() )
    {
        if ( IS_DEBUG )
        {
            LOG.debug( "Walking ancestors of {} to find referrals.", dn );
        }

        try
        {
            entry = session.getCoreSession().lookup( dn );

            boolean isReferral = ( ( ClonedServerEntry ) entry ).getOriginalEntry().contains(
                SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.REFERRAL_OC );

            if ( isReferral )
            {
                farthestReferralAncestor = entry;
            }

            dn = dn.getParent();
        }
        catch ( LdapException e )
        {
            if ( IS_DEBUG )
            {
                LOG.debug( "Entry for {} not found.", dn );
            }

            // update the Dn as we strip last component
            dn = dn.getParent();
        }
    }

    return farthestReferralAncestor;
}
 
Example 8
Source File: ExceptionInterceptor.java    From MyVirtualDirectory with Apache License 2.0 4 votes vote down vote up
/**
 * In the pre-invocation state this interceptor method checks to see if the entry to be added already exists.  If it
 * does an exception is raised.
 */
public void add( AddOperationContext addContext ) throws LdapException
{
    Dn name = addContext.getDn();

    if ( subschemSubentryDn.equals( name ) )
    {
        throw new LdapEntryAlreadyExistsException( I18n.err( I18n.ERR_249 ) );
    }

    Dn suffix = nexus.getSuffixDn( name );

    // we're adding the suffix entry so just ignore stuff to mess with the parent
    if ( suffix.equals( name ) )
    {
        next( addContext );
        return;
    }

    Dn parentDn = name.getParent();

    // check if we're trying to add to a parent that is an alias
    boolean notAnAlias;

    synchronized ( notAliasCache )
    {
        notAnAlias = notAliasCache.containsKey( parentDn.getNormName() );
    }

    /*if ( !notAnAlias )
    {
        // We don't know if the parent is an alias or not, so we will launch a
        // lookup, and update the cache if it's not an alias
        Entry attrs;

        try
        {
            CoreSession session = addContext.getSession();
            LookupOperationContext lookupContext = new LookupOperationContext( session, parentDn, SchemaConstants.ALL_ATTRIBUTES_ARRAY );

            attrs = directoryService.getPartitionNexus().lookup( lookupContext );
        }
        catch ( Exception e )
        {
            LdapNoSuchObjectException e2 = new LdapNoSuchObjectException(
                I18n.err( I18n.ERR_251_PARENT_NOT_FOUND, parentDn.getName() ) );
            throw e2;
        }

        Attribute objectClass = ( ( ClonedServerEntry ) attrs ).getOriginalEntry().get(
            OBJECT_CLASS_AT );

        if ( objectClass.contains( SchemaConstants.ALIAS_OC ) )
        {
            String msg = I18n.err( I18n.ERR_252_ALIAS_WITH_CHILD_NOT_ALLOWED, name.getName(), parentDn.getName() );
            LdapAliasException e = new LdapAliasException( msg );
            //e.setResolvedName( DNFactory.create( parentDn.getName() ) );
            throw e;
        }
        else
        {
            synchronized ( notAliasCache )
            {
                notAliasCache.put( parentDn.getNormName(), parentDn );
            }
        }
    }*/

    next( addContext );
}
 
Example 9
Source File: SearchRequestHandler.java    From MyVirtualDirectory with Apache License 2.0 4 votes vote down vote up
/**
 * Searches up the ancestry of a Dn searching for the farthest referral
 * ancestor.  This is required to properly handle referrals.  Note that
 * this function is quite costly since it attempts to lookup all the
 * ancestors up the hierarchy just to see if they represent referrals.
 * Techniques can be employed later to improve this performance hit by
 * having an intelligent referral cache.
 *
 * @return the farthest referral ancestor or null
 * @throws Exception if there are problems during this search
 */
// This will suppress PMD.EmptyCatchBlock warnings in this method
@SuppressWarnings("PMD.EmptyCatchBlock")
public static final Entry getFarthestReferralAncestor( LdapSession session, Dn target ) throws Exception
{
    Entry entry;
    Entry farthestReferralAncestor = null;
    Dn dn = target;

    dn = dn.getParent();

    while ( !dn.isEmpty() )
    {
        if ( IS_DEBUG )
        {
            LOG.debug( "Walking ancestors of {} to find referrals.", dn );
        }

        try
        {
            entry = session.getCoreSession().lookup( dn );

            boolean isReferral = ( ( ClonedServerEntry ) entry ).getOriginalEntry().contains(
                SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.REFERRAL_OC );

            if ( isReferral )
            {
                farthestReferralAncestor = entry;
            }

            dn = dn.getParent();
        }
        catch ( LdapException e )
        {
            if ( IS_DEBUG )
            {
                LOG.debug( "Entry for {} not found.", dn );
            }

            // update the Dn as we strip last component
            dn = dn.getParent();
        }
    }

    return farthestReferralAncestor;
}
 
Example 10
Source File: ExceptionInterceptor.java    From MyVirtualDirectory with Apache License 2.0 4 votes vote down vote up
/**
 * In the pre-invocation state this interceptor method checks to see if the entry to be added already exists.  If it
 * does an exception is raised.
 */
public void add( AddOperationContext addContext ) throws LdapException
{
    Dn name = addContext.getDn();

    if ( subschemSubentryDn.equals( name ) )
    {
        throw new LdapEntryAlreadyExistsException( I18n.err( I18n.ERR_249 ) );
    }

    Dn suffix = nexus.getSuffixDn( name );

    // we're adding the suffix entry so just ignore stuff to mess with the parent
    if ( suffix.equals( name ) )
    {
        next( addContext );
        return;
    }

    Dn parentDn = name.getParent();

    // check if we're trying to add to a parent that is an alias
    boolean notAnAlias;

    synchronized ( notAliasCache )
    {
        notAnAlias = notAliasCache.containsKey( parentDn.getNormName() );
    }

    /*if ( !notAnAlias )
    {
        // We don't know if the parent is an alias or not, so we will launch a
        // lookup, and update the cache if it's not an alias
        Entry attrs;

        try
        {
            CoreSession session = addContext.getSession();
            LookupOperationContext lookupContext = new LookupOperationContext( session, parentDn, SchemaConstants.ALL_ATTRIBUTES_ARRAY );

            attrs = directoryService.getPartitionNexus().lookup( lookupContext );
        }
        catch ( Exception e )
        {
            LdapNoSuchObjectException e2 = new LdapNoSuchObjectException(
                I18n.err( I18n.ERR_251_PARENT_NOT_FOUND, parentDn.getName() ) );
            throw e2;
        }

        Attribute objectClass = ( ( ClonedServerEntry ) attrs ).getOriginalEntry().get(
            OBJECT_CLASS_AT );

        if ( objectClass.contains( SchemaConstants.ALIAS_OC ) )
        {
            String msg = I18n.err( I18n.ERR_252_ALIAS_WITH_CHILD_NOT_ALLOWED, name.getName(), parentDn.getName() );
            LdapAliasException e = new LdapAliasException( msg );
            //e.setResolvedName( DNFactory.create( parentDn.getName() ) );
            throw e;
        }
        else
        {
            synchronized ( notAliasCache )
            {
                notAliasCache.put( parentDn.getNormName(), parentDn );
            }
        }
    }*/

    next( addContext );
}