Browse Source

InterceptingClientHttpRequest adapts to StreamingHttpOutputMessage

Issue: SPR-16582
pull/999/merge
Juergen Hoeller 8 years ago
parent
commit
41730220f4
  1. 4
      spring-web/src/main/java/org/springframework/http/HttpInputMessage.java
  2. 4
      spring-web/src/main/java/org/springframework/http/HttpOutputMessage.java
  3. 14
      spring-web/src/main/java/org/springframework/http/StreamingHttpOutputMessage.java
  4. 8
      spring-web/src/main/java/org/springframework/http/client/ClientHttpRequestInterceptor.java
  5. 3
      spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequestFactory.java
  6. 16
      spring-web/src/main/java/org/springframework/http/client/InterceptingClientHttpRequest.java
  7. 26
      spring-web/src/main/java/org/springframework/http/converter/AbstractHttpMessageConverter.java
  8. 9
      spring-web/src/main/java/org/springframework/http/converter/BufferedImageHttpMessageConverter.java
  9. 18
      spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java
  10. 35
      spring-web/src/test/java/org/springframework/http/client/AbstractHttpRequestFactoryTestCase.java
  11. 41
      spring-web/src/test/java/org/springframework/http/client/InterceptingStreamingHttpComponentsTests.java
  12. 11
      spring-web/src/test/java/org/springframework/http/client/StreamingHttpComponentsClientHttpRequestFactoryTests.java
  13. 30
      spring-web/src/test/java/org/springframework/http/client/StreamingSimpleClientHttpRequestFactoryTests.java

4
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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 body of the message as an input stream.
* @return the input stream body (never {@code null}) * @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; InputStream getBody() throws IOException;

4
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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 body of the message as an output stream.
* @return the output stream body (never {@code null}) * @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; OutputStream getBody() throws IOException;

14
spring-web/src/main/java/org/springframework/http/StreamingHttpOutputMessage.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -30,16 +30,16 @@ import java.io.OutputStream;
public interface StreamingHttpOutputMessage extends HttpOutputMessage { public interface StreamingHttpOutputMessage extends HttpOutputMessage {
/** /**
* Set the streaming body for this message. * Set the streaming body callback for this message.
* @param body the streaming body * @param body the streaming body callback
*/ */
void setBody(Body body); void setBody(Body body);
/** /**
* Defines the contract for bodies that can be written directly to an {@link OutputStream}. * Defines the contract for bodies that can be written directly to an
* It is useful with HTTP client libraries that provide indirect access to an * {@link OutputStream}. Useful with HTTP client libraries that provide
* {@link OutputStream} via a callback mechanism. * indirect access to an {@link OutputStream} via a callback mechanism.
*/ */
@FunctionalInterface @FunctionalInterface
interface Body { interface Body {
@ -47,7 +47,7 @@ public interface StreamingHttpOutputMessage extends HttpOutputMessage {
/** /**
* Write this body to the given {@link OutputStream}. * Write this body to the given {@link OutputStream}.
* @param outputStream the output stream to write to * @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; void writeTo(OutputStream outputStream) throws IOException;
} }

8
spring-web/src/main/java/org/springframework/http/client/ClientHttpRequestInterceptor.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -22,8 +22,8 @@ import org.springframework.http.HttpRequest;
/** /**
* Intercepts client-side HTTP requests. Implementations of this interface can be * Intercepts client-side HTTP requests. Implementations of this interface can be
* {@linkplain org.springframework.web.client.RestTemplate#setInterceptors(java.util.List) * {@linkplain org.springframework.web.client.RestTemplate#setInterceptors registered}
* registered} with the {@link org.springframework.web.client.RestTemplate RestTemplate}, * with the {@link org.springframework.web.client.RestTemplate RestTemplate},
* as to modify the outgoing {@link ClientHttpRequest} and/or the incoming * as to modify the outgoing {@link ClientHttpRequest} and/or the incoming
* {@link ClientHttpResponse}. * {@link ClientHttpResponse}.
* *
@ -40,7 +40,6 @@ public interface ClientHttpRequestInterceptor {
* Intercept the given request, and return a response. The given * Intercept the given request, and return a response. The given
* {@link ClientHttpRequestExecution} allows the interceptor to pass on the * {@link ClientHttpRequestExecution} allows the interceptor to pass on the
* request and response to the next entity in the chain. * request and response to the next entity in the chain.
*
* <p>A typical implementation of this method would follow the following pattern: * <p>A typical implementation of this method would follow the following pattern:
* <ol> * <ol>
* <li>Examine the {@linkplain HttpRequest request} and body</li> * <li>Examine the {@linkplain HttpRequest request} and body</li>
@ -56,7 +55,6 @@ public interface ClientHttpRequestInterceptor {
* </ul> * </ul>
* <li>Optionally wrap the response to filter HTTP attributes.</li> * <li>Optionally wrap the response to filter HTTP attributes.</li>
* </ol> * </ol>
*
* @param request the request, containing method, URI, and headers * @param request the request, containing method, URI, and headers
* @param body the body of the request * @param body the body of the request
* @param execution the request execution * @param execution the request execution

3
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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -145,6 +145,7 @@ public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequest
* Indicates whether this request factory should buffer the request body internally. * 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 * <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. * recommended to change this property to {@code false}, so as not to run out of memory.
* @since 4.0
*/ */
public void setBufferRequestBody(boolean bufferRequestBody) { public void setBufferRequestBody(boolean bufferRequestBody) {
this.bufferRequestBody = bufferRequestBody; this.bufferRequestBody = bufferRequestBody;

16
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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -20,11 +20,11 @@ import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.http.HttpRequest; import org.springframework.http.HttpRequest;
import org.springframework.http.StreamingHttpOutputMessage;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.StreamUtils; import org.springframework.util.StreamUtils;
@ -95,11 +95,15 @@ class InterceptingClientHttpRequest extends AbstractBufferingClientHttpRequest {
HttpMethod method = request.getMethod(); HttpMethod method = request.getMethod();
Assert.state(method != null, "No standard HTTP method"); Assert.state(method != null, "No standard HTTP method");
ClientHttpRequest delegate = requestFactory.createRequest(request.getURI(), method); ClientHttpRequest delegate = requestFactory.createRequest(request.getURI(), method);
for (Map.Entry<String, List<String>> entry : request.getHeaders().entrySet()) { request.getHeaders().forEach((key, value) -> delegate.getHeaders().addAll(key, value));
delegate.getHeaders().addAll(entry.getKey(), entry.getValue());
}
if (body.length > 0) { if (body.length > 0) {
StreamUtils.copy(body, delegate.getBody()); if (delegate instanceof StreamingHttpOutputMessage) {
StreamingHttpOutputMessage streamingOutputMessage = (StreamingHttpOutputMessage) delegate;
streamingOutputMessage.setBody(outputStream -> StreamUtils.copy(body, outputStream));
}
else {
StreamUtils.copy(body, delegate.getBody());
}
} }
return delegate.execute(); return delegate.execute();
} }

26
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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -208,23 +208,17 @@ public abstract class AbstractHttpMessageConverter<T> implements HttpMessageConv
addDefaultHeaders(headers, t, contentType); addDefaultHeaders(headers, t, contentType);
if (outputMessage instanceof StreamingHttpOutputMessage) { if (outputMessage instanceof StreamingHttpOutputMessage) {
StreamingHttpOutputMessage streamingOutputMessage = StreamingHttpOutputMessage streamingOutputMessage = (StreamingHttpOutputMessage) outputMessage;
(StreamingHttpOutputMessage) outputMessage; streamingOutputMessage.setBody(outputStream -> writeInternal(t, new HttpOutputMessage() {
streamingOutputMessage.setBody(new StreamingHttpOutputMessage.Body() {
@Override @Override
public void writeTo(final OutputStream outputStream) throws IOException { public OutputStream getBody() {
writeInternal(t, new HttpOutputMessage() { return outputStream;
@Override
public OutputStream getBody() throws IOException {
return outputStream;
}
@Override
public HttpHeaders getHeaders() {
return headers;
}
});
} }
}); @Override
public HttpHeaders getHeaders() {
return headers;
}
}));
} }
else { else {
writeInternal(t, outputMessage); writeInternal(t, outputMessage);

9
spring-web/src/main/java/org/springframework/http/converter/BufferedImageHttpMessageConverter.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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -221,12 +221,7 @@ public class BufferedImageHttpMessageConverter implements HttpMessageConverter<B
if (outputMessage instanceof StreamingHttpOutputMessage) { if (outputMessage instanceof StreamingHttpOutputMessage) {
StreamingHttpOutputMessage streamingOutputMessage = (StreamingHttpOutputMessage) outputMessage; StreamingHttpOutputMessage streamingOutputMessage = (StreamingHttpOutputMessage) outputMessage;
streamingOutputMessage.setBody(new StreamingHttpOutputMessage.Body() { streamingOutputMessage.setBody(outputStream -> writeInternal(image, selectedContentType, outputStream));
@Override
public void writeTo(OutputStream outputStream) throws IOException {
writeInternal(image, selectedContentType, outputStream);
}
});
} }
else { else {
writeInternal(image, selectedContentType, outputMessage.getBody()); writeInternal(image, selectedContentType, outputMessage.getBody());

18
spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -319,12 +319,7 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
if (outputMessage instanceof StreamingHttpOutputMessage) { if (outputMessage instanceof StreamingHttpOutputMessage) {
StreamingHttpOutputMessage streamingOutputMessage = (StreamingHttpOutputMessage) outputMessage; StreamingHttpOutputMessage streamingOutputMessage = (StreamingHttpOutputMessage) outputMessage;
streamingOutputMessage.setBody(new StreamingHttpOutputMessage.Body() { streamingOutputMessage.setBody(outputStream -> StreamUtils.copy(bytes, outputStream));
@Override
public void writeTo(OutputStream outputStream) throws IOException {
StreamUtils.copy(bytes, outputStream);
}
});
} }
else { else {
StreamUtils.copy(bytes, outputMessage.getBody()); StreamUtils.copy(bytes, outputMessage.getBody());
@ -347,12 +342,9 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
if (outputMessage instanceof StreamingHttpOutputMessage) { if (outputMessage instanceof StreamingHttpOutputMessage) {
StreamingHttpOutputMessage streamingOutputMessage = (StreamingHttpOutputMessage) outputMessage; StreamingHttpOutputMessage streamingOutputMessage = (StreamingHttpOutputMessage) outputMessage;
streamingOutputMessage.setBody(new StreamingHttpOutputMessage.Body() { streamingOutputMessage.setBody(outputStream -> {
@Override writeParts(outputStream, parts, boundary);
public void writeTo(OutputStream outputStream) throws IOException { writeEnd(outputStream, boundary);
writeParts(outputStream, parts, boundary);
writeEnd(outputStream, boundary);
}
}); });
} }
else { else {

35
spring-web/src/test/java/org/springframework/http/client/AbstractHttpRequestFactoryTestCase.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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,8 +16,6 @@
package org.springframework.http.client; package org.springframework.http.client;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI; import java.net.URI;
import java.util.Arrays; import java.util.Arrays;
import java.util.Locale; import java.util.Locale;
@ -69,6 +67,7 @@ public abstract class AbstractHttpRequestFactoryTestCase extends AbstractMockWeb
ClientHttpRequest request = factory.createRequest(uri, HttpMethod.GET); ClientHttpRequest request = factory.createRequest(uri, HttpMethod.GET);
assertEquals("Invalid HTTP method", HttpMethod.GET, request.getMethod()); assertEquals("Invalid HTTP method", HttpMethod.GET, request.getMethod());
assertEquals("Invalid HTTP URI", uri, request.getURI()); assertEquals("Invalid HTTP URI", uri, request.getURI());
ClientHttpResponse response = request.execute(); ClientHttpResponse response = request.execute();
try { try {
assertEquals("Invalid status code", HttpStatus.NOT_FOUND, response.getStatusCode()); assertEquals("Invalid status code", HttpStatus.NOT_FOUND, response.getStatusCode());
@ -82,6 +81,7 @@ public abstract class AbstractHttpRequestFactoryTestCase extends AbstractMockWeb
public void echo() throws Exception { public void echo() throws Exception {
ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/echo"), HttpMethod.PUT); ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/echo"), HttpMethod.PUT);
assertEquals("Invalid HTTP method", HttpMethod.PUT, request.getMethod()); assertEquals("Invalid HTTP method", HttpMethod.PUT, request.getMethod());
String headerName = "MyHeader"; String headerName = "MyHeader";
String headerValue1 = "value1"; String headerValue1 = "value1";
request.getHeaders().add(headerName, headerValue1); request.getHeaders().add(headerName, headerValue1);
@ -89,19 +89,15 @@ public abstract class AbstractHttpRequestFactoryTestCase extends AbstractMockWeb
request.getHeaders().add(headerName, headerValue2); request.getHeaders().add(headerName, headerValue2);
final byte[] body = "Hello World".getBytes("UTF-8"); final byte[] body = "Hello World".getBytes("UTF-8");
request.getHeaders().setContentLength(body.length); request.getHeaders().setContentLength(body.length);
if (request instanceof StreamingHttpOutputMessage) { if (request instanceof StreamingHttpOutputMessage) {
StreamingHttpOutputMessage streamingRequest = StreamingHttpOutputMessage streamingRequest = (StreamingHttpOutputMessage) request;
(StreamingHttpOutputMessage) request; streamingRequest.setBody(outputStream -> StreamUtils.copy(body, outputStream));
streamingRequest.setBody(new StreamingHttpOutputMessage.Body() {
@Override
public void writeTo(OutputStream outputStream) throws IOException {
StreamUtils.copy(body, outputStream);
}
});
} }
else { else {
StreamUtils.copy(body, request.getBody()); StreamUtils.copy(body, request.getBody());
} }
ClientHttpResponse response = request.execute(); ClientHttpResponse response = request.execute();
try { try {
assertEquals("Invalid status code", HttpStatus.OK, response.getStatusCode()); assertEquals("Invalid status code", HttpStatus.OK, response.getStatusCode());
@ -119,17 +115,14 @@ public abstract class AbstractHttpRequestFactoryTestCase extends AbstractMockWeb
@Test(expected = IllegalStateException.class) @Test(expected = IllegalStateException.class)
public void multipleWrites() throws Exception { public void multipleWrites() throws Exception {
ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/echo"), HttpMethod.POST); ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/echo"), HttpMethod.POST);
final byte[] body = "Hello World".getBytes("UTF-8"); final byte[] body = "Hello World".getBytes("UTF-8");
if (request instanceof StreamingHttpOutputMessage) { if (request instanceof StreamingHttpOutputMessage) {
StreamingHttpOutputMessage streamingRequest = StreamingHttpOutputMessage streamingRequest = (StreamingHttpOutputMessage) request;
(StreamingHttpOutputMessage) request; streamingRequest.setBody(outputStream -> {
streamingRequest.setBody(new StreamingHttpOutputMessage.Body() { StreamUtils.copy(body, outputStream);
@Override outputStream.flush();
public void writeTo(OutputStream outputStream) throws IOException { outputStream.close();
StreamUtils.copy(body, outputStream);
outputStream.flush();
outputStream.close();
}
}); });
} }
else { else {
@ -143,9 +136,11 @@ public abstract class AbstractHttpRequestFactoryTestCase extends AbstractMockWeb
@Test(expected = UnsupportedOperationException.class) @Test(expected = UnsupportedOperationException.class)
public void headersAfterExecute() throws Exception { public void headersAfterExecute() throws Exception {
ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/echo"), HttpMethod.POST); ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/echo"), HttpMethod.POST);
request.getHeaders().add("MyHeader", "value"); request.getHeaders().add("MyHeader", "value");
byte[] body = "Hello World".getBytes("UTF-8"); byte[] body = "Hello World".getBytes("UTF-8");
FileCopyUtils.copy(body, request.getBody()); FileCopyUtils.copy(body, request.getBody());
ClientHttpResponse response = request.execute(); ClientHttpResponse response = request.execute();
try { try {
request.getHeaders().add("MyHeader", "value"); request.getHeaders().add("MyHeader", "value");

41
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);
}
}

11
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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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; import org.springframework.http.HttpMethod;
public class StreamingHttpComponentsClientHttpRequestFactoryTests /**
extends AbstractHttpRequestFactoryTestCase { * @author Arjen Poutsma
*/
public class StreamingHttpComponentsClientHttpRequestFactoryTests extends AbstractHttpRequestFactoryTestCase {
@Override @Override
protected ClientHttpRequestFactory createRequestFactory() { protected ClientHttpRequestFactory createRequestFactory() {
HttpComponentsClientHttpRequestFactory requestFactory = HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
new HttpComponentsClientHttpRequestFactory();
requestFactory.setBufferRequestBody(false); requestFactory.setBufferRequestBody(false);
return requestFactory; return requestFactory;
} }

30
spring-web/src/test/java/org/springframework/http/client/StreamingSimpleHttpRequestFactoryTests.java → 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,7 +16,6 @@
package org.springframework.http.client; package org.springframework.http.client;
import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.URI; import java.net.URI;
import java.util.Collections; import java.util.Collections;
@ -27,12 +26,14 @@ import org.junit.Test;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.http.HttpRequest;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import static org.junit.Assert.*; import static org.junit.Assert.*;
public class StreamingSimpleHttpRequestFactoryTests extends AbstractHttpRequestFactoryTestCase { /**
* @author Arjen Poutsma
*/
public class StreamingSimpleClientHttpRequestFactoryTests extends AbstractHttpRequestFactoryTestCase {
@Override @Override
protected ClientHttpRequestFactory createRequestFactory() { protected ClientHttpRequestFactory createRequestFactory() {
@ -41,21 +42,16 @@ public class StreamingSimpleHttpRequestFactoryTests extends AbstractHttpRequestF
return factory; return factory;
} }
// SPR-8809 @Test // SPR-8809
@Test
public void interceptor() throws Exception { public void interceptor() throws Exception {
final String headerName = "MyHeader"; final String headerName = "MyHeader";
final String headerValue = "MyValue"; final String headerValue = "MyValue";
ClientHttpRequestInterceptor interceptor = new ClientHttpRequestInterceptor() { ClientHttpRequestInterceptor interceptor = (request, body, execution) -> {
@Override request.getHeaders().add(headerName, headerValue);
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) return execution.execute(request, body);
throws IOException {
request.getHeaders().add(headerName, headerValue);
return execution.execute(request, body);
}
}; };
InterceptingClientHttpRequestFactory factory = new InterceptingClientHttpRequestFactory(createRequestFactory(), InterceptingClientHttpRequestFactory factory = new InterceptingClientHttpRequestFactory(
Collections.singletonList(interceptor)); createRequestFactory(), Collections.singletonList(interceptor));
ClientHttpResponse response = null; ClientHttpResponse response = null;
try { try {
@ -81,8 +77,8 @@ public class StreamingSimpleHttpRequestFactoryTests extends AbstractHttpRequestF
ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/methods/post"), HttpMethod.POST); ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/methods/post"), HttpMethod.POST);
final int BUF_SIZE = 4096; final int BUF_SIZE = 4096;
final int ITERATIONS = Integer.MAX_VALUE / BUF_SIZE; final int ITERATIONS = Integer.MAX_VALUE / BUF_SIZE;
// final int contentLength = ITERATIONS * BUF_SIZE; // final int contentLength = ITERATIONS * BUF_SIZE;
// request.getHeaders().setContentLength(contentLength); // request.getHeaders().setContentLength(contentLength);
OutputStream body = request.getBody(); OutputStream body = request.getBody();
for (int i = 0; i < ITERATIONS; i++) { for (int i = 0; i < ITERATIONS; i++) {
byte[] buffer = new byte[BUF_SIZE]; byte[] buffer = new byte[BUF_SIZE];
Loading…
Cancel
Save