Yest you can. I wrote a CxfOutInterceptor to receive an XML message. Here is the code:
import org.apache.cxf.common.util.StringUtils; import org.apache.cxf.interceptor.Fault; import org.apache.cxf.io.CacheAndWriteOutputStream; import org.apache.cxf.io.CachedOutputStream; import org.apache.cxf.io.CachedOutputStreamCallback; import org.apache.cxf.message.Message; import org.apache.cxf.phase.AbstractPhaseInterceptor; import org.apache.cxf.phase.Phase; import org.springframework.beans.factory.annotation.Autowired; import java.io.IOException; import java.io.OutputStream; import java.io.Writer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class CxfOutInterceptor extends AbstractPhaseInterceptor<Message> { private static final Logger LOGGER = LoggerFactory.getLogger(CxfInInterceptor.class); public CxfOutInterceptor() { super(Phase.PRE_STREAM); } public static final String SINGLE_KEY = CxfOutInterceptor.class.getName() + ".Processed"; private static final int LIMIT = 10 * 1024 * 1024; @Override public void handleFault(Message message) { LOGGER.trace("handleFault"); try { internalHandleMessage(message); } catch (Throwable ex) { LOGGER.error("Exception thrown by internalHandleMessage: ", ex); } finally { LOGGER.trace("handleFault - end"); } } @Override public void handleMessage(Message message) throws Fault { LOGGER.trace("handleMessage"); try { if (onceOnly(message)) { LOGGER.debug("handled message previously"); return; } internalHandleMessage(message); } finally { LOGGER.trace("handleMessage - end"); } } private class LogCallback implements CachedOutputStreamCallback { private final Message message; private final OutputStream origStream; public LogCallback(final Message msg, final OutputStream os) { this.message = msg; this.origStream = os; } @Override public void onFlush(CachedOutputStream cos) { } @Override public void onClose(CachedOutputStream cos) { StringBuilder requestBuilder = new StringBuilder(); String encoding = (String) message.get(Message.ENCODING); try { writePayload(requestBuilder, cos, encoding);
You will receive an XML message in the onClose method. See Comment: //requestBuilder - is your actuall XML of the message.
source share