Skip to content

Commit 5ae57bc

Browse files
committed
Fix BodyDeferringAsyncHandler piped streams doc sample, close AsyncHttpClient#1451
Motivation: Javadoc sample fails because the PipedOutputStream might not connected to its PipedInputStream counterpart when bodyparts are received. Modifications: * Fix javadoc: PipedInputStream must be connected prior to sending the request. * Add piped streams based test Result: Sample works
1 parent b637c3e commit 5ae57bc

File tree

2 files changed

+43
-16
lines changed

2 files changed

+43
-16
lines changed

client/src/main/java/org/asynchttpclient/handler/BodyDeferringAsyncHandler.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,20 @@
6363
* <br>
6464
* <pre>
6565
* PipedOutputStream pout = new PipedOutputStream();
66-
* BodyDeferringAsyncHandler bdah = new BodyDeferringAsyncHandler(pout);
67-
* // client executes async
68-
* Future&lt;Response&gt; fr = client.prepareGet(&quot;http://foo.com/aresource&quot;).execute(bdah);
69-
* // main thread will block here until headers are available
70-
* Response response = bdah.getResponse();
71-
* if (response.getStatusCode() == 200) {
72-
* InputStream pin = new BodyDeferringInputStream(fr,new PipedInputStream(pout));
73-
* // consume InputStream
74-
* ...
75-
* } else {
76-
* // handle unexpected response status code
77-
* ...
66+
* try (PipedInputStream pin = new PipedInputStream(pout)) {
67+
* BodyDeferringAsyncHandler handler = new BodyDeferringAsyncHandler(pout);
68+
* ListenableFuture<&lt;Response&gt; respFut = client.prepareGet(getTargetUrl()).execute(handler);
69+
* Response resp = handler.getResponse();
70+
* // main thread will block here until headers are available
71+
* if (resp.getStatusCode() == 200) {
72+
* try (InputStream is = new BodyDeferringInputStream(respFut, handler, pin)) {
73+
* // consume InputStream
74+
* ...
75+
* }
76+
* } else {
77+
* // handle unexpected response status code
78+
* ...
79+
* }
7880
* }
7981
* </pre>
8082
*/

client/src/test/java/org/asynchttpclient/handler/BodyDeferringAsyncHandlerTest.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.io.OutputStream;
2424
import java.io.PipedInputStream;
2525
import java.io.PipedOutputStream;
26+
import java.nio.charset.StandardCharsets;
2627
import java.util.concurrent.ExecutionException;
2728
import java.util.concurrent.Future;
2829
import java.util.concurrent.TimeoutException;
@@ -31,10 +32,12 @@
3132
import javax.servlet.http.HttpServletRequest;
3233
import javax.servlet.http.HttpServletResponse;
3334

35+
import org.apache.commons.io.IOUtils;
3436
import org.asynchttpclient.AbstractBasicTest;
3537
import org.asynchttpclient.AsyncHttpClient;
3638
import org.asynchttpclient.AsyncHttpClientConfig;
3739
import org.asynchttpclient.BoundRequestBuilder;
40+
import org.asynchttpclient.ListenableFuture;
3841
import org.asynchttpclient.Response;
3942
import org.asynchttpclient.exception.RemotelyClosedException;
4043
import org.asynchttpclient.handler.BodyDeferringAsyncHandler.BodyDeferringInputStream;
@@ -114,7 +117,7 @@ public AsyncHttpClientConfig getAsyncHttpClientConfig() {
114117
@Test(groups = "standalone")
115118
public void deferredSimple() throws IOException, ExecutionException, TimeoutException, InterruptedException {
116119
try (AsyncHttpClient client = asyncHttpClient(getAsyncHttpClientConfig())) {
117-
BoundRequestBuilder r = client.prepareGet("http://localhost/" + port1 + "/deferredSimple");
120+
BoundRequestBuilder r = client.prepareGet(getTargetUrl());
118121

119122
CountingOutputStream cos = new CountingOutputStream();
120123
BodyDeferringAsyncHandler bdah = new BodyDeferringAsyncHandler(cos);
@@ -138,7 +141,7 @@ public void deferredSimple() throws IOException, ExecutionException, TimeoutExce
138141
@Test(groups = "standalone", expectedExceptions = RemotelyClosedException.class)
139142
public void deferredSimpleWithFailure() throws Throwable {
140143
try (AsyncHttpClient client = asyncHttpClient(getAsyncHttpClientConfig())) {
141-
BoundRequestBuilder r = client.prepareGet("http://localhost/" + port1 + "/deferredSimpleWithFailure").addHeader("X-FAIL-TRANSFER", Boolean.TRUE.toString());
144+
BoundRequestBuilder r = client.prepareGet(getTargetUrl()).addHeader("X-FAIL-TRANSFER", Boolean.TRUE.toString());
142145

143146
CountingOutputStream cos = new CountingOutputStream();
144147
BodyDeferringAsyncHandler bdah = new BodyDeferringAsyncHandler(cos);
@@ -167,7 +170,7 @@ public void deferredSimpleWithFailure() throws Throwable {
167170
@Test(groups = "standalone")
168171
public void deferredInputStreamTrick() throws IOException, ExecutionException, TimeoutException, InterruptedException {
169172
try (AsyncHttpClient client = asyncHttpClient(getAsyncHttpClientConfig())) {
170-
BoundRequestBuilder r = client.prepareGet("http://localhost/" + port1 + "/deferredInputStreamTrick");
173+
BoundRequestBuilder r = client.prepareGet(getTargetUrl());
171174

172175
PipedOutputStream pos = new PipedOutputStream();
173176
PipedInputStream pis = new PipedInputStream(pos);
@@ -200,7 +203,7 @@ public void deferredInputStreamTrick() throws IOException, ExecutionException, T
200203
@Test(groups = "standalone", expectedExceptions = RemotelyClosedException.class)
201204
public void deferredInputStreamTrickWithFailure() throws Throwable {
202205
try (AsyncHttpClient client = asyncHttpClient(getAsyncHttpClientConfig())) {
203-
BoundRequestBuilder r = client.prepareGet("http://localhost/" + port1 + "/deferredInputStreamTrickWithFailure").addHeader("X-FAIL-TRANSFER", Boolean.TRUE.toString());
206+
BoundRequestBuilder r = client.prepareGet(getTargetUrl()).addHeader("X-FAIL-TRANSFER", Boolean.TRUE.toString());
204207
PipedOutputStream pos = new PipedOutputStream();
205208
PipedInputStream pis = new PipedInputStream(pos);
206209
BodyDeferringAsyncHandler bdah = new BodyDeferringAsyncHandler(pos);
@@ -240,4 +243,26 @@ public void testConnectionRefused() throws IOException, ExecutionException, Time
240243
bdah.getResponse();
241244
}
242245
}
246+
247+
@Test(groups = "standalone")
248+
public void testPipedStreams() throws Exception {
249+
try (AsyncHttpClient client = asyncHttpClient(getAsyncHttpClientConfig())) {
250+
PipedOutputStream pout = new PipedOutputStream();
251+
try (PipedInputStream pin = new PipedInputStream(pout)) {
252+
BodyDeferringAsyncHandler handler = new BodyDeferringAsyncHandler(pout);
253+
ListenableFuture<Response> respFut = client.prepareGet(getTargetUrl()).execute(handler);
254+
255+
Response resp = handler.getResponse();
256+
257+
if (resp.getStatusCode() == 200) {
258+
try (BodyDeferringInputStream is = new BodyDeferringInputStream(respFut, handler, pin)) {
259+
String body = IOUtils.toString(is, StandardCharsets.UTF_8);
260+
assertTrue(body.contains("ABCDEF"));
261+
}
262+
} else {
263+
throw new IOException("HTTP error " + resp.getStatusCode());
264+
}
265+
}
266+
}
267+
}
243268
}

0 commit comments

Comments
 (0)