tomee-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Anthony Fryer <>
Subject Re: cxf advanced examples
Date Thu, 05 Jun 2014 23:16:07 GMT
I have a simple cxf interceptor in a project that just forces the text of an
xml element in a web service response to be wrapped in a CDATA element.  It
would probably make for a good example.

There's 2 classes I have to implement the interceptor...

public class CDataContentWriter extends DelegatingXMLStreamWriter {
	private static final Pattern XML_CHARS = Pattern.compile("[&<>]");
	private String currentElementLocalName;

	public CDataContentWriter(XMLStreamWriter writer) {

	public void writeCharacters(String text) throws XMLStreamException {
		boolean useCData = XML_CHARS.matcher(text).find();
		if (useCData) {
		} else {

	public void writeStartElement(String prefix, String local, String uri)
			throws XMLStreamException {
		currentElementLocalName = local;
		super.writeStartElement(prefix, local, uri);

public class CDataWriterInterceptor extends
AbstractPhaseInterceptor<Message> {

	public CDataWriterInterceptor() {

	public void handleMessage(Message message) throws Fault {
		message.put("disable.outputstream.optimization", Boolean.TRUE);
		XMLStreamWriter writer =
		message.setContent(XMLStreamWriter.class, new CDataContentWriter(writer));

Then in my resources.xml (or you could use tomee.xml) i declare the
interceptor as a service...

   <Service id="cdataWriterInterceptor"

Lastly, I had to create an openejb-jar.xml (located in my WEB-INF folder)
which is where i hook up the interceptor service to individual web

<openejb-jar xmlns="">

  <ejb-deployment ejb-name="GetBoardingPassDataImpl">
      cxf.jaxws.out-interceptors = cdataWriterInterceptor
  <ejb-deployment ejb-name="BoardingPassBarCodeImpl">
      cxf.jaxws.out-interceptors = cdataWriterInterceptor

So I ended up with 2 webservices where the response xml will have CDATA
elements instead of escaped strings.  Hope this helps.



View this message in context:
Sent from the OpenEJB User mailing list archive at

View raw message