1

We have a CDataInterceptor in order to include a signed XML file into a soap envelope in a CDATA section.

But when we are executing the code we are getting the XML escaped, like & lt;CDATA instead of <[CDATA or & #xd; when a new line. This makes the service unable to consume the SOAP envelope because of invalid format.

The issue is here:

public class CDataXMLStreamWriter extends DelegatingXMLStreamWriter {

private static final Pattern XML_CHARS = Pattern.compile( "[&<>]" );
private static final String CDataOpen = "<![CDATA[";
private static final String CDataClose = "]]>";

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

@Override 
public void writeCharacters(String text) throws XMLStreamException { 
    boolean useCData = XML_CHARS.matcher( text ).find();

    if (useCData) {
        super.writeCharacters(CDataOpen);
        super.writeCharacters(text);
        super.writeCharacters(CDataClose);
        //super.writeCData(text);
    }else { 
        super.writeCharacters(text); 
    } 
}

public void writeStartElement(String local) throws XMLStreamException { 
    super.writeStartElement(local); 
} 

}

writeCharacters method escapes all this special characters. Do you know any solution to add this XML keeping escaped characters like <, > etc.?

BR

user1748166
  • 147
  • 1
  • 2
  • 14
  • Instead of using writeCharacters in the super class , have you tried writing CDATA directly using [writeCData][1]. I see you have used it , do you get some error or other unexpected behavior ? if (useCData) { super.writeCData(text); }else { super.writeCharacters(text); } [1]: https://cxf.apache.org/javadoc/latest/org/apache/cxf/staxutils/DelegatingXMLStreamWriter.html#writeCData(java.lang.String) – Ramachandran.A.G Sep 20 '16 at 14:30
  • Using writeCData includes a lot of CDATA sections in the XML file, that thing makes the service are unable to process the request... I think is making a CDATA section every new line – user1748166 Sep 20 '16 at 14:31
  • For example, in certificate section: <![CDATA[VPUzEfMB0GA1UE]]><![CDATA[ CwwWU0VSVklDSU9TIEVMRUNUUk9OSUNPUzEdMBsGA1UEAwwUQ29ycmVvIFVydWd1YXlvIC0gQ0Ew HhcNMTUxMTI3MTk0NzE4WhcNMTYxMTI3MTk0NzE4WjCBmzEmMCQGCSqGSIb3DQEJARYXdml0YW1p bmljby4wNUBnbWFpbC5jb20xEzARBgNVBAoMClZJVEFNSU5JQ08xEzARBgNVBAgMCk1vbnRldmlk ZW8xCzAJBgNVBAYTAlVZMRgwFgYDVQQFEw9SVUMyMTUyMTcxOTAwMTUxIDAeBgNVBAMMF1ZJTkFM UyBJVkFOSUNIIExFT05BUkRPMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC6LdZWhEJaqiFa Zb1VG+o – user1748166 Sep 20 '16 at 14:33

0 Answers0