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 ) ) { 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 ); }  boolean preHandle( HttpServletRequest request, HttpServletResponse response, Object handler ); @Override void afterCompletion( HttpServletRequest request, HttpServletResponse response, Object handler,
                                 Exception ex ); static int getActiveRequests(); String getLocalIpRegex(); @Value( "${hms.local.ip.regex}" ) void setLocalIpRegex( String localIpRegex ); String getPrivateIpEligibleApisRegEx(); @Value( "${hms.private.ip.eligible.apis.regex}" ) void setPrivateIpEligibleApisRegEx( String privateIpEligibleApisRegEx );  }
@Test @Theory public void testIfTheRequestIsFromValidSource( I1 input ) throws Exception { Mockito.when( request.getRequestURL() ).thenReturn( new StringBuffer( input.url ) ); Mockito.when( request.getRemoteAddr() ).thenReturn( input.remoteAdd ); interceptor.preHandle( request, response, handler ); }
@Test @Theory public void testIfTheRequestIsFromValidSource( I2 input ) throws Exception { Mockito.when( request.getRequestURL() ).thenReturn( new StringBuffer( input.url ) ); Mockito.when( request.getRemoteAddr() ).thenReturn( input.remoteAdd ); interceptor.preHandle( request, response, handler ); }
@Test( expected = HMSRestException.class ) @Theory public void testIfTheRequestIsFromValidSource( I3 input ) throws Exception { Mockito.when( request.getRequestURL() ).thenReturn( new StringBuffer( input.url ) ); Mockito.when( request.getRemoteAddr() ).thenReturn( input.remoteAdd ); interceptor.preHandle( request, response, handler ); }