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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
/*-
* ============LICENSE_START=======================================================
* ONAP - Logging
* ================================================================================
* Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Modifications Copyright (C) 2018 IBM.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
*/
package org.onap.logging.filter.base;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.zip.GZIPInputStream;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ReadListener;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.WriteListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
public class PayloadLoggingServletFilter extends AbstractServletFilter implements Filter {
private static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(PayloadLoggingServletFilter.class);
private static class ByteArrayServletStream extends ServletOutputStream {
ByteArrayOutputStream baos;
ByteArrayServletStream(ByteArrayOutputStream baos) {
this.baos = baos;
}
@Override
public void write(int param) throws IOException {
baos.write(param);
}
@Override
public boolean isReady() {
return true;
}
@Override
public void setWriteListener(WriteListener arg0) {
// this method does nothing
}
}
private static class ByteArrayPrintWriter extends PrintWriter {
private ByteArrayOutputStream baos;
private int errorCode = -1;
private String errorMsg = "";
private boolean errored = false;
public ByteArrayPrintWriter(ByteArrayOutputStream out) {
super(out);
this.baos = out;
}
public ServletOutputStream getStream() {
return new ByteArrayServletStream(baos);
}
public Boolean hasErrored() {
return errored;
}
public int getErrorCode() {
return errorCode;
}
public String getErrorMsg() {
return errorMsg;
}
public void setError(int code) {
errorCode = code;
errored = true;
}
public void setError(int code, String msg) {
errorMsg = msg;
errorCode = code;
errored = true;
}
}
private class BufferedServletInputStream extends ServletInputStream {
ByteArrayInputStream bais;
public BufferedServletInputStream(ByteArrayInputStream bais) {
this.bais = bais;
}
@Override
public int available() {
return bais.available();
}
@Override
public int read() {
return bais.read();
}
@Override
public int read(byte[] buf, int off, int len) {
return bais.read(buf, off, len);
}
@Override
public boolean isFinished() {
return available() < 1;
}
@Override
public boolean isReady() {
return true;
}
@Override
public void setReadListener(ReadListener arg0) {
// this method does nothing
}
}
private class BufferedRequestWrapper extends HttpServletRequestWrapper {
ByteArrayInputStream bais;
ByteArrayOutputStream baos;
BufferedServletInputStream bsis;
byte[] buffer;
public BufferedRequestWrapper(HttpServletRequest req) throws IOException {
super(req);
InputStream is = req.getInputStream();
baos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int letti;
while ((letti = is.read(buf)) > 0) {
baos.write(buf, 0, letti);
}
buffer = baos.toByteArray();
}
@Override
public ServletInputStream getInputStream() {
try {
bais = new ByteArrayInputStream(buffer);
bsis = new BufferedServletInputStream(bais);
} catch (Exception ex) {
log.error("Exception in getInputStream", ex);
}
return bsis;
}
public byte[] getBuffer() {
return buffer;
}
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// this method does nothing
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
final HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
BufferedRequestWrapper bufferedRequest = new BufferedRequestWrapper(httpRequest);
StringBuilder requestHeaders = new StringBuilder("REQUEST|");
requestHeaders.append(httpRequest.getMethod());
requestHeaders.append(":");
requestHeaders.append(httpRequest.getRequestURL().toString());
requestHeaders.append("|");
requestHeaders.append(getSecureRequestHeaders(httpRequest));
log.info(requestHeaders.toString());
log.info("REQUEST BODY|" + new String(bufferedRequest.getBuffer()));
final HttpServletResponse response = (HttpServletResponse) servletResponse;
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ByteArrayPrintWriter pw = new ByteArrayPrintWriter(baos);
HttpServletResponse wrappedResp = new HttpServletResponseWrapper(response) {
@Override
public PrintWriter getWriter() {
return pw;
}
@Override
public ServletOutputStream getOutputStream() {
return pw.getStream();
}
@Override
public void sendError(int sc) throws IOException {
super.sendError(sc);
pw.setError(sc);
}
@Override
public void sendError(int sc, String msg) throws IOException {
super.sendError(sc, msg);
pw.setError(sc, msg);
}
};
try {
filterChain.doFilter(bufferedRequest, wrappedResp);
} catch (Exception e) {
log.error("Chain Exception", e);
throw e;
} finally {
try {
byte[] bytes = baos.toByteArray();
StringBuilder responseHeaders = new StringBuilder("RESPONSE HEADERS|");
responseHeaders.append(formatResponseHeaders(response));
responseHeaders.append("Status:");
responseHeaders.append(response.getStatus());
responseHeaders.append(";IsCommited:" + wrappedResp.isCommitted());
log.info(responseHeaders.toString());
if ("gzip".equals(response.getHeader("Content-Encoding"))) {
log.info("UNGZIPED RESPONSE BODY|" + decompressGZIPByteArray(bytes));
} else {
log.info("RESPONSE BODY|" + new String(bytes));
}
if (pw.hasErrored()) {
log.info("ERROR RESPONSE|" + pw.getErrorCode() + ":" + pw.getErrorMsg());
} else {
if (!wrappedResp.isCommitted()) {
response.getOutputStream().write(bytes);
response.getOutputStream().flush();
}
}
} catch (Exception e) {
log.error("Exception in response filter", e);
}
}
}
@Override
public void destroy() {
// this method does nothing
}
private String decompressGZIPByteArray(byte[] bytes) {
BufferedReader in = null;
InputStreamReader inR = null;
ByteArrayInputStream byteS = null;
GZIPInputStream gzS = null;
StringBuilder str = new StringBuilder();
try {
byteS = new ByteArrayInputStream(bytes);
gzS = new GZIPInputStream(byteS);
inR = new InputStreamReader(gzS);
in = new BufferedReader(inR);
if (in != null) {
String content;
while ((content = in.readLine()) != null) {
str.append(content);
}
}
} catch (Exception e) {
log.error("Failed get read GZIPInputStream", e);
} finally {
if (byteS != null)
try {
byteS.close();
} catch (IOException e1) {
log.error("Failed to close ByteStream", e1);
}
if (gzS != null)
try {
gzS.close();
} catch (IOException e2) {
log.error("Failed to close GZStream", e2);
}
if (inR != null)
try {
inR.close();
} catch (IOException e3) {
log.error("Failed to close InputReader", e3);
}
if (in != null)
try {
in.close();
} catch (IOException e) {
log.error("Failed to close BufferedReader", e);
}
}
return str.toString();
}
}
|