Stop Apache CXF logging binary data attachments MultipartBody
I need to stop CXF from registering attachment binary data in an object MultipartBody
(which is sent AbstractLoggingInterceptor
in an outgoing message). When I add mine LoggingInInterceptor
, I set it setShowBinaryData
to false, but that doesn't seem to stop the binary data inside a multi-message message.
I'm not sure if I need to create a custom loggingInInterceptor, or if there is a way to configure existing interceptors to truncate any binary data it finds. Stopping it completely logging the MultipartBody response, or truncating the data are acceptable solutions.
source to share
showBinaryContent
the default is false, however binary data is logged based on content type. Currently, if your content type is not one of the following; binary data will be logged.
static {
BINARY_CONTENT_MEDIA_TYPES = new ArrayList<String>();
BINARY_CONTENT_MEDIA_TYPES.add("application/octet-stream");
BINARY_CONTENT_MEDIA_TYPES.add("image/png");
BINARY_CONTENT_MEDIA_TYPES.add("image/jpeg");
BINARY_CONTENT_MEDIA_TYPES.add("image/gif");
}
Say what your content type is application/zip
, you can create a custom interceptor and override isBinaryContent like below
import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.interceptor.LoggingMessage;
public class KPLogOutInterceptor extends LoggingOutInterceptor {
@Override
public boolean isBinaryContent(String contentType) {
return contentType != null && (BINARY_CONTENT_MEDIA_TYPES.contains(contentType)|| "application/zip".equals(contentType);
}
}
Another way without using content type is below.
import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.interceptor.LoggingMessage;
public class KPLogOutInterceptor extends LoggingOutInterceptor {
@Override
protected String formatLoggingMessage(LoggingMessage loggingMessage) {
return removePayload(loggingMessage.toString());
}
private String removePayload(String str){
StringBuilder builder = new StringBuilder(str);
if (str.indexOf("Payload:") + 9 > 8) {
builder.setLength(builder.indexOf("Payload:") + 8);
builder.append(" <content skipped>\n");
builder.append(StringUtils.repeat('-', 25));
}
return builder.toString();
}
}
source to share