diff --git a/core/src/main/java/org/springframework/ws/WebServiceMessage.java b/core/src/main/java/org/springframework/ws/WebServiceMessage.java index b6bbfe63..150709e2 100644 --- a/core/src/main/java/org/springframework/ws/WebServiceMessage.java +++ b/core/src/main/java/org/springframework/ws/WebServiceMessage.java @@ -42,8 +42,11 @@ public interface WebServiceMessage { Source getPayloadSource(); /** - * Returns the contents of the message as a {@link Result}.

Implementations that are read-only will throw an - * {@link UnsupportedOperationException}. + * Returns the contents of the message as a {@link Result}. + *

+ * Calling this method removes the current payload. + *

+ * Implementations that are read-only will throw an {@link UnsupportedOperationException}. * * @return the message contents * @throws UnsupportedOperationException if the message is read-only diff --git a/core/src/main/java/org/springframework/ws/soap/SoapBody.java b/core/src/main/java/org/springframework/ws/soap/SoapBody.java index c625977a..221a5f00 100644 --- a/core/src/main/java/org/springframework/ws/soap/SoapBody.java +++ b/core/src/main/java/org/springframework/ws/soap/SoapBody.java @@ -20,6 +20,8 @@ import java.util.Locale; import javax.xml.transform.Result; import javax.xml.transform.Source; +import org.springframework.ws.WebServiceMessage; + /** * Represents the Body element in a SOAP message. A SOAP body contains the payload of the * message. This payload can be custom XML, or a SoapFault (but not both). @@ -40,13 +42,17 @@ public interface SoapBody extends SoapElement { * Returns a Source that represents the contents of the body. * * @return the message contents + * @see WebServiceMessage#getPayloadSource() */ Source getPayloadSource(); /** * Returns a Result that represents the contents of the body. + *

+ * Calling this method removes the current content of the body. * * @return the message contents + * @see WebServiceMessage#getPayloadResult() */ Result getPayloadResult(); diff --git a/core/src/main/java/org/springframework/ws/soap/axiom/AxiomContentHandler.java b/core/src/main/java/org/springframework/ws/soap/axiom/AxiomContentHandler.java deleted file mode 100644 index f9e38db6..00000000 --- a/core/src/main/java/org/springframework/ws/soap/axiom/AxiomContentHandler.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 2006 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.ws.soap.axiom; - -import org.apache.axiom.om.OMElement; -import org.apache.axiom.om.impl.builder.SAXOMBuilder; -import org.springframework.util.Assert; -import org.xml.sax.SAXException; - -/** - * Specific SAX ContentHandler that adds the resulting AXIOM OMElement to a specified parent element when - * endDocument is called. Used for returing SAXResults from Axiom elements. - * - * @author Arjen Poutsma - * @since 1.0.0 - */ -class AxiomContentHandler extends SAXOMBuilder { - - private OMElement parentElement = null; - - public AxiomContentHandler(OMElement parentElement) { - Assert.notNull(parentElement, "No parentElement given"); - this.parentElement = parentElement; - } - - public void endDocument() throws SAXException { - super.endDocument(); - parentElement.addChild(super.getRootElement()); - } -} diff --git a/core/src/main/java/org/springframework/ws/soap/axiom/AxiomSoapBody.java b/core/src/main/java/org/springframework/ws/soap/axiom/AxiomSoapBody.java index 72fdfd1d..15929ddb 100644 --- a/core/src/main/java/org/springframework/ws/soap/axiom/AxiomSoapBody.java +++ b/core/src/main/java/org/springframework/ws/soap/axiom/AxiomSoapBody.java @@ -28,6 +28,7 @@ import org.apache.axiom.soap.SOAPFactory; import org.apache.axiom.soap.SOAPFault; import org.springframework.ws.soap.SoapBody; import org.springframework.ws.soap.SoapFault; +import org.springframework.ws.soap.axiom.support.AxiomUtils; import org.springframework.xml.transform.StaxSource; /** @@ -64,7 +65,8 @@ abstract class AxiomSoapBody extends AxiomSoapElement implements SoapBody { } public Result getPayloadResult() { - return new SAXResult(new AxiomContentHandler(getAxiomBody())); + AxiomUtils.removeContents(getAxiomBody()); + return new SAXResult(new AxiomHandler(getAxiomBody(), getAxiomFactory())); } public boolean hasFault() { diff --git a/core/src/main/java/org/springframework/ws/soap/axiom/AxiomSoapFaultDetail.java b/core/src/main/java/org/springframework/ws/soap/axiom/AxiomSoapFaultDetail.java index 1badf7b6..ceabb393 100644 --- a/core/src/main/java/org/springframework/ws/soap/axiom/AxiomSoapFaultDetail.java +++ b/core/src/main/java/org/springframework/ws/soap/axiom/AxiomSoapFaultDetail.java @@ -56,7 +56,7 @@ class AxiomSoapFaultDetail extends AxiomSoapElement implements SoapFaultDetail { } public Result getResult() { - return new SAXResult(new AxiomContentHandler(getAxiomFaultDetail())); + return new SAXResult(new AxiomHandler(getAxiomFaultDetail(), getAxiomFactory())); } protected SOAPFaultDetail getAxiomFaultDetail() { diff --git a/core/src/main/java/org/springframework/ws/soap/axiom/AxiomSoapFaultDetailElement.java b/core/src/main/java/org/springframework/ws/soap/axiom/AxiomSoapFaultDetailElement.java index 26fbb135..64cf29ff 100644 --- a/core/src/main/java/org/springframework/ws/soap/axiom/AxiomSoapFaultDetailElement.java +++ b/core/src/main/java/org/springframework/ws/soap/axiom/AxiomSoapFaultDetailElement.java @@ -38,7 +38,7 @@ class AxiomSoapFaultDetailElement extends AxiomSoapElement implements SoapFaultD public Result getResult() { try { - return new SAXResult(new AxiomContentHandler(getAxiomElement())); + return new SAXResult(new AxiomHandler(getAxiomElement(), getAxiomFactory())); } catch (OMException ex) { throw new AxiomSoapFaultException(ex); diff --git a/core/src/main/java/org/springframework/ws/soap/axiom/AxiomSoapHeader.java b/core/src/main/java/org/springframework/ws/soap/axiom/AxiomSoapHeader.java index 04c3f14f..6410ab25 100644 --- a/core/src/main/java/org/springframework/ws/soap/axiom/AxiomSoapHeader.java +++ b/core/src/main/java/org/springframework/ws/soap/axiom/AxiomSoapHeader.java @@ -44,7 +44,7 @@ abstract class AxiomSoapHeader extends AxiomSoapElement implements SoapHeader { } public Result getResult() { - return new SAXResult(new AxiomContentHandler(getAxiomHeader())); + return new SAXResult(new AxiomHandler(getAxiomHeader(), getAxiomFactory())); } public SoapHeaderElement addHeaderElement(QName name) { diff --git a/core/src/main/java/org/springframework/ws/soap/axiom/AxiomSoapHeaderElement.java b/core/src/main/java/org/springframework/ws/soap/axiom/AxiomSoapHeaderElement.java index 6d4f565e..5f10cf3d 100644 --- a/core/src/main/java/org/springframework/ws/soap/axiom/AxiomSoapHeaderElement.java +++ b/core/src/main/java/org/springframework/ws/soap/axiom/AxiomSoapHeaderElement.java @@ -49,7 +49,7 @@ class AxiomSoapHeaderElement extends AxiomSoapElement implements SoapHeaderEleme public Result getResult() { try { - return new SAXResult(new AxiomContentHandler(getAxiomHeaderBlock())); + return new SAXResult(new AxiomHandler(getAxiomHeaderBlock(), getAxiomFactory())); } catch (OMException ex) { throw new AxiomSoapHeaderException(ex); diff --git a/core/src/main/java/org/springframework/ws/soap/axiom/support/AxiomUtils.java b/core/src/main/java/org/springframework/ws/soap/axiom/support/AxiomUtils.java index 13512c5e..514af22d 100644 --- a/core/src/main/java/org/springframework/ws/soap/axiom/support/AxiomUtils.java +++ b/core/src/main/java/org/springframework/ws/soap/axiom/support/AxiomUtils.java @@ -16,12 +16,16 @@ package org.springframework.ws.soap.axiom.support; +import java.util.Iterator; import java.util.Locale; import javax.xml.namespace.QName; +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.springframework.util.StringUtils; import org.springframework.xml.namespace.QNameUtils; @@ -81,5 +85,13 @@ public abstract class AxiomUtils { return StringUtils.parseLocaleString(language); } + /** Removes the contents (i.e. children) of the container. */ + public static void removeContents(OMContainer container) { + for (Iterator iterator = container.getChildren(); iterator.hasNext();) { + OMNode child = (OMNode) iterator.next(); + child.detach(); + } + } + } diff --git a/core/src/test/java/org/springframework/ws/soap/AbstractSoapBodyTestCase.java b/core/src/test/java/org/springframework/ws/soap/AbstractSoapBodyTestCase.java index 461841d0..32a2cede 100644 --- a/core/src/test/java/org/springframework/ws/soap/AbstractSoapBodyTestCase.java +++ b/core/src/test/java/org/springframework/ws/soap/AbstractSoapBodyTestCase.java @@ -17,6 +17,11 @@ package org.springframework.ws.soap; import java.util.Locale; +import javax.xml.transform.dom.DOMResult; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; import org.springframework.xml.transform.StringResult; import org.springframework.xml.transform.StringSource; @@ -34,11 +39,21 @@ public abstract class AbstractSoapBodyTestCase extends AbstractSoapElementTestCa public void testPayload() throws Exception { String payload = ""; - StringSource contents = new StringSource(payload); - transformer.transform(contents, soapBody.getPayloadResult()); + transformer.transform(new StringSource(payload), soapBody.getPayloadResult()); assertPayloadEqual(payload); } + public void testGetPayloadResultTwice() throws Exception { + String payload = ""; + transformer.transform(new StringSource(payload), soapBody.getPayloadResult()); + transformer.transform(new StringSource(payload), soapBody.getPayloadResult()); + DOMResult domResult = new DOMResult(); + transformer.transform(soapBody.getSource(), domResult); + Element bodyElement = ((Document) domResult.getNode()).getDocumentElement(); + NodeList children = bodyElement.getChildNodes(); + assertEquals("Invalid amount of child nodes", 1, children.getLength()); + } + public void testNoFault() throws Exception { assertFalse("body has fault", soapBody.hasFault()); }