Skip to content

Commit be5f187

Browse files
committed
SWS-340
1 parent 708203f commit be5f187

File tree

5 files changed

+216
-3
lines changed

5 files changed

+216
-3
lines changed

core/src/main/java/org/springframework/ws/wsdl/wsdl11/DefaultWsdl11Definition.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.springframework.ws.wsdl.wsdl11.provider.DefaultMessagesProvider;
2525
import org.springframework.ws.wsdl.wsdl11.provider.InliningXsdSchemaTypesProvider;
2626
import org.springframework.ws.wsdl.wsdl11.provider.SoapProvider;
27+
import org.springframework.ws.wsdl.wsdl11.provider.SuffixBasedMessagesProvider;
2728
import org.springframework.ws.wsdl.wsdl11.provider.SuffixBasedPortTypesProvider;
2829
import org.springframework.xml.xsd.XsdSchema;
2930
import org.springframework.xml.xsd.XsdSchemaCollection;
@@ -54,7 +55,7 @@ public class DefaultWsdl11Definition implements Wsdl11Definition, InitializingBe
5455

5556
private final InliningXsdSchemaTypesProvider typesProvider = new InliningXsdSchemaTypesProvider();
5657

57-
private final DefaultMessagesProvider messagesProvider = new DefaultMessagesProvider();
58+
private final SuffixBasedMessagesProvider messagesProvider = new SuffixBasedMessagesProvider();
5859

5960
private final SuffixBasedPortTypesProvider portTypesProvider = new SuffixBasedPortTypesProvider();
6061

