I have a ganerated webservice client using the Apache CXF wsdl2java tool. It works fine, but in one method I have to send a string containing special characters. The generated client uses escape characters. I do not need this. I want to put a line inside the block "<[! CDATA []]>". I can add "<[! CDATA []]>" manually in code ", which will not be a problem, but I do not know how to disable cachracter escaping. Does anyone have such a problem? How to fix this in the simplest way?
[Edit]: After Daniel Kulp's answer, I did this:
I wrote an interceptor:
public class CDATAInterceptor extends AbstractPhaseInterceptor<Message> { public CDATAInterceptor(String phase) { super(phase); } public void handleMessage(Message message) { XMLStreamWriter writer = (XMLStreamWriter) message.getContent(XMLStreamWriter.class); if (writer != null && !(writer instanceof MyXmlWriter)) { message.setContent(XMLStreamWriter.class, new MyXmlWriter(writer)); } } }
I added it to any possible phase (just in case - I probably should go into MARSHAL or PRE_MARSHAL). Here is the code as I add interceptors:
MyService service = new MyService(new URL(http://my_url_adress?wsdl)); proxy = service.getMyServiceSoap12(); Client client = ClientProxy.getClient(proxy); client.getOutInterceptors().add(new CDATAInterceptor(Phase.INVOKE)); client.getOutInterceptors().add(new CDATAInterceptor(Phase.MARSHAL)); client.getOutInterceptors().add(new CDATAInterceptor(Phase.MARSHAL_ENDING)); client.getOutInterceptors().add(new CDATAInterceptor(Phase.POST_INVOKE)); client.getOutInterceptors().add(new CDATAInterceptor(Phase.POST_LOGICAL)); client.getOutInterceptors().add(new CDATAInterceptor(Phase.POST_LOGICAL_ENDING)); ...
Here is the code for the MyXmlWriter class:
public class MyXmlWriter implements XMLStreamWriter { protected XMLStreamWriter originalXmlWriter; public MyXmlWriter(XMLStreamWriter originalXmlWriter) { this.originalXmlWriter = originalXmlWriter; } public void writeStartElement(String localName) throws XMLStreamException { this.originalXmlWriter.writeStartElement(localName); } public void writeStartElement(String namespaceURI, String localName) throws XMLStreamException { this.originalXmlWriter.writeStartElement(namespaceURI, localName); } public void writeStartElement(String prefix, String localName, String namespaceURI) throws XMLStreamException { this.originalXmlWriter.writeStartElement(prefix, localName, namespaceURI); } public void writeEmptyElement(String namespaceURI, String localName) throws XMLStreamException { this.originalXmlWriter.writeEmptyElement(namespaceURI, localName); } public void writeEmptyElement(String prefix, String localName, String namespaceURI) throws XMLStreamException { this.originalXmlWriter.writeEmptyElement(prefix, localName, namespaceURI); } public void writeEmptyElement(String localName) throws XMLStreamException { this.originalXmlWriter.writeEmptyElement(localName); } public void writeEndElement() throws XMLStreamException { this.originalXmlWriter.writeEndElement(); } public void writeEndDocument() throws XMLStreamException { this.originalXmlWriter.writeEndDocument(); } public void close() throws XMLStreamException { this.originalXmlWriter.close(); } public void flush() throws XMLStreamException { this.originalXmlWriter.flush(); } public void writeAttribute(String localName, String value) throws XMLStreamException { this.originalXmlWriter.writeAttribute(localName, value); } public void writeAttribute(String prefix, String namespaceURI, String localName, String value) throws XMLStreamException { this.originalXmlWriter.writeAttribute(prefix, namespaceURI, localName, value); } public void writeAttribute(String namespaceURI, String localName, String value) throws XMLStreamException { this.originalXmlWriter.writeAttribute(namespaceURI, localName, value); } public void writeNamespace(String prefix, String namespaceURI) throws XMLStreamException { this.originalXmlWriter.writeNamespace(prefix, namespaceURI); } public void writeDefaultNamespace(String namespaceURI) throws XMLStreamException { this.originalXmlWriter.writeDefaultNamespace(namespaceURI); } public void writeComment(String data) throws XMLStreamException { this.originalXmlWriter.writeComment(data); } public void writeProcessingInstruction(String target) throws XMLStreamException { this.originalXmlWriter.writeProcessingInstruction(target); } public void writeProcessingInstruction(String target, String data) throws XMLStreamException { this.originalXmlWriter.writeProcessingInstruction(target, data); } public void writeCData(String data) throws XMLStreamException { this.originalXmlWriter.writeCData(data); } public void writeDTD(String dtd) throws XMLStreamException { this.originalXmlWriter.writeDTD(dtd); } public void writeEntityRef(String name) throws XMLStreamException { this.originalXmlWriter.writeEntityRef(name); } public void writeStartDocument() throws XMLStreamException { this.originalXmlWriter.writeStartDocument(); } public void writeStartDocument(String version) throws XMLStreamException { this.originalXmlWriter.writeStartDocument(version); } public void writeStartDocument(String encoding, String version) throws XMLStreamException { this.originalXmlWriter.writeStartDocument(encoding, version); } public void writeCharacters(String text) throws XMLStreamException { this.originalXmlWriter.writeCData(text); } public void writeCharacters(char[] text, int start, int len) throws XMLStreamException { this.originalXmlWriter.writeCData(String.copyValueOf(text, len, len)); } public String getPrefix(String uri) throws XMLStreamException { return this.originalXmlWriter.getPrefix(uri); } public void setPrefix(String prefix, String uri) throws XMLStreamException { this.originalXmlWriter.setPrefix(prefix, uri); } public void setDefaultNamespace(String uri) throws XMLStreamException { this.originalXmlWriter.setDefaultNamespace(uri); } public void setNamespaceContext(NamespaceContext context) throws XMLStreamException { this.originalXmlWriter.setNamespaceContext(context); } public NamespaceContext getNamespaceContext() { return this.originalXmlWriter.getNamespaceContext(); } public Object getProperty(String name) throws IllegalArgumentException { return this.originalXmlWriter.getProperty(name); } }
This does not work. I am debugging to check how this works, and I notice that the MyXmlWriter.writeCharacters (...) method has almost never been used. The output message still has escape characters instead of being inside CDATA.
[Edit2]: I just discovered that I need to add a line:
message.put("disable.outputstream.optimization", Boolean.TRUE);
source: http://cxf.547215.n5.nabble.com/CXF-jaxb-send-string-as-CData-td5524523.html