Addes setDocument/getDocument to SoapMessage, for use in Wss4jSecurityInterceptor

This commit is contained in:
Arjen Poutsma
2010-09-13 11:48:09 +00:00
parent 9e2a646aed
commit aeee118f96
15 changed files with 408 additions and 87 deletions

View File

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

View File

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

View File

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

View File

@@ -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))) {

View File

@@ -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