2016-12-16 119 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