2020import java .io .OutputStream ;
2121
2222import org .springframework .http .HttpHeaders ;
23+ import org .springframework .http .HttpOutputMessage ;
2324import org .springframework .http .StreamingHttpOutputMessage ;
2425import org .springframework .lang .Nullable ;
2526import org .springframework .util .Assert ;
2627import org .springframework .util .FastByteArrayOutputStream ;
2728
2829/**
29- * Abstract base for {@link ClientHttpRequest} that also implement
30- * {@link StreamingHttpOutputMessage}. Ensures that headers and
31- * body are not written multiple times.
30+ * Extension of {@link AbstractClientHttpRequest} that adds the ability to stream
31+ * request body content directly to the underlying HTTP client library through
32+ * the {@link StreamingHttpOutputMessage} contract.
33+ *
34+ * <p>It is necessary to call {@link #setBody} and stream the request body through
35+ * a callback for access to the {@code OutputStream}. The alternative to call
36+ * {@link #getBody()} is also supported as a fallback, but that does not stream,
37+ * and returns an aggregating {@code OutputStream} instead.
3238 *
3339 * @author Arjen Poutsma
40+ * @author Rossen Stoyanchev
3441 * @since 6.1
3542 */
3643abstract class AbstractStreamingClientHttpRequest extends AbstractClientHttpRequest
@@ -43,6 +50,12 @@ abstract class AbstractStreamingClientHttpRequest extends AbstractClientHttpRequ
4350 private FastByteArrayOutputStream bodyStream ;
4451
4552
53+ /**
54+ * Implements the {@link HttpOutputMessage} contract for request body content.
55+ * <p>Note that this method does not result in streaming, and the returned
56+ * {@code OutputStream} aggregates the full content in a byte[] before
57+ * sending. To use streaming, call {@link #setBody} instead.
58+ */
4659 @ Override
4760 protected final OutputStream getBodyInternal (HttpHeaders headers ) {
4861 Assert .state (this .body == null , "Invoke either getBody or setBody; not both" );
@@ -53,6 +66,10 @@ protected final OutputStream getBodyInternal(HttpHeaders headers) {
5366 return this .bodyStream ;
5467 }
5568
69+ /**
70+ * Implements the {@link StreamingHttpOutputMessage} contract for writing
71+ * request body by streaming directly to the underlying HTTP client.
72+ */
5673 @ Override
5774 public final void setBody (Body body ) {
5875 Assert .notNull (body , "Body must not be null" );
@@ -73,12 +90,14 @@ protected final ClientHttpResponse executeInternal(HttpHeaders headers) throws I
7390
7491
7592 /**
76- * Abstract template method that writes the given headers and content to the HTTP request.
77- * @param headers the HTTP headers
93+ * Abstract method for concrete implementations to write the headers and
94+ * {@link StreamingHttpOutputMessage.Body} to the HTTP request.
95+ * @param headers the HTTP headers for the request
7896 * @param body the HTTP body, may be {@code null} if no body was {@linkplain #setBody(Body) set}
7997 * @return the response object for the executed request
8098 * @since 6.1
8199 */
82- protected abstract ClientHttpResponse executeInternal (HttpHeaders headers , @ Nullable Body body ) throws IOException ;
100+ protected abstract ClientHttpResponse executeInternal (
101+ HttpHeaders headers , @ Nullable Body body ) throws IOException ;
83102
84103}
0 commit comments