Source:
<code>public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { long startTime = System.currentTimeMillis(); TeeHttpServletRequest req = new TeeHttpServletRequest( (HttpServletRequest) request); TeeHttpServletResponse resp = new TeeHttpServletResponse( (HttpServletResponse) response); try { MDC.put("requestType", "req"); String requestUUID = UUID.randomUUID().toString(); MDC.put("requestUuid", requestUUID); String correlationId = null; Enumeration<String> headerNames = (Enumeration<String>) ((HttpServletRequest) request) .getHeaderNames(); while (headerNames.hasMoreElements()) { String name = (String) headerNames.nextElement(); if ("X-Correlation-ID".equalsIgnoreCase(name)) { correlationId = ((HttpServletRequest) request).getHeader(name); } } if (correlationId != null) { if (CSID_REGEX.matcher(correlationId).matches()) { <START> MDC.put(CSID_KEY, <END> correlationId.length() > 40 ? correlationId.substring(0, 40) : correlationId); } else { log.warn("Correlation Id must consist of alphanumeric characters or hypens"); } } req.setAttribute("requestUuid", requestUUID); resp.setHeader(customHeaderName, requestUUID); if (log.isDebugEnabled()) { log.debug("{}", ServletLogger.logRequest(req)); } else { log.info("{}", ServletLogger.logBasicRequestInfo(req)); } chain.doFilter(req, resp); if (log.isDebugEnabled()) { log.debug("{}", ServletLogger.logResponse(resp, startTime)); } else { log.info("{}", ServletLogger.logBasicResponseInfo(resp, startTime)); } resp.finish(); } finally { MDC.clear(); } }</code><technical_language>I feel truncating is right approach here, if do, output a log message indicating truncated original down, track figure out happened in event a collision caused it</technical_language>

Target:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { long startTime = System.currentTimeMillis(); TeeHttpServletRequest req = new TeeHttpServletRequest( (HttpServletRequest) request); TeeHttpServletResponse resp = new TeeHttpServletResponse( (HttpServletResponse) response); try { MDC.put("requestType", "req"); String requestUUID = UUID.randomUUID().toString(); MDC.put("requestUuid", requestUUID); String correlationId = null; Enumeration<String> headerNames = (Enumeration<String>) ((HttpServletRequest) request) .getHeaderNames(); while (headerNames.hasMoreElements()) { String name = (String) headerNames.nextElement(); if ("X-Correlation-ID".equalsIgnoreCase(name)) { correlationId = ((HttpServletRequest) request).getHeader(name); } } if (correlationId != null) { if (CSID_REGEX.matcher(correlationId).matches()) { if (correlationId.length() > CSID_MAX_LENGTH) { log.warn("Correlation Id has more characters than {}, hence truncating to {}", correlationId, CSID_MAX_LENGTH, CSID_MAX_LENGTH); correlationId = correlationId.substring(0, CSID_MAX_LENGTH); } MDC.put(CSID_KEY, correlationId); } else { log.warn("Correlation Id must consist of alphanumeric characters or hypens"); } } req.setAttribute("requestUuid", requestUUID); resp.setHeader(customHeaderName, requestUUID); if (log.isDebugEnabled()) { log.debug("{}", ServletLogger.logRequest(req)); } else { log.info("{}", ServletLogger.logBasicRequestInfo(req)); } chain.doFilter(req, resp); if (log.isDebugEnabled()) { log.debug("{}", ServletLogger.logResponse(resp, startTime)); } else { log.info("{}", ServletLogger.logBasicResponseInfo(resp, startTime)); } resp.finish(); } finally { MDC.clear(); } }