Skip to content

Commit 1545bd2

Browse files
committed
SWS-420
1 parent 6674f34 commit 1545bd2

File tree

7 files changed

+190
-24
lines changed

7 files changed

+190
-24
lines changed

core/src/main/java/org/springframework/ws/soap/AbstractSoapMessage.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,4 @@ else if (SoapVersion.SOAP_12.getEnvelopeNamespaceUri().equals(envelopeNamespace)
8282
}
8383
return version;
8484
}
85-
8685
}

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.springframework.ws.soap.SoapEnvelope;
4444
import org.springframework.ws.soap.SoapMessage;
4545
import org.springframework.ws.soap.SoapVersion;
46+
import org.springframework.ws.soap.support.SoapUtils;
4647
import org.springframework.ws.transport.TransportConstants;
4748
import org.springframework.ws.transport.TransportOutputStream;
4849

@@ -162,15 +163,7 @@ public String getSoapAction() {
162163
}
163164

164165
public void setSoapAction(String soapAction) {
165-
if (soapAction == null) {
166-
soapAction = EMPTY_SOAP_ACTION;
167-
}
168-
if (!soapAction.startsWith("\"")) {
169-
soapAction = "\"" + soapAction;
170-
}
171-
if (!soapAction.endsWith("\"")) {
172-
soapAction = soapAction + "\"";
173-
}
166+
soapAction = SoapUtils.escapeAction(soapAction);
174167
this.soapAction = soapAction;
175168
}
176169

