class MaintenanceInterceptor extends HandlerInterceptorAdapter {

    public boolean preHandle( HttpServletRequest request, HttpServletResponse response, Object handler )
        throws Exception
    {

        String method = request.getMethod();
        String requestURL = request.getRequestURL().toString();
        String remoteAddr = request.getRemoteAddr();

        logger.info( "Received request: [ {} on {} from remote address {} ].", method, requestURL, remoteAddr );

        if ( !isTheRequestFromValidSource( requestURL, remoteAddr ) )
        {
            logger.error( "Resource access denied for requestURL: {} remoteAddr: {}", requestURL, remoteAddr );
            throw new HMSRestException( Status.FORBIDDEN.getStatusCode(), Status.FORBIDDEN.toString(),
                                        " Api access denied." );
        }

        ServiceState serviceState = ServiceManager.getServiceState();
        if ( serviceState.equals( ServiceState.NORMAL_MAINTENANCE )
            || serviceState.equals( ServiceState.FORCE_MAINTENANCE ) )
        {

            // allow upgrade monitoring api, even if service is in maintenance
            // Allow get/set HMS statue even if service is in maintenance
            if ( ( !( method.equalsIgnoreCase( RequestMethod.GET.toString() )
                && requestURL.matches( ".*/upgrade/monitor/.*" ) ) ) && !( requestURL.matches( ".*/state.*" ) ) )
            {

                logger.info( "HMS Service is in {} state. Aborting the request: [ {} on {} ].", serviceState, method,
                             requestURL );
                throw new HMSRestException( Status.SERVICE_UNAVAILABLE.getStatusCode(),
                                            Status.SERVICE_UNAVAILABLE.toString(), " Service is under maintenance." );
            }
        }
        return validateNodeAvialability( request, response );
    }

}