catch( ReferralException e ) { ReferralImpl refs = new ReferralImpl(); result.setReferral( refs ); result.setResultCode( ResultCodeEnum.REFERRAL ); result.setErrorMessage( "Encountered referral attempting to handle modifyDn request." );   do { refs.addLdapUrl( ( String ) e.getReferralInfo() ); } while( e.skipReferral() ); session.write( req.getResultResponse() ); return; }
    public void messageReceived( IoSession session, Object request )    {        ModifyDnRequest req = ( ModifyDnRequest ) request;        LdapResult result = req.getResultResponse().getLdapResult();        if ( LOG.isDebugEnabled() )        {        	LOG.debug( "req.getName() == [" + req.getName() +"]" );        }                if (req.getName() == null || req.getName().length() == 0)        {            // it is not allowed to modify the name of the Root DSE            String msg = "Modify DN is not allowed on Root DSE.";            result.setResultCode( ResultCodeEnum.PROTOCOLERROR );            result.setErrorMessage( msg );            session.write( req.getResultResponse() );        }        else        {            try            {                LdapContext ctx = SessionRegistry.getSingleton().getLdapContext( session, null, true );                if ( req.getControls().containsKey( ManageDsaITControl.CONTROL_OID ) )                {                    ctx.addToEnvironment( Context.REFERRAL, "ignore" );                }                else                {                    ctx.addToEnvironment( Context.REFERRAL, "throw" );                }                ctx.setRequestControls( ( Control[] ) req.getControls().values().toArray( EMPTY_CONTROLS ) );                String deleteRDN = String.valueOf( req.getDeleteOldRdn() );                ctx.addToEnvironment( "java.naming.ldap.deleteRDN", deleteRDN );                if (req.isMove())                {                    LdapName oldDn = new LdapName( req.getName() );                    LdapName newDn = null;                                        String newSuperior = req.getNewSuperior();                    if ( StringTools.isEmpty( newSuperior ) )                    {                    	newDn = (LdapName)oldDn.getPrefix( 1 );                    }                    else                    {                    	newDn = new LdapName( req.getNewSuperior() );                    }                                        if (req.getNewRdn() != null)                    {                        newDn.add( req.getNewRdn() );                    }                    else                    {                        newDn.add( oldDn.getRdn() );                    }                    ctx.rename( new LdapName( req.getName() ), newDn );                }                else                {                    LdapName newDn = new LdapName( req.getName() );                    newDn.remove( newDn.size() - 1 );                    newDn.add( req.getNewRdn() );                    ctx.rename( new LdapName( req.getName() ), newDn );                }            }            catch ( NamingException e )            {                String msg = "failed to modify DN of entry " + req.getName();                if (LOG.isDebugEnabled())                {                    msg += ":\n" + ExceptionUtils.getStackTrace( e );                }                ResultCodeEnum code;                if (e instanceof LdapException)                {                    code = ((LdapException) e).getResultCode();                }                else                {                    code = ResultCodeEnum.getBestEstimate( e, req.getType() );                }                result.setResultCode( code );                result.setErrorMessage( msg );                if ( ( e.getResolvedName() != null )                        && ((code == ResultCodeEnum.NOSUCHOBJECT) || (code == ResultCodeEnum.ALIASPROBLEM)                                || (code == ResultCodeEnum.INVALIDDNSYNTAX) || (code == ResultCodeEnum.ALIASDEREFERENCINGPROBLEM)))                {                    result.setMatchedDn( e.getResolvedName().toString() );                }                session.write( req.getResultResponse() );                return;            }            result.setResultCode( ResultCodeEnum.SUCCESS );            session.write( req.getResultResponse() );        }    }