Extract protected method in AbstractRequestLoggingFilter

Issue: SPR-16245
This commit is contained in:
Jeff Nelson
2017-11-29 17:04:29 -06:00
committed by Rossen Stoyanchev
parent c8bdb3c602
commit 30e40210a8

View File

@@ -306,21 +306,9 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter
}
if (isIncludePayload()) {
ContentCachingRequestWrapper wrapper =
WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);
if (wrapper != null) {
byte[] buf = wrapper.getContentAsByteArray();
if (buf.length > 0) {
int length = Math.min(buf.length, getMaxPayloadLength());
String payload;
try {
payload = new String(buf, 0, length, wrapper.getCharacterEncoding());
}
catch (UnsupportedEncodingException ex) {
payload = "[unknown]";
}
msg.append(";payload=").append(payload);
}
String payload = getMessagePayload(request);
if (payload != null) {
msg.append(";payload=").append(payload);
}
}
@@ -328,6 +316,29 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter
return msg.toString();
}
/**
* Extract the message payload.<p>Used by {@link #createMessage(HttpServletRequest, String, String)} in creating the payload portion of the message (only if {@link #isIncludePayload()} returns true)
*/
protected String getMessagePayload(HttpServletRequest request) {
ContentCachingRequestWrapper wrapper =
WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);
String payload = null;
if (wrapper != null) {
byte[] buf = wrapper.getContentAsByteArray();
if (buf.length > 0) {
int length = Math.min(buf.length, getMaxPayloadLength());
try {
payload = new String(buf, 0, length, wrapper.getCharacterEncoding());
}
catch (UnsupportedEncodingException ex) {
payload = "[unknown]";
}
}
}
return payload;
}
/**
* Determine whether to call the {@link #beforeRequest}/{@link #afterRequest}