ActiveServerFilter implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { if (isFilteredURI(servletRequest)) { LOG.debug("Is a filtered URI: {}. Passing request downstream.", ((HttpServletRequest)servletRequest).getRequestURI()); filterChain.doFilter(servletRequest, servletResponse); } else if (isInstanceActive()) { LOG.debug("Active. Passing request downstream"); filterChain.doFilter(servletRequest, servletResponse); } else if (serviceState.isInstanceInTransition()) { HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse; LOG.error("Instance in transition. Service may not be ready to return a result"); httpServletResponse.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE); } else { HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse; String activeServerAddress = activeInstanceState.getActiveServerAddress(); if (activeServerAddress == null) { LOG.error("Could not retrieve active server address as it is null. Cannot redirect request {}", ((HttpServletRequest)servletRequest).getRequestURI()); httpServletResponse.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE); } else { handleRedirect((HttpServletRequest) servletRequest, httpServletResponse, activeServerAddress); } } } @Inject  ActiveServerFilter(ActiveInstanceState activeInstanceState, ServiceState serviceState); @Override void init(FilterConfig filterConfig); @Override void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
                         FilterChain filterChain); @Override void destroy();  }
@Test public void testShouldPassThroughRequestsIfActive() throws IOException, ServletException { when(serviceState.getState()).thenReturn(ServiceState.ServiceStateValue.ACTIVE); when(servletRequest.getRequestURI()).thenReturn("api/atlas/types"); ActiveServerFilter activeServerFilter = new ActiveServerFilter(activeInstanceState, serviceState); activeServerFilter.doFilter(servletRequest, servletResponse, filterChain); verify(filterChain).doFilter(servletRequest, servletResponse); }
@Test public void testShouldFailIfCannotRetrieveActiveServerAddress() throws IOException, ServletException { when(serviceState.getState()).thenReturn(ServiceState.ServiceStateValue.PASSIVE); when(servletRequest.getRequestURI()).thenReturn("api/atlas/types"); ActiveServerFilter activeServerFilter = new ActiveServerFilter(activeInstanceState, serviceState); when(activeInstanceState.getActiveServerAddress()).thenReturn(null); activeServerFilter.doFilter(servletRequest, servletResponse, filterChain); verify(servletResponse).sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE); }
@Test public void testShouldRedirectRequestToActiveServerAddress() throws IOException, ServletException { when(serviceState.getState()).thenReturn(ServiceState.ServiceStateValue.PASSIVE); when(servletRequest.getRequestURI()).thenReturn("api/atlas/types"); ActiveServerFilter activeServerFilter = new ActiveServerFilter(activeInstanceState, serviceState); when(activeInstanceState.getActiveServerAddress()).thenReturn(ACTIVE_SERVER_ADDRESS); when(servletRequest.getRequestURI()).thenReturn("types"); when(servletRequest.getMethod()).thenReturn(HttpMethod.GET); activeServerFilter.doFilter(servletRequest, servletResponse, filterChain); verify(servletResponse).sendRedirect(ACTIVE_SERVER_ADDRESS+"types"); }
@Test public void testRedirectedRequestShouldContainQueryParameters() throws IOException, ServletException { when(serviceState.getState()).thenReturn(ServiceState.ServiceStateValue.PASSIVE); when(servletRequest.getRequestURI()).thenReturn("api/atlas/types"); ActiveServerFilter activeServerFilter = new ActiveServerFilter(activeInstanceState, serviceState); when(activeInstanceState.getActiveServerAddress()).thenReturn(ACTIVE_SERVER_ADDRESS); when(servletRequest.getMethod()).thenReturn(HttpMethod.GET); when(servletRequest.getRequestURI()).thenReturn("types"); when(servletRequest.getQueryString()).thenReturn("query=TRAIT"); activeServerFilter.doFilter(servletRequest, servletResponse, filterChain); verify(servletResponse).sendRedirect(ACTIVE_SERVER_ADDRESS+"types?query=TRAIT"); }
@Test public void testShouldRedirectPOSTRequest() throws IOException, ServletException { when(serviceState.getState()).thenReturn(ServiceState.ServiceStateValue.PASSIVE); when(servletRequest.getRequestURI()).thenReturn("api/atlas/types"); ActiveServerFilter activeServerFilter = new ActiveServerFilter(activeInstanceState, serviceState); when(activeInstanceState.getActiveServerAddress()).thenReturn(ACTIVE_SERVER_ADDRESS); when(servletRequest.getMethod()).thenReturn(HttpMethod.POST); when(servletRequest.getRequestURI()).thenReturn("types"); activeServerFilter.doFilter(servletRequest, servletResponse, filterChain); verify(servletResponse).setHeader("Location", ACTIVE_SERVER_ADDRESS+"types"); verify(servletResponse).setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT); }
@Test public void testShouldRedirectPUTRequest() throws IOException, ServletException { when(serviceState.getState()).thenReturn(ServiceState.ServiceStateValue.PASSIVE); when(servletRequest.getRequestURI()).thenReturn("api/atlas/types"); ActiveServerFilter activeServerFilter = new ActiveServerFilter(activeInstanceState, serviceState); when(activeInstanceState.getActiveServerAddress()).thenReturn(ACTIVE_SERVER_ADDRESS); when(servletRequest.getMethod()).thenReturn(HttpMethod.PUT); when(servletRequest.getRequestURI()).thenReturn("types"); activeServerFilter.doFilter(servletRequest, servletResponse, filterChain); verify(servletResponse).setHeader("Location", ACTIVE_SERVER_ADDRESS+"types"); verify(servletResponse).setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT); }
@Test public void testShouldRedirectDELETERequest() throws IOException, ServletException { when(serviceState.getState()).thenReturn(ServiceState.ServiceStateValue.PASSIVE); when(servletRequest.getRequestURI()).thenReturn("api/atlas/types"); ActiveServerFilter activeServerFilter = new ActiveServerFilter(activeInstanceState, serviceState); when(activeInstanceState.getActiveServerAddress()).thenReturn(ACTIVE_SERVER_ADDRESS); when(servletRequest.getMethod()).thenReturn(HttpMethod.DELETE); when(servletRequest.getRequestURI()). thenReturn("api/atlas/entities/6ebb039f-eaa5-4b9c-ae44-799c7910545d/traits/test_tag_ha3"); activeServerFilter.doFilter(servletRequest, servletResponse, filterChain); verify(servletResponse).setHeader("Location", ACTIVE_SERVER_ADDRESS + "api/atlas/entities/6ebb039f-eaa5-4b9c-ae44-799c7910545d/traits/test_tag_ha3"); verify(servletResponse).setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT); }
@Test public void testShouldReturnServiceUnavailableIfStateBecomingActive() throws IOException, ServletException { when(serviceState.getState()).thenReturn(ServiceState.ServiceStateValue.BECOMING_ACTIVE); when(servletRequest.getRequestURI()).thenReturn("api/atlas/types"); ActiveServerFilter activeServerFilter = new ActiveServerFilter(activeInstanceState, serviceState); activeServerFilter.doFilter(servletRequest, servletResponse, filterChain); verify(servletResponse).sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE); }
@Test public void testShouldNotRedirectAdminAPIs() throws IOException, ServletException { when(serviceState.getState()).thenReturn(ServiceState.ServiceStateValue.PASSIVE); when(servletRequest.getMethod()).thenReturn(HttpMethod.GET); when(servletRequest.getRequestURI()). thenReturn("api/atlas/admin/asmasn"); ActiveServerFilter activeServerFilter = new ActiveServerFilter(activeInstanceState, serviceState); activeServerFilter.doFilter(servletRequest, servletResponse, filterChain); verify(filterChain).doFilter(servletRequest, servletResponse); verifyZeroInteractions(activeInstanceState); }