Skip to content

Commit 6795904

Browse files
committed
Add support for customizing AxiomSoapMessage#writeTo
This commit introduces a protected method that allows to customize the way an AxiomSoapMessage is written. The content type to use is separated, and a method that takes the current OMOutputFormat has been added. Closes gh-540
1 parent c823957 commit 6795904

File tree

1 file changed

+51
-32
lines changed

1 file changed

+51
-32
lines changed

spring-ws-core/src/main/java/org/springframework/ws/soap/axiom/AxiomSoapMessage.java

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -262,46 +262,65 @@ public Attachment addAttachment(String contentId, DataHandler dataHandler) {
262262
@Override
263263
public void writeTo(OutputStream outputStream) throws IOException {
264264
try {
265+
writeTo(outputStream, getOutputFormat());
266+
}
267+
catch (XMLStreamException | OMException ex) {
268+
throw new AxiomSoapMessageException("Could not write message to OutputStream: " + ex.getMessage(), ex);
269+
}
270+
}
265271

266-
OMOutputFormat outputFormat = getOutputFormat();
267-
if (outputStream instanceof TransportOutputStream) {
268-
TransportOutputStream transportOutputStream = (TransportOutputStream) outputStream;
269-
String contentType = outputFormat.getContentType();
270-
if (!(outputFormat.isDoingSWA() || outputFormat.isOptimized())) {
271-
String charsetEncoding = this.axiomMessage.getCharsetEncoding();
272-
contentType += "; charset=" + charsetEncoding;
273-
}
274-
SoapVersion version = getVersion();
275-
if (SoapVersion.SOAP_11 == version) {
276-
transportOutputStream.addHeader(TransportConstants.HEADER_SOAP_ACTION, this.soapAction);
277-
transportOutputStream.addHeader(TransportConstants.HEADER_ACCEPT, version.getContentType());
278-
}
279-
else if (SoapVersion.SOAP_12 == version) {
280-
contentType += "; action=" + this.soapAction;
281-
transportOutputStream.addHeader(TransportConstants.HEADER_ACCEPT, version.getContentType());
282-
}
283-
transportOutputStream.addHeader(TransportConstants.HEADER_CONTENT_TYPE, contentType);
284-
272+
/**
273+
* Writes the entire message to the given output stream using the given
274+
* {@link OMOutputFormat}.
275+
* @param outputStream the stream to write to
276+
* @param outputFormat the {@link OMOutputFormat}
277+
* @since 4.1.0
278+
*/
279+
protected void writeTo(OutputStream outputStream, OMOutputFormat outputFormat)
280+
throws IOException, XMLStreamException {
281+
if (outputStream instanceof TransportOutputStream transportOutputStream) {
282+
SoapVersion version = getVersion();
283+
String contentType = determineContentType(outputFormat, version);
284+
if (SoapVersion.SOAP_11 == version) {
285+
transportOutputStream.addHeader(TransportConstants.HEADER_SOAP_ACTION, this.soapAction);
286+
transportOutputStream.addHeader(TransportConstants.HEADER_ACCEPT, version.getContentType());
287+
}
288+
else if (SoapVersion.SOAP_12 == version) {
289+
transportOutputStream.addHeader(TransportConstants.HEADER_ACCEPT, version.getContentType());
285290
}
286-
if (!(outputFormat.isOptimized()) & outputFormat.isDoingSWA()) {
287-
writeSwAMessage(outputStream, outputFormat);
291+
transportOutputStream.addHeader(TransportConstants.HEADER_CONTENT_TYPE, contentType);
292+
}
293+
if (!(outputFormat.isOptimized()) & outputFormat.isDoingSWA()) {
294+
writeSwAMessage(outputStream, outputFormat);
295+
}
296+
else {
297+
if (this.payloadCaching) {
298+
this.axiomMessage.serialize(outputStream, outputFormat);
288299
}
289300
else {
290-
if (this.payloadCaching) {
291-
this.axiomMessage.serialize(outputStream, outputFormat);
292-
}
293-
else {
294-
this.axiomMessage.serializeAndConsume(outputStream, outputFormat);
295-
}
301+
this.axiomMessage.serializeAndConsume(outputStream, outputFormat);
296302
}
297-
outputStream.flush();
298303
}
299-
catch (XMLStreamException ex) {
300-
throw new AxiomSoapMessageException("Could not write message to OutputStream: " + ex.getMessage(), ex);
304+
outputStream.flush();
305+
}
306+
307+
/**
308+
* Determine the {@value TransportConstants#HEADER_CONTENT_TYPE} header to use.
309+
* @param outputFormat the {@link OMOutputFormat}
310+
* @param soapVersion the {@link SoapVersion}
311+
* @return the content-type to use
312+
* @since 4.1.0
313+
*/
314+
protected String determineContentType(OMOutputFormat outputFormat, SoapVersion soapVersion) {
315+
String contentType = outputFormat.getContentType();
316+
if (!(outputFormat.isDoingSWA() || outputFormat.isOptimized())) {
317+
String charsetEncoding = this.axiomMessage.getCharsetEncoding();
318+
contentType += "; charset=" + charsetEncoding;
301319
}
302-
catch (OMException ex) {
303-
throw new AxiomSoapMessageException("Could not write message to OutputStream: " + ex.getMessage(), ex);
320+
if (SoapVersion.SOAP_12 == soapVersion) {
321+
contentType += "; action=" + this.soapAction;
304322
}
323+
return contentType;
305324
}
306325

307326
private OMOutputFormat getOutputFormat() {

0 commit comments

Comments
 (0)