2015-05-13 30 views
3

我需要停止CXF將附件的二進制數據記錄到MultipartBody對象(由出站消息中的AbstractLoggingInterceptor拋出)。當我添加我的LoggingInInterceptor時,我將setShowBinaryData設置爲false,但這似乎不會阻止多部分郵件中的二進制數據被記錄。停止MultipartBody附件的Apache CXF日誌記錄二進制數據

我不確定是否需要創建自定義loggingInInterceptor,或者是否存在配置現有攔截器以截斷它找到的任何二進制數據的方法。停止完全記錄MultipartBody響應或截斷數據都是可接受的解決方案。

回答

4

showBinaryContent默認爲false,但二進制數據根據內容類型進行記錄。當前如果您的內容類型不是以下任何一種;二進制數據將被記錄。

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

說你的內容類型是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); 
    } 
} 

的另一種方法,而無需使用內容類型如下圖所示。

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

非常感謝你,你的第二個建議非常有幫助! – MeanwhileInHell