简体   繁体   中英

Logging request/response with CXF dynamic client

I am using CXF dynamic client and want to do some logging. I want to log the incoming and outgoing soap xml. I need to do that with interceptors but have no clue on how to wire them so the client will use that.

Example code:

    MyWebservice ws = new MyWebservice_Service().getChargePointServiceSoap12();

How can i add interceptors to this client?

To outcome messages create a Interceptor like this:

import java.io.InputStream;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;

public class LogInterceptor extends AbstractPhaseInterceptor<Message> {

    public LogInterceptor() {
        super(Phase.PRE_INVOKE);
    }

    @Override
    public void handleMessage(Message message) throws Fault {
        InputStream contentInputStream = message.get(InputStream.class);
        // Do whatever you want with the content (the soap xml)
    }

    public void addInterceptor(Object portType) {
        Client client = ClientProxy.getClient(portType);
        client.getInInterceptors().add(this);
    }
}

Your code should call addInterceptor:

MyWebservice ws = new MyWebservice_Service().getChargePointServiceSoap12();
LogInterceptor logInterceptor = new LogInterceptor();
logInterceptor.addInterceptor(ws);

Finally for income messages you change the Phase on interceptor's constructor.

Docs: http://cxf.apache.org/docs/interceptors.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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