Skip to content

Commit 08e0a00

Browse files
committed
Make client be able to take a RequestBuilder
1 parent 4dc7eab commit 08e0a00

25 files changed

+167
-139
lines changed

client/src/main/java/org/asynchttpclient/AsyncHttpClient.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,14 @@ public interface AsyncHttpClient extends Closeable {
222222
* @return {@link RequestBuilder}
223223
*/
224224
BoundRequestBuilder prepareRequest(Request request);
225+
226+
/**
227+
* Construct a {@link RequestBuilder} using a {@link RequestBuilder}
228+
*
229+
* @param requestBuilder a {@link RequestBuilder}
230+
* @return {@link RequestBuilder}
231+
*/
232+
BoundRequestBuilder prepareRequest(RequestBuilder requestBuilder);
225233

226234
/**
227235
* Execute an HTTP request.
@@ -232,6 +240,16 @@ public interface AsyncHttpClient extends Closeable {
232240
* @return a {@link Future} of type T
233241
*/
234242
<T> ListenableFuture<T> executeRequest(Request request, AsyncHandler<T> handler);
243+
244+
/**
245+
* Execute an HTTP request.
246+
*
247+
* @param requestBuilder {@link RequestBuilder}
248+
* @param handler an instance of {@link AsyncHandler}
249+
* @param <T> Type of the value that will be returned by the associated {@link java.util.concurrent.Future}
250+
* @return a {@link Future} of type T
251+
*/
252+
<T> ListenableFuture<T> executeRequest(RequestBuilder requestBuilder, AsyncHandler<T> handler);
235253

236254
/**
237255
* Execute an HTTP request.
@@ -240,4 +258,12 @@ public interface AsyncHttpClient extends Closeable {
240258
* @return a {@link Future} of type Response
241259
*/
242260
ListenableFuture<Response> executeRequest(Request request);
261+
262+
/**
263+
* Execute an HTTP request.
264+
*
265+
* @param request {@link RequestBuilder}
266+
* @return a {@link Future} of type Response
267+
*/
268+
ListenableFuture<Response> executeRequest(RequestBuilder requestBuilder);
243269
}

