diff --git a/spring-web/src/main/java/org/springframework/http/HttpInputMessage.java b/spring-web/src/main/java/org/springframework/http/HttpInputMessage.java
index 9aeab46b9a..61874849f7 100644
--- a/spring-web/src/main/java/org/springframework/http/HttpInputMessage.java
+++ b/spring-web/src/main/java/org/springframework/http/HttpInputMessage.java
@@ -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;
diff --git a/spring-web/src/main/java/org/springframework/http/HttpOutputMessage.java b/spring-web/src/main/java/org/springframework/http/HttpOutputMessage.java
index fdee15b0c9..f8c7f866c8 100644
--- a/spring-web/src/main/java/org/springframework/http/HttpOutputMessage.java
+++ b/spring-web/src/main/java/org/springframework/http/HttpOutputMessage.java
@@ -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;
diff --git a/spring-web/src/main/java/org/springframework/http/StreamingHttpOutputMessage.java b/spring-web/src/main/java/org/springframework/http/StreamingHttpOutputMessage.java
index 66facbb588..92b6a6d64d 100644
--- a/spring-web/src/main/java/org/springframework/http/StreamingHttpOutputMessage.java
+++ b/spring-web/src/main/java/org/springframework/http/StreamingHttpOutputMessage.java
@@ -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;
}
diff --git a/spring-web/src/main/java/org/springframework/http/client/ClientHttpRequestInterceptor.java b/spring-web/src/main/java/org/springframework/http/client/ClientHttpRequestInterceptor.java
index 410cdeb2ff..67b6596944 100644
--- a/spring-web/src/main/java/org/springframework/http/client/ClientHttpRequestInterceptor.java
+++ b/spring-web/src/main/java/org/springframework/http/client/ClientHttpRequestInterceptor.java
@@ -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}.
*
- *
The main entry point for interceptors is {@link #intercept(HttpRequest, byte[], ClientHttpRequestExecution)}.
+ *
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.
*
A typical implementation of this method would follow the following pattern:
*
* - Examine the {@linkplain HttpRequest request} and body
- * - Optionally {@linkplain org.springframework.http.client.support.HttpRequestWrapper wrap} the request to filter HTTP attributes.
+ * - Optionally {@linkplain org.springframework.http.client.support.HttpRequestWrapper
+ * wrap} the request to filter HTTP attributes.
* - Optionally modify the body of the request.
* - Either
*
- * - execute the request using {@link ClientHttpRequestExecution#execute(org.springframework.http.HttpRequest, byte[])},
+ * - execute the request using
+ * {@link ClientHttpRequestExecution#execute(org.springframework.http.HttpRequest, byte[])},
* or
* - do not execute the request to block the execution altogether.
*
* - Optionally wrap the response to filter HTTP attributes.
*
- *
* @param request the request, containing method, URI, and headers
* @param body the body of the request
* @param execution the request execution
diff --git a/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequestFactory.java b/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequestFactory.java
index e81f9145cc..c626ae7009 100644
--- a/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequestFactory.java
+++ b/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequestFactory.java
@@ -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.
* 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;
diff --git a/spring-web/src/main/java/org/springframework/http/client/InterceptingClientHttpRequest.java b/spring-web/src/main/java/org/springframework/http/client/InterceptingClientHttpRequest.java
index 95228d3862..141690e0c4 100644
--- a/spring-web/src/main/java/org/springframework/http/client/InterceptingClientHttpRequest.java
+++ b/spring-web/src/main/java/org/springframework/http/client/InterceptingClientHttpRequest.java
@@ -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();
}
diff --git a/spring-web/src/main/java/org/springframework/http/converter/AbstractHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/AbstractHttpMessageConverter.java
index 3bcf378523..88019fc56c 100644
--- a/spring-web/src/main/java/org/springframework/http/converter/AbstractHttpMessageConverter.java
+++ b/spring-web/src/main/java/org/springframework/http/converter/AbstractHttpMessageConverter.java
@@ -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 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 {
diff --git a/spring-web/src/main/java/org/springframework/http/converter/BufferedImageHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/BufferedImageHttpMessageConverter.java
index 359cac6302..f40dd598ea 100644
--- a/spring-web/src/main/java/org/springframework/http/converter/BufferedImageHttpMessageConverter.java
+++ b/spring-web/src/main/java/org/springframework/http/converter/BufferedImageHttpMessageConverter.java
@@ -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.
diff --git a/spring-web/src/test/java/org/springframework/http/client/AbstractHttpRequestFactoryTestCase.java b/spring-web/src/test/java/org/springframework/http/client/AbstractHttpRequestFactoryTestCase.java
index c81f183ee0..524fe2a61d 100644
--- a/spring-web/src/test/java/org/springframework/http/client/AbstractHttpRequestFactoryTestCase.java
+++ b/spring-web/src/test/java/org/springframework/http/client/AbstractHttpRequestFactoryTestCase.java
@@ -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.
@@ -69,6 +69,7 @@ public abstract class AbstractHttpRequestFactoryTestCase extends AbstractMockWeb
ClientHttpRequest request = factory.createRequest(uri, HttpMethod.GET);
assertEquals("Invalid HTTP method", HttpMethod.GET, request.getMethod());
assertEquals("Invalid HTTP URI", uri, request.getURI());
+
ClientHttpResponse response = request.execute();
try {
assertEquals("Invalid status code", HttpStatus.NOT_FOUND, response.getStatusCode());
@@ -82,6 +83,7 @@ public abstract class AbstractHttpRequestFactoryTestCase extends AbstractMockWeb
public void echo() throws Exception {
ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/echo"), HttpMethod.PUT);
assertEquals("Invalid HTTP method", HttpMethod.PUT, request.getMethod());
+
String headerName = "MyHeader";
String headerValue1 = "value1";
request.getHeaders().add(headerName, headerValue1);
@@ -89,6 +91,7 @@ public abstract class AbstractHttpRequestFactoryTestCase extends AbstractMockWeb
request.getHeaders().add(headerName, headerValue2);
final byte[] body = "Hello World".getBytes("UTF-8");
request.getHeaders().setContentLength(body.length);
+
if (request instanceof StreamingHttpOutputMessage) {
StreamingHttpOutputMessage streamingRequest =
(StreamingHttpOutputMessage) request;
@@ -102,6 +105,7 @@ public abstract class AbstractHttpRequestFactoryTestCase extends AbstractMockWeb
else {
StreamUtils.copy(body, request.getBody());
}
+
ClientHttpResponse response = request.execute();
try {
assertEquals("Invalid status code", HttpStatus.OK, response.getStatusCode());
@@ -121,8 +125,7 @@ public abstract class AbstractHttpRequestFactoryTestCase extends AbstractMockWeb
ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/echo"), HttpMethod.POST);
final byte[] body = "Hello World".getBytes("UTF-8");
if (request instanceof StreamingHttpOutputMessage) {
- StreamingHttpOutputMessage streamingRequest =
- (StreamingHttpOutputMessage) request;
+ StreamingHttpOutputMessage streamingRequest = (StreamingHttpOutputMessage) request;
streamingRequest.setBody(new StreamingHttpOutputMessage.Body() {
@Override
public void writeTo(OutputStream outputStream) throws IOException {
@@ -136,16 +139,18 @@ public abstract class AbstractHttpRequestFactoryTestCase extends AbstractMockWeb
StreamUtils.copy(body, request.getBody());
}
- ClientHttpResponse response = request.execute();
+ request.execute();
FileCopyUtils.copy(body, request.getBody());
}
@Test(expected = UnsupportedOperationException.class)
public void headersAfterExecute() throws Exception {
ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/echo"), HttpMethod.POST);
+
request.getHeaders().add("MyHeader", "value");
byte[] body = "Hello World".getBytes("UTF-8");
FileCopyUtils.copy(body, request.getBody());
+
ClientHttpResponse response = request.execute();
try {
request.getHeaders().add("MyHeader", "value");
diff --git a/spring-web/src/test/java/org/springframework/http/client/InterceptingStreamingHttpComponentsTests.java b/spring-web/src/test/java/org/springframework/http/client/InterceptingStreamingHttpComponentsTests.java
new file mode 100644
index 0000000000..9416c81874
--- /dev/null
+++ b/spring-web/src/test/java/org/springframework/http/client/InterceptingStreamingHttpComponentsTests.java
@@ -0,0 +1,41 @@
+/*
+ * 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.http.client;
+
+import org.junit.Test;
+
+import org.springframework.http.HttpMethod;
+
+/**
+ * @author Juergen Hoeller
+ */
+public class InterceptingStreamingHttpComponentsTests extends AbstractHttpRequestFactoryTestCase {
+
+ @Override
+ protected ClientHttpRequestFactory createRequestFactory() {
+ HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
+ requestFactory.setBufferRequestBody(false);
+ return new InterceptingClientHttpRequestFactory(requestFactory, null);
+ }
+
+ @Override
+ @Test
+ public void httpMethods() throws Exception {
+ assertHttpMethod("patch", HttpMethod.PATCH);
+ }
+
+}
diff --git a/spring-web/src/test/java/org/springframework/http/client/StreamingHttpComponentsClientHttpRequestFactoryTests.java b/spring-web/src/test/java/org/springframework/http/client/StreamingHttpComponentsClientHttpRequestFactoryTests.java
index 090e3fe5b4..5ad9eab2d7 100644
--- a/spring-web/src/test/java/org/springframework/http/client/StreamingHttpComponentsClientHttpRequestFactoryTests.java
+++ b/spring-web/src/test/java/org/springframework/http/client/StreamingHttpComponentsClientHttpRequestFactoryTests.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2013 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.
@@ -20,13 +20,14 @@ import org.junit.Test;
import org.springframework.http.HttpMethod;
-public class StreamingHttpComponentsClientHttpRequestFactoryTests
- extends AbstractHttpRequestFactoryTestCase {
+/**
+ * @author Arjen Poutsma
+ */
+public class StreamingHttpComponentsClientHttpRequestFactoryTests extends AbstractHttpRequestFactoryTestCase {
@Override
protected ClientHttpRequestFactory createRequestFactory() {
- HttpComponentsClientHttpRequestFactory requestFactory =
- new HttpComponentsClientHttpRequestFactory();
+ HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setBufferRequestBody(false);
return requestFactory;
}
diff --git a/spring-web/src/test/java/org/springframework/http/client/StreamingSimpleHttpRequestFactoryTests.java b/spring-web/src/test/java/org/springframework/http/client/StreamingSimpleClientHttpRequestFactoryTests.java
similarity index 76%
rename from spring-web/src/test/java/org/springframework/http/client/StreamingSimpleHttpRequestFactoryTests.java
rename to spring-web/src/test/java/org/springframework/http/client/StreamingSimpleClientHttpRequestFactoryTests.java
index 2c6cc6699a..e494e46291 100644
--- a/spring-web/src/test/java/org/springframework/http/client/StreamingSimpleHttpRequestFactoryTests.java
+++ b/spring-web/src/test/java/org/springframework/http/client/StreamingSimpleClientHttpRequestFactoryTests.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2013 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.
@@ -16,7 +16,6 @@
package org.springframework.http.client;
-import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.util.Collections;
@@ -27,12 +26,14 @@ import org.junit.Test;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
-import org.springframework.http.HttpRequest;
import org.springframework.http.HttpStatus;
import static org.junit.Assert.*;
-public class StreamingSimpleHttpRequestFactoryTests extends AbstractHttpRequestFactoryTestCase {
+/**
+ * @author Arjen Poutsma
+ */
+public class StreamingSimpleClientHttpRequestFactoryTests extends AbstractHttpRequestFactoryTestCase {
@Override
protected ClientHttpRequestFactory createRequestFactory() {
@@ -41,21 +42,16 @@ public class StreamingSimpleHttpRequestFactoryTests extends AbstractHttpRequestF
return factory;
}
- // SPR-8809
- @Test
+ @Test // SPR-8809
public void interceptor() throws Exception {
final String headerName = "MyHeader";
final String headerValue = "MyValue";
- ClientHttpRequestInterceptor interceptor = new ClientHttpRequestInterceptor() {
- @Override
- public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
- throws IOException {
- request.getHeaders().add(headerName, headerValue);
- return execution.execute(request, body);
- }
+ ClientHttpRequestInterceptor interceptor = (request, body, execution) -> {
+ request.getHeaders().add(headerName, headerValue);
+ return execution.execute(request, body);
};
- InterceptingClientHttpRequestFactory factory = new InterceptingClientHttpRequestFactory(createRequestFactory(),
- Collections.singletonList(interceptor));
+ InterceptingClientHttpRequestFactory factory = new InterceptingClientHttpRequestFactory(
+ createRequestFactory(), Collections.singletonList(interceptor));
ClientHttpResponse response = null;
try {
@@ -81,8 +77,8 @@ public class StreamingSimpleHttpRequestFactoryTests extends AbstractHttpRequestF
ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/methods/post"), HttpMethod.POST);
final int BUF_SIZE = 4096;
final int ITERATIONS = Integer.MAX_VALUE / BUF_SIZE;
-// final int contentLength = ITERATIONS * BUF_SIZE;
-// request.getHeaders().setContentLength(contentLength);
+ // final int contentLength = ITERATIONS * BUF_SIZE;
+ // request.getHeaders().setContentLength(contentLength);
OutputStream body = request.getBody();
for (int i = 0; i < ITERATIONS; i++) {
byte[] buffer = new byte[BUF_SIZE];