Skip to content

Commit bd8bdbb

Browse files
author
Stephane Landelle
committed
Have SimpleAHCTransferListener receive an UriComponents
1 parent d159274 commit bd8bdbb

File tree

3 files changed

+34
-32
lines changed

3 files changed

+34
-32
lines changed

api/src/main/java/org/asynchttpclient/SimpleAsyncHttpClient.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.asynchttpclient.resumable.ResumableIOExceptionFilter;
1919
import org.asynchttpclient.simple.HeaderMap;
2020
import org.asynchttpclient.simple.SimpleAHCTransferListener;
21+
import org.asynchttpclient.uri.UriComponents;
2122
import org.slf4j.Logger;
2223
import org.slf4j.LoggerFactory;
2324

@@ -283,7 +284,7 @@ private Future<Response> execute(RequestBuilder rb, BodyConsumer bodyConsumer, T
283284

284285
Request request = rb.build();
285286
ProgressAsyncHandler<Response> handler = new BodyConsumerAsyncHandler(bodyConsumer, throwableHandler, errorDocumentBehaviour,
286-
request.getUrl(), listener);
287+
request.getURI(), listener);
287288

288289
if (resumeEnabled && request.getMethod().equals("GET") && bodyConsumer != null && bodyConsumer instanceof ResumableBodyConsumer) {
289290
ResumableBodyConsumer fileBodyConsumer = (ResumableBodyConsumer) bodyConsumer;
@@ -735,7 +736,7 @@ private final static class BodyConsumerAsyncHandler extends AsyncCompletionHandl
735736
private final BodyConsumer bodyConsumer;
736737
private final ThrowableHandler exceptionHandler;
737738
private final ErrorDocumentBehaviour errorDocumentBehaviour;
738-
private final String url;
739+
private final UriComponents uri;
739740
private final SimpleAHCTransferListener listener;
740741

741742
private boolean accumulateBody = false;
@@ -744,11 +745,11 @@ private final static class BodyConsumerAsyncHandler extends AsyncCompletionHandl
744745
private long total = -1;
745746

746747
public BodyConsumerAsyncHandler(BodyConsumer bodyConsumer, ThrowableHandler exceptionHandler,
747-
ErrorDocumentBehaviour errorDocumentBehaviour, String url, SimpleAHCTransferListener listener) {
748+
ErrorDocumentBehaviour errorDocumentBehaviour, UriComponents uri, SimpleAHCTransferListener listener) {
748749
this.bodyConsumer = bodyConsumer;
749750
this.exceptionHandler = exceptionHandler;
750751
this.errorDocumentBehaviour = errorDocumentBehaviour;
751-
this.url = url;
752+
this.uri = uri;
752753
this.listener = listener;
753754
}
754755

@@ -846,13 +847,13 @@ private void calculateTotal(HttpResponseHeaders headers) {
846847

847848
@Override
848849
public STATE onContentWriteProgress(long amount, long current, long total) {
849-
fireSent(url, amount, current, total);
850+
fireSent(uri, amount, current, total);
850851
return super.onContentWriteProgress(amount, current, total);
851852
}
852853

853854
private void fireStatus(HttpResponseStatus status) {
854855
if (listener != null) {
855-
listener.onStatus(url, status.getStatusCode(), status.getStatusText());
856+
listener.onStatus(uri, status.getStatusCode(), status.getStatusText());
856857
}
857858
}
858859

@@ -862,27 +863,26 @@ private void fireReceived(HttpResponseBodyPart content) {
862863
amount += remaining;
863864

864865
if (listener != null) {
865-
listener.onBytesReceived(url, amount, remaining, total);
866+
listener.onBytesReceived(uri, amount, remaining, total);
866867
}
867868
}
868869

869870
private void fireHeaders(HttpResponseHeaders headers) {
870871
if (listener != null) {
871-
listener.onHeaders(url, new HeaderMap(headers.getHeaders()));
872+
listener.onHeaders(uri, new HeaderMap(headers.getHeaders()));
872873
}
873874
}
874875

875-
private void fireSent(String url, long amount, long current, long total) {
876+
private void fireSent(UriComponents uri, long amount, long current, long total) {
876877
if (listener != null) {
877-
listener.onBytesSent(url, amount, current, total);
878+
listener.onBytesSent(uri, amount, current, total);
878879
}
879880
}
880881

881882
private void fireCompleted(Response response) {
882883
if (listener != null) {
883-
listener.onCompleted(url, response.getStatusCode(), response.getStatusText());
884+
listener.onCompleted(uri, response.getStatusCode(), response.getStatusText());
884885
}
885886
}
886887
}
887-
888888
}

api/src/main/java/org/asynchttpclient/simple/SimpleAHCTransferListener.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515

1616
import org.asynchttpclient.SimpleAsyncHttpClient;
17+
import org.asynchttpclient.uri.UriComponents;
1718

1819
/**
1920
* A simple transfer listener for use with the {@link SimpleAsyncHttpClient}.
@@ -29,51 +30,51 @@ public interface SimpleAHCTransferListener {
2930
/**
3031
* This method is called after the connection status is received.
3132
*
32-
* @param url the url for the connection.
33+
* @param uri the uri
3334
* @param statusCode the received status code.
3435
* @param statusText the received status text.
3536
*/
36-
void onStatus(String url, int statusCode, String statusText);
37+
void onStatus(UriComponents uri, int statusCode, String statusText);
3738

3839
/**
3940
* This method is called after the response headers are received.
4041
*
41-
* @param url the url for the connection.
42+
* @param uri the uri
4243
* @param headers the received headers, never {@code null}.
4344
*/
44-
void onHeaders(String url, HeaderMap headers);
45+
void onHeaders(UriComponents uri, HeaderMap headers);
4546

4647
/**
4748
* This method is called when bytes of the responses body are received.
4849
*
49-
* @param url the url for the connection.
50+
* @param uri the uri
5051
* @param amount the number of transferred bytes so far.
5152
* @param current the number of transferred bytes since the last call to this
5253
* method.
5354
* @param total the total number of bytes to be transferred. This is taken
5455
* from the Content-Length-header and may be unspecified (-1).
5556
*/
56-
void onBytesReceived(String url, long amount, long current, long total);
57+
void onBytesReceived(UriComponents uri, long amount, long current, long total);
5758

5859
/**
5960
* This method is called when bytes are sent.
6061
*
61-
* @param url the url for the connection.
62+
* @param uri the uri
6263
* @param amount the number of transferred bytes so far.
6364
* @param current the number of transferred bytes since the last call to this
6465
* method.
6566
* @param total the total number of bytes to be transferred. This is taken
6667
* from the Content-Length-header and may be unspecified (-1).
6768
*/
68-
void onBytesSent(String url, long amount, long current, long total);
69+
void onBytesSent(UriComponents uri, long amount, long current, long total);
6970

7071
/**
7172
* This method is called when the request is completed.
7273
*
73-
* @param url the url for the connection.
74+
* @param uri the uri
7475
* @param statusCode the received status code.
7576
* @param statusText the received status text.
7677
*/
77-
void onCompleted(String url, int statusCode, String statusText);
78+
void onCompleted(UriComponents uri, int statusCode, String statusText);
7879
}
7980