client/src/main/java/org/asynchttpclient/DefaultAsyncHttpClient.java

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,33 +43,38 @@ public class DefaultAsyncHttpClient implements AsyncHttpClient {
4343
private final Timer nettyTimer;
4444

4545
/**
46-
* Default signature calculator to use for all requests constructed by this client instance.
46+
* Default signature calculator to use for all requests constructed by this
47+
* client instance.
4748
*
4849
* @since 1.1
4950
*/
5051
protected SignatureCalculator signatureCalculator;
5152

5253
/**
53-
* Create a new HTTP Asynchronous Client using the default {@link DefaultAsyncHttpClientConfig} configuration. The
54-
* default {@link AsyncHttpClient} that will be used will be based on the classpath configuration.
54+
* Create a new HTTP Asynchronous Client using the default
55+
* {@link DefaultAsyncHttpClientConfig} configuration. The default
56+
* {@link AsyncHttpClient} that will be used will be based on the classpath
57+
* configuration.
5558
*
56-
* If none of those providers are found, then the engine will throw an IllegalStateException.
59+
* If none of those providers are found, then the engine will throw an
60+
* IllegalStateException.
5761
*/
5862
public DefaultAsyncHttpClient() {
5963
this(new DefaultAsyncHttpClientConfig.Builder().build());
6064
}
6165

6266
/**
63-
* Create a new HTTP Asynchronous Client using the specified {@link DefaultAsyncHttpClientConfig} configuration.
64-
* This configuration will be passed to the default {@link AsyncHttpClient} that will be selected based on
65-
* the classpath configuration.
67+
* Create a new HTTP Asynchronous Client using the specified
68+
* {@link DefaultAsyncHttpClientConfig} configuration. This configuration
69+
* will be passed to the default {@link AsyncHttpClient} that will be
70+
* selected based on the classpath configuration.
6671
*
6772
* @param config a {@link DefaultAsyncHttpClientConfig}
6873
*/
6974
public DefaultAsyncHttpClient(AsyncHttpClientConfig config) {
70-
75+
7176
this.config = config;
72-
77+
7378
allowStopNettyTimer = config.getNettyTimer() == null;
7479
nettyTimer = allowStopNettyTimer ? newNettyTimer() : config.getNettyTimer();
7580

@@ -83,8 +88,7 @@ private Timer newNettyTimer() {
8388
timer.start();
8489
return timer;
8590
}
86-
87-
91+
8892
@Override
8993
public void close() {
9094
if (closed.compareAndSet(false, true)) {
@@ -172,6 +176,11 @@ public BoundRequestBuilder prepareRequest(Request request) {
172176
return requestBuilder(request);
173177
}
174178

179+
@Override
180+
public BoundRequestBuilder prepareRequest(RequestBuilder requestBuilder) {
181+
return prepareRequest(requestBuilder.build());
182+
}
183+
175184
@Override
176185
public <T> ListenableFuture<T> executeRequest(Request request, AsyncHandler<T> handler) {
177186

@@ -191,11 +200,21 @@ public <T> ListenableFuture<T> executeRequest(Request request, AsyncHandler<T> h
191200
}
192201
}
193202

203+
@Override
204+
public <T> ListenableFuture<T> executeRequest(RequestBuilder requestBuilder, AsyncHandler<T> handler) {
205+
return executeRequest(requestBuilder.build(), handler);
206+
}
207+
194208
@Override
195209
public ListenableFuture<Response> executeRequest(Request request) {
196210
return executeRequest(request, new AsyncCompletionHandlerBase());
197211
}
198212

213+
@Override
214+
public ListenableFuture<Response> executeRequest(RequestBuilder requestBuilder) {
215+
return executeRequest(requestBuilder.build());
216+
}
217+
199218
private <T> ListenableFuture<T> execute(Request request, final AsyncHandler<T> asyncHandler) {
200219
try {
201220
return requestSender.sendRequest(request, asyncHandler, null, false);
@@ -204,9 +223,10 @@ private <T> ListenableFuture<T> execute(Request request, final AsyncHandler<T> a
204223
return new ListenableFuture.CompletedFailure<>(e);
205224
}
206225
}
207-
226+
208227
/**
209-
* Configure and execute the associated {@link RequestFilter}. This class may decorate the {@link Request} and {@link AsyncHandler}
228+
* Configure and execute the associated {@link RequestFilter}. This class
229+
* may decorate the {@link Request} and {@link AsyncHandler}
210230
*
211231
* @param fc {@link FilterContext}
212232
* @return {@link FilterContext}
@@ -234,7 +254,7 @@ private <T> FilterContext<T> preProcessRequest(FilterContext<T> fc) throws Filte
234254
public ChannelPool getChannelPool() {
235255
return channelManager.getChannelPool();
236256
}
237-
257+
238258
protected BoundRequestBuilder requestBuilder(String method, String url) {
239259
return new BoundRequestBuilder(this, method, config.isDisableUrlEncodingForBoundRequests()).setUrl(url).setSignatureCalculator(signatureCalculator);
240260
}

client/src/main/java/org/asynchttpclient/handler/resumable/ResumableAsyncHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public Request adjustRequestRange(Request request) {
204204
byteTransferred.set(ri);
205205
}
206206

207-
// The Resumbale
207+
// The Resumable
208208
if (resumableListener != null && resumableListener.length() > 0 && byteTransferred.get() != resumableListener.length()) {
209209
byteTransferred.set(resumableListener.length());
210210
}

client/src/test/java/org/asynchttpclient/BasicHttpTest.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public class BasicHttpTest extends AbstractBasicTest {
5858
@Test(groups = { "standalone", "async" })
5959
public void asyncProviderEncodingTest() throws Exception {
6060
try (AsyncHttpClient client = asyncHttpClient()) {
61-
Request request = new RequestBuilder("GET").setUrl(getTargetUrl() + "?q=+%20x").build();
61+
Request request = get(getTargetUrl() + "?q=+%20x").build();
6262
assertEquals(request.getUrl(), getTargetUrl() + "?q=+%20x");
6363

6464
String url = client.executeRequest(request, new AsyncCompletionHandler<String>() {
@@ -81,7 +81,7 @@ public void onThrowable(Throwable t) {
8181
@Test(groups = { "standalone", "async" })
8282
public void asyncProviderEncodingTest2() throws Exception {
8383
try (AsyncHttpClient client = asyncHttpClient()) {
84-
Request request = new RequestBuilder("GET").setUrl(getTargetUrl() + "").addQueryParam("q", "a b").build();
84+
Request request = get(getTargetUrl() + "").addQueryParam("q", "a b").build();
8585

8686
String url = client.executeRequest(request, new AsyncCompletionHandler<String>() {
8787
@Override
@@ -103,7 +103,7 @@ public void onThrowable(Throwable t) {
103103
@Test(groups = { "standalone", "async" })
104104
public void emptyRequestURI() throws Exception {
105105
try (AsyncHttpClient client = asyncHttpClient()) {
106-
Request request = new RequestBuilder("GET").setUrl(getTargetUrl()).build();
106+
Request request = get(getTargetUrl()).build();
107107

108108
String url = client.executeRequest(request, new AsyncCompletionHandler<String>() {
109109
@Override
@@ -131,7 +131,7 @@ public void asyncProviderContentLenghtGETTest() throws Exception {
131131
try (AsyncHttpClient client = asyncHttpClient()) {
132132
final CountDownLatch l = new CountDownLatch(1);
133133

134-
Request request = new RequestBuilder("GET").setUrl(getTargetUrl()).build();
134+
Request request = get(getTargetUrl()).build();
135135
client.executeRequest(request, new AsyncCompletionHandlerAdapter() {
136136

137137
@Override
@@ -170,7 +170,7 @@ public void onThrowable(Throwable t) {
170170
public void asyncContentTypeGETTest() throws Exception {
171171
try (AsyncHttpClient client = asyncHttpClient()) {
172172
final CountDownLatch l = new CountDownLatch(1);
173-
Request request = new RequestBuilder("GET").setUrl(getTargetUrl()).build();
173+
Request request = get(getTargetUrl()).build();
174174
client.executeRequest(request, new AsyncCompletionHandlerAdapter() {
175175

176176
@Override
@@ -194,7 +194,7 @@ public Response onCompleted(Response response) throws Exception {
194194
public void asyncHeaderGETTest() throws Exception {
195195
try (AsyncHttpClient client = asyncHttpClient()) {
196196
final CountDownLatch l = new CountDownLatch(1);
197-
Request request = new RequestBuilder("GET").setUrl(getTargetUrl()).build();
197+
Request request = get(getTargetUrl()).build();
198198
client.executeRequest(request, new AsyncCompletionHandlerAdapter() {
199199

200200
@Override
@@ -225,7 +225,7 @@ public void asyncHeaderPOSTTest() throws Exception {
225225
h.add("Test3", "Test3");
226226
h.add("Test4", "Test4");
227227
h.add("Test5", "Test5");
228-
Request request = new RequestBuilder("GET").setUrl(getTargetUrl()).setHeaders(h).build();
228+
Request request = get(getTargetUrl()).setHeaders(h).build();
229229

230230
client.executeRequest(request, new AsyncCompletionHandlerAdapter() {
231231

@@ -260,7 +260,7 @@ public void asyncParamPOSTTest() throws Exception {
260260
for (int i = 0; i < 5; i++) {
261261
m.put("param_" + i, Arrays.asList("value_" + i));
262262
}
263-
Request request = new RequestBuilder("POST").setUrl(getTargetUrl()).setHeaders(h).setFormParams(m).build();
263+
Request request = post(getTargetUrl()).setHeaders(h).setFormParams(m).build();
264264
client.executeRequest(request, new AsyncCompletionHandlerAdapter() {
265265

266266
@Override
@@ -287,7 +287,7 @@ public Response onCompleted(Response response) throws Exception {
287287
public void asyncStatusHEADTest() throws Exception {
288288
try (AsyncHttpClient client = asyncHttpClient()) {
289289
final CountDownLatch l = new CountDownLatch(1);
290-
Request request = new RequestBuilder("HEAD").setUrl(getTargetUrl()).build();
290+
Request request = head(getTargetUrl()).build();
291291
Response response = client.executeRequest(request, new AsyncCompletionHandlerAdapter() {
292292

293293
@Override
@@ -319,7 +319,7 @@ public Response onCompleted(Response response) throws Exception {
319319
public void asyncStatusHEADContentLenghtTest() throws Exception {
320320
try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(120 * 1000))) {
321321
final CountDownLatch l = new CountDownLatch(1);
322-
Request request = new RequestBuilder("HEAD").setUrl(getTargetUrl()).build();
322+
Request request = head(getTargetUrl()).build();
323323

324324
client.executeRequest(request, new AsyncCompletionHandlerAdapter() {
325325
@Override
@@ -679,7 +679,7 @@ public void asyncRequestVirtualServerPOSTTest() throws Exception {
679679
for (int i = 0; i < 5; i++) {
680680
m.put("param_" + i, Arrays.asList("value_" + i));
681681
}
682-
Request request = new RequestBuilder("POST").setUrl(getTargetUrl()).setHeaders(h).setFormParams(m).setVirtualHost("localhost:" + port1).build();
682+
Request request = post(getTargetUrl()).setHeaders(h).setFormParams(m).setVirtualHost("localhost:" + port1).build();
683683

684684
Response response = client.executeRequest(request, new AsyncCompletionHandlerAdapter()).get();
685685

@@ -1133,7 +1133,7 @@ public Response onCompleted(Response response) throws Exception {
11331133
}
11341134
};
11351135

1136-
Request req = new RequestBuilder("GET").setUrl(getTargetUrl() + "?foo=bar").build();
1136+
Request req = get(getTargetUrl() + "?foo=bar").build();
11371137

11381138
client.executeRequest(req, handler).get();
11391139

@@ -1388,7 +1388,7 @@ public void mirrorByteTest() throws Exception {
13881388

13891389
@Test(groups = { "standalone", "async" })
13901390
public void testNewConnectionEventsFired() throws Exception {
1391-
Request request = new RequestBuilder("GET").setUrl("http://127.0.0.1/" + port1 + "/Test").build();
1391+
Request request = get("http://127.0.0.1/" + port1 + "/Test").build();
13921392

13931393
try (AsyncHttpClient client = asyncHttpClient()) {
13941394
EventCollectingHandler handler = new EventCollectingHandler();

client/src/test/java/org/asynchttpclient/Head302Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public AbstractHandler configureHandler() throws Exception {
6666
public void testHEAD302() throws IOException, BrokenBarrierException, InterruptedException, ExecutionException, TimeoutException {
6767
try (AsyncHttpClient client = asyncHttpClient()) {
6868
final CountDownLatch l = new CountDownLatch(1);
69-
Request request = new RequestBuilder("HEAD").setUrl("http://127.0.0.1/" + port1 + "/Test").build();
69+
Request request = head("http://127.0.0.1/" + port1 + "/Test").build();
7070

7171
client.executeRequest(request, new AsyncCompletionHandlerBase() {
7272
@Override

client/src/test/java/org/asynchttpclient/MultipleHeaderTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public void testMultipleOtherHeaders() throws IOException, ExecutionException, T
9393
final String[] xffHeaders = new String[] { null, null };
9494

9595
try (AsyncHttpClient ahc = asyncHttpClient()) {
96-
Request req = new RequestBuilder("GET").setUrl("http://localhost/" + port1 + "/MultiOther").build();
96+
Request req = get("http://localhost/" + port1 + "/MultiOther").build();
9797
final CountDownLatch latch = new CountDownLatch(1);
9898
ahc.executeRequest(req, new AsyncHandler<Void>() {
9999
public void onThrowable(Throwable t) {
@@ -142,7 +142,7 @@ public void testMultipleEntityHeaders() throws IOException, ExecutionException,
142142
final String[] clHeaders = new String[] { null, null };
143143

144144
try (AsyncHttpClient ahc = asyncHttpClient()) {
145-
Request req = new RequestBuilder("GET").setUrl("http://localhost/" + port1 + "/MultiEnt").build();
145+
Request req = get("http://localhost/" + port1 + "/MultiEnt").build();
146146
final CountDownLatch latch = new CountDownLatch(1);
147147
ahc.executeRequest(req, new AsyncHandler<Void>() {
148148
public void onThrowable(Throwable t) {

client/src/test/java/org/asynchttpclient/PostRedirectGetTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public <T> FilterContext<T> filter(FilterContext<T> ctx) throws FilterException
8383
};
8484

8585
try (AsyncHttpClient p = asyncHttpClient(config().setFollowRedirect(true).setStrict302Handling(strict).addResponseFilter(responseFilter))) {
86-
Request request = new RequestBuilder("POST").setUrl(getTargetUrl()).addFormParam("q", "a b").addHeader("x-redirect", +status + "@" + "http://localhost/" + port1 + "/foo/bar/baz").addHeader("x-negative", "true").build();
86+
Request request = post(getTargetUrl()).addFormParam("q", "a b").addHeader("x-redirect", +status + "@" + "http://localhost/" + port1 + "/foo/bar/baz").addHeader("x-negative", "true").build();
8787
Future<Integer> responseFuture = p.executeRequest(request, new AsyncCompletionHandler<Integer>() {
8888

8989
@Override
@@ -118,7 +118,7 @@ public <T> FilterContext<T> filter(FilterContext<T> ctx) throws FilterException
118118
};
119119

120120
try (AsyncHttpClient p = asyncHttpClient(config().setFollowRedirect(true).addResponseFilter(responseFilter))) {
121-
Request request = new RequestBuilder("POST").setUrl(getTargetUrl()).addFormParam("q", "a b").addHeader("x-redirect", +status + "@" + "http://localhost/" + port1 + "/foo/bar/baz").build();
121+
Request request = post(getTargetUrl()).addFormParam("q", "a b").addHeader("x-redirect", +status + "@" + "http://localhost/" + port1 + "/foo/bar/baz").build();
122122
Future<Integer> responseFuture = p.executeRequest(request, new AsyncCompletionHandler<Integer>() {
123123

124124
@Override

client/src/test/java/org/asynchttpclient/RedirectConnectionUsageTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@ public void testGetRedirectFinalUrl() throws Exception {
7676
.build();
7777

7878
try (AsyncHttpClient c = asyncHttpClient(config)) {
79-
Request r = new RequestBuilder("GET").setUrl(servletEndpointRedirectUrl).build();
80-
81-
ListenableFuture<Response> response = c.executeRequest(r);
79+
ListenableFuture<Response> response = c.executeRequest(get(servletEndpointRedirectUrl));
8280
Response res = null;
8381
res = response.get();
8482
assertNotNull(res.getResponseBody());

client/src/test/java/org/asynchttpclient/RemoteSiteTest.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,8 @@ public void testGoogleComWithTimeout() throws Exception {
9999
public void asyncStatusHEADContentLenghtTest() throws Exception {
100100
try (AsyncHttpClient p = asyncHttpClient(config().setFollowRedirect(true))) {
101101
final CountDownLatch l = new CountDownLatch(1);
102-
Request request = new RequestBuilder("HEAD").setUrl("http://www.google.com/").build();
103102

104-
p.executeRequest(request, new AsyncCompletionHandlerAdapter() {
103+
p.executeRequest(head("http://www.google.com/"), new AsyncCompletionHandlerAdapter() {
105104
@Override
106105
public Response onCompleted(Response response) throws Exception {
107106
try {
@@ -177,14 +176,13 @@ public void stripQueryStringTest() throws Exception {
177176
@Test(groups = "online")
178177
public void evilCoookieTest() throws Exception {
179178
try (AsyncHttpClient c = asyncHttpClient()) {
180-
RequestBuilder builder2 = new RequestBuilder("GET");
181-
builder2.setFollowRedirect(true);
182-
builder2.setUrl("http://www.google.com/");
183-
builder2.addHeader("Content-Type", "text/plain");
184-
builder2.addCookie(new Cookie("evilcookie", "test", false, ".google.com", "/", Long.MIN_VALUE, false, false));
185-
Request request2 = builder2.build();
186-
Response response = c.executeRequest(request2).get();
179+
RequestBuilder builder = get("http://localhost")//
180+
.setFollowRedirect(true)//
181+
.setUrl("http://www.google.com/")//
182+
.addHeader("Content-Type", "text/plain")//
183+
.addCookie(new Cookie("evilcookie", "test", false, ".google.com", "/", Long.MIN_VALUE, false, false));
187184

185+
Response response = c.executeRequest(builder.build()).get();
188186
assertNotNull(response);
189187
assertEquals(response.getStatusCode(), 200);
190188
}

0 commit comments

Comments
 (0)