Apache CXF拦截器:无法修改Out Interceptor中的响应流

时间:2016-12-16 22:26:08

标签: spring apache rest cxf interceptor

需要编写一个out绑定拦截器来修改响应。 我已经编写了一个拦截响应的拦截器。由于在SEND阶段之前无法修改outStream,因此在SEND阶段添加了拦截器。

我尝试使用新的数据字节更改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;
 }

}

1 个答案:

答案 0 :(得分:0)

stackoverflow.com/a/12948702/6371459