ContentDecompressionFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (request instanceof HttpServletRequest && response instanceof HttpServletResponse) { HttpServletRequest httpRequest = (HttpServletRequest) request; String contentEncoding = httpRequest.getHeader(HttpHeaders.CONTENT_ENCODING); request = new HttpServletRequestWrapper(httpRequest) { @Override public ServletInputStream getInputStream() throws IOException { ServletInputStream originalStream = httpRequest.getInputStream(); ServletInputStream newStream; if ("gzip".equals(contentEncoding)) { newStream = new ServletInputStreamWrapper(new GzipCompressorInputStream(originalStream)); } else if ("deflate".equals(contentEncoding)) { newStream = new ServletInputStreamWrapper(new DeflateCompressorInputStream(originalStream)); } else { newStream = originalStream; } return newStream; } }; } chain.doFilter(request, response); }  @Override void init(FilterConfig filterConfig); @Override void doFilter(ServletRequest request, ServletResponse response, FilterChain chain); @Override void destroy();  }
@Test public void test() throws Exception { when(request.getHeader(HttpHeaders.CONTENT_ENCODING)).thenReturn(contentEncoding); when(request.getInputStream()).thenReturn(new ServletInputStreamWrapper(new ByteArrayInputStream(inputBytes))); ArgumentCaptor<HttpServletRequest> requestCaptor = ArgumentCaptor.forClass(HttpServletRequest.class); doNothing().when(chain).doFilter(requestCaptor.capture(), any(ServletResponse.class)); filter.doFilter(request, response, chain); HttpServletRequest outputRequest = requestCaptor.getValue(); byte[] outputBytes = toByteArray(outputRequest.getInputStream()); assertTrue(Arrays.equals(outputBytes, TEST_BYTES)); }