Skip to content

Commit c35389b

Browse files
committed
SWS-266
1 parent 2af6be2 commit c35389b

File tree

5 files changed

+42
-4
lines changed

5 files changed

+42
-4
lines changed

core/src/main/java/org/springframework/ws/WebServiceMessage.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,11 @@ public interface WebServiceMessage {
4242
Source getPayloadSource();
4343

4444
/**
45-
* Returns the contents of the message as a {@link Result}. <p>Implementations that are read-only will throw an
46-
* {@link UnsupportedOperationException}.
45+
* Returns the contents of the message as a {@link Result}.
46+
* <p/>
47+
* Calling this method removes the current payload.
48+
* <p/>
49+
* Implementations that are read-only will throw an {@link UnsupportedOperationException}.
4750
*
4851
* @return the message contents
4952
* @throws UnsupportedOperationException if the message is read-only

core/src/main/java/org/springframework/ws/soap/SoapBody.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import javax.xml.transform.Result;
2121
import javax.xml.transform.Source;
2222

23+
import org.springframework.ws.WebServiceMessage;
24+
2325
/**
2426
* Represents the <code>Body</code> element in a SOAP message. A SOAP body contains the <strong>payload</strong> of the
2527
* message. This payload can be custom XML, or a <code>SoapFault</code> (but not both).
@@ -40,13 +42,17 @@ public interface SoapBody extends SoapElement {
4042
* Returns a <code>Source</code> that represents the contents of the body.
4143
*
4244
* @return the message contents
45+
* @see WebServiceMessage#getPayloadSource()
4346
*/
4447
Source getPayloadSource();
4548

4649
/**
4750
* Returns a <code>Result</code> that represents the contents of the body.
51+
* <p/>
52+
* Calling this method removes the current content of the body.
4853
*
4954
* @return the message contents
55+
* @see WebServiceMessage#getPayloadResult()
5056
*/
5157
Result getPayloadResult();
5258

core/src/main/java/org/springframework/ws/soap/axiom/AxiomSoapBody.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import org.springframework.ws.soap.SoapBody;
3232
import org.springframework.ws.soap.SoapFault;
33+
import org.springframework.ws.soap.axiom.support.AxiomUtils;
3334
import org.springframework.xml.transform.StaxSource;
3435

3536
/**
@@ -68,6 +69,7 @@ else if (payloadCaching) {
6869
}
6970

7071
public Result getPayloadResult() {
72+
AxiomUtils.removeContents(getAxiomBody());
7173
return new SAXResult(new AxiomContentHandler(getAxiomBody(), getAxiomFactory()));
7274
}
7375

core/src/main/java/org/springframework/ws/soap/axiom/support/AxiomUtils.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@
1616

1717
package org.springframework.ws.soap.axiom.support;
1818

19+
import java.util.Iterator;
1920
import java.util.Locale;
2021
import javax.xml.namespace.QName;
2122

23+
import org.apache.axiom.om.OMContainer;
2224
import org.apache.axiom.om.OMElement;
2325
import org.apache.axiom.om.OMException;
2426
import org.apache.axiom.om.OMNamespace;
27+
import org.apache.axiom.om.OMNode;
28+
2529
import org.springframework.util.StringUtils;
2630
import org.springframework.xml.namespace.QNameUtils;
2731

@@ -81,5 +85,13 @@ public static Locale toLocale(String language) {
8185
return StringUtils.parseLocaleString(language);
8286
}
8387

88+
/** Removes the contents (i.e. children) of the container. */
89+
public static void removeContents(OMContainer container) {
90+
for (Iterator iterator = container.getChildren(); iterator.hasNext();) {
91+
OMNode child = (OMNode) iterator.next();
92+
child.detach();
93+
}
94+
}
95+
8496

8597
}

core/src/test/java/org/springframework/ws/soap/AbstractSoapBodyTestCase.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
package org.springframework.ws.soap;
1818

1919
import java.util.Locale;
20+
import javax.xml.transform.dom.DOMResult;
21+
22+
import org.w3c.dom.Document;
23+
import org.w3c.dom.Element;
24+
import org.w3c.dom.NodeList;
2025

2126
import org.springframework.xml.transform.StringResult;
2227
import org.springframework.xml.transform.StringSource;
@@ -34,11 +39,21 @@ protected final SoapElement createSoapElement() throws Exception {
3439

3540
public void testPayload() throws Exception {
3641
String payload = "<payload xmlns='http://www.springframework.org' />";
37-
StringSource contents = new StringSource(payload);
38-
transformer.transform(contents, soapBody.getPayloadResult());
42+
transformer.transform(new StringSource(payload), soapBody.getPayloadResult());
3943
assertPayloadEqual(payload);
4044
}
4145

46+
public void testGetPayloadResultTwice() throws Exception {
47+
String payload = "<payload xmlns='http://www.springframework.org' />";
48+
transformer.transform(new StringSource(payload), soapBody.getPayloadResult());
49+
transformer.transform(new StringSource(payload), soapBody.getPayloadResult());
50+
DOMResult domResult = new DOMResult();
51+
transformer.transform(soapBody.getSource(), domResult);
52+
Element bodyElement = ((Document) domResult.getNode()).getDocumentElement();
53+
NodeList children = bodyElement.getChildNodes();
54+
assertEquals("Invalid amount of child nodes", 1, children.getLength());
55+
}
56+
4257
public void testNoFault() throws Exception {
4358
assertFalse("body has fault", soapBody.hasFault());
4459
}

0 commit comments

Comments
 (0)