Addes setDocument/getDocument to SoapMessage, for use in Wss4jSecurityInterceptor
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2005 the original author or authors.
|
||||
* Copyright 2005-2010 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.
|
||||
@@ -19,6 +19,8 @@ package org.springframework.ws.soap;
|
||||
import org.springframework.ws.FaultAwareWebServiceMessage;
|
||||
import org.springframework.ws.mime.MimeMessage;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
/**
|
||||
* Represents an abstraction for SOAP messages, providing access to a SOAP Envelope. The contents of the SOAP body can
|
||||
* be retrieved by <code>getPayloadSource()</code> and <code>getPayloadResult()</code> on
|
||||
@@ -74,4 +76,20 @@ public interface SoapMessage extends MimeMessage, FaultAwareWebServiceMessage {
|
||||
*/
|
||||
SoapVersion getVersion();
|
||||
|
||||
/**
|
||||
* Returns this message as a {@link Document}.
|
||||
*
|
||||
* Depending on the underlying implementation, this Document may be 'live' or not.
|
||||
* @return this soap message as a DOM document
|
||||
*/
|
||||
Document getDocument();
|
||||
|
||||
/**
|
||||
* Returns this message as a {@link Document}.
|
||||
*
|
||||
* Depending on the underlying implementation, this Document may be 'live' or not.
|
||||
* @return this soap message as a DOM document
|
||||
*/
|
||||
void setDocument(Document document);
|
||||
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ import org.springframework.ws.soap.AbstractSoapMessage;
|
||||
import org.springframework.ws.soap.SoapEnvelope;
|
||||
import org.springframework.ws.soap.SoapMessage;
|
||||
import org.springframework.ws.soap.SoapVersion;
|
||||
import org.springframework.ws.soap.axiom.support.AxiomUtils;
|
||||
import org.springframework.ws.soap.support.SoapUtils;
|
||||
import org.springframework.ws.stream.StreamingPayload;
|
||||
import org.springframework.ws.stream.StreamingWebServiceMessage;
|
||||
@@ -48,6 +49,7 @@ import org.apache.axiom.soap.SOAPEnvelope;
|
||||
import org.apache.axiom.soap.SOAPFactory;
|
||||
import org.apache.axiom.soap.SOAPMessage;
|
||||
import org.apache.axiom.soap.SOAPProcessingException;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
/**
|
||||
* AXIOM-specific implementation of the {@link SoapMessage} interface. Created via the {@link AxiomSoapMessageFactory},
|
||||
@@ -195,6 +197,23 @@ public class AxiomSoapMessage extends AbstractSoapMessage implements StreamingWe
|
||||
this.soapAction = soapAction;
|
||||
}
|
||||
|
||||
public Document getDocument() {
|
||||
return AxiomUtils.toDocument(axiomMessage.getSOAPEnvelope());
|
||||
}
|
||||
|
||||
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);
|
||||
// restore the Soap Action
|
||||
setSoapAction(soapAction);
|
||||
}
|
||||
|
||||
public boolean isXopPackage() {
|
||||
try {
|
||||
return MTOMConstants.MTOM_TYPE.equals(attachments.getAttachmentSpecType());
|
||||
|
||||
@@ -135,10 +135,7 @@ public abstract class AxiomUtils {
|
||||
}
|
||||
}
|
||||
catch (Exception ex) {
|
||||
IllegalArgumentException iaex =
|
||||
new IllegalArgumentException("Error in converting SOAP Envelope to Document");
|
||||
iaex.initCause(ex);
|
||||
throw iaex;
|
||||
throw new IllegalArgumentException("Error in converting SOAP Envelope to Document", ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,11 +16,14 @@
|
||||
|
||||
package org.springframework.ws.soap.saaj;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.Iterator;
|
||||
import javax.activation.DataHandler;
|
||||
import javax.xml.soap.AttachmentPart;
|
||||
import javax.xml.soap.MessageFactory;
|
||||
import javax.xml.soap.MimeHeaders;
|
||||
import javax.xml.soap.SOAPBody;
|
||||
import javax.xml.soap.SOAPElement;
|
||||
@@ -41,6 +44,12 @@ import org.springframework.ws.soap.saaj.support.SaajUtils;
|
||||
import org.springframework.ws.soap.support.SoapUtils;
|
||||
import org.springframework.ws.transport.TransportConstants;
|
||||
|
||||
import org.w3c.dom.DOMImplementation;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.ls.DOMImplementationLS;
|
||||
import org.w3c.dom.ls.LSOutput;
|
||||
import org.w3c.dom.ls.LSSerializer;
|
||||
|
||||
/**
|
||||
* SAAJ-specific implementation of the {@link SoapMessage} interface. Created via the {@link SaajSoapMessageFactory},
|
||||
* wraps a {@link SOAPMessage}.
|
||||
@@ -53,6 +62,8 @@ public class SaajSoapMessage extends AbstractSoapMessage {
|
||||
|
||||
private static final String CONTENT_TYPE_XOP = "application/xop+xml";
|
||||
|
||||
private final MessageFactory messageFactory;
|
||||
|
||||
private SOAPMessage saajMessage;
|
||||
|
||||
private SoapEnvelope envelope;
|
||||
@@ -67,7 +78,17 @@ public class SaajSoapMessage extends AbstractSoapMessage {
|
||||
* @param soapMessage the SAAJ SOAPMessage
|
||||
*/
|
||||
public SaajSoapMessage(SOAPMessage soapMessage) {
|
||||
this(soapMessage, true);
|
||||
this(soapMessage, true, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new <code>SaajSoapMessage</code> based on the given SAAJ <code>SOAPMessage</code>.
|
||||
*
|
||||
* @param soapMessage the SAAJ SOAPMessage
|
||||
* @param messageFactory the SAAJ message factory
|
||||
*/
|
||||
public SaajSoapMessage(SOAPMessage soapMessage, MessageFactory messageFactory) {
|
||||
this(soapMessage, true, messageFactory);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -78,6 +99,18 @@ public class SaajSoapMessage extends AbstractSoapMessage {
|
||||
* whether a {@code xml:lang} attribute is allowed on SOAP 1.1 {@code <faultstring>} elements
|
||||
*/
|
||||
public SaajSoapMessage(SOAPMessage soapMessage, boolean langAttributeOnSoap11FaultString) {
|
||||
this(soapMessage, langAttributeOnSoap11FaultString, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new <code>SaajSoapMessage</code> based on the given SAAJ <code>SOAPMessage</code>.
|
||||
*
|
||||
* @param soapMessage the SAAJ SOAPMessage
|
||||
* @param langAttributeOnSoap11FaultString
|
||||
* whether a {@code xml:lang} attribute is allowed on SOAP 1.1 {@code <faultstring>} elements
|
||||
* @param messageFactory the message factory
|
||||
*/
|
||||
public SaajSoapMessage(SOAPMessage soapMessage, boolean langAttributeOnSoap11FaultString, MessageFactory messageFactory) {
|
||||
Assert.notNull(soapMessage, "soapMessage must not be null");
|
||||
saajMessage = soapMessage;
|
||||
this.langAttributeOnSoap11FaultString = langAttributeOnSoap11FaultString;
|
||||
@@ -85,6 +118,7 @@ public class SaajSoapMessage extends AbstractSoapMessage {
|
||||
if (ObjectUtils.isEmpty(headers.getHeader(TransportConstants.HEADER_SOAP_ACTION))) {
|
||||
headers.addHeader(TransportConstants.HEADER_SOAP_ACTION, "\"\"");
|
||||
}
|
||||
this.messageFactory = messageFactory;
|
||||
}
|
||||
|
||||
/** Return the SAAJ <code>SOAPMessage</code> that this <code>SaajSoapMessage</code> is based on. */
|
||||
@@ -156,6 +190,54 @@ public class SaajSoapMessage extends AbstractSoapMessage {
|
||||
|
||||
}
|
||||
|
||||
public Document getDocument() {
|
||||
Assert.state(messageFactory != null, "Could find message factory to use");
|
||||
// return saajSoapMessage.getSaajMessage().getSOAPPart(); // does not work, see SWS-345
|
||||
try {
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
getSaajMessage().writeTo(bos);
|
||||
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
|
||||
SOAPMessage saajMessage = messageFactory.createMessage(getSaajMessage().getMimeHeaders(), bis);
|
||||
setSaajMessage(saajMessage);
|
||||
return saajMessage.getSOAPPart();
|
||||
}
|
||||
catch (SOAPException ex) {
|
||||
throw new SaajSoapMessageException("Could not save changes", ex);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new SaajSoapMessageException("Could not save changes", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void setDocument(Document document) {
|
||||
if (saajMessage.getSOAPPart() != document) {
|
||||
Assert.state(messageFactory != null, "Could find message factory to use");
|
||||
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());
|
||||
|
||||
this.saajMessage = messageFactory.createMessage(saajMessage.getMimeHeaders(), bis);
|
||||
|
||||
}
|
||||
catch (SOAPException ex) {
|
||||
throw new SaajSoapMessageException("Could not read input stream", ex);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new SaajSoapMessageException("Could not read input stream", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void writeTo(OutputStream outputStream) throws IOException {
|
||||
MimeHeaders mimeHeaders = getImplementation().getMimeHeaders(getSaajMessage());
|
||||
if (ObjectUtils.isEmpty(mimeHeaders.getHeader(TransportConstants.HEADER_ACCEPT))) {
|
||||
|
||||
@@ -168,7 +168,7 @@ public class SaajSoapMessageFactory implements SoapMessageFactory<SaajSoapMessag
|
||||
try {
|
||||
SOAPMessage saajMessage = messageFactory.createMessage();
|
||||
postProcess(saajMessage);
|
||||
return new SaajSoapMessage(saajMessage, langAttributeOnSoap11FaultString);
|
||||
return new SaajSoapMessage(saajMessage, langAttributeOnSoap11FaultString, messageFactory);
|
||||
}
|
||||
catch (SOAPException ex) {
|
||||
throw new SoapMessageCreationException("Could not create empty message: " + ex.getMessage(), ex);
|
||||
@@ -181,7 +181,7 @@ public class SaajSoapMessageFactory implements SoapMessageFactory<SaajSoapMessag
|
||||
inputStream = checkForUtf8ByteOrderMark(inputStream);
|
||||
SOAPMessage saajMessage = messageFactory.createMessage(mimeHeaders, inputStream);
|
||||
postProcess(saajMessage);
|
||||
return new SaajSoapMessage(saajMessage);
|
||||
return new SaajSoapMessage(saajMessage, langAttributeOnSoap11FaultString, messageFactory);
|
||||
}
|
||||
catch (SOAPException ex) {
|
||||
// SAAJ 1.3 RI has a issue with handling multipart XOP content types which contain "startinfo" rather than
|
||||
|
||||
@@ -130,4 +130,14 @@ public abstract class AbstractSoapMessageTestCase extends AbstractMimeMessageTes
|
||||
@Test
|
||||
public abstract void testWriteToTransportResponseAttachment() throws Exception;
|
||||
|
||||
@Test
|
||||
public abstract void testToDocument() throws Exception;
|
||||
|
||||
@Test
|
||||
public abstract void testSetLiveDocument() throws Exception;
|
||||
|
||||
@Test
|
||||
public abstract void testSetOtherDocument() throws Exception;
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ public class SaajSoap11MessageTest extends AbstractSoap11MessageTestCase {
|
||||
MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
|
||||
saajMessage = messageFactory.createMessage();
|
||||
saajMessage.getSOAPHeader().detachNode();
|
||||
return new SaajSoapMessage(saajMessage);
|
||||
return new SaajSoapMessage(saajMessage, true, messageFactory);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -40,7 +40,7 @@ public class SaajSoap12MessageTest extends AbstractSoap12MessageTestCase {
|
||||
MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
|
||||
saajMessage = messageFactory.createMessage();
|
||||
saajMessage.getSOAPHeader().detachNode();
|
||||
return new SaajSoapMessage(saajMessage);
|
||||
return new SaajSoapMessage(saajMessage, true, messageFactory);
|
||||
}
|
||||
|
||||
public void testGetPayloadSource() throws Exception {
|
||||
|
||||
@@ -16,7 +16,12 @@
|
||||
|
||||
package org.springframework.ws.soap.soap11;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.transform.dom.DOMResult;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
@@ -29,6 +34,8 @@ import org.springframework.ws.transport.MockTransportOutputStream;
|
||||
import org.springframework.xml.transform.StringSource;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
|
||||
import static org.junit.Assert.*;
|
||||
@@ -81,4 +88,69 @@ public abstract class AbstractSoap11MessageTestCase extends AbstractSoapMessageT
|
||||
assertTrue("Content-Type for attachment message does not contains type=\"text/xml\"",
|
||||
contentType.indexOf("type=\"text/xml\"") != -1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void testToDocument() throws Exception {
|
||||
transformer.transform(new StringSource("<payload xmlns='http://www.springframework.org' />"),
|
||||
soapMessage.getSoapBody().getPayloadResult());
|
||||
|
||||
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||
documentBuilderFactory.setNamespaceAware(true);
|
||||
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
|
||||
Document expected = documentBuilder.newDocument();
|
||||
Element envelope = expected.createElementNS("http://schemas.xmlsoap.org/soap/envelope/", "Envelope");
|
||||
expected.appendChild(envelope);
|
||||
Element body = expected.createElementNS("http://schemas.xmlsoap.org/soap/envelope/", "Body");
|
||||
envelope.appendChild(body);
|
||||
Element payload = expected.createElementNS("http://www.springframework.org", "payload");
|
||||
body.appendChild(payload);
|
||||
|
||||
Document result = soapMessage.getDocument();
|
||||
|
||||
assertXMLEqual(expected, result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void testSetLiveDocument() throws Exception {
|
||||
transformer.transform(new StringSource("<payload xmlns='http://www.springframework.org' />"),
|
||||
soapMessage.getSoapBody().getPayloadResult());
|
||||
|
||||
Document document = soapMessage.getDocument();
|
||||
|
||||
soapMessage.setDocument(document);
|
||||
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
soapMessage.writeTo(bos);
|
||||
|
||||
String result = bos.toString("UTF-8");
|
||||
assertXMLEqual(
|
||||
"<Envelope xmlns='http://schemas.xmlsoap.org/soap/envelope/'><Body><payload xmlns='http://www.springframework.org' /></Body></Envelope>",
|
||||
result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void testSetOtherDocument() throws Exception {
|
||||
transformer.transform(new StringSource("<payload xmlns='http://www.springframework.org' />"),
|
||||
soapMessage.getSoapBody().getPayloadResult());
|
||||
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
soapMessage.writeTo(bos);
|
||||
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
|
||||
|
||||
DOMResult domResult = new DOMResult();
|
||||
transformer.transform(new StreamSource(bis), domResult);
|
||||
|
||||
Document document = (Document) domResult.getNode();
|
||||
|
||||
soapMessage.setDocument(document);
|
||||
|
||||
bos = new ByteArrayOutputStream();
|
||||
soapMessage.writeTo(bos);
|
||||
|
||||
String result = bos.toString("UTF-8");
|
||||
assertXMLEqual(
|
||||
"<Envelope xmlns='http://schemas.xmlsoap.org/soap/envelope/'><Body><payload xmlns='http://www.springframework.org' /></Body></Envelope>",
|
||||
result);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,7 +16,12 @@
|
||||
|
||||
package org.springframework.ws.soap.soap12;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.transform.dom.DOMResult;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
@@ -30,6 +35,8 @@ import org.springframework.ws.transport.TransportConstants;
|
||||
import org.springframework.xml.transform.StringSource;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
|
||||
import static org.junit.Assert.*;
|
||||
@@ -85,4 +92,71 @@ public abstract class AbstractSoap12MessageTestCase extends AbstractSoapMessageT
|
||||
contentType.indexOf("type=\"application/soap+xml\"") != -1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void testToDocument() throws Exception {
|
||||
transformer.transform(new StringSource("<payload xmlns='http://www.springframework.org' />"),
|
||||
soapMessage.getSoapBody().getPayloadResult());
|
||||
|
||||
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||
documentBuilderFactory.setNamespaceAware(true);
|
||||
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
|
||||
Document expected = documentBuilder.newDocument();
|
||||
Element envelope = expected.createElementNS("http://www.w3.org/2003/05/soap-envelope", "Envelope");
|
||||
expected.appendChild(envelope);
|
||||
Element body = expected.createElementNS("http://www.w3.org/2003/05/soap-envelope", "Body");
|
||||
envelope.appendChild(body);
|
||||
Element payload = expected.createElementNS("http://www.springframework.org", "payload");
|
||||
body.appendChild(payload);
|
||||
|
||||
Document result = soapMessage.getDocument();
|
||||
|
||||
assertXMLEqual(expected, result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void testSetLiveDocument() throws Exception {
|
||||
transformer.transform(new StringSource("<payload xmlns='http://www.springframework.org' />"),
|
||||
soapMessage.getSoapBody().getPayloadResult());
|
||||
|
||||
Document document = soapMessage.getDocument();
|
||||
|
||||
soapMessage.setDocument(document);
|
||||
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
soapMessage.writeTo(bos);
|
||||
|
||||
String result = bos.toString("UTF-8");
|
||||
assertXMLEqual(
|
||||
"<Envelope xmlns='http://www.w3.org/2003/05/soap-envelope'><Body><payload xmlns='http://www.springframework.org' /></Body></Envelope>",
|
||||
result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void testSetOtherDocument() throws Exception {
|
||||
transformer.transform(new StringSource("<payload xmlns='http://www.springframework.org' />"),
|
||||
soapMessage.getSoapBody().getPayloadResult());
|
||||
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
soapMessage.writeTo(bos);
|
||||
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
|
||||
|
||||
DOMResult domResult = new DOMResult();
|
||||
transformer.transform(new StreamSource(bis), domResult);
|
||||
|
||||
Document document = (Document) domResult.getNode();
|
||||
|
||||
soapMessage.setDocument(document);
|
||||
|
||||
bos = new ByteArrayOutputStream();
|
||||
soapMessage.writeTo(bos);
|
||||
|
||||
String result = bos.toString("UTF-8");
|
||||
assertXMLEqual(
|
||||
"<Envelope xmlns='http://www.w3.org/2003/05/soap-envelope'><Body><payload xmlns='http://www.springframework.org' /></Body></Envelope>",
|
||||
result);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user