Skip to content

Commit

Permalink
Allow repeatable writes in StreamingHttpOutputMessage
Browse files Browse the repository at this point in the history
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
  • Loading branch information
poutsma committed Oct 24, 2023
1 parent 71330dd commit e0ac000
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2023 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.
Expand Down Expand Up @@ -50,6 +50,18 @@ interface Body {
* @throws IOException in case of I/O errors
*/
void writeTo(OutputStream outputStream) throws IOException;

/**
* Indicates whether this body is capable of
* {@linkplain #writeTo(OutputStream) writing its data} more than
* once. Default implementation returns {@code false}.
* @return {@code true} if this body can be written repeatedly,
* {@code false} otherwise
* @since 6.1
*/
default boolean repeatable() {
return false;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.http.client;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;

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

if (this.request instanceof StreamingHttpOutputMessage streamingHttpOutputMessage) {
streamingHttpOutputMessage.setBody(outputStream -> StreamUtils.copy(bufferedOutput, outputStream));
streamingHttpOutputMessage.setBody(new StreamingHttpOutputMessage.Body() {
@Override
public void writeTo(OutputStream outputStream) throws IOException {
StreamUtils.copy(bufferedOutput, outputStream);
}

@Override
public boolean repeatable() {
return true;
}
});
}
else {
StreamUtils.copy(bufferedOutput, this.request.getBody());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,12 @@ public void writeTo(OutputStream outStream) throws IOException {

@Override
public boolean isRepeatable() {
return false;
return this.body.repeatable();
}

@Override
public boolean isStreaming() {
return true;
return false;
}

@Override
Expand Down

0 comments on commit e0ac000

Please sign in to comment.