From 1545bd2c608f0384bf3ed48df9f27c17c9c37510 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Fri, 26 Sep 2008 13:49:56 +0000 Subject: [PATCH] SWS-420 --- .../ws/soap/AbstractSoapMessage.java | 1 - .../ws/soap/axiom/AxiomSoapMessage.java | 18 ++-- .../soap/axiom/AxiomSoapMessageFactory.java | 9 +- .../ws/soap/saaj/SaajSoapMessage.java | 46 +++++++-- .../ws/soap/support/SoapUtils.java | 96 +++++++++++++++++++ .../ws/soap/support/package.html | 5 + .../ws/soap/support/SoapUtilsTest.java | 39 ++++++++ 7 files changed, 190 insertions(+), 24 deletions(-) create mode 100644 core/src/main/java/org/springframework/ws/soap/support/SoapUtils.java create mode 100644 core/src/main/java/org/springframework/ws/soap/support/package.html create mode 100644 core/src/test/java/org/springframework/ws/soap/support/SoapUtilsTest.java diff --git a/core/src/main/java/org/springframework/ws/soap/AbstractSoapMessage.java b/core/src/main/java/org/springframework/ws/soap/AbstractSoapMessage.java index d6076474..4f5d50a5 100644 --- a/core/src/main/java/org/springframework/ws/soap/AbstractSoapMessage.java +++ b/core/src/main/java/org/springframework/ws/soap/AbstractSoapMessage.java @@ -82,5 +82,4 @@ public abstract class AbstractSoapMessage extends AbstractMimeMessage implements } return version; } - } diff --git a/core/src/main/java/org/springframework/ws/soap/axiom/AxiomSoapMessage.java b/core/src/main/java/org/springframework/ws/soap/axiom/AxiomSoapMessage.java index 2ee84cb0..966037b3 100644 --- a/core/src/main/java/org/springframework/ws/soap/axiom/AxiomSoapMessage.java +++ b/core/src/main/java/org/springframework/ws/soap/axiom/AxiomSoapMessage.java @@ -43,6 +43,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.support.SoapUtils; import org.springframework.ws.transport.TransportConstants; import org.springframework.ws.transport.TransportOutputStream; @@ -162,15 +163,7 @@ public class AxiomSoapMessage extends AbstractSoapMessage { } public void setSoapAction(String soapAction) { - if (soapAction == null) { - soapAction = EMPTY_SOAP_ACTION; - } - if (!soapAction.startsWith("\"")) { - soapAction = "\"" + soapAction; - } - if (!soapAction.endsWith("\"")) { - soapAction = soapAction + "\""; - } + soapAction = SoapUtils.escapeAction(soapAction); this.soapAction = soapAction; } @@ -230,8 +223,13 @@ public class AxiomSoapMessage extends AbstractSoapMessage { if (!hasAttachments) { contentType += "; charset=\"" + charsetEncoding + "\""; } + if (SoapVersion.SOAP_11 == getVersion()) { + transportOutputStream.addHeader(TransportConstants.HEADER_SOAP_ACTION, soapAction); + } + else if (SoapVersion.SOAP_12 == getVersion()) { + contentType += "; action=" + soapAction; + } transportOutputStream.addHeader(TransportConstants.HEADER_CONTENT_TYPE, contentType); - transportOutputStream.addHeader(TransportConstants.HEADER_SOAP_ACTION, soapAction); } if (!(format.isOptimized()) & format.isDoingSWA()) { writeSwAMessage(outputStream, format); diff --git a/core/src/main/java/org/springframework/ws/soap/axiom/AxiomSoapMessageFactory.java b/core/src/main/java/org/springframework/ws/soap/axiom/AxiomSoapMessageFactory.java index b2fd69f1..8773fcb6 100644 --- a/core/src/main/java/org/springframework/ws/soap/axiom/AxiomSoapMessageFactory.java +++ b/core/src/main/java/org/springframework/ws/soap/axiom/AxiomSoapMessageFactory.java @@ -48,6 +48,7 @@ import org.springframework.ws.soap.SoapMessageFactory; import org.springframework.ws.soap.SoapVersion; import org.springframework.ws.soap.server.endpoint.interceptor.PayloadValidatingInterceptor; import org.springframework.ws.soap.server.endpoint.mapping.SoapActionEndpointMapping; +import org.springframework.ws.soap.support.SoapUtils; import org.springframework.ws.transport.TransportConstants; import org.springframework.ws.transport.TransportInputStream; @@ -179,15 +180,17 @@ public class AxiomSoapMessageFactory implements SoapMessageFactory, Initializing } public WebServiceMessage createWebServiceMessage(InputStream inputStream) throws IOException { - if (!(inputStream instanceof TransportInputStream)) { - throw new IllegalArgumentException("AxiomSoapMessageFactory requires a TransportInputStream"); - } + Assert.isInstanceOf(TransportInputStream.class, inputStream, + "AxiomSoapMessageFactory requires a TransportInputStream"); TransportInputStream transportInputStream = (TransportInputStream) inputStream; String contentType = getHeaderValue(transportInputStream, TransportConstants.HEADER_CONTENT_TYPE); if (!StringUtils.hasLength(contentType)) { throw new IllegalArgumentException("TransportInputStream contains no Content-Type header"); } String soapAction = getHeaderValue(transportInputStream, TransportConstants.HEADER_SOAP_ACTION); + if (!StringUtils.hasLength(soapAction)) { + soapAction = SoapUtils.extractActionFromContentType(contentType); + } try { if (isMultiPartRelated(contentType)) { return createMultiPartAxiomSoapMessage(inputStream, contentType, soapAction); diff --git a/core/src/main/java/org/springframework/ws/soap/saaj/SaajSoapMessage.java b/core/src/main/java/org/springframework/ws/soap/saaj/SaajSoapMessage.java index 45e249f6..9d7dfeb1 100644 --- a/core/src/main/java/org/springframework/ws/soap/saaj/SaajSoapMessage.java +++ b/core/src/main/java/org/springframework/ws/soap/saaj/SaajSoapMessage.java @@ -36,7 +36,9 @@ import org.springframework.ws.mime.AttachmentException; 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.saaj.support.SaajUtils; +import org.springframework.ws.soap.support.SoapUtils; import org.springframework.ws.transport.TransportConstants; /** @@ -96,22 +98,46 @@ public class SaajSoapMessage extends AbstractSoapMessage { public String getSoapAction() { MimeHeaders mimeHeaders = getImplementation().getMimeHeaders(getSaajMessage()); - String[] values = mimeHeaders.getHeader(TransportConstants.HEADER_SOAP_ACTION); - return ObjectUtils.isEmpty(values) ? "" : values[0]; + if (SoapVersion.SOAP_11 == getVersion()) { + String[] actions = mimeHeaders.getHeader(TransportConstants.HEADER_SOAP_ACTION); + return ObjectUtils.isEmpty(actions) ? TransportConstants.EMPTY_SOAP_ACTION : actions[0]; + } + else if (SoapVersion.SOAP_12 == getVersion()) { + String[] contentTypes = mimeHeaders.getHeader(TransportConstants.HEADER_CONTENT_TYPE); + return !ObjectUtils.isEmpty(contentTypes) ? SoapUtils.extractActionFromContentType(contentTypes[0]) : + TransportConstants.EMPTY_SOAP_ACTION; + } + else { + throw new IllegalStateException("Unsupported SOAP version: " + getVersion()); + } } public void setSoapAction(String soapAction) { - if (soapAction == null) { - soapAction = ""; - } MimeHeaders mimeHeaders = getImplementation().getMimeHeaders(getSaajMessage()); - if (!soapAction.startsWith("\"")) { - soapAction = "\"" + soapAction; + soapAction = SoapUtils.escapeAction(soapAction); + if (SoapVersion.SOAP_11 == getVersion()) { + mimeHeaders.setHeader(TransportConstants.HEADER_SOAP_ACTION, soapAction); } - if (!soapAction.endsWith("\"")) { - soapAction = soapAction + "\""; + else if (SoapVersion.SOAP_12 == getVersion()) { + // force save of Content Type header + if (saajMessage.saveRequired()) { + try { + saajMessage.saveChanges(); + } + catch (SOAPException ex) { + throw new SaajSoapMessageException("Could not save message", ex); + } + } + String[] contentTypes = mimeHeaders.getHeader(TransportConstants.HEADER_CONTENT_TYPE); + String contentType = !ObjectUtils.isEmpty(contentTypes) ? contentTypes[0] : getVersion().getContentType(); + contentType = SoapUtils.setActionInContentType(contentType, soapAction); + mimeHeaders.setHeader(TransportConstants.HEADER_CONTENT_TYPE, contentType); + mimeHeaders.removeHeader(TransportConstants.HEADER_SOAP_ACTION); } - mimeHeaders.setHeader(TransportConstants.HEADER_SOAP_ACTION, soapAction); + else { + throw new IllegalStateException("Unsupported SOAP version: " + getVersion()); + } + } public void writeTo(OutputStream outputStream) throws IOException { diff --git a/core/src/main/java/org/springframework/ws/soap/support/SoapUtils.java b/core/src/main/java/org/springframework/ws/soap/support/SoapUtils.java new file mode 100644 index 00000000..7ea0aa48 --- /dev/null +++ b/core/src/main/java/org/springframework/ws/soap/support/SoapUtils.java @@ -0,0 +1,96 @@ +/* + * Copyright 2008 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ws.soap.support; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.springframework.util.Assert; +import org.springframework.util.StringUtils; +import org.springframework.ws.transport.TransportConstants; + +/** + * Contains various utility methods for handling SOAP messages. + * + * @author Arjen Poutsma + * @since 1.5.5 + */ +public abstract class SoapUtils { + + private static final Pattern ACTION_PATTERN = Pattern.compile("action\\s*=\\s*([^;]+)"); + + private SoapUtils() { + } + + /** Escapes the given SOAP action to be surrounded by quotes. */ + public static String escapeAction(String soapAction) { + if (soapAction == null) { + soapAction = ""; + } + if (!soapAction.startsWith("\"")) { + soapAction = "\"" + soapAction; + } + if (!soapAction.endsWith("\"")) { + soapAction = soapAction + "\""; + } + return soapAction; + } + + /** + * Returns the value of the action parameter in the given SOAP 1.2 content type. + * + * @param contentType the SOAP 1.2 content type + * @return the action + */ + public static String extractActionFromContentType(String contentType) { + if (contentType != null) { + Matcher matcher = ACTION_PATTERN.matcher(contentType); + if (matcher.find() && matcher.groupCount() == 1) { + return matcher.group(1).trim(); + } + } + return TransportConstants.EMPTY_SOAP_ACTION; + } + + /** + * Replaces or adds the value of the action parameter in the given SOAP 1.2 content type. + * + * @param contentType the SOAP 1.2 content type + * @param action the action + * @return the new content type + */ + public static String setActionInContentType(String contentType, String action) { + Assert.hasLength(contentType, "'contentType' must not be empty"); + if (StringUtils.hasText(action)) { + Matcher matcher = ACTION_PATTERN.matcher(contentType); + if (matcher.find() && matcher.groupCount() == 1) { + StringBuffer buffer = new StringBuffer(); + matcher.appendReplacement(buffer, action); + matcher.appendTail(buffer); + return buffer.toString(); + } + else { + return contentType + "; action=" + action; + } + } + else { + return contentType; + } + } + + +} diff --git a/core/src/main/java/org/springframework/ws/soap/support/package.html b/core/src/main/java/org/springframework/ws/soap/support/package.html new file mode 100644 index 00000000..a1f2543c --- /dev/null +++ b/core/src/main/java/org/springframework/ws/soap/support/package.html @@ -0,0 +1,5 @@ + + +Classes supporting the org.springframework.ws.soap package. + + \ No newline at end of file diff --git a/core/src/test/java/org/springframework/ws/soap/support/SoapUtilsTest.java b/core/src/test/java/org/springframework/ws/soap/support/SoapUtilsTest.java new file mode 100644 index 00000000..83f9f801 --- /dev/null +++ b/core/src/test/java/org/springframework/ws/soap/support/SoapUtilsTest.java @@ -0,0 +1,39 @@ +/* + * Copyright 2008 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ws.soap.support; + +import junit.framework.TestCase; + +public class SoapUtilsTest extends TestCase { + + public void testExtractActionFromContentType() throws Exception { + String soapAction = "http://springframework.org/spring-ws/Action"; + + String contentType = "application/soap+xml; action=" + soapAction; + String result = SoapUtils.extractActionFromContentType(contentType); + assertEquals("Invalid SOAP action", soapAction, result); + + contentType = "application/soap+xml; action = " + soapAction; + result = SoapUtils.extractActionFromContentType(contentType); + assertEquals("Invalid SOAP action", soapAction, result); + + contentType = "application/soap+xml; action=" + soapAction + " ; charset=UTF-8"; + result = SoapUtils.extractActionFromContentType(contentType); + assertEquals("Invalid SOAP action", soapAction, result); + } + +} \ No newline at end of file