|
| 1 | +package fr.insee.genesis.configuration; |
| 2 | + |
| 3 | +import jakarta.servlet.FilterChain; |
| 4 | +import jakarta.servlet.ServletException; |
| 5 | +import jakarta.servlet.annotation.WebFilter; |
| 6 | +import jakarta.servlet.http.HttpServletRequest; |
| 7 | +import jakarta.servlet.http.HttpServletResponse; |
| 8 | +import lombok.extern.slf4j.Slf4j; |
| 9 | +import org.springframework.core.annotation.Order; |
| 10 | +import org.springframework.stereotype.Component; |
| 11 | +import org.springframework.web.filter.OncePerRequestFilter; |
| 12 | +import org.springframework.web.util.ContentCachingRequestWrapper; |
| 13 | +import org.springframework.web.util.ContentCachingResponseWrapper; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | +import java.nio.charset.StandardCharsets; |
| 17 | + |
| 18 | + |
| 19 | +@Component |
| 20 | +@WebFilter(urlPatterns = "/*") |
| 21 | +@Order(-999) |
| 22 | +@Slf4j |
| 23 | +public class LogRequestFilter extends OncePerRequestFilter { |
| 24 | + |
| 25 | + private static final String REQUEST_MESSAGE_FORMAT = |
| 26 | + "CALL {} {} - " |
| 27 | + // + "Content-Type : {} \n " |
| 28 | + // + "Headers : {} \n " |
| 29 | + + "Params : {} - " |
| 30 | + + "Body : {} \n "; |
| 31 | + |
| 32 | + private static final String RESPONSE_MESSAGE_FORMAT = |
| 33 | + "END {} {} - " |
| 34 | + + "Status : {} - " |
| 35 | + // + "Content-Type : {} \n " |
| 36 | + // + "Headers : {} \n " |
| 37 | + + "Body : {} \n"; |
| 38 | + |
| 39 | + @Override |
| 40 | + protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) |
| 41 | + throws ServletException, IOException { |
| 42 | + |
| 43 | + //Cache request to avoid calling twice the same inputStream |
| 44 | + ContentCachingRequestWrapper req = new ContentCachingRequestWrapper(request); |
| 45 | + ContentCachingResponseWrapper resp = new ContentCachingResponseWrapper(response); |
| 46 | + |
| 47 | + log.info(REQUEST_MESSAGE_FORMAT, |
| 48 | + req.getMethod(), req.getRequestURI(), |
| 49 | + // req.getContentType(), |
| 50 | + // new ServletServerHttpRequest(req).getHeaders(), //Headers |
| 51 | + request.getQueryString(),//Params |
| 52 | + new String(req.getContentAsByteArray(), StandardCharsets.UTF_8));//Body |
| 53 | + |
| 54 | + |
| 55 | + // Execution request chain |
| 56 | + filterChain.doFilter(req, resp); |
| 57 | + |
| 58 | + |
| 59 | + log.info(RESPONSE_MESSAGE_FORMAT, |
| 60 | + req.getMethod(), req.getRequestURI(), |
| 61 | + resp.getStatus(), |
| 62 | + getResponseBody(req, resp)); //Body |
| 63 | + |
| 64 | + // Finally remember to respond to the client with the cached data. |
| 65 | + resp.copyBodyToResponse(); |
| 66 | + } |
| 67 | + |
| 68 | + private String getResponseBody(ContentCachingRequestWrapper req, ContentCachingResponseWrapper resp) { |
| 69 | + if (req.getRequestURI().contains("swagger-ui") ||req.getRequestURI().contains("api-docs")) return "Hidden Swagger response"; |
| 70 | + return new String(resp.getContentAsByteArray(), StandardCharsets.UTF_8); |
| 71 | + } |
| 72 | + |
| 73 | +} |
0 commit comments