Apache CXF Interceptors: Unable to modify the response Stream in a Out Interceptor

1.2k Views Asked by At

Need to write an out bound interceptor to modify the response. I have written an interceptor for an out bound response . The Interceptor is added in the SEND Phase as the outStream was unavailable to be modified before the SEND phase.

I have tried changing the existing output stream in the org.apache.cxf.message.Message object with the new bytes of data and also tried to add a new OutputStream in the Message object. But none of them worked.

Please let me know if any one faced the same and if there is a solution for it.

Thanks.

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

There are 0 best solutions below