DiskSpaceFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { final HttpServletRequest httpRequest = (HttpServletRequest) request; if (isUriInLocalPaths(httpRequest)) { LOGGER.debug("Passing through request for {}", httpRequest.getRequestURI()); chain.doFilter(request, response); } else { if (isUriInCheckPaths(httpRequest)) { try { if (!spaceChecker.isSpaceAvailable()) { ((HttpServletResponse) response).sendError(proxyConfiguration.getErrorStatus()); } } catch (DiskSpaceCheckerException e) { LOGGER.error("Exception thrown by DiskSpaceChecker", e); ((HttpServletResponse) response).sendError(proxyConfiguration.getErrorStatus()); } } if (!response.isCommitted()) { RequestDispatcher dispatcher = httpRequest.getRequestDispatcher(getProxyPath(httpRequest)); dispatcher.forward(request, response); } } } DiskSpaceFilter(DiskSpaceChecker spaceChecker, ProxyConfiguration proxyConfiguration, String... localPaths); @Override void init(FilterConfig filterConfig); @Override void doFilter(ServletRequest request, ServletResponse response,
						 FilterChain chain); @Override void destroy();  }
@Test public void passThroughLocalPath() throws Exception { final String requestUri = localPaths[0]; final HttpServletRequest req = mock(HttpServletRequest.class); when(req.getRequestURI()).thenReturn(requestUri); when(req.getContextPath()).thenReturn(""); final ServletResponse response = mock(ServletResponse.class); final FilterChain chain = mock(FilterChain.class); filter.doFilter(req, response, chain); verify(req, atLeastOnce()).getRequestURI(); verify(chain).doFilter(req, response); }
@Test public void proxyWithoutCheckingState() throws Exception { final String requestUri = "/solr/select"; final String requestQuery = "q=test"; final String proxiedUri = DiskSpaceProxyServlet.PROXY_PATH_PREFIX + "/solr/select?" + requestQuery; final RequestDispatcher dispatcher = mock(RequestDispatcher.class); final HttpServletRequest req = mock(HttpServletRequest.class); when(req.getRequestURI()).thenReturn(requestUri); when(req.getContextPath()).thenReturn(""); when(req.getQueryString()).thenReturn(requestQuery); when(req.getRequestDispatcher(proxiedUri)).thenReturn(dispatcher); final ServletResponse response = mock(ServletResponse.class); final FilterChain chain = mock(FilterChain.class); filter.doFilter(req, response, chain); verify(req, atLeastOnce()).getRequestURI(); verify(req, atLeastOnce()).getQueryString(); verify(req).getRequestDispatcher(proxiedUri); verify(dispatcher).forward(req, response); }
@Test public void returnErrorWhenDiskCheckFails() throws Exception { when(checker.isSpaceAvailable()).thenThrow(new DiskSpaceCheckerException("Error")); final String requestUri = "/solr/update"; final HttpServletRequest req = mock(HttpServletRequest.class); when(req.getRequestURI()).thenReturn(requestUri); when(req.getContextPath()).thenReturn(""); when(req.getQueryString()).thenReturn(null); final HttpServletResponse response = mock(HttpServletResponse.class); when(response.isCommitted()).thenReturn(true); final FilterChain chain = mock(FilterChain.class); filter.doFilter(req, response, chain); verify(req, atLeastOnce()).getRequestURI(); verify(response).sendError(errorCode); verify(checker).isSpaceAvailable(); }
@Test public void returnErrorWhenDiskFull() throws Exception { when(checker.isSpaceAvailable()).thenReturn(false); final String requestUri = "/solr/update"; final HttpServletRequest req = mock(HttpServletRequest.class); when(req.getRequestURI()).thenReturn(requestUri); when(req.getContextPath()).thenReturn(""); when(req.getQueryString()).thenReturn(null); final HttpServletResponse response = mock(HttpServletResponse.class); when(response.isCommitted()).thenReturn(true); final FilterChain chain = mock(FilterChain.class); filter.doFilter(req, response, chain); verify(req, atLeastOnce()).getRequestURI(); verify(response).sendError(errorCode); verify(checker).isSpaceAvailable(); }
@Test public void proxyWhenStateCheckPasses() throws Exception { when(checker.isSpaceAvailable()).thenReturn(true); final String requestUri = "/solr/banana/update"; final String requestQuery = "q=test"; final String proxiedUri = DiskSpaceProxyServlet.PROXY_PATH_PREFIX + requestUri + "?" + requestQuery; final RequestDispatcher dispatcher = mock(RequestDispatcher.class); final HttpServletRequest req = mock(HttpServletRequest.class); when(req.getRequestURI()).thenReturn(requestUri); when(req.getContextPath()).thenReturn(""); when(req.getQueryString()).thenReturn(requestQuery); when(req.getRequestDispatcher(proxiedUri)).thenReturn(dispatcher); final ServletResponse response = mock(ServletResponse.class); final FilterChain chain = mock(FilterChain.class); filter.doFilter(req, response, chain); verify(req, atLeastOnce()).getRequestURI(); verify(req, atLeastOnce()).getQueryString(); verify(req).getRequestDispatcher(proxiedUri); verify(dispatcher).forward(req, response); verify(checker).isSpaceAvailable(); }
@Test public void skipCheckWhenRequestNotPOST() throws Exception { final String requestUri = "/solr/postUpdate"; final String requestQuery = "q=test"; final String proxiedUri = DiskSpaceProxyServlet.PROXY_PATH_PREFIX + requestUri + "?" + requestQuery; final RequestDispatcher dispatcher = mock(RequestDispatcher.class); final HttpServletRequest req = mock(HttpServletRequest.class); when(req.getRequestURI()).thenReturn(requestUri); when(req.getContextPath()).thenReturn(""); when(req.getQueryString()).thenReturn(requestQuery); when(req.getMethod()).thenReturn("GET"); when(req.getRequestDispatcher(proxiedUri)).thenReturn(dispatcher); final ServletResponse response = mock(ServletResponse.class); final FilterChain chain = mock(FilterChain.class); filter.doFilter(req, response, chain); verify(req, atLeastOnce()).getRequestURI(); verify(req, atLeastOnce()).getQueryString(); verify(req).getRequestDispatcher(proxiedUri); verify(dispatcher).forward(req, response); }
@Test public void checkSpaceForPOSTRequest() throws Exception { when(checker.isSpaceAvailable()).thenReturn(true); final String requestUri = "/solr/postUpdate"; final String requestQuery = "q=test"; final String proxiedUri = DiskSpaceProxyServlet.PROXY_PATH_PREFIX + requestUri + "?" + requestQuery; final RequestDispatcher dispatcher = mock(RequestDispatcher.class); final HttpServletRequest req = mock(HttpServletRequest.class); when(req.getRequestURI()).thenReturn(requestUri); when(req.getContextPath()).thenReturn(""); when(req.getQueryString()).thenReturn(requestQuery); when(req.getMethod()).thenReturn("POST"); when(req.getRequestDispatcher(proxiedUri)).thenReturn(dispatcher); final ServletResponse response = mock(ServletResponse.class); final FilterChain chain = mock(FilterChain.class); filter.doFilter(req, response, chain); verify(req, atLeastOnce()).getRequestURI(); verify(req, atLeastOnce()).getQueryString(); verify(req).getRequestDispatcher(proxiedUri); verify(dispatcher).forward(req, response); verify(checker).isSpaceAvailable(); }