Skip to content

Commit 6a0c5dd

Browse files
committed
Refactoring in AbstractBufferingClientHttpRequest
Extract a protected method for subclasses to use to perform the actual (end-of-chain) request execution. See gh-33785
1 parent b9efa91 commit 6a0c5dd

File tree

3 files changed

+46
-56
lines changed

3 files changed

+46
-56
lines changed

spring-web/src/main/java/org/springframework/http/client/AbstractBufferingClientHttpRequest.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,7 +20,9 @@
2020
import java.io.OutputStream;
2121

2222
import org.springframework.http.HttpHeaders;
23+
import org.springframework.http.StreamingHttpOutputMessage;
2324
import org.springframework.util.FastByteArrayOutputStream;
25+
import org.springframework.util.StreamUtils;
2426

2527
/**
2628
* Base implementation of {@link ClientHttpRequest} that buffers output
@@ -59,5 +61,43 @@ protected ClientHttpResponse executeInternal(HttpHeaders headers) throws IOExcep
5961
protected abstract ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput)
6062
throws IOException;
6163

64+
/**
65+
* Execute with the given request and body.
66+
* @param request the request to execute with
67+
* @param body the body to send
68+
* @param bufferResponse whether to buffer the response
69+
* @return the resulting response
70+
* @throws IOException in case of I/O errors from execution
71+
* @since 7.0
72+
*/
73+
protected ClientHttpResponse executeWithRequest(
74+
ClientHttpRequest request, byte[] body, boolean bufferResponse) throws IOException {
75+
76+
if (body.length > 0) {
77+
long contentLength = request.getHeaders().getContentLength();
78+
if (contentLength > -1 && contentLength != body.length) {
79+
request.getHeaders().setContentLength(body.length);
80+
}
81+
if (request instanceof StreamingHttpOutputMessage streamingOutputMessage) {
82+
streamingOutputMessage.setBody(new StreamingHttpOutputMessage.Body() {
83+
@Override
84+
public void writeTo(OutputStream outputStream) throws IOException {
85+
StreamUtils.copy(body, outputStream);
86+
}
87+
88+
@Override
89+
public boolean repeatable() {
90+
return true;
91+
}
92+
});
93+
}
94+
else {
95+
StreamUtils.copy(body, request.getBody());
96+
}
97+
}
98+
99+
ClientHttpResponse response = request.execute();
100+
return (bufferResponse ? new BufferingClientHttpResponseWrapper(response) : response);
101+
}
62102

63103
}

spring-web/src/main/java/org/springframework/http/client/BufferingClientHttpRequestWrapper.java

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,13 +17,10 @@
1717
package org.springframework.http.client;
1818

1919
import java.io.IOException;
20-
import java.io.OutputStream;
2120
import java.net.URI;
2221

2322
import org.springframework.http.HttpHeaders;
2423
import org.springframework.http.HttpMethod;
25-
import org.springframework.http.StreamingHttpOutputMessage;
26-
import org.springframework.util.StreamUtils;
2724

2825
/**
2926
* Simple implementation of {@link ClientHttpRequest} that wraps another request.
@@ -54,28 +51,7 @@ public URI getURI() {
5451
@Override
5552
protected ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException {
5653
this.request.getHeaders().putAll(headers);
57-
58-
if (bufferedOutput.length > 0) {
59-
if (this.request instanceof StreamingHttpOutputMessage streamingHttpOutputMessage) {
60-
streamingHttpOutputMessage.setBody(new StreamingHttpOutputMessage.Body() {
61-
@Override
62-
public void writeTo(OutputStream outputStream) throws IOException {
63-
StreamUtils.copy(bufferedOutput, outputStream);
64-
}
65-
66-
@Override
67-
public boolean repeatable() {
68-
return true;
69-
}
70-
});
71-
}
72-
else {
73-
StreamUtils.copy(bufferedOutput, this.request.getBody());
74-
}
75-
}
76-
77-
ClientHttpResponse response = this.request.execute();
78-
return new BufferingClientHttpResponseWrapper(response);
54+
return executeWithRequest(this.request, bufferedOutput, true);
7955
}
8056

8157
}

spring-web/src/main/java/org/springframework/http/client/InterceptingClientHttpRequest.java

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,12 @@
1717
package org.springframework.http.client;
1818

1919
import java.io.IOException;
20-
import java.io.OutputStream;
2120
import java.net.URI;
2221
import java.util.List;
2322

2423
import org.springframework.http.HttpHeaders;
2524
import org.springframework.http.HttpMethod;
2625
import org.springframework.http.HttpRequest;
27-
import org.springframework.http.StreamingHttpOutputMessage;
28-
import org.springframework.util.StreamUtils;
2926

3027
/**
3128
* Wrapper for a {@link ClientHttpRequest} that has support for {@link ClientHttpRequestInterceptor
@@ -80,7 +77,7 @@ private ClientHttpRequestExecution getExecution() {
8077
}
8178

8279

83-
private static class EndOfChainRequestExecution implements ClientHttpRequestExecution {
80+
private class EndOfChainRequestExecution implements ClientHttpRequestExecution {
8481

8582
private final ClientHttpRequestFactory requestFactory;
8683

@@ -90,33 +87,10 @@ public EndOfChainRequestExecution(ClientHttpRequestFactory requestFactory) {
9087

9188
@Override
9289
public ClientHttpResponse execute(HttpRequest request, byte[] body) throws IOException {
93-
HttpMethod method = request.getMethod();
94-
ClientHttpRequest delegate = this.requestFactory.createRequest(request.getURI(), method);
90+
ClientHttpRequest delegate = this.requestFactory.createRequest(request.getURI(), request.getMethod());
9591
request.getHeaders().forEach((key, value) -> delegate.getHeaders().addAll(key, value));
9692
request.getAttributes().forEach((key, value) -> delegate.getAttributes().put(key, value));
97-
if (body.length > 0) {
98-
long contentLength = delegate.getHeaders().getContentLength();
99-
if (contentLength > -1 && contentLength != body.length) {
100-
delegate.getHeaders().setContentLength(body.length);
101-
}
102-
if (delegate instanceof StreamingHttpOutputMessage streamingOutputMessage) {
103-
streamingOutputMessage.setBody(new StreamingHttpOutputMessage.Body() {
104-
@Override
105-
public void writeTo(OutputStream outputStream) throws IOException {
106-
StreamUtils.copy(body, outputStream);
107-
}
108-
109-
@Override
110-
public boolean repeatable() {
111-
return true;
112-
}
113-
});
114-
}
115-
else {
116-
StreamUtils.copy(body, delegate.getBody());
117-
}
118-
}
119-
return delegate.execute();
93+
return executeWithRequest(delegate, body, false);
12094
}
12195
}
12296

0 commit comments

Comments
 (0)