Refactoring in AbstractBufferingClientHttpRequest
Extract a protected method for subclasses to use to perform the actual (end-of-chain) request execution. See gh-33785
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
* Copyright 2002-2025 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.
|
||||
@@ -20,7 +20,9 @@ import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.StreamingHttpOutputMessage;
|
||||
import org.springframework.util.FastByteArrayOutputStream;
|
||||
import org.springframework.util.StreamUtils;
|
||||
|
||||
/**
|
||||
* Base implementation of {@link ClientHttpRequest} that buffers output
|
||||
@@ -59,5 +61,43 @@ abstract class AbstractBufferingClientHttpRequest extends AbstractClientHttpRequ
|
||||
protected abstract ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput)
|
||||
throws IOException;
|
||||
|
||||
/**
|
||||
* Execute with the given request and body.
|
||||
* @param request the request to execute with
|
||||
* @param body the body to send
|
||||
* @param bufferResponse whether to buffer the response
|
||||
* @return the resulting response
|
||||
* @throws IOException in case of I/O errors from execution
|
||||
* @since 7.0
|
||||
*/
|
||||
protected ClientHttpResponse executeWithRequest(
|
||||
ClientHttpRequest request, byte[] body, boolean bufferResponse) throws IOException {
|
||||
|
||||
if (body.length > 0) {
|
||||
long contentLength = request.getHeaders().getContentLength();
|
||||
if (contentLength > -1 && contentLength != body.length) {
|
||||
request.getHeaders().setContentLength(body.length);
|
||||
}
|
||||
if (request instanceof StreamingHttpOutputMessage streamingOutputMessage) {
|
||||
streamingOutputMessage.setBody(new StreamingHttpOutputMessage.Body() {
|
||||
@Override
|
||||
public void writeTo(OutputStream outputStream) throws IOException {
|
||||
StreamUtils.copy(body, outputStream);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean repeatable() {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
StreamUtils.copy(body, request.getBody());
|
||||
}
|
||||
}
|
||||
|
||||
ClientHttpResponse response = request.execute();
|
||||
return (bufferResponse ? new BufferingClientHttpResponseWrapper(response) : response);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
* Copyright 2002-2025 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.
|
||||
@@ -17,13 +17,10 @@
|
||||
package org.springframework.http.client;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URI;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.StreamingHttpOutputMessage;
|
||||
import org.springframework.util.StreamUtils;
|
||||
|
||||
/**
|
||||
* Simple implementation of {@link ClientHttpRequest} that wraps another request.
|
||||
@@ -54,28 +51,7 @@ final class BufferingClientHttpRequestWrapper extends AbstractBufferingClientHtt
|
||||
@Override
|
||||
protected ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException {
|
||||
this.request.getHeaders().putAll(headers);
|
||||
|
||||
if (bufferedOutput.length > 0) {
|
||||
if (this.request instanceof StreamingHttpOutputMessage streamingHttpOutputMessage) {
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
ClientHttpResponse response = this.request.execute();
|
||||
return new BufferingClientHttpResponseWrapper(response);
|
||||
return executeWithRequest(this.request, bufferedOutput, true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -17,15 +17,12 @@
|
||||
package org.springframework.http.client;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpRequest;
|
||||
import org.springframework.http.StreamingHttpOutputMessage;
|
||||
import org.springframework.util.StreamUtils;
|
||||
|
||||
/**
|
||||
* Wrapper for a {@link ClientHttpRequest} that has support for {@link ClientHttpRequestInterceptor
|
||||
@@ -80,7 +77,7 @@ class InterceptingClientHttpRequest extends AbstractBufferingClientHttpRequest {
|
||||
}
|
||||
|
||||
|
||||
private static class EndOfChainRequestExecution implements ClientHttpRequestExecution {
|
||||
private class EndOfChainRequestExecution implements ClientHttpRequestExecution {
|
||||
|
||||
private final ClientHttpRequestFactory requestFactory;
|
||||
|
||||
@@ -90,33 +87,10 @@ class InterceptingClientHttpRequest extends AbstractBufferingClientHttpRequest {
|
||||
|
||||
@Override
|
||||
public ClientHttpResponse execute(HttpRequest request, byte[] body) throws IOException {
|
||||
HttpMethod method = request.getMethod();
|
||||
ClientHttpRequest delegate = this.requestFactory.createRequest(request.getURI(), method);
|
||||
ClientHttpRequest delegate = this.requestFactory.createRequest(request.getURI(), request.getMethod());
|
||||
request.getHeaders().forEach((key, value) -> delegate.getHeaders().addAll(key, value));
|
||||
request.getAttributes().forEach((key, value) -> delegate.getAttributes().put(key, value));
|
||||
if (body.length > 0) {
|
||||
long contentLength = delegate.getHeaders().getContentLength();
|
||||
if (contentLength > -1 && contentLength != body.length) {
|
||||
delegate.getHeaders().setContentLength(body.length);
|
||||
}
|
||||
if (delegate instanceof StreamingHttpOutputMessage streamingOutputMessage) {
|
||||
streamingOutputMessage.setBody(new StreamingHttpOutputMessage.Body() {
|
||||
@Override
|
||||
public void writeTo(OutputStream outputStream) throws IOException {
|
||||
StreamUtils.copy(body, outputStream);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean repeatable() {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
StreamUtils.copy(body, delegate.getBody());
|
||||
}
|
||||
}
|
||||
return delegate.execute();
|
||||
return executeWithRequest(delegate, body, false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user