Skip to content

Commit e0ac000

Browse files
committed
Allow repeatable writes in StreamingHttpOutputMessage
This commit adds a repeatable property to StreamingHttpOutputMessage.Body, indicating that the body can be written multiple times. In HttpComponentsClientHttpRequest, this property is exposed via org.apache.hc.core5.http.HttpEntity.isRepeatable, to allow for redirects. Closes gh-31449
1 parent 71330dd commit e0ac000

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

spring-web/src/main/java/org/springframework/http/StreamingHttpOutputMessage.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -50,6 +50,18 @@ interface Body {
5050
* @throws IOException in case of I/O errors
5151
*/
5252
void writeTo(OutputStream outputStream) throws IOException;
53+
54+
/**
55+
* Indicates whether this body is capable of
56+
* {@linkplain #writeTo(OutputStream) writing its data} more than
57+
* once. Default implementation returns {@code false}.
58+
* @return {@code true} if this body can be written repeatedly,
59+
* {@code false} otherwise
60+
* @since 6.1
61+
*/
62+
default boolean repeatable() {
63+
return false;
64+
}
5365
}
5466

5567
}

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.http.client;
1818

1919
import java.io.IOException;
20+
import java.io.OutputStream;
2021
import java.net.URI;
2122

2223
import org.springframework.http.HttpHeaders;
@@ -55,7 +56,17 @@ protected ClientHttpResponse executeInternal(HttpHeaders headers, byte[] buffere
5556
this.request.getHeaders().putAll(headers);
5657

5758
if (this.request instanceof StreamingHttpOutputMessage streamingHttpOutputMessage) {
58-
streamingHttpOutputMessage.setBody(outputStream -> StreamUtils.copy(bufferedOutput, outputStream));
59+
streamingHttpOutputMessage.setBody(new StreamingHttpOutputMessage.Body() {
60+
@Override
61+
public void writeTo(OutputStream outputStream) throws IOException {
62+
StreamUtils.copy(bufferedOutput, outputStream);
63+
}
64+
65+
@Override
66+
public boolean repeatable() {
67+
return true;
68+
}
69+
});
5970
}
6071
else {
6172
StreamUtils.copy(bufferedOutput, this.request.getBody());

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,12 @@ public void writeTo(OutputStream outStream) throws IOException {
153153

154154
@Override
155155
public boolean isRepeatable() {
156-
return false;
156+
return this.body.repeatable();
157157
}
158158

159159
@Override
160160
public boolean isStreaming() {
161-
return true;
161+
return false;
162162
}
163163

164164
@Override

0 commit comments

Comments
 (0)