This commit is contained in:
Arjen Poutsma
2008-01-21 13:38:37 +00:00
parent 2af6be2c9f
commit c35389b564
5 changed files with 42 additions and 4 deletions

View File

@@ -42,8 +42,11 @@ public interface WebServiceMessage {
Source getPayloadSource();
/**
* Returns the contents of the message as a {@link Result}. <p>Implementations that are read-only will throw an
* {@link UnsupportedOperationException}.
* Returns the contents of the message as a {@link Result}.
* <p/>
* Calling this method removes the current payload.
* <p/>
* Implementations that are read-only will throw an {@link UnsupportedOperationException}.
*
* @return the message contents
* @throws UnsupportedOperationException if the message is read-only

View File

@@ -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 <code>Body</code> element in a SOAP message. A SOAP body contains the <strong>payload</strong> of the
* message. This payload can be custom XML, or a <code>SoapFault</code> (but not both).
@@ -40,13 +42,17 @@ public interface SoapBody extends SoapElement {
* Returns a <code>Source</code> that represents the contents of the body.
*
* @return the message contents
* @see WebServiceMessage#getPayloadSource()
*/
Source getPayloadSource();
/**
* Returns a <code>Result</code> that represents the contents of the body.
* <p/>
* Calling this method removes the current content of the body.
*
* @return the message contents
* @see WebServiceMessage#getPayloadResult()
*/
Result getPayloadResult();

View File

@@ -30,6 +30,7 @@ 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;
/**
@@ -68,6 +69,7 @@ abstract class AxiomSoapBody extends AxiomSoapElement implements SoapBody {
}
public Result getPayloadResult() {
AxiomUtils.removeContents(getAxiomBody());
return new SAXResult(new AxiomContentHandler(getAxiomBody(), getAxiomFactory()));
}

View File

@@ -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();
}
}
}

View File

@@ -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 = "<payload xmlns='http://www.springframework.org' />";
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 = "<payload xmlns='http://www.springframework.org' />";
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());
}