Revert "Allow ExchangeStrategies customizations in WebClient"
This reverts commit b3020bc484.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2018 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.
|
||||
@@ -43,18 +43,13 @@ final class DefaultExchangeStrategiesBuilder implements ExchangeStrategies.Build
|
||||
}
|
||||
|
||||
|
||||
private final ClientCodecConfigurer codecConfigurer;
|
||||
private final ClientCodecConfigurer codecConfigurer = ClientCodecConfigurer.create();
|
||||
|
||||
|
||||
public DefaultExchangeStrategiesBuilder() {
|
||||
this.codecConfigurer = ClientCodecConfigurer.create();
|
||||
this.codecConfigurer.registerDefaults(false);
|
||||
}
|
||||
|
||||
private DefaultExchangeStrategiesBuilder(DefaultExchangeStrategies other) {
|
||||
this.codecConfigurer = other.codecConfigurer.clone();
|
||||
}
|
||||
|
||||
|
||||
public void defaultConfiguration() {
|
||||
this.codecConfigurer.registerDefaults(true);
|
||||
@@ -74,28 +69,19 @@ final class DefaultExchangeStrategiesBuilder implements ExchangeStrategies.Build
|
||||
|
||||
private static class DefaultExchangeStrategies implements ExchangeStrategies {
|
||||
|
||||
private final ClientCodecConfigurer codecConfigurer;
|
||||
|
||||
private final List<HttpMessageReader<?>> readers;
|
||||
|
||||
private final List<HttpMessageWriter<?>> writers;
|
||||
|
||||
public DefaultExchangeStrategies(ClientCodecConfigurer codecConfigurer) {
|
||||
this.codecConfigurer = codecConfigurer;
|
||||
this.readers = unmodifiableCopy(this.codecConfigurer.getReaders());
|
||||
this.writers = unmodifiableCopy(this.codecConfigurer.getWriters());
|
||||
this.readers = unmodifiableCopy(codecConfigurer.getReaders());
|
||||
this.writers = unmodifiableCopy(codecConfigurer.getWriters());
|
||||
}
|
||||
|
||||
private static <T> List<T> unmodifiableCopy(List<? extends T> list) {
|
||||
return Collections.unmodifiableList(new ArrayList<>(list));
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
@Override
|
||||
public Builder mutate() {
|
||||
return new DefaultExchangeStrategiesBuilder(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<HttpMessageReader<?>> messageReaders() {
|
||||
return this.readers;
|
||||
|
||||
@@ -40,7 +40,6 @@ import org.springframework.web.util.UriBuilderFactory;
|
||||
* Default implementation of {@link WebClient.Builder}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Brian Clozel
|
||||
* @since 5.0
|
||||
*/
|
||||
final class DefaultWebClientBuilder implements WebClient.Builder {
|
||||
@@ -80,16 +79,14 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
|
||||
@Nullable
|
||||
private ClientHttpConnector connector;
|
||||
|
||||
@Nullable
|
||||
private ExchangeStrategies.Builder strategies;
|
||||
|
||||
private List<Consumer<ExchangeStrategies.Builder>> strategiesConfigurers;
|
||||
private ExchangeStrategies exchangeStrategies;
|
||||
|
||||
@Nullable
|
||||
private ExchangeFunction exchangeFunction;
|
||||
|
||||
|
||||
public DefaultWebClientBuilder() {
|
||||
this.exchangeStrategies = ExchangeStrategies.withDefaults();
|
||||
}
|
||||
|
||||
public DefaultWebClientBuilder(DefaultWebClientBuilder other) {
|
||||
@@ -111,7 +108,7 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
|
||||
this.defaultRequest = other.defaultRequest;
|
||||
this.filters = other.filters != null ? new ArrayList<>(other.filters) : null;
|
||||
this.connector = other.connector;
|
||||
this.strategies = other.strategies;
|
||||
this.exchangeStrategies = other.exchangeStrategies;
|
||||
this.exchangeFunction = other.exchangeFunction;
|
||||
}
|
||||
|
||||
@@ -206,23 +203,9 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public WebClient.Builder exchangeStrategies(ExchangeStrategies strategies) {
|
||||
Assert.notNull(strategies, "ExchangeStrategies must not be null");
|
||||
this.strategies = strategies.mutate();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WebClient.Builder exchangeStrategies(ExchangeStrategies.Builder strategies) {
|
||||
Assert.notNull(strategies, "ExchangeStrategies must not be null");
|
||||
this.strategies = strategies;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WebClient.Builder exchangeStrategies(Consumer<ExchangeStrategies.Builder> configurer) {
|
||||
this.strategiesConfigurers.add(configurer);
|
||||
this.exchangeStrategies = strategies;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -246,7 +229,7 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
|
||||
@Override
|
||||
public WebClient build() {
|
||||
ExchangeFunction exchange = (this.exchangeFunction == null ?
|
||||
ExchangeFunctions.create(getOrInitConnector(), initExchangeStrategies()) :
|
||||
ExchangeFunctions.create(getOrInitConnector(), this.exchangeStrategies) :
|
||||
this.exchangeFunction);
|
||||
ExchangeFunction filteredExchange = (this.filters != null ? this.filters.stream()
|
||||
.reduce(ExchangeFilterFunction::andThen)
|
||||
@@ -271,19 +254,6 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
|
||||
throw new IllegalStateException("No suitable default ClientHttpConnector found");
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private ExchangeStrategies initExchangeStrategies() {
|
||||
if (CollectionUtils.isEmpty(this.strategiesConfigurers)) {
|
||||
return this.strategies != null ? this.strategies.build() : ExchangeStrategies.withDefaults();
|
||||
}
|
||||
|
||||
ExchangeStrategies.Builder builder =
|
||||
this.strategies != null ? this.strategies : ExchangeStrategies.builder();
|
||||
|
||||
this.strategiesConfigurers.forEach(configurer -> configurer.accept(builder));
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
private UriBuilderFactory initUriBuilderFactory() {
|
||||
if (this.uriBuilderFactory != null) {
|
||||
return this.uriBuilderFactory;
|
||||
|
||||
@@ -47,18 +47,6 @@ public interface ExchangeStrategies {
|
||||
*/
|
||||
List<HttpMessageWriter<?>> messageWriters();
|
||||
|
||||
/**
|
||||
* Return a builder to create a new {@link ExchangeStrategies} instance
|
||||
* replicated from the current instance.
|
||||
* @since 5.1.12
|
||||
* @deprecated APIs should consume {@link ExchangeStrategies} as final or accept an
|
||||
* {@link ExchangeStrategies.Builder builder}.
|
||||
*/
|
||||
@Deprecated
|
||||
default Builder mutate() {
|
||||
throw new UnsupportedOperationException("This ExchangeStrategies implementation does not support mutation.");
|
||||
}
|
||||
|
||||
|
||||
// Static builder methods
|
||||
|
||||
|
||||
@@ -66,7 +66,6 @@ import org.springframework.web.util.UriBuilderFactory;
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Arjen Poutsma
|
||||
* @author Sebastien Deleuze
|
||||
* @author Brian Clozel
|
||||
* @since 5.0
|
||||
*/
|
||||
public interface WebClient {
|
||||
@@ -291,35 +290,12 @@ public interface WebClient {
|
||||
Builder clientConnector(ClientHttpConnector connector);
|
||||
|
||||
/**
|
||||
* Provide the {@link ExchangeStrategies} to use.
|
||||
* <p>This is useful for changing the default settings, yet still allowing
|
||||
* further customizations via {@link #exchangeStrategies(Consumer)}.
|
||||
* If not set, defaults are obtained from {@link ExchangeStrategies#withDefaults()}.
|
||||
* Configure the {@link ExchangeStrategies} to use.
|
||||
* <p>By default this is obtained from {@link ExchangeStrategies#withDefaults()}.
|
||||
* @param strategies the strategies to use
|
||||
* @deprecated as of 5.1, in favor of {@link #exchangeStrategies(ExchangeStrategies.Builder)}
|
||||
*/
|
||||
@Deprecated
|
||||
Builder exchangeStrategies(ExchangeStrategies strategies);
|
||||
|
||||
/**
|
||||
* Provide the {@link ExchangeStrategies.Builder} to use.
|
||||
* <p>This is useful for changing the default settings, yet still allowing
|
||||
* further customizations via {@link #exchangeStrategies(Consumer)}.
|
||||
* If not set, defaults are obtained from {@link ExchangeStrategies#builder()}.
|
||||
* @param strategies the strategies to use
|
||||
* @since 5.1.12
|
||||
*/
|
||||
Builder exchangeStrategies(ExchangeStrategies.Builder strategies);
|
||||
|
||||
/**
|
||||
* Customize the {@link ExchangeStrategies}.
|
||||
* <p>Allows further customization on {@link ExchangeStrategies},
|
||||
* mutating them if they were {@link #exchangeStrategies(ExchangeStrategies) set},
|
||||
* or starting from {@link ExchangeStrategies#withDefaults() defaults}.
|
||||
* @since 5.1.12
|
||||
*/
|
||||
Builder exchangeStrategies(Consumer<ExchangeStrategies.Builder> configurer);
|
||||
|
||||
/**
|
||||
* Provide an {@link ExchangeFunction} pre-configured with
|
||||
* {@link ClientHttpConnector} and {@link ExchangeStrategies}.
|
||||
|
||||
@@ -39,15 +39,4 @@ public class ExchangeStrategiesTests {
|
||||
assertThat(strategies.messageWriters().isEmpty()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
public void mutate() {
|
||||
ExchangeStrategies strategies = ExchangeStrategies.empty().build();
|
||||
assertThat(strategies.messageReaders().isEmpty()).isTrue();
|
||||
assertThat(strategies.messageWriters().isEmpty()).isTrue();
|
||||
ExchangeStrategies mutated = strategies.mutate().codecs(codecs -> codecs.registerDefaults(true)).build();
|
||||
assertThat(mutated.messageReaders().isEmpty()).isFalse();
|
||||
assertThat(mutated.messageWriters().isEmpty()).isFalse();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user