InterceptingClientHttpRequest adapts to StreamingHttpOutputMessage
Issue: SPR-16582
(cherry picked from commit 4173022)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2018 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.
|
||||
@@ -34,7 +34,7 @@ public interface HttpInputMessage extends HttpMessage {
|
||||
/**
|
||||
* Return the body of the message as an input stream.
|
||||
* @return the input stream body (never {@code null})
|
||||
* @throws IOException in case of I/O Errors
|
||||
* @throws IOException in case of I/O errors
|
||||
*/
|
||||
InputStream getBody() throws IOException;
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2018 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.
|
||||
@@ -34,7 +34,7 @@ public interface HttpOutputMessage extends HttpMessage {
|
||||
/**
|
||||
* Return the body of the message as an output stream.
|
||||
* @return the output stream body (never {@code null})
|
||||
* @throws IOException in case of I/O Errors
|
||||
* @throws IOException in case of I/O errors
|
||||
*/
|
||||
OutputStream getBody() throws IOException;
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2018 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.
|
||||
@@ -30,23 +30,23 @@ import java.io.OutputStream;
|
||||
public interface StreamingHttpOutputMessage extends HttpOutputMessage {
|
||||
|
||||
/**
|
||||
* Set the streaming body for this message.
|
||||
* @param body the streaming body
|
||||
* Set the streaming body callback for this message.
|
||||
* @param body the streaming body callback
|
||||
*/
|
||||
void setBody(Body body);
|
||||
|
||||
|
||||
/**
|
||||
* Defines the contract for bodies that can be written directly to an {@link OutputStream}.
|
||||
* It is useful with HTTP client libraries that provide indirect access to an
|
||||
* {@link OutputStream} via a callback mechanism.
|
||||
* Defines the contract for bodies that can be written directly to an
|
||||
* {@link OutputStream}. Useful with HTTP client libraries that provide
|
||||
* indirect access to an {@link OutputStream} via a callback mechanism.
|
||||
*/
|
||||
interface Body {
|
||||
|
||||
/**
|
||||
* Write this body to the given {@link OutputStream}.
|
||||
* @param outputStream the output stream to write to
|
||||
* @throws IOException in case of errors
|
||||
* @throws IOException in case of I/O errors
|
||||
*/
|
||||
void writeTo(OutputStream outputStream) throws IOException;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2018 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.
|
||||
@@ -21,12 +21,14 @@ import java.io.IOException;
|
||||
import org.springframework.http.HttpRequest;
|
||||
|
||||
/**
|
||||
* Intercepts client-side HTTP requests. Implementations of this interface can be {@linkplain
|
||||
* org.springframework.web.client.RestTemplate#setInterceptors(java.util.List) registered} with the
|
||||
* {@link org.springframework.web.client.RestTemplate RestTemplate}, as to modify the outgoing {@link ClientHttpRequest}
|
||||
* and/or the incoming {@link ClientHttpResponse}.
|
||||
* Intercepts client-side HTTP requests. Implementations of this interface can be
|
||||
* {@linkplain org.springframework.web.client.RestTemplate#setInterceptors registered}
|
||||
* with the {@link org.springframework.web.client.RestTemplate RestTemplate},
|
||||
* as to modify the outgoing {@link ClientHttpRequest} and/or the incoming
|
||||
* {@link ClientHttpResponse}.
|
||||
*
|
||||
* <p>The main entry point for interceptors is {@link #intercept(HttpRequest, byte[], ClientHttpRequestExecution)}.
|
||||
* <p>The main entry point for interceptors is
|
||||
* {@link #intercept(HttpRequest, byte[], ClientHttpRequestExecution)}.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 3.1
|
||||
@@ -34,23 +36,24 @@ import org.springframework.http.HttpRequest;
|
||||
public interface ClientHttpRequestInterceptor {
|
||||
|
||||
/**
|
||||
* Intercept the given request, and return a response. The given {@link ClientHttpRequestExecution} allows
|
||||
* the interceptor to pass on the request and response to the next entity in the chain.
|
||||
*
|
||||
* Intercept the given request, and return a response. The given
|
||||
* {@link ClientHttpRequestExecution} allows the interceptor to pass on the
|
||||
* request and response to the next entity in the chain.
|
||||
* <p>A typical implementation of this method would follow the following pattern:
|
||||
* <ol>
|
||||
* <li>Examine the {@linkplain HttpRequest request} and body</li>
|
||||
* <li>Optionally {@linkplain org.springframework.http.client.support.HttpRequestWrapper wrap} the request to filter HTTP attributes.</li>
|
||||
* <li>Optionally {@linkplain org.springframework.http.client.support.HttpRequestWrapper
|
||||
* wrap} the request to filter HTTP attributes.</li>
|
||||
* <li>Optionally modify the body of the request.</li>
|
||||
* <li><strong>Either</strong>
|
||||
* <ul>
|
||||
* <li>execute the request using {@link ClientHttpRequestExecution#execute(org.springframework.http.HttpRequest, byte[])},</li>
|
||||
* <li>execute the request using
|
||||
* {@link ClientHttpRequestExecution#execute(org.springframework.http.HttpRequest, byte[])},</li>
|
||||
* <strong>or</strong>
|
||||
* <li>do not execute the request to block the execution altogether.</li>
|
||||
* </ul>
|
||||
* <li>Optionally wrap the response to filter HTTP attributes.</li>
|
||||
* </ol>
|
||||
*
|
||||
* @param request the request, containing method, URI, and headers
|
||||
* @param body the body of the request
|
||||
* @param execution the request execution
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 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.
|
||||
@@ -195,6 +195,7 @@ public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequest
|
||||
* Indicates whether this request factory should buffer the request body internally.
|
||||
* <p>Default is {@code true}. When sending large amounts of data via POST or PUT, it is
|
||||
* recommended to change this property to {@code false}, so as not to run out of memory.
|
||||
* @since 4.0
|
||||
*/
|
||||
public void setBufferRequestBody(boolean bufferRequestBody) {
|
||||
this.bufferRequestBody = bufferRequestBody;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 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,6 +17,7 @@
|
||||
package org.springframework.http.client;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URI;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
@@ -25,6 +26,7 @@ import java.util.Map;
|
||||
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;
|
||||
|
||||
/**
|
||||
@@ -80,7 +82,7 @@ class InterceptingClientHttpRequest extends AbstractBufferingClientHttpRequest {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClientHttpResponse execute(HttpRequest request, byte[] body) throws IOException {
|
||||
public ClientHttpResponse execute(HttpRequest request, final byte[] body) throws IOException {
|
||||
if (this.iterator.hasNext()) {
|
||||
ClientHttpRequestInterceptor nextInterceptor = this.iterator.next();
|
||||
return nextInterceptor.intercept(request, body, this);
|
||||
@@ -94,7 +96,18 @@ class InterceptingClientHttpRequest extends AbstractBufferingClientHttpRequest {
|
||||
}
|
||||
}
|
||||
if (body.length > 0) {
|
||||
StreamUtils.copy(body, delegate.getBody());
|
||||
if (delegate instanceof StreamingHttpOutputMessage) {
|
||||
StreamingHttpOutputMessage streamingOutputMessage = (StreamingHttpOutputMessage) delegate;
|
||||
streamingOutputMessage.setBody(new StreamingHttpOutputMessage.Body() {
|
||||
@Override
|
||||
public void writeTo(final OutputStream outputStream) throws IOException {
|
||||
StreamUtils.copy(body, outputStream);
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
StreamUtils.copy(body, delegate.getBody());
|
||||
}
|
||||
}
|
||||
return delegate.execute();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 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.
|
||||
@@ -205,8 +205,7 @@ public abstract class AbstractHttpMessageConverter<T> implements HttpMessageConv
|
||||
addDefaultHeaders(headers, t, contentType);
|
||||
|
||||
if (outputMessage instanceof StreamingHttpOutputMessage) {
|
||||
StreamingHttpOutputMessage streamingOutputMessage =
|
||||
(StreamingHttpOutputMessage) outputMessage;
|
||||
StreamingHttpOutputMessage streamingOutputMessage = (StreamingHttpOutputMessage) outputMessage;
|
||||
streamingOutputMessage.setBody(new StreamingHttpOutputMessage.Body() {
|
||||
@Override
|
||||
public void writeTo(final OutputStream outputStream) throws IOException {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2018 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.
|
||||
|
||||
Reference in New Issue
Block a user