简体   繁体   English

如何从 cxf outInterceptor 获取响应体

[英]how to get response body from cxf outInterceptor

I am writing some code to collect some controller's request param and response body.我正在编写一些代码来收集一些控制器的请求参数和响应正文。

Since the project framework is apache CXF, which version is 3.1.18 ,由于项目框架是apache CXF,版本是3.1.18

I write an interceptor extends AbstractPhaseInterceptor to collect param in phase Phase.RECEIVE , which is working.我写了一个拦截器扩展 AbstractPhaseInterceptor 来收集阶段Phase.RECEIVE参数,它正在工作。

But when a write an outInterceptor extends AbstractPhaseInterceptor to collect the response of the controller, I find there no way for me to do this, there just one method handleMessage(Message message) in the interceptor, I can not fetch anything I want from the message但是当写一个 outInterceptor 扩展 AbstractPhaseInterceptor 来收集控制器的响应时,我发现我没有办法做到这一点,拦截器中只有一个方法handleMessage(Message message) ,我无法从消息中获取任何我想要的东西

Can anybody help me?有谁能够帮助我? I am new to CXF.我是 CXF 的新手。 Thanks!谢谢!

I found the answer from the other blob我从另一个 blob 中找到了答案

package XXX.web.webservice.interceptor;  

import java.io.ByteArrayInputStream;  
import java.io.IOException;  
import java.io.InputStream;  
import java.io.OutputStream;  

import org.apache.commons.io.IOUtils;  
import org.apache.cxf.io.CachedOutputStream;  
import org.apache.cxf.message.Message;  
import org.apache.cxf.phase.AbstractPhaseInterceptor;  
import org.apache.cxf.phase.Phase;  
import org.apache.log4j.Logger;  

public class ArtifactOutInterceptor extends AbstractPhaseInterceptor<Message>{  
    private static final Logger log = Logger.getLogger(ArtifactOutInterceptor.class);  

    public ArtifactOutInterceptor() {  
        //这儿使用pre_stream,意思为在流关闭之前  
        super(Phase.PRE_STREAM);  
    }  

    public void handleMessage(Message message) {  

        try {  

            OutputStream os = message.getContent(OutputStream.class);  

            CachedStream cs = new CachedStream();  

            message.setContent(OutputStream.class, cs);  

            message.getInterceptorChain().doIntercept(message);  

            CachedOutputStream csnew = (CachedOutputStream) message.getContent(OutputStream.class);  
            InputStream in = csnew.getInputStream();  

            String xml = IOUtils.toString(in);  

            //这里对xml做处理,处理完后同理,写回流中  
            IOUtils.copy(new ByteArrayInputStream(xml.getBytes()), os);  

            cs.close();  
            os.flush();  

            message.setContent(OutputStream.class, os);  


        } catch (Exception e) {  
            log.error("Error when split original inputStream. CausedBy : " + "\n" + e);  
        }  
    }  

    private class CachedStream extends CachedOutputStream {  

        public CachedStream() {  

            super();  

        }  

        protected void doFlush() throws IOException {  

            currentStream.flush();  

        }  

        protected void doClose() throws IOException {  

        }  

        protected void onWrite() throws IOException {  

        }  

    }  

}  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM