Skip to content

Commit 6298292

Browse files
committed
AsyncRestTemplate triggers no-output HTTP requests immediately as well
Issue: SPR-14093
1 parent 2dae4d8 commit 6298292

File tree

5 files changed

+28
-10
lines changed

5 files changed

+28
-10
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -89,6 +89,10 @@ public ClientHttpResponse call() throws Exception {
8989
if (connection.getDoOutput()) {
9090
FileCopyUtils.copy(bufferedOutput, connection.getOutputStream());
9191
}
92+
else {
93+
// Immediately trigger the request in a no-output scenario as well
94+
connection.getResponseCode();
95+
}
9296
return new SimpleClientHttpResponse(connection);
9397
}
9498
});

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -68,20 +68,21 @@ public URI getURI() {
6868
@Override
6969
protected ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException {
7070
addHeaders(this.connection, headers);
71-
7271
// JDK <1.8 doesn't support getOutputStream with HTTP DELETE
7372
if (HttpMethod.DELETE == getMethod() && bufferedOutput.length == 0) {
7473
this.connection.setDoOutput(false);
7574
}
76-
7775
if (this.connection.getDoOutput() && this.outputStreaming) {
7876
this.connection.setFixedLengthStreamingMode(bufferedOutput.length);
7977
}
8078
this.connection.connect();
8179
if (this.connection.getDoOutput()) {
8280
FileCopyUtils.copy(bufferedOutput, this.connection.getOutputStream());
8381
}
84-
82+
else {
83+
// Immediately trigger the request in a no-output scenario as well
84+
this.connection.getResponseCode();
85+
}
8586
return new SimpleClientHttpResponse(this.connection);
8687
}
8788

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -108,6 +108,8 @@ public ClientHttpResponse call() throws Exception {
108108
else {
109109
SimpleBufferingClientHttpRequest.addHeaders(connection, headers);
110110
connection.connect();
111+
// Immediately trigger the request in a no-output scenario as well
112+
connection.getResponseCode();
111113
}
112114
}
113115
catch (IOException ex) {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -94,6 +94,8 @@ protected ClientHttpResponse executeInternal(HttpHeaders headers) throws IOExcep
9494
else {
9595
SimpleBufferingClientHttpRequest.addHeaders(this.connection, headers);
9696
this.connection.connect();
97+
// Immediately trigger the request in a no-output scenario as well
98+
this.connection.getResponseCode();
9799
}
98100
}
99101
catch (IOException ex) {

spring-web/src/test/java/org/springframework/http/client/BufferedSimpleHttpRequestFactoryTests.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,16 +16,19 @@
1616

1717
package org.springframework.http.client;
1818

19-
import static org.junit.Assert.assertEquals;
20-
19+
import java.io.ByteArrayInputStream;
2120
import java.io.IOException;
21+
import java.io.InputStream;
2222
import java.net.HttpURLConnection;
2323
import java.net.ProtocolException;
2424
import java.net.URL;
2525

2626
import org.junit.Test;
27+
2728
import org.springframework.http.HttpMethod;
2829

30+
import static org.junit.Assert.*;
31+
2932
public class BufferedSimpleHttpRequestFactoryTests extends AbstractHttpRequestFactoryTestCase {
3033

3134
@Override
@@ -89,5 +92,11 @@ public void disconnect() {
8992
public boolean usingProxy() {
9093
return false;
9194
}
95+
96+
@Override
97+
public InputStream getInputStream() throws IOException {
98+
return new ByteArrayInputStream(new byte[0]);
99+
}
92100
}
101+
93102
}

0 commit comments

Comments
 (0)