Skip to content

Commit c9bb1a1

Browse files
committed
implement async HTTPClient methods in JDKHttpClient (it's complete now)
1 parent 3c72408 commit c9bb1a1

2 files changed

Lines changed: 71 additions & 3 deletions

File tree

scribejava-core/src/main/java/com/github/scribejava/core/httpclient/jdk/JDKHttpClient.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,35 @@ public void close() throws IOException {
3434
public <T> Future<T> executeAsync(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl,
3535
byte[] bodyContents, OAuthAsyncRequestCallback<T> callback,
3636
OAuthRequestAsync.ResponseConverter<T> converter) {
37-
throw new UnsupportedOperationException("Not supported yet.");
37+
try {
38+
final T response = converter.convert(execute(userAgent, headers, httpVerb, completeUrl, bodyContents));
39+
callback.onCompleted(response);
40+
return new JDKHttpFuture<>(response);
41+
} catch (InterruptedException | ExecutionException | IOException e) {
42+
callback.onThrowable(e);
43+
return new JDKHttpFuture<>(e);
44+
}
3845
}
3946

4047
@Override
4148
public <T> Future<T> executeAsync(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl,
4249
String bodyContents, OAuthAsyncRequestCallback<T> callback,
4350
OAuthRequestAsync.ResponseConverter<T> converter) {
44-
throw new UnsupportedOperationException("Not supported yet.");
51+
try {
52+
final T response = converter.convert(execute(userAgent, headers, httpVerb, completeUrl, bodyContents));
53+
callback.onCompleted(response);
54+
return new JDKHttpFuture<>(response);
55+
} catch (InterruptedException | ExecutionException | IOException e) {
56+
callback.onThrowable(e);
57+
return new JDKHttpFuture<>(e);
58+
}
4559
}
4660

4761
@Override
4862
public <T> Future<T> executeAsync(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl,
4963
File bodyContents, OAuthAsyncRequestCallback<T> callback,
5064
OAuthRequestAsync.ResponseConverter<T> converter) {
51-
throw new UnsupportedOperationException("Not supported yet.");
65+
throw new UnsupportedOperationException("JDKHttpClient do not support File payload for the moment");
5266
}
5367

5468
@Override
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.github.scribejava.core.httpclient.jdk;
2+
3+
import java.util.concurrent.ExecutionException;
4+
import java.util.concurrent.Future;
5+
import java.util.concurrent.TimeUnit;
6+
import java.util.concurrent.TimeoutException;
7+
8+
public class JDKHttpFuture<V> implements Future<V> {
9+
10+
private final Exception exception;
11+
private final V response;
12+
13+
public JDKHttpFuture(Exception exception) {
14+
this(null, exception);
15+
}
16+
17+
public JDKHttpFuture(V response) {
18+
this(response, null);
19+
}
20+
21+
private JDKHttpFuture(V response, Exception exception) {
22+
this.response = response;
23+
this.exception = exception;
24+
}
25+
26+
@Override
27+
public boolean cancel(boolean mayInterruptIfRunning) {
28+
return false;
29+
}
30+
31+
@Override
32+
public boolean isCancelled() {
33+
return false;
34+
}
35+
36+
@Override
37+
public boolean isDone() {
38+
return true;
39+
}
40+
41+
@Override
42+
public V get() throws InterruptedException, ExecutionException {
43+
if (exception != null) {
44+
throw new ExecutionException(exception);
45+
}
46+
47+
return response;
48+
}
49+
50+
@Override
51+
public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
52+
return get();
53+
}
54+
}

0 commit comments

Comments
 (0)