From c1ab3eab69bc9473b518254bd078e5d13c0e0e75 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 4 Jun 2018 12:54:26 -0700 Subject: [PATCH] Polish --- .../client/WebServiceTemplateBuilder.java | 97 +++++++++---------- 1 file changed, 46 insertions(+), 51 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/webservices/client/WebServiceTemplateBuilder.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/webservices/client/WebServiceTemplateBuilder.java index c8d831c3fb..a855d780ca 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/webservices/client/WebServiceTemplateBuilder.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/webservices/client/WebServiceTemplateBuilder.java @@ -26,6 +26,7 @@ import java.util.Set; import javax.xml.transform.TransformerFactory; import org.springframework.beans.BeanUtils; +import org.springframework.boot.context.properties.PropertyMapper; import org.springframework.oxm.Marshaller; import org.springframework.oxm.Unmarshaller; import org.springframework.util.Assert; @@ -76,9 +77,17 @@ public class WebServiceTemplateBuilder { private final WebServiceMessageFactory messageFactory; public WebServiceTemplateBuilder(WebServiceTemplateCustomizer... customizers) { - this(true, Collections.emptySet(), Collections.emptySet(), - append(Collections.emptySet(), customizers), - new WebServiceMessageSenders(), null, null, null, null, null); + this.detectHttpMessageSender = true; + this.interceptors = null; + this.internalCustomizers = null; + this.customizers = Collections + .unmodifiableSet(new LinkedHashSet<>(Arrays.asList(customizers))); + this.messageSenders = new WebServiceMessageSenders(); + this.marshaller = null; + this.unmarshaller = null; + this.destinationProvider = null; + this.transformerFactoryClass = null; + this.messageFactory = null; } private WebServiceTemplateBuilder(boolean detectHttpMessageSender, @@ -89,11 +98,11 @@ public class WebServiceTemplateBuilder { Unmarshaller unmarshaller, DestinationProvider destinationProvider, Class transformerFactoryClass, WebServiceMessageFactory messageFactory) { + this.detectHttpMessageSender = detectHttpMessageSender; this.interceptors = interceptors; this.internalCustomizers = internalCustomizers; this.customizers = customizers; this.messageSenders = messageSenders; - this.detectHttpMessageSender = detectHttpMessageSender; this.marshaller = marshaller; this.unmarshaller = unmarshaller; this.destinationProvider = destinationProvider; @@ -357,7 +366,7 @@ public class WebServiceTemplateBuilder { **/ public WebServiceTemplateBuilder setWebServiceMessageFactory( WebServiceMessageFactory messageFactory) { - Assert.notNull(messageFactory, "messageFactory must not be null"); + Assert.notNull(messageFactory, "MessageFactory must not be null"); return new WebServiceTemplateBuilder(this.detectHttpMessageSender, this.interceptors, this.internalCustomizers, this.customizers, this.messageSenders, this.marshaller, this.unmarshaller, @@ -425,7 +434,6 @@ public class WebServiceTemplateBuilder { /** * Set the default URI to be used on operations that do not have a URI parameter. - *

* Typically, either this property is set, or * {@link #setDestinationProvider(DestinationProvider)}, but not both. * @param defaultUri the destination provider URI to be used on operations that do not @@ -439,10 +447,8 @@ public class WebServiceTemplateBuilder { } /** - * Set the {@link DestinationProvider} to use - *

- * Typically, either this property is set, or {@link #setDefaultUri(String)}, but not - * both. + * Set the {@link DestinationProvider} to use. Typically, either this property is set, + * or {@link #setDefaultUri(String)}, but not both. * @param destinationProvider the destination provider to be used on operations that * do not have a URI parameter. * @return a new builder instance. @@ -450,7 +456,7 @@ public class WebServiceTemplateBuilder { */ public WebServiceTemplateBuilder setDestinationProvider( DestinationProvider destinationProvider) { - Assert.notNull(destinationProvider, "destinationProvider must not be null"); + Assert.notNull(destinationProvider, "DestinationProvider must not be null"); return new WebServiceTemplateBuilder(this.detectHttpMessageSender, this.interceptors, this.internalCustomizers, this.customizers, this.messageSenders, this.marshaller, this.unmarshaller, @@ -492,49 +498,45 @@ public class WebServiceTemplateBuilder { * @see #build(Class) */ public T configure(T webServiceTemplate) { - Assert.notNull(webServiceTemplate, "webServiceTemplate must not be null"); + Assert.notNull(webServiceTemplate, "WebServiceTemplate must not be null"); configureMessageSenders(webServiceTemplate); - if (!CollectionUtils.isEmpty(this.internalCustomizers)) { - for (WebServiceTemplateCustomizer internalCustomizer : this.internalCustomizers) { - internalCustomizer.customize(webServiceTemplate); - } - } - if (this.marshaller != null) { - webServiceTemplate.setMarshaller(this.marshaller); - } - if (this.unmarshaller != null) { - webServiceTemplate.setUnmarshaller(this.unmarshaller); - } - if (this.destinationProvider != null) { - webServiceTemplate.setDestinationProvider(this.destinationProvider); - } - if (this.transformerFactoryClass != null) { - webServiceTemplate.setTransformerFactoryClass(this.transformerFactoryClass); - } - if (this.messageFactory != null) { - webServiceTemplate.setMessageFactory(this.messageFactory); - } + PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull(); + applyCustomizers(webServiceTemplate, this.internalCustomizers); + map.from(() -> this.marshaller).to(webServiceTemplate::setMarshaller); + map.from(() -> this.unmarshaller).to(webServiceTemplate::setUnmarshaller); + map.from(() -> this.destinationProvider) + .to(webServiceTemplate::setDestinationProvider); + map.from(() -> this.transformerFactoryClass) + .to(webServiceTemplate::setTransformerFactoryClass); + map.from(() -> this.messageFactory).to(webServiceTemplate::setMessageFactory); if (!CollectionUtils.isEmpty(this.interceptors)) { - webServiceTemplate.setInterceptors( - append(this.interceptors, webServiceTemplate.getInterceptors()) - .toArray(new ClientInterceptor[0])); - } - if (!CollectionUtils.isEmpty(this.customizers)) { - for (WebServiceTemplateCustomizer customizer : this.customizers) { - customizer.customize(webServiceTemplate); + Set merged = new LinkedHashSet<>(this.interceptors); + if (webServiceTemplate.getInterceptors() != null) { + merged.addAll(Arrays.asList(webServiceTemplate.getInterceptors())); } + webServiceTemplate.setInterceptors(merged.toArray(new ClientInterceptor[0])); } + applyCustomizers(webServiceTemplate, this.customizers); return webServiceTemplate; } + private void applyCustomizers(WebServiceTemplate webServiceTemplate, + Set customizers) { + if (!CollectionUtils.isEmpty(customizers)) { + for (WebServiceTemplateCustomizer internalCustomizer : customizers) { + internalCustomizer.customize(webServiceTemplate); + } + } + } + private void configureMessageSenders( T webServiceTemplate) { if (this.messageSenders.isOnlyAdditional() && this.detectHttpMessageSender) { - Set mergedMessageSenders = append( + Set merged = append( this.messageSenders.getMessageSenders(), new HttpWebServiceMessageSenderBuilder().build()); - webServiceTemplate.setMessageSenders( - mergedMessageSenders.toArray(new WebServiceMessageSender[0])); + webServiceTemplate + .setMessageSenders(merged.toArray(new WebServiceMessageSender[0])); } else if (!CollectionUtils.isEmpty(this.messageSenders.getMessageSenders())) { webServiceTemplate.setMessageSenders(this.messageSenders.getMessageSenders() @@ -542,15 +544,8 @@ public class WebServiceTemplateBuilder { } } - private static Set append(Set set, T[] additions) { - return append(set, additions != null - ? new LinkedHashSet<>(Arrays.asList(additions)) : Collections.emptySet()); - } - - private static Set append(Set set, T addition) { - Set result = new LinkedHashSet<>(set != null ? set : Collections.emptySet()); - result.add(addition); - return Collections.unmodifiableSet(result); + private Set append(Set set, T addition) { + return append(set, Collections.singleton(addition)); } private static Set append(Set set, Collection additions) {