简体   繁体   English

停止Apache CXF记录MultipartBody附件的二进制数据

[英]Stop Apache CXF logging binary data of MultipartBody attachments

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). 我需要阻止CXF在MultipartBody对象中记录附件的二进制数据(由出站消息中的AbstractLoggingInterceptor抛出)。 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. 当我添加我的LoggingInInterceptor ,我将setShowBinaryData设置为false,但这似乎不会阻止多部分消息中的二进制数据被记录。

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. 我不确定是否需要创建自定义loggingInInterceptor,或者是否有一种配置现有拦截器的方法来截断它找到的任何二进制数据。 Stopping it logging the MultipartBody response entirely, or truncating the data are both acceptable solutions. 停止它完全记录MultipartBody响应,或截断数据都是可接受的解决方案。

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; showBinaryContent默认为false,但是二进制数据会根据内容类型进行记录,目前如果您的内容类型不是以下任何一种; 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 假设您的内容类型是application/zip ,您可以创建自定义拦截器并覆盖isBinaryContent,如下所示

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();  
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM