3
3
import java .io .IOException ;
4
4
import java .util .Optional ;
5
5
6
- import javax .servlet .Filter ;
7
6
import javax .servlet .FilterChain ;
8
- import javax .servlet .FilterConfig ;
9
7
import javax .servlet .ServletException ;
10
- import javax .servlet .ServletRequest ;
11
- import javax .servlet .ServletResponse ;
12
8
import javax .servlet .http .HttpServletRequest ;
13
9
import javax .servlet .http .HttpServletResponse ;
14
10
17
13
import org .apache .commons .lang3 .concurrent .LazyInitializer ;
18
14
import org .slf4j .Logger ;
19
15
import org .slf4j .LoggerFactory ;
20
- import org .slf4j .MDC ;
21
16
22
- import com .sap .hcp .cf .logging .common .LogContext ;
23
- import com .sap .hcp .cf .logging .common .LogOptionalFieldsSettings ;
24
- import com .sap .hcp .cf .logging .common .request .HttpHeader ;
25
- import com .sap .hcp .cf .logging .common .request .HttpHeaders ;
26
- import com .sap .hcp .cf .logging .common .request .RequestRecord ;
27
17
import com .sap .hcp .cf .logging .servlet .dynlog .DynLogConfiguration ;
28
18
import com .sap .hcp .cf .logging .servlet .dynlog .DynLogEnvironment ;
29
19
import com .sap .hcp .cf .logging .servlet .dynlog .DynamicLogLevelProcessor ;
61
51
* by subclassing the filter and overwrite its methods.
62
52
* </p>
63
53
*/
64
- public class RequestLoggingFilter implements Filter {
54
+ public class RequestLoggingFilter extends RequestLoggingBaseFilter {
65
55
66
56
private static final Logger LOG = LoggerFactory .getLogger (RequestLoggingFilter .class );
67
57
68
- public static final String LOG_PROVIDER = "[SERVLET]" ;
69
- public static final String WRAP_RESPONSE_INIT_PARAM = "wrapResponse" ;
70
- public static final String WRAP_REQUEST_INIT_PARAM = "wrapRequest" ;
58
+ public static final String LOG_PROVIDER = RequestLoggingBaseFilter . LOG_PROVIDER ;
59
+ public static final String WRAP_RESPONSE_INIT_PARAM = RequestLoggingBaseFilter . WRAP_REQUEST_INIT_PARAM ;
60
+ public static final String WRAP_REQUEST_INIT_PARAM = RequestLoggingBaseFilter . WRAP_REQUEST_INIT_PARAM ;
71
61
72
- private boolean wrapResponse = true ;
73
- private boolean wrapRequest = true ;
74
62
private ConcurrentInitializer <DynLogConfiguration > dynLogEnvironment ;
75
63
private ConcurrentInitializer <DynamicLogLevelProcessor > dynamicLogLevelProcessor ;
76
- private RequestRecordFactory requestRecordFactory ;
77
64
78
65
public RequestLoggingFilter () {
79
66
this (createDefaultRequestRecordFactory ());
80
67
}
81
68
82
- private static RequestRecordFactory createDefaultRequestRecordFactory () {
83
- String invokingClass = RequestLoggingFilter .class .getName ();
84
- LogOptionalFieldsSettings logOptionalFieldsSettings = new LogOptionalFieldsSettings (invokingClass );
85
- return new RequestRecordFactory (logOptionalFieldsSettings );
86
- }
87
-
88
69
public RequestLoggingFilter (RequestRecordFactory requestRecordFactory ) {
89
70
this (requestRecordFactory , createDefaultDynLogEnvironment ());
90
71
}
@@ -100,7 +81,7 @@ public RequestLoggingFilter(ConcurrentInitializer<DynLogConfiguration> dynLogEnv
100
81
101
82
public RequestLoggingFilter (RequestRecordFactory requestRecordFactory ,
102
83
ConcurrentInitializer <DynLogConfiguration > dynLogEnvironment ) {
103
- this . requestRecordFactory = requestRecordFactory ;
84
+ super ( requestRecordFactory ) ;
104
85
this .dynLogEnvironment = dynLogEnvironment ;
105
86
this .dynamicLogLevelProcessor = new LazyInitializer <DynamicLogLevelProcessor >() {
106
87
@@ -120,7 +101,7 @@ public RequestLoggingFilter(ConcurrentInitializer<DynLogConfiguration> dynLogEnv
120
101
public RequestLoggingFilter (RequestRecordFactory requestRecordFactory ,
121
102
ConcurrentInitializer <DynLogConfiguration > dynLogEnvironment ,
122
103
ConcurrentInitializer <DynamicLogLevelProcessor > dynamicLogLevelProcessor ) {
123
- this . requestRecordFactory = requestRecordFactory ;
104
+ super ( requestRecordFactory ) ;
124
105
this .dynLogEnvironment = dynLogEnvironment ;
125
106
this .dynamicLogLevelProcessor = dynamicLogLevelProcessor ;
126
107
}
@@ -146,91 +127,14 @@ protected Optional<DynamicLogLevelProcessor> getDynLogLevelProcessor() {
146
127
}
147
128
148
129
@ Override
149
- public void init (FilterConfig filterConfig ) throws ServletException {
150
- String value = filterConfig .getInitParameter (WRAP_RESPONSE_INIT_PARAM );
151
- if (value != null && "false" .equalsIgnoreCase (value )) {
152
- wrapResponse = false ;
153
- }
154
- value = filterConfig .getInitParameter (WRAP_REQUEST_INIT_PARAM );
155
- if (value != null && "false" .equalsIgnoreCase (value )) {
156
- wrapRequest = false ;
157
- }
158
- }
159
-
160
- @ Override
161
- public void doFilter (ServletRequest request , ServletResponse response , FilterChain chain ) throws IOException ,
162
- ServletException {
163
- if (HttpServletRequest .class .isAssignableFrom (request .getClass ()) && HttpServletResponse .class .isAssignableFrom (
164
- response .getClass ())) {
165
- doFilterRequest ((HttpServletRequest ) request , (HttpServletResponse ) response , chain );
166
- } else {
167
- chain .doFilter (request , response );
168
- }
169
- }
170
-
171
- @ Override
172
- public void destroy () {
173
- MDC .clear ();
174
- }
175
-
176
- private void doFilterRequest (HttpServletRequest httpRequest , HttpServletResponse httpResponse , FilterChain chain )
177
- throws IOException ,
178
- ServletException {
130
+ protected void doFilterRequest (HttpServletRequest httpRequest , HttpServletResponse httpResponse , FilterChain chain )
131
+ throws IOException ,
132
+ ServletException {
179
133
activateDynamicLogLevels (httpRequest );
180
- /*
181
- * -- make sure correlation id is read from headers
182
- */
183
- LogContext .initializeContext (HttpHeaderUtilities .getHeaderValue (httpRequest , HttpHeaders .CORRELATION_ID ));
184
-
185
134
try {
186
-
187
- RequestRecord rr = requestRecordFactory .create (httpRequest );
188
- httpRequest .setAttribute (MDC .class .getName (), MDC .getCopyOfContextMap ());
189
-
190
- if (!httpResponse .isCommitted () && httpResponse .getHeader (HttpHeaders .CORRELATION_ID .getName ()) == null ) {
191
- httpResponse .setHeader (HttpHeaders .CORRELATION_ID .getName (), LogContext .getCorrelationId ());
192
- }
193
-
194
- /*
195
- * If request logging is disabled skip request instrumentation and
196
- * continue the filter chain immediately.
197
- */
198
- if (!RequestLogger .isRequestLoggingEnabled ()) {
199
- doFilter (chain , httpRequest , httpResponse );
200
- return ;
201
- }
202
-
203
- /*
204
- * -- we essentially do three things here: -- a) we create a log
205
- * record using our library and log it via STDOUT -- b) keep track
206
- * of certain header fields so that they are available in later
207
- * processing steps -- b) inject a response wrapper to keep track of
208
- * content length (hopefully)
209
- */
210
- if (wrapResponse ) {
211
- httpResponse = new ContentLengthTrackingResponseWrapper (httpResponse );
212
- }
213
-
214
- if (wrapRequest ) {
215
- httpRequest = new ContentLengthTrackingRequestWrapper (httpRequest );
216
- }
217
-
218
- RequestLogger loggingVisitor = new RequestLogger (rr , httpRequest , httpResponse );
219
- httpRequest = new LoggingContextRequestWrapper (httpRequest , loggingVisitor );
220
-
221
- /* -- start measuring right before calling up the filter chain -- */
222
- rr .start ();
223
- doFilter (chain , httpRequest , httpResponse );
224
-
225
- if (!httpRequest .isAsyncStarted ()) {
226
- loggingVisitor .logRequest ();
227
- }
228
- /*
229
- * -- close this
230
- */
135
+ super .doFilterRequest (httpRequest , httpResponse , chain );
231
136
} finally {
232
137
deactivateDynamicLogLevels ();
233
- resetLogContext ();
234
138
}
235
139
}
236
140
@@ -245,19 +149,4 @@ private void deactivateDynamicLogLevels() {
245
149
getDynLogLevelProcessor ().ifPresent (DynamicLogLevelProcessor ::removeDynamicLogLevelFromMDC );
246
150
}
247
151
248
- private void resetLogContext () {
249
- for (HttpHeader header : HttpHeaders .propagated ()) {
250
- LogContext .remove (header .getField ());
251
- }
252
- LogContext .resetContextFields ();
253
- }
254
-
255
- private void doFilter (FilterChain chain , HttpServletRequest httpRequest , HttpServletResponse httpResponse )
256
- throws IOException ,
257
- ServletException {
258
- if (chain != null ) {
259
- chain .doFilter (httpRequest , httpResponse );
260
- }
261
- }
262
-
263
152
}
0 commit comments