Skip to content

Commit d979695

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 5c71b1e commit d979695

File tree

2 files changed

+42
-16
lines changed

2 files changed

+42
-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
@@ -62,18 +62,20 @@
6262
* <br>
6363
* <pre>
6464
* PipedOutputStream pout = new PipedOutputStream();
65-
* BodyDeferringAsyncHandler bdah = new BodyDeferringAsyncHandler(pout);
66-
* // client executes async
67-
* Future&lt;Response&gt; fr = client.prepareGet(&quot;http://foo.com/aresource&quot;).execute(bdah);
68-
* // main thread will block here until headers are available
69-
* Response response = bdah.getResponse();
70-
* if (response.getStatusCode() == 200) {
71-
* InputStream pin = new BodyDeferringInputStream(fr,new PipedInputStream(pout));
72-
* // consume InputStream
73-
* ...
74-
* } else {
75-
* // handle unexpected response status code
76-
* ...
65+
* try (PipedInputStream pin = new PipedInputStream(pout)) {
66+
* BodyDeferringAsyncHandler handler = new BodyDeferringAsyncHandler(pout);
67+
* ListenableFuture<&lt;Response&gt; respFut = client.prepareGet(getTargetUrl()).execute(handler);
68+
* Response resp = handler.getResponse();
69+
* // main thread will block here until headers are available
70+
* if (resp.getStatusCode() == 200) {
71+
* try (InputStream is = new BodyDeferringInputStream(respFut, handler, pin)) {
72+
* // consume InputStream
73+
* ...
74+
* }
75+
* } else {
76+
* // handle unexpected response status code
77+
* ...
78+
* }
7779
* }
7880
* </pre>
7981
*/

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

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.OutputStream;
2222
import java.io.PipedInputStream;
2323
import java.io.PipedOutputStream;
24+
import java.nio.charset.StandardCharsets;
2425
import java.util.concurrent.ExecutionException;
2526
import java.util.concurrent.Future;
2627
import java.util.concurrent.TimeoutException;
@@ -29,10 +30,12 @@
2930
import javax.servlet.http.HttpServletRequest;
3031
import javax.servlet.http.HttpServletResponse;
3132

33+
import org.apache.commons.io.IOUtils;
3234
import org.asynchttpclient.AbstractBasicTest;
3335
import org.asynchttpclient.AsyncHttpClient;
3436
import org.asynchttpclient.AsyncHttpClientConfig;
3537
import org.asynchttpclient.BoundRequestBuilder;
38+
import org.asynchttpclient.ListenableFuture;
3639
import org.asynchttpclient.Response;
3740
import org.asynchttpclient.handler.BodyDeferringAsyncHandler.BodyDeferringInputStream;
3841
import org.eclipse.jetty.server.Request;
@@ -112,7 +115,7 @@ public AsyncHttpClientConfig getAsyncHttpClientConfig() {
112115
@Test(groups = "standalone")
113116
public void deferredSimple() throws IOException, ExecutionException, TimeoutException, InterruptedException {
114117
try (AsyncHttpClient client = asyncHttpClient(getAsyncHttpClientConfig())) {
115-
BoundRequestBuilder r = client.prepareGet("http://localhost/" + port1 + "/deferredSimple");
118+
BoundRequestBuilder r = client.prepareGet(getTargetUrl());
116119

117120
CountingOutputStream cos = new CountingOutputStream();
118121
BodyDeferringAsyncHandler bdah = new BodyDeferringAsyncHandler(cos);
@@ -136,8 +139,7 @@ public void deferredSimple() throws IOException, ExecutionException, TimeoutExce
136139
@Test(groups = "standalone", enabled = false)
137140
public void deferredSimpleWithFailure() throws IOException, ExecutionException, TimeoutException, InterruptedException {
138141
try (AsyncHttpClient client = asyncHttpClient(getAsyncHttpClientConfig())) {
139-
BoundRequestBuilder r = client.prepareGet("http://localhost:" + port1 + "/deferredSimpleWithFailure").addHeader("X-FAIL-TRANSFER",
140-
Boolean.TRUE.toString());
142+
BoundRequestBuilder r = client.prepareGet(getTargetUrl()).addHeader("X-FAIL-TRANSFER", Boolean.TRUE.toString());
141143

142144
CountingOutputStream cos = new CountingOutputStream();
143145
BodyDeferringAsyncHandler bdah = new BodyDeferringAsyncHandler(cos);
@@ -166,7 +168,7 @@ public void deferredSimpleWithFailure() throws IOException, ExecutionException,
166168
@Test(groups = "standalone")
167169
public void deferredInputStreamTrick() throws IOException, ExecutionException, TimeoutException, InterruptedException {
168170
try (AsyncHttpClient client = asyncHttpClient(getAsyncHttpClientConfig())) {
169-
BoundRequestBuilder r = client.prepareGet("http://localhost/" + port1 + "/deferredInputStreamTrick");
171+
BoundRequestBuilder r = client.prepareGet(getTargetUrl());
170172

171173
PipedOutputStream pos = new PipedOutputStream();
172174
PipedInputStream pis = new PipedInputStream(pos);
@@ -241,4 +243,26 @@ public void testConnectionRefused() throws IOException, ExecutionException, Time
241243
bdah.getResponse();
242244
}
243245
}
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+
}
244268
}

0 commit comments

Comments
 (0)