} catch( ReferralException e ) { ReferralImpl refs = new ReferralImpl(); result.setReferral( refs ); result.setResultCode( ResultCodeEnum.REFERRAL ); result.setErrorMessage( "Encountered referral attempting to handle add request." );   do { refs.addLdapUrl( ( String ) e.getReferralInfo() ); } while( e.skipReferral() ); session.write( req.getResultResponse() ); return;
    public void messageReceived( IoSession session, Object request )    {        AddRequest req = ( AddRequest ) request;        LdapResult result = req.getResultResponse().getLdapResult();                if ( log.isDebugEnabled() )        {            log.debug( "Received a Add message : " + req.toString() );        }        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 ) );            ctx.createSubcontext( req.getEntry(), req.getAttributes() );        }        catch( NamingException e )        {            String msg = "failed to add entry " + req.getEntry();            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() );    }