Added toDocument and toEnvelope

This commit is contained in:
Arjen Poutsma
2008-02-07 15:19:16 +00:00
parent 5ec4b401bc
commit 73e8e234a4
2 changed files with 127 additions and 4 deletions

View File

@@ -16,17 +16,31 @@
package org.springframework.ws.soap.axiom.support;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Iterator;
import java.util.Locale;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.stream.XMLInputFactory;
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.OMNode;
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;
import org.springframework.ws.soap.axiom.AxiomSoapEnvelopeException;
import org.springframework.xml.namespace.QNameUtils;
/**
@@ -93,5 +107,58 @@ public abstract class AxiomUtils {
}
}
/**
* Converts a given AXIOM {@link org.apache.axiom.soap.SOAPEnvelope} to a {@link Document}.
*
* @param envelope the SOAP envelope
* @return the converted document
* @throws AxiomSoapEnvelopeException in case of errors
* @see org.apache.rampart.util.Axis2Util.getDocumentFromSOAPEnvelope(SOAPEnvelope, boolean)
*/
public static Document toDocument(SOAPEnvelope envelope) {
try {
if (envelope instanceof Element) {
return ((Element) envelope).getOwnerDocument();
}
else {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
envelope.build();
envelope.serialize(bos);
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
return documentBuilderFactory.newDocumentBuilder().parse(bis);
}
}
catch (Exception ex) {
throw new AxiomSoapEnvelopeException("Error in converting SOAP Envelope to Document", ex);
}
}
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());
StAXSOAPModelBuilder stAXSOAPModelBuilder =
new StAXSOAPModelBuilder(XMLInputFactory.newInstance().createXMLStreamReader(bis), null);
return stAXSOAPModelBuilder.getSOAPEnvelope();
}
catch (Exception ex) {
throw new AxiomSoapEnvelopeException("Error in converting SOAP Envelope to Document", ex);
}
}
}

View File

@@ -16,16 +16,33 @@
package org.springframework.ws.soap.axiom.support;
import junit.framework.TestCase;
import java.io.StringWriter;
import java.util.Locale;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.soap.SOAP11Constants;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axiom.soap.SOAPMessage;
import org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder;
import org.apache.axiom.soap.impl.llom.soap11.SOAP11Factory;
import org.custommonkey.xmlunit.XMLTestCase;
import org.custommonkey.xmlunit.XMLUnit;
import org.w3c.dom.Document;
import javax.xml.namespace.QName;
import java.util.Locale;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.FileCopyUtils;
import org.springframework.xml.sax.SaxUtils;
public class AxiomUtilsTest extends TestCase {
public class AxiomUtilsTest extends XMLTestCase {
private OMElement element;
@@ -33,6 +50,7 @@ public class AxiomUtilsTest extends TestCase {
OMFactory factory = OMAbstractFactory.getOMFactory();
OMNamespace namespace = factory.createOMNamespace("http://www.springframework.org", "prefix");
element = factory.createOMElement("element", namespace);
XMLUnit.setIgnoreWhitespace(true);
}
public void testToNamespaceDeclared() throws Exception {
@@ -75,4 +93,42 @@ public class AxiomUtilsTest extends TestCase {
assertEquals("Invalid conversion", Locale.CANADA_FRENCH, AxiomUtils.toLocale("fr-CA"));
assertEquals("Invalid conversion", Locale.ENGLISH, AxiomUtils.toLocale("en"));
}
public void testToDocument() throws Exception {
Resource resource = new ClassPathResource("org/springframework/ws/soap/soap11/soap11.xml");
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document expected = documentBuilder.parse(SaxUtils.createInputSource(resource));
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLStreamReader reader = inputFactory.createXMLStreamReader(resource.getInputStream());
StAXSOAPModelBuilder builder =
new StAXSOAPModelBuilder(reader, new SOAP11Factory(), SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
SOAPMessage soapMessage = builder.getSoapMessage();
Document result = AxiomUtils.toDocument(soapMessage.getSOAPEnvelope());
assertXMLEqual("Invalid document generated from SOAPEnvelope", expected, result);
}
public void testToEnvelope() throws Exception {
Resource resource = new ClassPathResource("org/springframework/ws/soap/soap11/soap11.xml");
byte[] buf = FileCopyUtils.copyToByteArray(resource.getFile());
String expected = new String(buf, "UTF-8");
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(SaxUtils.createInputSource(resource));
SOAPEnvelope envelope = AxiomUtils.toEnvelope(document);
StringWriter writer = new StringWriter();
envelope.serialize(writer);
String result = writer.toString();
assertXMLEqual("Invalid SOAPEnvelope generated from document", expected, result);
}
}