-
Notifications
You must be signed in to change notification settings - Fork 0
/
RequestDTO
150 lines (116 loc) · 4.88 KB
/
RequestDTO
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
@Component
public class CustomFilter extends OncePerRequestFilter {
private final ObjectMapper objectMapper;
public CustomFilter(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
if ("POST".equalsIgnoreCase(request.getMethod())) {
HttpServletRequest wrappedRequest = new CachedBodyHttpServletRequest(request);
// Read the request body and convert it to a requestDTO object
RequestDTO requestDTO = parseRequestDTO(wrappedRequest);
if (requestDTO != null) {
// Process requestDTO as needed
System.out.println("Received requestDTO: " + requestDTO);
}
}
filterChain.doFilter(request, response);
}
private RequestDTO parseRequestDTO(HttpServletRequest request) throws IOException {
// Convert request body JSON to RequestDTO object
try (InputStream inputStream = request.getInputStream()) {
return objectMapper.readValue(inputStream, RequestDTO.class);
}
}
}
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.*;
public class CachedBodyHttpServletRequest extends HttpServletRequestWrapper {
private byte[] cachedBody;
public CachedBodyHttpServletRequest(HttpServletRequest request) throws IOException {
super(request);
InputStream requestInputStream = request.getInputStream();
this.cachedBody = requestInputStream.readAllBytes();
}
@Override
public ServletInputStream getInputStream() {
return new CachedBodyServletInputStream(this.cachedBody);
}
@Override
public BufferedReader getReader() {
return new BufferedReader(new InputStreamReader(getInputStream()));
}
private static class CachedBodyServletInputStream extends ServletInputStream {
private final ByteArrayInputStream byteArrayInputStream;
public CachedBodyServletInputStream(byte[] cachedBody) {
this.byteArrayInputStream = new ByteArrayInputStream(cachedBody);
}
@Override
public int read() {
return byteArrayInputStream.read();
}
@Override
public boolean isFinished() {
return byteArrayInputStream.available() == 0;
}
@Override
public boolean isReady() {
return true;
}
@Override
public void setReadListener(ReadListener readListener) {
// No implementation needed
}
}
}
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.util.ContentCachingRequestWrapper;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Component
public class CustomFilter extends OncePerRequestFilter {
private final ObjectMapper objectMapper = new ObjectMapper();
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
// Wrap request with ContentCachingRequestWrapper to cache the body
ContentCachingRequestWrapper wrappedRequest = new ContentCachingRequestWrapper(request);
filterChain.doFilter(wrappedRequest, response);
// After filter chain, access the cached request body
if ("POST".equalsIgnoreCase(request.getMethod())) {
RequestDTO requestDTO = parseRequestDTO(wrappedRequest);
if (requestDTO != null) {
System.out.println("Received requestDTO: " + requestDTO);
}
}
}
private RequestDTO parseRequestDTO(ContentCachingRequestWrapper request) throws IOException {
byte[] body = request.getContentAsByteArray();
return objectMapper.readValue(body, RequestDTO.class);
}
}