} catch( ReferralException e ) { ReferralImpl refs = new ReferralImpl(); result.setReferral( refs ); result.setResultCode( ResultCodeEnum.REFERRAL ); result.setErrorMessage( "Encountered referral attempting to handle compare request." );   do { refs.addLdapUrl( ( String ) e.getReferralInfo() ); } while( e.skipReferral() ); session.write( req.getResultResponse() ); return;
    public void messageReceived( IoSession session, Object request )    {        CompareRequest req = ( CompareRequest ) request;        LdapResult result = req.getResultResponse().getLdapResult();        try        {            LdapContext ctx = SessionRegistry.getSingleton().getLdapContext( session, null, true );            ServerLdapContext newCtx = ( ServerLdapContext ) ctx.lookup( "" );            if ( req.getControls().containsKey( ManageDsaITControl.CONTROL_OID ) )            {                newCtx.addToEnvironment( Context.REFERRAL, "ignore" );            }            else            {                newCtx.addToEnvironment( Context.REFERRAL, "throw" );            }            newCtx.setRequestControls( ( Control[] ) req.getControls().values().toArray( EMPTY_CONTROLS ) );            LdapName name = new LdapName( req.getName() );            if ( newCtx.compare( name, req.getAttributeId(), req.getAssertionValue() ) )            {                result.setResultCode( ResultCodeEnum.COMPARETRUE );            }            else            {                result.setResultCode( ResultCodeEnum.COMPAREFALSE );            }        }        catch ( Exception e )        {            String msg = "failed to compare 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 instanceof NamingException )            {            	NamingException ne = (NamingException)e;            	                if ( ( ne.getResolvedName() != null ) &&                        ( ( code == ResultCodeEnum.NOSUCHOBJECT ) ||                          ( code == ResultCodeEnum.ALIASPROBLEM ) ||                          ( code == ResultCodeEnum.INVALIDDNSYNTAX ) ||                          ( code == ResultCodeEnum.ALIASDEREFERENCINGPROBLEM ) ) )                {                    result.setMatchedDn( ne.getResolvedName().toString() );                }            }            session.write( req.getResultResponse() );            return;        }        result.setMatchedDn( req.getName() );        session.write( req.getResultResponse() );    }