Created
March 25, 2025 08:38
-
-
Save jkuipers/dc455a6614b3747e4c4b180c711626f2 to your computer and use it in GitHub Desktop.
Wrapper for {@link ClientHttpResponse} that ensures clients can repeatedly read the given response body
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.springframework.http.HttpHeaders; | |
import org.springframework.http.HttpStatusCode; | |
import org.springframework.http.client.ClientHttpResponse; | |
import java.io.ByteArrayInputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
/** | |
* Wrapper for {@link ClientHttpResponse} that ensures clients can repeatedly read the given response body. | |
*/ | |
public class BufferedClientHttpResponseWrapper implements ClientHttpResponse { | |
private final ClientHttpResponse response; | |
private final byte[] body; | |
public BufferedClientHttpResponseWrapper(ClientHttpResponse response, byte[] body) { | |
this.response = response; | |
this.body = body; | |
} | |
@Override | |
public HttpStatusCode getStatusCode() throws IOException { | |
return this.response.getStatusCode(); | |
} | |
@SuppressWarnings("removal") | |
@Override | |
public int getRawStatusCode() throws IOException { | |
return this.response.getRawStatusCode(); | |
} | |
@Override | |
public String getStatusText() throws IOException { | |
return this.response.getStatusText(); | |
} | |
@Override | |
public HttpHeaders getHeaders() { | |
return this.response.getHeaders(); | |
} | |
@Override | |
public InputStream getBody() { | |
return new ByteArrayInputStream(this.body); | |
} | |
@Override | |
public void close() { | |
this.response.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment