From f189e86c49e25464047859fe0247290a510ec7dc Mon Sep 17 00:00:00 2001 From: Andreas Veithen Date: Sat, 20 May 2017 17:12:56 +0100 Subject: [PATCH] SWS-986 - Upgrade Axiom and use new API to convert Document to SOAPMessage Original pull-request: https://github.com/spring-projects/spring-ws/pull/82 --- build.gradle | 2 +- .../ws/soap/axiom/AxiomSoapMessage.java | 9 ++- .../ws/soap/axiom/support/AxiomUtils.java | 61 +------------------ 3 files changed, 8 insertions(+), 64 deletions(-) diff --git a/build.gradle b/build.gradle index d67ab147..77c000b6 100644 --- a/build.gradle +++ b/build.gradle @@ -14,7 +14,7 @@ configure(allprojects) { ext.springVersion = "4.2.8.RELEASE" ext.springSecurityVersion = "4.0.4.RELEASE" - ext.axiomVersion = "1.2.16" + ext.axiomVersion = "1.2.20" ext.smackVersion = "4.1.9" apply plugin: "java" diff --git a/spring-ws-core/src/main/java/org/springframework/ws/soap/axiom/AxiomSoapMessage.java b/spring-ws-core/src/main/java/org/springframework/ws/soap/axiom/AxiomSoapMessage.java index ecebb500..10b21ab8 100644 --- a/spring-ws-core/src/main/java/org/springframework/ws/soap/axiom/AxiomSoapMessage.java +++ b/spring-ws-core/src/main/java/org/springframework/ws/soap/axiom/AxiomSoapMessage.java @@ -25,11 +25,13 @@ import java.io.Writer; import java.util.Iterator; import javax.activation.DataHandler; import javax.xml.stream.XMLStreamException; +import javax.xml.transform.dom.DOMSource; import org.apache.axiom.attachments.Attachments; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.OMException; import org.apache.axiom.om.OMOutputFormat; +import org.apache.axiom.om.OMXMLBuilderFactory; import org.apache.axiom.om.impl.MTOMConstants; import org.apache.axiom.om.impl.OMMultipartWriter; import org.apache.axiom.soap.SOAPBody; @@ -212,12 +214,9 @@ public class AxiomSoapMessage extends AbstractSoapMessage implements StreamingWe public void setDocument(Document document) { // save the Soap Action String soapAction = getSoapAction(); - SOAPEnvelope envelope = AxiomUtils.toEnvelope(document); - SOAPMessage newMessage = axiomFactory.createSOAPMessage(); - newMessage.setSOAPEnvelope(envelope); - // replace the Axiom message - setAxiomMessage(newMessage); + setAxiomMessage(OMXMLBuilderFactory.createSOAPModelBuilder(axiomFactory.getMetaFactory(), + new DOMSource(document)).getSOAPMessage()); // restore the Soap Action setSoapAction(soapAction); } diff --git a/spring-ws-core/src/main/java/org/springframework/ws/soap/axiom/support/AxiomUtils.java b/spring-ws-core/src/main/java/org/springframework/ws/soap/axiom/support/AxiomUtils.java index 76153e80..f94dd94c 100644 --- a/spring-ws-core/src/main/java/org/springframework/ws/soap/axiom/support/AxiomUtils.java +++ b/spring-ws-core/src/main/java/org/springframework/ws/soap/axiom/support/AxiomUtils.java @@ -18,29 +18,21 @@ package org.springframework.ws.soap.axiom.support; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.OutputStream; import java.util.Iterator; import java.util.Locale; import javax.xml.namespace.QName; import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.stream.XMLInputFactory; +import javax.xml.transform.dom.DOMSource; import org.apache.axiom.om.OMContainer; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.OMException; import org.apache.axiom.om.OMNamespace; -import org.apache.axiom.om.util.StAXUtils; +import org.apache.axiom.om.OMXMLBuilderFactory; import org.apache.axiom.soap.SOAPEnvelope; -import org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder; -import org.w3c.dom.DOMImplementation; import org.w3c.dom.Document; import org.w3c.dom.Element; -import org.w3c.dom.ls.DOMImplementationLS; -import org.w3c.dom.ls.LSOutput; -import org.w3c.dom.ls.LSSerializer; -import org.springframework.util.Assert; import org.springframework.util.StringUtils; /** @@ -145,54 +137,7 @@ public abstract class AxiomUtils { * @throws IllegalArgumentException in case of errors */ public static SOAPEnvelope toEnvelope(Document document) { - try { - DOMImplementation implementation = document.getImplementation(); - Assert.isInstanceOf(DOMImplementationLS.class, implementation); - - DOMImplementationLS loadSaveImplementation = (DOMImplementationLS) implementation; - LSOutput output = loadSaveImplementation.createLSOutput(); - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - output.setByteStream(bos); - - LSSerializer serializer = loadSaveImplementation.createLSSerializer(); - serializer.write(document, output); - - ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); - - XMLInputFactory inputFactory = StAXUtils.getXMLInputFactory(); - - @SuppressWarnings("deprecation") - StAXSOAPModelBuilder stAXSOAPModelBuilder = - new StAXSOAPModelBuilder(inputFactory.createXMLStreamReader(bis), null); - SOAPEnvelope envelope = stAXSOAPModelBuilder.getSOAPEnvelope(); - - // Necessary to build a correct Axiom tree, see SWS-483 - envelope.serialize(new NullOutputStream()); - - return envelope; - } - catch (Exception ex) { - IllegalArgumentException iaex = - new IllegalArgumentException("Error in converting Document to SOAP Envelope"); - iaex.initCause(ex); - throw iaex; - } - } - - /** OutputStream that does nothing. */ - private static class NullOutputStream extends OutputStream { - - @Override - public void write(int b) throws IOException { - } - - @Override - public void write(byte[] b) throws IOException { - } - - @Override - public void write(byte[] b, int off, int len) throws IOException { - } + return OMXMLBuilderFactory.createSOAPModelBuilder(new DOMSource(document)).getSOAPEnvelope(); } }