api/src/test/java/org/asynchttpclient/async/SimpleAsyncHttpClientTest.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.asynchttpclient.multipart.ByteArrayPart;
2828
import org.asynchttpclient.simple.HeaderMap;
2929
import org.asynchttpclient.simple.SimpleAHCTransferListener;
30+
import org.asynchttpclient.uri.UriComponents;
3031
import org.asynchttpclient.util.StandardCharsets;
3132
import org.testng.annotations.Test;
3233

@@ -176,19 +177,19 @@ public void testSimpleTransferListener() throws Exception {
176177

177178
SimpleAHCTransferListener listener = new SimpleAHCTransferListener() {
178179

179-
public void onStatus(String url, int statusCode, String statusText) {
180+
public void onStatus(UriComponents uri, int statusCode, String statusText) {
180181
try {
181182
assertEquals(statusCode, 200);
182-
assertEquals(url, getTargetUrl());
183+
assertEquals(uri.toUrl(), getTargetUrl());
183184
} catch (Error e) {
184185
errors.add(e);
185186
throw e;
186187
}
187188
}
188189

189-
public void onHeaders(String url, HeaderMap headers) {
190+
public void onHeaders(UriComponents uri, HeaderMap headers) {
190191
try {
191-
assertEquals(url, getTargetUrl());
192+
assertEquals(uri.toUrl(), getTargetUrl());
192193
assertNotNull(headers);
193194
assertTrue(!headers.isEmpty());
194195
assertEquals(headers.getFirstValue("X-Custom"), "custom");
@@ -198,19 +199,19 @@ public void onHeaders(String url, HeaderMap headers) {
198199
}
199200
}
200201

201-
public void onCompleted(String url, int statusCode, String statusText) {
202+
public void onCompleted(UriComponents uri, int statusCode, String statusText) {
202203
try {
203204
assertEquals(statusCode, 200);
204-
assertEquals(url, getTargetUrl());
205+
assertEquals(uri.toUrl(), getTargetUrl());
205206
} catch (Error e) {
206207
errors.add(e);
207208
throw e;
208209
}
209210
}
210211

211-
public void onBytesSent(String url, long amount, long current, long total) {
212+
public void onBytesSent(UriComponents uri, long amount, long current, long total) {
212213
try {
213-
assertEquals(url, getTargetUrl());
214+
assertEquals(uri.toUrl(), getTargetUrl());
214215
// FIXME Netty bug, see https://github.com/netty/netty/issues/1855
215216
// assertEquals(total, MY_MESSAGE.getBytes().length);
216217
} catch (Error e) {
@@ -219,9 +220,9 @@ public void onBytesSent(String url, long amount, long current, long total) {
219220
}
220221
}
221222

222-
public void onBytesReceived(String url, long amount, long current, long total) {
223+
public void onBytesReceived(UriComponents uri, long amount, long current, long total) {
223224
try {
224-
assertEquals(url, getTargetUrl());
225+
assertEquals(uri.toUrl(), getTargetUrl());
225226
assertEquals(total, -1);
226227
} catch (Error e) {
227228
errors.add(e);

0 commit comments

Comments
 (0)