Skip to content

Commit

Permalink
CXF-9037: NPE when using com.ctc.wstx.returnNullForDefaultNamespace=t…
Browse files Browse the repository at this point in the history
…rue (#1962)

* CXF-9037: NPE when using com.ctc.wstx.returnNullForDefaultNamespace=true

* Address code review comments

(cherry picked from commit 6b31ab0)
  • Loading branch information
reta committed Jul 19, 2024
1 parent d5efa01 commit 1c88633
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
5 changes: 5 additions & 0 deletions core/src/main/java/org/apache/cxf/staxutils/StaxSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ protected void parse() throws SAXException {
if (nsUri == null) {
nsUri = "";
}
// see please "com.ctc.wstx.returnNullForDefaultNamespace" property
if (nsPrefix == null) {
nsPrefix = "";
}

contentHandler.startPrefixMapping(nsPrefix, nsUri);
}
contentHandler.startElement(uri == null ? "" : uri, localName, qname, getAttributes());
Expand Down
37 changes: 37 additions & 0 deletions core/src/test/java/org/apache/cxf/staxutils/StaxUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;

import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilderFactory;
Expand All @@ -47,11 +48,14 @@

import org.xml.sax.InputSource;

import com.ctc.wstx.stax.WstxInputFactory;

import org.apache.cxf.helpers.DOMUtils;
import org.apache.cxf.helpers.IOUtils;

import org.junit.Test;

import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -242,6 +246,39 @@ public void testEmptyNamespace() throws Exception {
cycleString(testString);
}


@Test
public void testNullForDefaultNamespace() throws Exception {
final String bodyXml = "<?xml version=\"1.0\"?>\n"
+ "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
+ " <soap:Body>\n"
+ " <testReq xmlns=\"http://test.com/test\">\n"
+ " <reqBody>&lt;&lt;&lt;&lt;&lt;</reqBody>\n"
+ " </testReq>\n"
+ " </soap:Body>\n"
+ "</soap:Envelope>\n";

try (ByteArrayInputStream in = new ByteArrayInputStream(bodyXml.getBytes(StandardCharsets.UTF_8))) {
final WstxInputFactory factory = new WstxInputFactory();
factory.setProperty("com.ctc.wstx.returnNullForDefaultNamespace", "true");

final Source beforeSource = new StaxSource(factory.createXMLStreamReader(in));
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);

TransformerFactory tf = TransformerFactory.newInstance();

Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "Q{http://test.com/test}reqBody");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

transformer.transform(beforeSource, result);
assertThat(writer.toString(), startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"));
}
}

private void cycleString(String s) throws Exception {
StringReader reader = new StringReader(s);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
Expand Down

0 comments on commit 1c88633

Please sign in to comment.