@@ -106,16 +107,19 @@ public void setPortTypeName(String portTypeName) {
106107
/** Sets the suffix used to detect request elements in the schema. */
107108
public void setRequestSuffix(String requestSuffix) {
108109
portTypesProvider.setRequestSuffix(requestSuffix);
110+
messagesProvider.setRequestSuffix(requestSuffix);
109111
}
110112

111113
/** Sets the suffix used to detect response elements in the schema. */
112114
public void setResponseSuffix(String responseSuffix) {
113115
portTypesProvider.setResponseSuffix(responseSuffix);
116+
messagesProvider.setResponseSuffix(responseSuffix);
114117
}
115118

116119
/** Sets the suffix used to detect fault elements in the schema. */
117120
public void setFaultSuffix(String faultSuffix) {
118121
portTypesProvider.setFaultSuffix(faultSuffix);
122+
messagesProvider.setResponseSuffix(faultSuffix);
119123
}
120124

121125
/** Indicates whether a SOAP 1.1 binding should be created. */

core/src/main/java/org/springframework/ws/wsdl/wsdl11/provider/DefaultMessagesProvider.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void addMessages(Definition definition) throws WSDLException {
6565

6666
private void createMessages(Definition definition, Element schemaElement) throws WSDLException {
6767
String schemaTargetNamespace = schemaElement.getAttribute("targetNamespace");
68-
Assert.hasText("No targetNamespace defined on schema");
68+
Assert.hasText(schemaTargetNamespace, "No targetNamespace defined on schema");
6969
if (logger.isDebugEnabled()) {
7070
logger.debug("Looking for elements in schema with target namespace [" + schemaTargetNamespace + "]");
7171
}
@@ -75,7 +75,7 @@ private void createMessages(Definition definition, Element schemaElement) throws
7575
if (child.getNodeType() == Node.ELEMENT_NODE) {
7676
Element childElement = (Element) child;
7777
if (isMessageElement(childElement)) {
78-
QName elementName = new QName(schemaTargetNamespace, childElement.getAttribute("name"));
78+
QName elementName = new QName(schemaTargetNamespace, getElementName(childElement));
7979
Message message = definition.createMessage();
8080
populateMessage(definition, message, elementName);
8181
Part part = definition.createPart();
@@ -88,6 +88,16 @@ private void createMessages(Definition definition, Element schemaElement) throws
8888
}
8989
}
9090

91+
/**
92+
* Returns the name attribute of the given element.
93+
*
94+
* @param element the element whose name to return
95+
* @return the name of the element
96+
*/
97+
protected String getElementName(Element element) {
98+
return element.getAttribute("name");
99+
}
100+
91101
/**
92102
* Indicates whether the given element should be includes as {@link Message} in the definition.
93103
* <p/>
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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.wsdl.wsdl11.provider;
18+
19+
import org.w3c.dom.Element;
20+
21+
import org.springframework.util.Assert;
22+
23+
/**
24+
* Implementation of the {@link MessagesProvider} interface that is based on suffixes.
25+
*
26+
* @author Arjen Poutsma
27+
* @since 1.5.1
28+
*/
29+
public class SuffixBasedMessagesProvider extends DefaultMessagesProvider {
30+
31+
/** The default suffix used to detect request elements in the schema. */
32+
public static final String DEFAULT_REQUEST_SUFFIX = "Request";
33+
34+
/** The default suffix used to detect response elements in the schema. */
35+
public static final String DEFAULT_RESPONSE_SUFFIX = "Response";
36+
37+
/** The default suffix used to detect fault elements in the schema. */
38+
public static final String DEFAULT_FAULT_SUFFIX = "Fault";
39+
40+
private String requestSuffix = DEFAULT_REQUEST_SUFFIX;
41+
42+
private String responseSuffix = DEFAULT_RESPONSE_SUFFIX;
43+
44+
private String faultSuffix = DEFAULT_FAULT_SUFFIX;
45+
46+
/**
47+
* Returns the suffix used to detect request elements in the schema.
48+
*
49+
* @see #DEFAULT_REQUEST_SUFFIX
50+
*/
51+
public String getRequestSuffix() {
52+
return requestSuffix;
53+
}
54+
55+
/**
56+
* Sets the suffix used to detect request elements in the schema.
57+
*
58+
* @see #DEFAULT_REQUEST_SUFFIX
59+
*/
60+
public void setRequestSuffix(String requestSuffix) {
61+
this.requestSuffix = requestSuffix;
62+
}
63+
64+
/**
65+
* Returns the suffix used to detect response elements in the schema.
66+
*
67+
* @see #DEFAULT_RESPONSE_SUFFIX
68+
*/
69+
public String getResponseSuffix() {
70+
return responseSuffix;
71+
}
72+
73+
/**
74+
* Sets the suffix used to detect response elements in the schema.
75+
*
76+
* @see #DEFAULT_RESPONSE_SUFFIX
77+
*/
78+
public void setResponseSuffix(String responseSuffix) {
79+
this.responseSuffix = responseSuffix;
80+
}
81+
82+
/**
83+
* Returns the suffix used to detect fault elements in the schema.
84+
*
85+
* @see #DEFAULT_FAULT_SUFFIX
86+
*/
87+
public String getFaultSuffix() {
88+
return faultSuffix;
89+
}
90+
91+
/**
92+
* Sets the suffix used to detect fault elements in the schema.
93+
*
94+
* @see #DEFAULT_FAULT_SUFFIX
95+
*/
96+
public void setFaultSuffix(String faultSuffix) {
97+
this.faultSuffix = faultSuffix;
98+
}
99+
100+
protected boolean isMessageElement(Element element) {
101+
if (super.isMessageElement(element)) {
102+
String elementName = getElementName(element);
103+
Assert.hasText(elementName, "Element has no name");
104+
return elementName.endsWith(getRequestSuffix()) || elementName.endsWith(getResponseSuffix()) ||
105+
elementName.endsWith(getFaultSuffix());
106+
}
107+
else {
108+
return false;
109+
}
110+
}
111+
}

core/src/test/java/org/springframework/ws/wsdl/wsdl11/provider/DefaultMessagesProviderTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ public void testAddMessages() throws Exception {
6868

6969
provider.addMessages(definition);
7070

71+
assertEquals("Invalid amount of messages created", 3, definition.getMessages().size());
72+
7173
Message message = definition.getMessage(new QName(definitionNamespace, "GetOrderRequest"));
7274
assertNotNull("Message not created", message);
7375
Part part = message.getPart("GetOrderRequest");
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright ${YEAR} 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.wsdl.wsdl11.provider;
18+
19+
import javax.wsdl.Definition;
20+
import javax.wsdl.Message;
21+
import javax.wsdl.Part;
22+
import javax.wsdl.Types;
23+
import javax.wsdl.extensions.schema.Schema;
24+
import javax.wsdl.factory.WSDLFactory;
25+
import javax.xml.namespace.QName;
26+
import javax.xml.parsers.DocumentBuilder;
27+
import javax.xml.parsers.DocumentBuilderFactory;
28+
29+
import junit.framework.TestCase;
30+
import org.w3c.dom.Document;
31+
32+
import org.springframework.core.io.ClassPathResource;
33+
import org.springframework.core.io.Resource;
34+
import org.springframework.xml.sax.SaxUtils;
35+
36+
public class SuffixBasedMessagesProviderTest extends TestCase {
37+
38+
private SuffixBasedMessagesProvider provider;
39+
40+
private Definition definition;
41+
42+
private DocumentBuilder documentBuilder;
43+
44+
protected void setUp() throws Exception {
45+
provider = new SuffixBasedMessagesProvider();
46+
provider.setFaultSuffix("Foo");
47+
WSDLFactory factory = WSDLFactory.newInstance();
48+
definition = factory.newDefinition();
49+
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
50+
documentBuilderFactory.setNamespaceAware(true);
51+
documentBuilder = documentBuilderFactory.newDocumentBuilder();
52+
}
53+
54+
public void testAddMessages() throws Exception {
55+
String definitionNamespace = "http://springframework.org/spring-ws";
56+
definition.addNamespace("tns", definitionNamespace);
57+
definition.setTargetNamespace(definitionNamespace);
58+
String schemaNamespace = "http://www.springframework.org/spring-ws/schema";
59+
definition.addNamespace("schema", schemaNamespace);
60+
61+
Resource resource = new ClassPathResource("schema.xsd", getClass());
62+
Document schemaDocument = documentBuilder.parse(SaxUtils.createInputSource(resource));
63+
Types types = definition.createTypes();
64+
definition.setTypes(types);
65+
Schema schema = (Schema) definition.getExtensionRegistry()
66+
.createExtension(Types.class, new QName("http://www.w3.org/2001/XMLSchema", "schema"));
67+
types.addExtensibilityElement(schema);
68+
schema.setElement(schemaDocument.getDocumentElement());
69+
70+
provider.addMessages(definition);
71+
72+
assertEquals("Invalid amount of messages created", 2, definition.getMessages().size());
73+
74+
Message message = definition.getMessage(new QName(definitionNamespace, "GetOrderRequest"));
75+
assertNotNull("Message not created", message);
76+
Part part = message.getPart("GetOrderRequest");
77+
assertNotNull("Part not created", part);
78+
assertEquals("Invalid element on part", new QName(schemaNamespace, "GetOrderRequest"), part.getElementName());
79+
80+
message = definition.getMessage(new QName(definitionNamespace, "GetOrderResponse"));
81+
assertNotNull("Message not created", message);
82+
part = message.getPart("GetOrderResponse");
83+
assertNotNull("Part not created", part);
84+
assertEquals("Invalid element on part", new QName(schemaNamespace, "GetOrderResponse"), part.getElementName());
85+
}
86+
}

0 commit comments

Comments
 (0)