27
27
import org .springframework .util .Assert ;
28
28
import org .springframework .util .StringUtils ;
29
29
import org .springframework .web .util .ContentCachingRequestWrapper ;
30
+ import org .springframework .web .util .WebUtils ;
30
31
31
32
/**
32
33
* Base class for {@code Filter}s that perform logging operations before and after a request
@@ -86,7 +87,7 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter
86
87
87
88
/**
88
89
* Set whether the query string should be included in the log message.
89
- * <p>Should be configured using an {@code < init-param> } for parameter name
90
+ * <p>Should be configured using an {@code < init-param> } for parameter name
90
91
* "includeQueryString" in the filter definition in {@code web.xml}.
91
92
*/
92
93
public void setIncludeQueryString (boolean includeQueryString ) {
@@ -103,7 +104,7 @@ protected boolean isIncludeQueryString() {
103
104
/**
104
105
* Set whether the client address and session id should be included in the
105
106
* log message.
106
- * <p>Should be configured using an {@code < init-param> } for parameter name
107
+ * <p>Should be configured using an {@code < init-param> } for parameter name
107
108
* "includeClientInfo" in the filter definition in {@code web.xml}.
108
109
*/
109
110
public void setIncludeClientInfo (boolean includeClientInfo ) {
@@ -120,7 +121,7 @@ protected boolean isIncludeClientInfo() {
120
121
121
122
/**
122
123
* Set whether the request payload (body) should be included in the log message.
123
- * <p>Should be configured using an {@code < init-param> } for parameter name
124
+ * <p>Should be configured using an {@code < init-param> } for parameter name
124
125
* "includePayload" in the filter definition in {@code web.xml}.
125
126
*/
126
127
@@ -270,21 +271,23 @@ protected String createMessage(HttpServletRequest request, String prefix, String
270
271
msg .append (";user=" ).append (user );
271
272
}
272
273
}
273
- if (isIncludePayload () && request instanceof ContentCachingRequestWrapper ) {
274
- ContentCachingRequestWrapper wrapper = (ContentCachingRequestWrapper ) request ;
275
- byte [] buf = wrapper .getContentAsByteArray ();
276
- if (buf .length > 0 ) {
277
- int length = Math .min (buf .length , getMaxPayloadLength ());
278
- String payload ;
279
- try {
280
- payload = new String (buf , 0 , length , wrapper .getCharacterEncoding ());
274
+ if (isIncludePayload ()) {
275
+ ContentCachingRequestWrapper wrapper =
276
+ WebUtils .getNativeRequest (request , ContentCachingRequestWrapper .class );
277
+ if (wrapper != null ) {
278
+ byte [] buf = wrapper .getContentAsByteArray ();
279
+ if (buf .length > 0 ) {
280
+ int length = Math .min (buf .length , getMaxPayloadLength ());
281
+ String payload ;
282
+ try {
283
+ payload = new String (buf , 0 , length , wrapper .getCharacterEncoding ());
284
+ }
285
+ catch (UnsupportedEncodingException ex ) {
286
+ payload = "[unknown]" ;
287
+ }
288
+ msg .append (";payload=" ).append (payload );
281
289
}
282
- catch (UnsupportedEncodingException e ) {
283
- payload = "[unknown]" ;
284
- }
285
- msg .append (";payload=" ).append (payload );
286
290
}
287
-
288
291
}
289
292
msg .append (suffix );
290
293
return msg .toString ();
0 commit comments