This is an automated email from the ASF dual-hosted git repository. reta pushed a commit to branch 3.5.x-fixes in repository https://gitbox.apache.org/repos/asf/cxf.git
commit 420280f342a85d18f59d7864fad7917cb9757b88 Author: Andriy Redko <[email protected]> AuthorDate: Fri Jul 19 17:18:09 2024 -0400 CXF-9037: NPE when using com.ctc.wstx.returnNullForDefaultNamespace=true (#1962) * CXF-9037: NPE when using com.ctc.wstx.returnNullForDefaultNamespace=true * Address code review comments (cherry picked from commit 6b31ab01eaf234411503d781b42b7828d85a2cb6) (cherry picked from commit 1c88633158d6c034ef2566be6fa7e07969dd5278) (cherry picked from commit 3184b1ea9c4bd7546b725d2ce1e7c7b5df7bfe42) --- .../java/org/apache/cxf/staxutils/StaxSource.java | 5 +++ .../org/apache/cxf/staxutils/StaxUtilsTest.java | 37 ++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/core/src/main/java/org/apache/cxf/staxutils/StaxSource.java b/core/src/main/java/org/apache/cxf/staxutils/StaxSource.java index f74058406d..2416d5596b 100644 --- a/core/src/main/java/org/apache/cxf/staxutils/StaxSource.java +++ b/core/src/main/java/org/apache/cxf/staxutils/StaxSource.java @@ -142,6 +142,11 @@ public class StaxSource extends SAXSource implements XMLReader { 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()); diff --git a/core/src/test/java/org/apache/cxf/staxutils/StaxUtilsTest.java b/core/src/test/java/org/apache/cxf/staxutils/StaxUtilsTest.java index 1e97337c75..d2272da7b4 100644 --- a/core/src/test/java/org/apache/cxf/staxutils/StaxUtilsTest.java +++ b/core/src/test/java/org/apache/cxf/staxutils/StaxUtilsTest.java @@ -25,6 +25,7 @@ import java.io.InputStream; 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; @@ -47,11 +48,14 @@ import org.w3c.dom.Element; 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; @@ -242,6 +246,39 @@ public class StaxUtilsTest { 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><<<<<</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();
