Skip to content

Commit 819a13f

Browse files
committed
Apply predicate to BufferingClientHttpRequestFactory
The bufferingPredicate configured in RestClient.Builder can and should also be used in the (existing) shouldBuffer protected method in BufferingClientHttpRequestFactory since the factory is no longer intended for direct use, and in any case the setting should apply in all cases including when there are no interceptors. See gh-33785
1 parent 808d1b6 commit 819a13f

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

Diff for: spring-web/src/main/java/org/springframework/http/client/BufferingClientHttpRequestFactory.java

+22-9
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.net.URI;
2121
import java.util.function.BiPredicate;
2222

23+
import org.jspecify.annotations.Nullable;
24+
2325
import org.springframework.http.HttpMethod;
2426

2527
/**
@@ -38,26 +40,37 @@
3840
*/
3941
public class BufferingClientHttpRequestFactory extends AbstractClientHttpRequestFactoryWrapper {
4042

43+
private final BiPredicate<URI, HttpMethod> bufferingPredicate;
44+
45+
4146
/**
4247
* Create a buffering wrapper for the given {@link ClientHttpRequestFactory}.
4348
* @param requestFactory the target request factory to wrap
4449
*/
4550
public BufferingClientHttpRequestFactory(ClientHttpRequestFactory requestFactory) {
51+
this(requestFactory, null);
52+
}
53+
54+
/**
55+
* Constructor variant with an additional predicate to decide whether to
56+
* buffer the response.
57+
*/
58+
public BufferingClientHttpRequestFactory(
59+
ClientHttpRequestFactory requestFactory,
60+
@Nullable BiPredicate<URI, HttpMethod> bufferingPredicate) {
61+
4662
super(requestFactory);
63+
this.bufferingPredicate = (bufferingPredicate != null ? bufferingPredicate : (uri, method) -> true);
4764
}
4865

4966

67+
5068
@Override
51-
protected ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod, ClientHttpRequestFactory requestFactory)
52-
throws IOException {
69+
protected ClientHttpRequest createRequest(
70+
URI uri, HttpMethod httpMethod, ClientHttpRequestFactory requestFactory) throws IOException {
5371

5472
ClientHttpRequest request = requestFactory.createRequest(uri, httpMethod);
55-
if (shouldBuffer(uri, httpMethod)) {
56-
return new BufferingClientHttpRequestWrapper(request);
57-
}
58-
else {
59-
return request;
60-
}
73+
return (shouldBuffer(uri, httpMethod) ? new BufferingClientHttpRequestWrapper(request) : request);
6174
}
6275

6376
/**
@@ -70,7 +83,7 @@ protected ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod, Client
7083
* @return {@code true} if the exchange should be buffered; {@code false} otherwise
7184
*/
7285
protected boolean shouldBuffer(URI uri, HttpMethod httpMethod) {
73-
return true;
86+
return this.bufferingPredicate.test(uri, httpMethod);
7487
}
7588

7689
}

Diff for: spring-web/src/main/java/org/springframework/web/client/DefaultRestClient.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,8 @@ private ClientHttpRequest createRequest(URI uri) throws IOException {
656656
}
657657
}
658658
else if (DefaultRestClient.this.bufferingPredicate != null) {
659-
factory = new BufferingClientHttpRequestFactory(DefaultRestClient.this.clientRequestFactory);
659+
factory = new BufferingClientHttpRequestFactory(
660+
DefaultRestClient.this.clientRequestFactory, DefaultRestClient.this.bufferingPredicate);
660661
}
661662
else {
662663
factory = DefaultRestClient.this.clientRequestFactory;

0 commit comments

Comments
 (0)