2016-12-16 118 views
0

需要編寫出站攔截器來修改響應。 我寫了一個出站響應的攔截器。 Interceptor被添加到發送階段,因爲在發送階段之前outStream不可修改。Apache CXF攔截器:無法修改Out Out攔截器中的響應流

我試圖用新的字節數據更改org.apache.cxf.message.Message對象中的現有輸出流,並試圖在Message對象中添加新的OutputStream。但他們都沒有工作。

請讓我知道,如果任何人面對相同的,如果有解決方案。

謝謝。

public class SResponseInterceptor2 extends AbstractPhaseInterceptor<Message> { 

private static final Logger LOGGER = LogManager.getLogger(SResponseInterceptor2.class.getName()); 

public SResponseInterceptor2() { 
    super(Phase.SEND); 
    addBefore(MessageSenderInterceptor.class.getName()); 
    LOGGER.info(">>>>>>>>>>>>>>>>>>>>>>>>"); 
} 

public void handleMessage(Message message) throws Fault { 

    OutputStream outputStream = message.getContent(OutputStream.class); 
    if(outputStream!=null && outputStream instanceof CacheAndWriteOutputStream){ 
     CacheAndWriteOutputStream cachedOutputStream = (CacheAndWriteOutputStream)outputStream; 
     try{ 

      String inputMessage = new String(cachedOutputStream.getBytes()); 
      cachedOutputStream.flush(); 

      byte[] bytes = changeResponse(inputMessage).getBytes(); 

cachedOutputStream.write(bytes); 

    /* Tried adding a new Stream with the updated data too 

     OutputStream modifiedOutputStream = new ByteArrayOutputStream(); 
      CacheAndWriteOutputStream cWStream = new CacheAndWriteOutputStream(modifiedOutputStream); 
      cWStream.write(bytes, 0, bytes.length); 
      message.setContent(OutputStream.class, cWStream); 
*/ 
      message.setContent(OutputStream.class, cachedOutputStream); 
     } catch (IOException ioe) { 
      LOGGER.error("Error while changing the Response ." + ioe.getMessage()); 
      ioe.printStackTrace(); 
     }finally{ 

     } 


     private String changeResponse(String responseMessage) { 
      responseMessage = responseMessage.replaceAll("sResponse",       "sResponse??????"); 
      LOGGER.info("After change message is " + responseMessage); 
       return responseMessage; 
} 

}

+0

檢查這個答案http://stackoverflow.com/a/12948702/6371459我覺得是一樣的你 – pedrofb

+0

太感謝很多。它的工作。我建議改進Apache的文檔。 – vinr

+0

嗨Pedroft ..建議工作,但輸出流(LoadingByteArrayOutputStream)包裝在CacheOutputStream被限制爲處理2046字節的數據只有當新消息message.getInterceptorChain()。doIntercept(message);返回沒有輸出流中的數據的消息,如果它有巨大的數據處理。試着改變OutputStream的大小限制,但它沒有幫助。任何建議??謝謝 – vinr

回答

0

stackoverflow.com/a/12948702/6371459