I need to stop CXF from logging the binary data of attachments in a MultipartBody object (which is being thrown out by the AbstractLoggingInterceptor in the Outbound Message). When I add my LoggingInInterceptor, I am setting setShowBinaryData to false, but this doesn't seem to stop binary data within a multipart message from being logged.
I am unsure whether I need to create a custom loggingInInterceptor, or whether there is a way of configuring the existing interceptors to truncate any binary data it finds. Stopping it logging the MultipartBody response entirely, or truncating the data are both acceptable solutions.
showBinaryContent by default is false, however binary data gets logged based on content type, Currently if your content type is not any of the following; the binary data would 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 your content type is application/zip, you can Create Custom Interceptor and override isBinaryContent as shown 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 as shown 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();
}
}
来源:https://stackoverflow.com/questions/30214049/stop-apache-cxf-logging-binary-data-of-multipartbody-attachments