String msg = "failed to modify DN of entry " + req.getName();
String msg = "failed to modify DN of entry " + req.getName() + ": " + e.getMessage();
    public void messageReceived( IoSession session, Object request ) throws Exception    {        ModifyDnRequest req = ( ModifyDnRequest ) request;        LdapResult result = req.getResultResponse().getLdapResult();        if ( IS_DEBUG )        {            log.debug( "req.getName() == [" + req.getName() + "]" );        }        if ( req.getName().isEmpty() )        {            // 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() )                {                    LdapDN oldDn = req.getName();                    LdapDN newDn = null;                    LdapDN newSuperior = req.getNewSuperior();                                        if ( newSuperior.isEmpty() )                    {                        if ( oldDn.isEmpty() )                        {                            newDn = oldDn;                        }                        else                        {                            newDn = (LdapDN)oldDn.getPrefix( oldDn.size() - 1 );                        }                    }                    else                    {                        newDn = newSuperior;                    }                    if ( req.getNewRdn() != null )                    {                        newDn.add( req.getNewRdn() );                    }                    else                    {                        newDn.add( oldDn.getRdn() );                    }                    ctx.rename( req.getName(), newDn );                }                else                {                    LdapDN newDn = ( LdapDN ) req.getName().clone();                    newDn.remove( newDn.size() - 1 );                    newDn.add( req.getNewRdn() );                    ctx.rename( req.getName(), newDn );                }            }            catch ( ReferralException e )            {                ReferralImpl refs = new ReferralImpl();                result.setReferral( refs );                result.setResultCode( ResultCodeEnum.REFERRAL );                result.setErrorMessage( "Encountered referral attempting to handle modifyDn request." );                result.setMatchedDn( (LdapDN)e.getResolvedName() );                do                {                    refs.addLdapUrl( ( String ) e.getReferralInfo() );                }                while ( e.skipReferral() );                                session.write( req.getResultResponse() );                return;            }            catch ( NamingException e )            {                String msg = "failed to modify DN of entry " + req.getName();                                if ( IS_DEBUG )                {                    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( (LdapDN)e.getResolvedName() );                }                session.write( req.getResultResponse() );                return;            }            result.setResultCode( ResultCodeEnum.SUCCESS );            session.write( req.getResultResponse() );        }    }