@@ -230,8 +223,13 @@ public void writeTo(OutputStream outputStream) throws IOException {
230223
if (!hasAttachments) {
231224
contentType += "; charset=\"" + charsetEncoding + "\"";
232225
}
226+
if (SoapVersion.SOAP_11 == getVersion()) {
227+
transportOutputStream.addHeader(TransportConstants.HEADER_SOAP_ACTION, soapAction);
228+
}
229+
else if (SoapVersion.SOAP_12 == getVersion()) {
230+
contentType += "; action=" + soapAction;
231+
}
233232
transportOutputStream.addHeader(TransportConstants.HEADER_CONTENT_TYPE, contentType);
234-
transportOutputStream.addHeader(TransportConstants.HEADER_SOAP_ACTION, soapAction);
235233
}
236234
if (!(format.isOptimized()) & format.isDoingSWA()) {
237235
writeSwAMessage(outputStream, format);

core/src/main/java/org/springframework/ws/soap/axiom/AxiomSoapMessageFactory.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.springframework.ws.soap.SoapVersion;
4949
import org.springframework.ws.soap.server.endpoint.interceptor.PayloadValidatingInterceptor;
5050
import org.springframework.ws.soap.server.endpoint.mapping.SoapActionEndpointMapping;
51+
import org.springframework.ws.soap.support.SoapUtils;
5152
import org.springframework.ws.transport.TransportConstants;
5253
import org.springframework.ws.transport.TransportInputStream;
5354

@@ -179,15 +180,17 @@ public WebServiceMessage createWebServiceMessage() {
179180
}
180181

181182
public WebServiceMessage createWebServiceMessage(InputStream inputStream) throws IOException {
182-
if (!(inputStream instanceof TransportInputStream)) {
183-
throw new IllegalArgumentException("AxiomSoapMessageFactory requires a TransportInputStream");
184-
}
183+
Assert.isInstanceOf(TransportInputStream.class, inputStream,
184+
"AxiomSoapMessageFactory requires a TransportInputStream");
185185
TransportInputStream transportInputStream = (TransportInputStream) inputStream;
186186
String contentType = getHeaderValue(transportInputStream, TransportConstants.HEADER_CONTENT_TYPE);
187187
if (!StringUtils.hasLength(contentType)) {
188188
throw new IllegalArgumentException("TransportInputStream contains no Content-Type header");
189189
}
190190
String soapAction = getHeaderValue(transportInputStream, TransportConstants.HEADER_SOAP_ACTION);
191+
if (!StringUtils.hasLength(soapAction)) {
192+
soapAction = SoapUtils.extractActionFromContentType(contentType);
193+
}
191194
try {
192195
if (isMultiPartRelated(contentType)) {
193196
return createMultiPartAxiomSoapMessage(inputStream, contentType, soapAction);

core/src/main/java/org/springframework/ws/soap/saaj/SaajSoapMessage.java

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@
3636
import org.springframework.ws.soap.AbstractSoapMessage;
3737
import org.springframework.ws.soap.SoapEnvelope;
3838
import org.springframework.ws.soap.SoapMessage;
39+
import org.springframework.ws.soap.SoapVersion;
3940
import org.springframework.ws.soap.saaj.support.SaajUtils;
41+
import org.springframework.ws.soap.support.SoapUtils;
4042
import org.springframework.ws.transport.TransportConstants;
4143

4244
/**
@@ -96,22 +98,46 @@ public SoapEnvelope getEnvelope() {
9698

9799
public String getSoapAction() {
98100
MimeHeaders mimeHeaders = getImplementation().getMimeHeaders(getSaajMessage());
99-
String[] values = mimeHeaders.getHeader(TransportConstants.HEADER_SOAP_ACTION);
100-
return ObjectUtils.isEmpty(values) ? "" : values[0];
101+
if (SoapVersion.SOAP_11 == getVersion()) {
102+
String[] actions = mimeHeaders.getHeader(TransportConstants.HEADER_SOAP_ACTION);
103+
return ObjectUtils.isEmpty(actions) ? TransportConstants.EMPTY_SOAP_ACTION : actions[0];
104+
}
105+
else if (SoapVersion.SOAP_12 == getVersion()) {
106+
String[] contentTypes = mimeHeaders.getHeader(TransportConstants.HEADER_CONTENT_TYPE);
107+
return !ObjectUtils.isEmpty(contentTypes) ? SoapUtils.extractActionFromContentType(contentTypes[0]) :
108+
TransportConstants.EMPTY_SOAP_ACTION;
109+
}
110+
else {
111+
throw new IllegalStateException("Unsupported SOAP version: " + getVersion());
112+
}
101113
}
102114

103115
public void setSoapAction(String soapAction) {
104-
if (soapAction == null) {
105-
soapAction = "";
106-
}
107116
MimeHeaders mimeHeaders = getImplementation().getMimeHeaders(getSaajMessage());
108-
if (!soapAction.startsWith("\"")) {
109-
soapAction = "\"" + soapAction;
117+
soapAction = SoapUtils.escapeAction(soapAction);
118+
if (SoapVersion.SOAP_11 == getVersion()) {
119+
mimeHeaders.setHeader(TransportConstants.HEADER_SOAP_ACTION, soapAction);
110120
}
111-
if (!soapAction.endsWith("\"")) {
112-
soapAction = soapAction + "\"";
121+
else if (SoapVersion.SOAP_12 == getVersion()) {
122+
// force save of Content Type header
123+
if (saajMessage.saveRequired()) {
124+
try {
125+
saajMessage.saveChanges();
126+
}
127+
catch (SOAPException ex) {
128+
throw new SaajSoapMessageException("Could not save message", ex);
129+
}
130+
}
131+
String[] contentTypes = mimeHeaders.getHeader(TransportConstants.HEADER_CONTENT_TYPE);
132+
String contentType = !ObjectUtils.isEmpty(contentTypes) ? contentTypes[0] : getVersion().getContentType();
133+
contentType = SoapUtils.setActionInContentType(contentType, soapAction);
134+
mimeHeaders.setHeader(TransportConstants.HEADER_CONTENT_TYPE, contentType);
135+
mimeHeaders.removeHeader(TransportConstants.HEADER_SOAP_ACTION);
113136
}
114-
mimeHeaders.setHeader(TransportConstants.HEADER_SOAP_ACTION, soapAction);
137+
else {
138+
throw new IllegalStateException("Unsupported SOAP version: " + getVersion());
139+
}
140+
115141
}
116142

117143
public void writeTo(OutputStream outputStream) throws IOException {
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2008 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.ws.soap.support;
18+
19+
import java.util.regex.Matcher;
20+
import java.util.regex.Pattern;
21+
22+
import org.springframework.util.Assert;
23+
import org.springframework.util.StringUtils;
24+
import org.springframework.ws.transport.TransportConstants;
25+
26+
/**
27+
* Contains various utility methods for handling SOAP messages.
28+
*
29+
* @author Arjen Poutsma
30+
* @since 1.5.5
31+
*/
32+
public abstract class SoapUtils {
33+
34+
private static final Pattern ACTION_PATTERN = Pattern.compile("action\\s*=\\s*([^;]+)");
35+
36+
private SoapUtils() {
37+
}
38+
39+
/** Escapes the given SOAP action to be surrounded by quotes. */
40+
public static String escapeAction(String soapAction) {
41+
if (soapAction == null) {
42+
soapAction = "";
43+
}
44+
if (!soapAction.startsWith("\"")) {
45+
soapAction = "\"" + soapAction;
46+
}
47+
if (!soapAction.endsWith("\"")) {
48+
soapAction = soapAction + "\"";
49+
}
50+
return soapAction;
51+
}
52+
53+
/**
54+
* Returns the value of the action parameter in the given SOAP 1.2 content type.
55+
*
56+
* @param contentType the SOAP 1.2 content type
57+
* @return the action
58+
*/
59+
public static String extractActionFromContentType(String contentType) {
60+
if (contentType != null) {
61+
Matcher matcher = ACTION_PATTERN.matcher(contentType);
62+
if (matcher.find() && matcher.groupCount() == 1) {
63+
return matcher.group(1).trim();
64+
}
65+
}
66+
return TransportConstants.EMPTY_SOAP_ACTION;
67+
}
68+
69+
/**
70+
* Replaces or adds the value of the action parameter in the given SOAP 1.2 content type.
71+
*
72+
* @param contentType the SOAP 1.2 content type
73+
* @param action the action
74+
* @return the new content type
75+
*/
76+
public static String setActionInContentType(String contentType, String action) {
77+
Assert.hasLength(contentType, "'contentType' must not be empty");
78+
if (StringUtils.hasText(action)) {
79+
Matcher matcher = ACTION_PATTERN.matcher(contentType);
80+
if (matcher.find() && matcher.groupCount() == 1) {
81+
StringBuffer buffer = new StringBuffer();
82+
matcher.appendReplacement(buffer, action);
83+
matcher.appendTail(buffer);
84+
return buffer.toString();
85+
}
86+
else {
87+
return contentType + "; action=" + action;
88+
}
89+
}
90+
else {
91+
return contentType;
92+
}
93+
}
94+
95+
96+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<body>
3+
Classes supporting the org.springframework.ws.soap package.
4+
</body>
5+
</html>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2008 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.ws.soap.support;
18+
19+
import junit.framework.TestCase;
20+
21+
public class SoapUtilsTest extends TestCase {
22+
23+
public void testExtractActionFromContentType() throws Exception {
24+
String soapAction = "http://springframework.org/spring-ws/Action";
25+
26+
String contentType = "application/soap+xml; action=" + soapAction;
27+
String result = SoapUtils.extractActionFromContentType(contentType);
28+
assertEquals("Invalid SOAP action", soapAction, result);
29+
30+
contentType = "application/soap+xml; action = " + soapAction;
31+
result = SoapUtils.extractActionFromContentType(contentType);
32+
assertEquals("Invalid SOAP action", soapAction, result);
33+
34+
contentType = "application/soap+xml; action=" + soapAction + " ; charset=UTF-8";
35+
result = SoapUtils.extractActionFromContentType(contentType);
36+
assertEquals("Invalid SOAP action", soapAction, result);
37+
}
38+
39+
}

0 commit comments

Comments
 (0)