TrimFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletResponse resp = (HttpServletResponse) response; TrimResponseWrapper trimResponse = new TrimResponseWrapper(resp); chain.doFilter(request, trimResponse); Boolean trimmable = trimResponse.isTrimmable(); if ((trimmable != null) && trimmable.booleanValue()) { byte[] data = getTrimmedData(trimResponse); response.setContentLength(data.length); response.getOutputStream().write(data); } }  @Override void init(FilterConfig filterConfig); @Override void destroy(); @Override void doFilter(ServletRequest request, ServletResponse response, FilterChain chain);  }
@Test public void doFilterWithOutputStream() throws IOException, ServletException { doFilter(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { HttpServletResponse response = (HttpServletResponse) invocation.getArguments()[1]; byte[] data = TEXT.getBytes(Charsets.UTF_8); response.setContentType(CONTENT_TYPE); response.setContentLength(data.length); response.getOutputStream().write(data); return null; } }, CONTENT_TYPE, TRIMMED_TEXT_DATA); }
@Test public void doFilterWithWriter() throws IOException, ServletException { doFilter(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { HttpServletResponse response = (HttpServletResponse) invocation.getArguments()[1]; byte[] data = TEXT.getBytes(Charsets.UTF_8); response.setContentLength(data.length); response.setContentType(CONTENT_TYPE); response.getWriter().print(TEXT); return null; } }, CONTENT_TYPE, TRIMMED_TEXT_DATA); }
@Test public void doFilterWithOutputStreamButNonTrimmableContent() throws IOException, ServletException { final byte[] data = { 1, 2, 3 }; doFilter(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { HttpServletResponse response = (HttpServletResponse) invocation.getArguments()[1]; response.setContentType("image/png"); response.setContentLength(data.length); for (byte b : data) { response.getOutputStream().write(b); } return null; } }, "image/png", data); }
@Test public void doFilterWithWriterButNonTrimmableContent() throws IOException, ServletException { doFilter(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { HttpServletResponse response = (HttpServletResponse) invocation.getArguments()[1]; response.setContentType("image/png"); response.setContentLength(TEXT.getBytes(Charsets.UTF_8).length); response.getWriter().print(TEXT.substring(0, 1)); response.getWriter().print(TEXT.substring(1)); return null; } }, "image/png", TEXT.getBytes(Charsets.UTF_8)); }
@Test public void doFilterWithUnknownContentType() throws IOException, ServletException { final byte[] data = { 1, 2, 3 }; doFilter(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { HttpServletResponse response = (HttpServletResponse) invocation.getArguments()[1]; response.setContentLength(data.length); response.getOutputStream().write(data); return null; } }, null, data); }