package de.dfki.nlp.io; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpRequest; import org.springframework.http.HttpStatus; import org.springframework.http.client.ClientHttpRequestExecution; import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.http.client.ClientHttpResponse; import org.springframework.util.FileCopyUtils; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; /** * Class that buffers the Client respone body, so it can be read multiple times. * Reimplementation of the same spring class - due to package privacy. */ public class BufferingClientHttpResponseWrapper implements ClientHttpRequestInterceptor { @Override public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { // wrap the response return new WrappenClientHttpResponse(execution.execute(request, body)); } private class WrappenClientHttpResponse implements ClientHttpResponse { private final ClientHttpResponse delegate; private byte[] bytes; private WrappenClientHttpResponse(ClientHttpResponse delegate) { this.delegate = delegate; } @Override public HttpStatus getStatusCode() throws IOException { return delegate.getStatusCode(); } @Override public int getRawStatusCode() throws IOException { return delegate.getRawStatusCode(); } @Override public String getStatusText() throws IOException { return delegate.getStatusText(); } @Override public void close() { delegate.close(); } @Override public InputStream getBody() throws IOException { if (bytes == null) { bytes = FileCopyUtils.copyToByteArray(delegate.getBody()); } return new ByteArrayInputStream(bytes); } @Override public HttpHeaders getHeaders() { return delegate.getHeaders(); } } }