Skip to content

Commit 7eb8fcf

Browse files
committed
Add support for explicit names for WSDL and Schema
This commit improves Wsdl11Definition and XmlSchema to offer an optional name, rather than relying on the name of the bean name. This allows to provide an explicit name without having to expose the bean with that name. If no explicit name is provided, we fall-back on the name of the bean as we used to do. Closes gh-1746
1 parent a3bd2d8 commit 7eb8fcf

File tree

11 files changed

+266
-54
lines changed

11 files changed

+266
-54
lines changed

spring-ws-core/src/main/java/org/springframework/ws/transport/http/MessageDispatcherServlet.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.Collections;
2020
import java.util.Map;
21+
import java.util.stream.Collectors;
2122

2223
import jakarta.servlet.http.HttpServletRequest;
2324
import jakarta.servlet.http.HttpServletResponse;
@@ -476,24 +477,34 @@ private void initMessageReceiver(ApplicationContext context) {
476477
}
477478

478479
private void initWsdlDefinitions(ApplicationContext context) {
479-
this.wsdlDefinitions = BeanFactoryUtils.beansOfTypeIncludingAncestors(context, WsdlDefinition.class, true,
480-
false);
480+
this.wsdlDefinitions = BeanFactoryUtils
481+
.beansOfTypeIncludingAncestors(context, WsdlDefinition.class, true, false)
482+
.entrySet()
483+
.stream()
484+
.collect(Collectors.toMap(
485+
(entry) -> (entry.getValue().getName() != null) ? entry.getValue().getName() : entry.getKey(),
486+
Map.Entry::getValue));
481487
if (this.logger.isDebugEnabled()) {
482488
for (Map.Entry<String, WsdlDefinition> entry : this.wsdlDefinitions.entrySet()) {
483-
String beanName = entry.getKey();
489+
String name = entry.getKey();
484490
WsdlDefinition definition = entry.getValue();
485-
this.logger.debug("Published [" + definition + "] as " + beanName + WSDL_SUFFIX_NAME);
491+
this.logger.debug("Published [" + definition + "] as " + name + WSDL_SUFFIX_NAME);
486492
}
487493
}
488494
}
489495

490496
private void initXsdSchemas(ApplicationContext context) {
491-
this.xsdSchemas = BeanFactoryUtils.beansOfTypeIncludingAncestors(context, XsdSchema.class, true, false);
497+
this.xsdSchemas = BeanFactoryUtils.beansOfTypeIncludingAncestors(context, XsdSchema.class, true, false)
498+
.entrySet()
499+
.stream()
500+
.collect(Collectors.toMap(
501+
(entry) -> (entry.getValue().getName() != null) ? entry.getValue().getName() : entry.getKey(),
502+
Map.Entry::getValue));
492503
if (this.logger.isDebugEnabled()) {
493504
for (Map.Entry<String, XsdSchema> entry : this.xsdSchemas.entrySet()) {
494-
String beanName = entry.getKey();
505+
String name = entry.getKey();
495506
XsdSchema schema = entry.getValue();
496-
this.logger.debug("Published [" + schema + "] as " + beanName + XSD_SUFFIX_NAME);
507+
this.logger.debug("Published [" + schema + "] as " + name + XSD_SUFFIX_NAME);
497508
}
498509
}
499510
}

spring-ws-core/src/main/java/org/springframework/ws/wsdl/WsdlDefinition.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import javax.xml.transform.Source;
2020

21+
import org.jspecify.annotations.Nullable;
22+
2123
/**
2224
* Represents an abstraction for WSDL definitions.
2325
*
@@ -26,6 +28,14 @@
2628
*/
2729
public interface WsdlDefinition {
2830

31+
/**
32+
* Return the name of the definition, if known.
33+
* @since 5.1.0
34+
*/
35+
default @Nullable String getName() {
36+
return null;
37+
}
38+
2939
/**
3040
* Returns the {@code Source} of the definition.
3141
* @return the {@code Source} of this WSDL definition

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import org.jspecify.annotations.Nullable;
2424

25+
import org.springframework.beans.factory.BeanNameAware;
2526
import org.springframework.beans.factory.InitializingBean;
2627
import org.springframework.util.StringUtils;
2728
import org.springframework.ws.wsdl.wsdl11.provider.DefaultMessagesProvider;
@@ -53,7 +54,9 @@
5354
* @author Arjen Poutsma
5455
* @since 1.5.0
5556
*/
56-
public class DefaultWsdl11Definition implements Wsdl11Definition, InitializingBean {
57+
public class DefaultWsdl11Definition implements Wsdl11Definition, BeanNameAware, InitializingBean {
58+
59+
private @Nullable String name;
5760

5861
private final InliningXsdSchemaTypesProvider typesProvider = new InliningXsdSchemaTypesProvider();
5962

@@ -175,6 +178,13 @@ public void setServiceName(String serviceName) {
175178
this.serviceName = serviceName;
176179
}
177180

181+
@Override
182+
public void setBeanName(String name) {
183+
if (this.name == null) {
184+
this.name = name;
185+
}
186+
}
187+
178188
@Override
179189
public void afterPropertiesSet() throws Exception {
180190
if (!StringUtils.hasText(this.delegate.getTargetNamespace())
@@ -185,9 +195,24 @@ public void afterPropertiesSet() throws Exception {
185195
if (!StringUtils.hasText(this.serviceName) && StringUtils.hasText(this.portTypesProvider.getPortTypeName())) {
186196
this.soapProvider.setServiceName(this.portTypesProvider.getPortTypeName() + "Service");
187197
}
198+
this.delegate.setName(this.name);
188199
this.delegate.afterPropertiesSet();
189200
}
190201

202+
@Override
203+
public @Nullable String getName() {
204+
return this.delegate.getName();
205+
}
206+
207+
/**
208+
* Set the name of this definition.
209+
* @param name the name
210+
* @since 5.1.0
211+
*/
212+
public void setName(@Nullable String name) {
213+
this.name = name;
214+
}
215+
191216
@Override
192217
public Source getSource() {
193218
return this.delegate.getSource();

spring-ws-core/src/main/java/org/springframework/ws/wsdl/wsdl11/ProviderBasedWsdl4jDefinition.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import org.jspecify.annotations.Nullable;
2424

25+
import org.springframework.beans.factory.BeanNameAware;
2526
import org.springframework.beans.factory.InitializingBean;
2627
import org.springframework.util.Assert;
2728
import org.springframework.ws.wsdl.wsdl11.provider.BindingsProvider;
@@ -59,7 +60,7 @@
5960
* @see #setBindingsProvider(BindingsProvider)
6061
* @see #setServicesProvider(ServicesProvider)
6162
*/
62-
public class ProviderBasedWsdl4jDefinition extends Wsdl4jDefinition implements InitializingBean {
63+
public class ProviderBasedWsdl4jDefinition extends Wsdl4jDefinition implements BeanNameAware, InitializingBean {
6364

6465
/** The prefix used to register the target namespace in the WSDL. */
6566
public static final String TARGET_NAMESPACE_PREFIX = "tns";
@@ -224,6 +225,13 @@ public void setTargetNamespace(String targetNamespace) {
224225
this.targetNamespace = targetNamespace;
225226
}
226227

228+
@Override
229+
public void setBeanName(String name) {
230+
if (this.name == null) {
231+
this.name = name;
232+
}
233+
}
234+
227235
@Override
228236
public void afterPropertiesSet() throws WSDLException {
229237
Assert.notNull(this.targetNamespace, "'targetNamespace' is required");

spring-ws-core/src/main/java/org/springframework/ws/wsdl/wsdl11/SimpleWsdl11Definition.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.xml.sax.SAXException;
2525
import org.xml.sax.XMLReader;
2626

27+
import org.springframework.beans.factory.BeanNameAware;
2728
import org.springframework.beans.factory.InitializingBean;
2829
import org.springframework.core.io.Resource;
2930
import org.springframework.util.Assert;
@@ -40,10 +41,12 @@
4041
* @author Arjen Poutsma
4142
* @since 1.0.0
4243
*/
43-
public class SimpleWsdl11Definition implements Wsdl11Definition, InitializingBean {
44+
public class SimpleWsdl11Definition implements Wsdl11Definition, BeanNameAware, InitializingBean {
4445

4546
private @Nullable Resource wsdlResource;
4647

48+
private @Nullable String name;
49+
4750
/**
4851
* Create a new instance of the {@link SimpleWsdl11Definition} class.
4952
* <p>
@@ -60,8 +63,29 @@ public SimpleWsdl11Definition() {
6063
* {@code null}
6164
*/
6265
public SimpleWsdl11Definition(Resource wsdlResource) {
66+
this(wsdlResource, null);
67+
}
68+
69+
/**
70+
* Create a new instance of the {@link SimpleWsdl11Definition} class with the
71+
* specified resource, and name.
72+
* @param wsdlResource the WSDL resource; must not be {@code null}
73+
* @param name the name of the definition
74+
* @throws IllegalArgumentException if the supplied {@code wsdlResource} is
75+
* {@code null}
76+
* @since 5.1.0
77+
*/
78+
public SimpleWsdl11Definition(Resource wsdlResource, @Nullable String name) {
6379
Assert.notNull(wsdlResource, "wsdlResource must not be null");
6480
this.wsdlResource = wsdlResource;
81+
this.name = name;
82+
}
83+
84+
@Override
85+
public void setBeanName(String name) {
86+
if (this.name == null) {
87+
this.name = name;
88+
}
6589
}
6690

6791
@Override
@@ -70,6 +94,20 @@ public void afterPropertiesSet() throws Exception {
7094
Assert.isTrue(this.wsdlResource.exists(), "wsdl '" + this.wsdlResource + "' does not exist");
7195
}
7296

97+
@Override
98+
public @Nullable String getName() {
99+
return this.name;
100+
}
101+
102+
/**
103+
* Set the name of this definition.
104+
* @param name the name
105+
* @since 5.1.0
106+
*/
107+
public void setName(@Nullable String name) {
108+
this.name = name;
109+
}
110+
73111
@Override
74112
public Source getSource() {
75113
Assert.notNull(this.wsdlResource, "wsdl is required");

spring-ws-core/src/main/java/org/springframework/ws/wsdl/wsdl11/Wsdl4jDefinition.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
*/
4444
public class Wsdl4jDefinition implements Wsdl11Definition {
4545

46+
protected @Nullable String name;
47+
4648
private @Nullable Definition definition;
4749

4850
/** WSDL4J is not thread safe, hence the need for a monitor. */
@@ -63,6 +65,20 @@ public Wsdl4jDefinition(Definition definition) {
6365
setDefinition(definition);
6466
}
6567

68+
@Override
69+
public @Nullable String getName() {
70+
return this.name;
71+
}
72+
73+
/**
74+
* Set the name of this definition.
75+
* @param name the name
76+
* @since 5.1.0
77+
*/
78+
public void setName(@Nullable String name) {
79+
this.name = name;
80+
}
81+
6682
/** Returns the WSDL4J {@code Definition}. */
6783
public @Nullable Definition getDefinition() {
6884
synchronized (this.monitor) {

0 commit comments

Comments
 (0)