Log incoming and outgoing SOAP messages

Often it is nice to see what requests are sent to the service and what the anwer is. Even when the request is not a valid SOAP request. This can be done with interceptors and annotating the service


@InInterceptors(interceptors = { "com.company.interceptors.LoggingInInterceptor" })
@OutInterceptors(interceptors = { "com.company.interceptors.LoggingOutInterceptor" })


public class LoggingInInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
    private static final int MAXINLOGSIZE = 5000;

    public LoggingInInterceptor() {
        log.debug("linking to {}", getPhase());

    public void handleMessage(SoapMessage message) throws Fault {
        // See "org.apache.cxf.interceptor.LoggingInInterceptor" for an example
        try {
            // use the appropriate input stream and restore it later
            InputStream is = message.getContent(InputStream.class);
            InputStream bis = is instanceof DelegatingInputStream?((DelegatingInputStream)is).getInputStream():is;

            // only copy up to the limit since that's all we need to log we can stream the rest
            CachedOutputStream cos = new CachedOutputStream();
            IOUtils.copyAtLeast(bis, cos, MAXINLOGSIZE);
            bis = new SequenceInputStream(cos.getInputStream(), bis);

            // restore the delegating input stream or the input stream
            if (is instanceof DelegatingInputStream) {
            } else {
                message.setContent(InputStream.class, bis);
            String body = IOUtils.toString(cos.getInputStream());
            log.debug("request: '{}'", body);
        } catch (IOException e) {
            log.error(e.getMessage(), e);


public class LoggingOutInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
    public LoggingOutInterceptor() {

    public void handleMessage(SoapMessage message) throws Fault {
        OutputStream os = message.getContent(OutputStream.class);
        CacheAndWriteOutputStream cwos = new CacheAndWriteOutputStream(os);
        message.setContent(OutputStream.class, cwos);
        cwos.registerCallback(new LoggingOutCallBack());

    class LoggingOutCallBack implements CachedOutputStreamCallback {
        // See "org.apache.cxf.interceptor.LoggingOutInterceptor" for an example
        private static final int MAXOUTLOGSIZE = 5000;

        public void onClose(CachedOutputStream cos) {
            try {
                StringBuilder builder = new StringBuilder();
                String body = builder.toString();
                body = StringUtils.left(body, MAXOUTLOGSIZE);
                log.debug("response: '{}'", body);
                //empty out the cache
                cos.resetOut(null, false);
            } catch (IOException e) {

        public void onFlush(CachedOutputStream cos) {}