AbstractMDCPutFilter extends OncePerRequestFilter { @Override protected final void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { String key = getMDCKey(request, response); String value = getMDCValue(request, response); MDC.put(key, cutValue(value)); try { filterChain.doFilter(request, response); } finally { if (removeValue) { MDC.remove(key); } } }  void setRemoveValue(boolean removeValue); void setMaxMDCValueLength(int maxMDCValueLength);  }
@Test public void testDoFilterInternal_leave_value() throws ServletException, IOException { MDC.put("dummyKey", "dummyValue"); testTarget.doFilterInternal(mockRequest, mockResponse, mockFilterChain); assertThat(MDC.getCopyOfContextMap().size(), is(2)); assertThat(MDC.get("dummyKey"), is("dummyValue")); assertThat(MDC.get("key"), is("value")); }
@Test public void testMaxMDCValueLength_default() throws ServletException, IOException { this.testTarget = new AbstractMDCPutFilter() { @Override protected String getMDCKey(HttpServletRequest request, HttpServletResponse response) { return "key"; } @Override protected String getMDCValue(HttpServletRequest request, HttpServletResponse response) { return "12345678901234567890123456789012a"; } }; testTarget.doFilterInternal(mockRequest, mockResponse, mockFilterChain); assertThat(mockFilterChain.actualMdcPutValue, is( "12345678901234567890123456789012")); }