|
1 | 1 | package com.greenlearner.product.filters; |
2 | 2 |
|
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import com.greenlearner.product.dto.Product; |
3 | 5 | import lombok.extern.slf4j.Slf4j; |
4 | 6 | import org.apache.commons.io.IOUtils; |
5 | 7 | import org.apache.commons.io.output.TeeOutputStream; |
6 | | -import org.springframework.core.annotation.Order; |
| 8 | +import org.springframework.beans.factory.annotation.Autowired; |
7 | 9 | import org.springframework.stereotype.Component; |
8 | 10 |
|
9 | 11 | import javax.servlet.*; |
|
12 | 14 | import javax.servlet.http.HttpServletResponse; |
13 | 15 | import javax.servlet.http.HttpServletResponseWrapper; |
14 | 16 | import java.io.*; |
| 17 | +import java.util.LinkedHashMap; |
| 18 | +import java.util.List; |
15 | 19 |
|
16 | 20 | /** |
17 | 21 | * @author - GreenLearner(https://www.youtube.com/c/greenlearner) |
18 | 22 | */ |
19 | 23 | @Component |
20 | 24 | @Slf4j |
21 | | -@Order(1) |
| 25 | +//@Order(1) |
22 | 26 | public class RequestResponseLoggers implements Filter { |
| 27 | + |
| 28 | + @Autowired |
| 29 | + private ObjectMapper objectMapper; |
| 30 | + |
23 | 31 | @Override |
24 | 32 | public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { |
25 | 33 |
|
| 34 | + //password |
| 35 | + //ssn, pan, aadhar |
| 36 | + //personal |
| 37 | + |
| 38 | + |
26 | 39 | MyCustomHttpRequestWrapper requestWrapper = new MyCustomHttpRequestWrapper((HttpServletRequest) request); |
27 | | - log.info("Requeust URI: {}", requestWrapper.getRequestURI()); |
| 40 | + |
| 41 | + String uri = requestWrapper.getRequestURI(); |
| 42 | + log.info("Requeust URI: {}", uri); |
| 43 | + |
28 | 44 | log.info("Requeust Method: {}", requestWrapper.getMethod()); |
29 | | - log.info("Requeust Body: {}", new String(requestWrapper.getByteArray()).replaceAll("\n", " ")); |
| 45 | + String requestData = new String(requestWrapper.getByteArray()).replaceAll("\n", " "); |
| 46 | + |
| 47 | + if("/v1/addProduct".equalsIgnoreCase(uri)){ |
| 48 | + Product product = objectMapper.readValue(requestData, Product.class); |
30 | 49 |
|
31 | | - MyCustomHttpResponseWrapper responseWrapper = new MyCustomHttpResponseWrapper((HttpServletResponse)response); |
| 50 | + product.setCurrency("****"); |
| 51 | + |
| 52 | + requestData = objectMapper.writeValueAsString(product); |
| 53 | + } |
| 54 | + |
| 55 | + log.info("Requeust Body: {}", requestData); |
| 56 | + |
| 57 | + MyCustomHttpResponseWrapper responseWrapper = new MyCustomHttpResponseWrapper((HttpServletResponse) response); |
32 | 58 |
|
33 | 59 | chain.doFilter(requestWrapper, responseWrapper); |
34 | 60 |
|
| 61 | + String responseResult = new String(responseWrapper.getBaos().toByteArray()); |
| 62 | + if("/v1/addProduct".equalsIgnoreCase(uri)){ |
| 63 | + Product product = objectMapper.readValue(responseResult, Product.class); |
| 64 | + |
| 65 | + product.setCurrency("****"); |
| 66 | + |
| 67 | + responseResult = objectMapper.writeValueAsString(product); |
| 68 | + } |
35 | 69 | log.info("Response status - {}", responseWrapper.getStatus()); |
36 | | - log.info("Response Body - {}", newString(responseWrapper.getBaos().toByteArray())); |
| 70 | + log.info("Response Body - {}", responseResult); |
37 | 71 | } |
38 | 72 |
|
39 | 73 |
|
|
0 commit comments