Shreyas Dave Shreyas Dave - 1 month ago 18
Java Question

Issue with marshalling of SOAP CXF webservice request

I am calling a SOAP webserive using

I am facing an issue with WebService request call, where the request string (XML) is getting formatted while send.

Expected Request:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:serviceRequest xmlns:ns2="urn:example.com/xml/schemas/v1_0/">
<referenceNum>5361610270000002</referenceNum>
<client>ABCD</client>
<serviceName>GET.PERSONAL.DETAIL</serviceName>
<UserId>USER_1234</UserId>
<data><![CDATA[<ns2:getDetailRequest xmlns:ns2="urn:example.com/xml/schemas/v1_0/">
<HEADER>
<serviceName>GET.PERSONAL.DETAIL</serviceName>
<serviceType>SYNC</serviceType>
<serviceVersion>1.0</serviceVersion>
</HEADER>
</ns2:getDetailRequest>]]>
</data>
</ns2:serviceRequest>
</soap:Body>
</soap:Envelope>


Actual Request Sent:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:serviceRequest xmlns:ns2="urn:example.com/xml/schemas/v1_0/">
<referenceNum>5361610270000002</referenceNum>
<client>ABCD</client>
<serviceName>GET.PERSONAL.DETAIL</serviceName>
<UserId>USER_1234</UserId>
<data>&lt;![CDATA[&lt;ns2:getDetailRequest xmlns:ns2="urn:example.com/xml/schemas/v1_0/"&gt;
&lt;HEADER&gt;
&lt;serviceName&gt;GET.PERSONAL.DETAIL&lt;/serviceName&gt;
&lt;serviceType&gt;SYNC&lt;/serviceType&gt;
&lt;serviceVersion&gt;1.0&lt;/serviceVersion&gt;
&lt;/HEADER&gt;
&lt;/ns2:getAccountDetailRequest&gt;]]&gt
</data>
</ns2:serviceRequest>
</soap:Body>
</soap:Envelope>


I am using Apache CXF (SOAP) for webservice call. Marshaling of Java Object is handled internally by CXF and JAXB libraries.

Has anyone faced such issue where elements inside <[CDATA[ ]]> gets formatted during marshaling or How to fix this ?

Answer

I found the solution using Interceptor.

public class CustomSoapCxfInterceptor extends AbstractSoapInterceptor {

    public CustomSoapCxfInterceptor() {
        super(Phase.PRE_STREAM);
        getAfter().add(AttachmentOutInterceptor.class.getName());

    }

    @Override
    public void handleMessage(SoapMessage message) {
        try {
            message.put("disable.outputstream.optimization", Boolean.TRUE);
            XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(message.getContent(OutputStream.class));

            CDataXMLStreamWriter cDataXMLStreamWriter = new CDataXMLStreamWriter(writer);
            message.setContent(XMLStreamWriter.class, cDataXMLStreamWriter);
        } catch (Exception exception) {
            DefaultLogger.error(exception);
        }
    }
}

Where CDataXMLStreamWriter extends DelegatingXMLStreamWriter

public class CDataXMLStreamWriter extends DelegatingXMLStreamWriter {

    private int counter = 1;

    private String currentElementName;

    public CDataXMLStreamWriter(XMLStreamWriter del) {
        super(del);
    }

    @Override
    public void writeCharacters(String text) throws XMLStreamException {
        boolean useCData = checkIfCDATAneededForCurrentElement();
        if (useCData) {
            super.writeCData(text);
        } else {
            super.writeCharacters(text);
        }
    }

    private boolean checkIfCDATAneededForCurrentElement() {
        if ("data".equals(currentElementName))
            return true;
        return false;
    }

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

    @Override
    public void writeEndElement() throws XMLStreamException {
        if ("data".equals(currentElementName)) {
            if (this.counter == 1) {
                super.writeEndElement();
            }
            this.counter++;
        } else {
            super.writeEndElement();
        }

    }

}