Skip to content

Commit e8ba323

Browse files
committed
* add RuntimeException processing in async http clients (delivered to onError callbacks) + check 200 status code from response in OAuth2AccessTokenExtractor (thanks to https://github.com/jochen314)
1 parent 7a8ca94 commit e8ba323

16 files changed

Lines changed: 131 additions & 135 deletions

File tree

changelog

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* add Multipart functionality to JDK Http Client (thanks to https://github.com/eos1d3)
44
* switch OAuth2 ClientAuthenticationType from enum to interface to be extensible according to
55
https://tools.ietf.org/html/rfc6749#section-2.3.2 (thanks to https://github.com/zawn)
6+
* add RuntimeException processing in async http clients (delivered to onError callbacks) (thanks to https://github.com/jochen314)
7+
* check 200 status code from response in OAuth2AccessTokenExtractor (thanks to https://github.com/jochen314)
68

79
[5.5.0]
810
* fix error parsing for Fitbit (thanks to https://github.com/danmana)

checkstyle.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
<module name="InnerAssignment"/>
4646
<module name="MissingSwitchDefault"/>
4747
<module name="MultipleVariableDeclarations"/>
48-
<module name="NoClone"/>
4948
<module name="NoFinalizer"/>
5049
<module name="OneStatementPerLine"/>
5150
<module name="OverloadMethodsDeclarationOrder"/>

scribejava-core/src/main/java/com/github/scribejava/core/extractors/OAuth2AccessTokenExtractor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static OAuth2AccessTokenExtractor instance() {
3939
@Override
4040
public OAuth2AccessToken extract(Response response) throws IOException {
4141
if (response.getCode() != 200) {
42-
throw new OAuthException("Response code not 200 but " + response.getCode());
42+
throw new OAuthException("Response code is not 200 but '" + response.getCode() + '\'');
4343
}
4444
final String body = response.getBody();
4545
Preconditions.checkEmptyString(body,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ private <T> Future<T> doExecuteAsync(String userAgent, Map<String, String> heade
7979
callback.onCompleted(t);
8080
}
8181
return new JDKHttpFuture<>(t);
82-
} catch (Throwable e) {
82+
} catch (IOException | RuntimeException e) {
8383
if (callback != null) {
8484
callback.onThrowable(e);
8585
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@
1111
*/
1212
public class JDKHttpFuture<V> implements Future<V> {
1313

14-
private final Throwable exception;
14+
private final Exception exception;
1515
private final V response;
1616

17-
public JDKHttpFuture(Throwable exception) {
17+
public JDKHttpFuture(Exception exception) {
1818
this(null, exception);
1919
}
2020

2121
public JDKHttpFuture(V response) {
2222
this(response, null);
2323
}
2424

25-
private JDKHttpFuture(V response, Throwable exception) {
25+
private JDKHttpFuture(V response, Exception exception) {
2626
this.response = response;
2727
this.exception = exception;
2828
}

scribejava-core/src/test/java/com/github/scribejava/core/extractors/OAuth2AccessTokenExtractorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void shouldExtractTokenFromResponseWithManyParameters() throws IOExceptio
6161
@Test(expected = OAuthException.class)
6262
public void shouldThrowExceptionIfErrorResponse() throws IOException {
6363
final String response = "";
64-
this.extractor.extract(error(response));
64+
extractor.extract(error(response));
6565
}
6666

6767
@Test(expected = OAuthException.class)
@@ -85,7 +85,7 @@ private static Response ok(String body) {
8585
return new Response(200, /* message */ null, /* headers */ Collections.<String, String>emptyMap(), body);
8686
}
8787

88-
private static Response error(final String body) {
88+
private static Response error(String body) {
8989
return new Response(400, /* message */ null, /* headers */ Collections.<String, String>emptyMap(), body);
9090
}
9191
}

scribejava-httpclient-ahc/src/main/java/com/github/scribejava/httpclient/ahc/OAuthAsyncCompletionHandler.java

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,26 @@ public OAuthAsyncCompletionHandler(OAuthAsyncRequestCallback<T> callback,
2020
}
2121

2222
@Override
23-
public T onCompleted(org.asynchttpclient.Response ahcResponse) throws IOException {
24-
final Map<String, String> headersMap = new HashMap<>();
25-
for (Map.Entry<String, String> header : ahcResponse.getHeaders()) {
26-
headersMap.put(header.getKey(), header.getValue());
23+
public T onCompleted(org.asynchttpclient.Response ahcResponse) {
24+
try {
25+
final Map<String, String> headersMap = new HashMap<>();
26+
for (Map.Entry<String, String> header : ahcResponse.getHeaders()) {
27+
headersMap.put(header.getKey(), header.getValue());
28+
}
29+
30+
final Response response = new Response(ahcResponse.getStatusCode(), ahcResponse.getStatusText(), headersMap,
31+
ahcResponse.getResponseBodyAsStream());
32+
33+
@SuppressWarnings("unchecked")
34+
final T t = converter == null ? (T) response : converter.convert(response);
35+
if (callback != null) {
36+
callback.onCompleted(t);
37+
}
38+
return t;
39+
} catch (IOException | RuntimeException e) {
40+
onThrowable(e);
41+
return null;
2742
}
28-
29-
final Response response = new Response(ahcResponse.getStatusCode(), ahcResponse.getStatusText(), headersMap,
30-
ahcResponse.getResponseBodyAsStream());
31-
32-
@SuppressWarnings("unchecked")
33-
final T t = converter == null ? (T) response : converter.convert(response);
34-
if (callback != null) {
35-
callback.onCompleted(t);
36-
}
37-
return t;
3843
}
3944

4045
@Override

scribejava-httpclient-apache/src/main/java/com/github/scribejava/httpclient/apache/OAuthAsyncCompletionHandler.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616
import com.github.scribejava.core.model.OAuthAsyncRequestCallback;
1717
import com.github.scribejava.core.model.OAuthRequest.ResponseConverter;
1818
import com.github.scribejava.core.model.Response;
19+
import java.io.IOException;
1920

2021
public class OAuthAsyncCompletionHandler<T> implements FutureCallback<HttpResponse> {
2122

2223
private final OAuthAsyncRequestCallback<T> callback;
2324
private final ResponseConverter<T> converter;
2425
private final CountDownLatch latch;
2526
private T result;
26-
private Throwable exception;
27+
private Exception exception;
2728

2829
public OAuthAsyncCompletionHandler(OAuthAsyncRequestCallback<T> callback, ResponseConverter<T> converter) {
2930
this.callback = callback;
@@ -41,17 +42,16 @@ public void completed(HttpResponse httpResponse) {
4142

4243
final StatusLine statusLine = httpResponse.getStatusLine();
4344

44-
final Response response =
45-
new Response(statusLine.getStatusCode(), statusLine.getReasonPhrase(), headersMap,
46-
httpResponse.getEntity().getContent());
45+
final Response response = new Response(statusLine.getStatusCode(), statusLine.getReasonPhrase(), headersMap,
46+
httpResponse.getEntity().getContent());
4747

4848
@SuppressWarnings("unchecked")
4949
final T t = converter == null ? (T) response : converter.convert(response);
5050
result = t;
5151
if (callback != null) {
5252
callback.onCompleted(result);
5353
}
54-
} catch (Throwable e) {
54+
} catch (IOException | RuntimeException e) {
5555
exception = e;
5656
if (callback != null) {
5757
callback.onThrowable(e);

scribejava-httpclient-apache/src/test/java/com/github/scribejava/httpclient/apache/OauthAsyncCompletionHandlerTest.java renamed to scribejava-httpclient-apache/src/test/java/com/github/scribejava/httpclient/apache/OAuthAsyncCompletionHandlerTest.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@
2424
import com.github.scribejava.core.model.OAuthRequest;
2525
import com.github.scribejava.core.model.Response;
2626

27-
public class OauthAsyncCompletionHandlerTest {
27+
public class OAuthAsyncCompletionHandlerTest {
2828

2929
private static final AllGoodResponseConverter ALL_GOOD_RESPONSE_CONVERTER = new AllGoodResponseConverter();
3030
private static final ExceptionResponseConverter EXCEPTION_RESPONSE_CONVERTER = new ExceptionResponseConverter();
31-
private static final OAuthExceptionResponseConverter OAUTH_EXCEPTION_RESPONSE_CONVERTER =
32-
new OAuthExceptionResponseConverter();
31+
private static final OAuthExceptionResponseConverter OAUTH_EXCEPTION_RESPONSE_CONVERTER
32+
= new OAuthExceptionResponseConverter();
3333

3434
private OAuthAsyncCompletionHandler<String> handler;
3535
private TestCallback callback;
@@ -67,8 +67,8 @@ public void setUp() {
6767
@Test
6868
public void shouldReleaseLatchOnSuccess() throws Exception {
6969
handler = new OAuthAsyncCompletionHandler<>(callback, ALL_GOOD_RESPONSE_CONVERTER);
70-
final HttpResponse response =
71-
new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("4", 1, 1), 200, "ok"));
70+
final HttpResponse response
71+
= new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("4", 1, 1), 200, "ok"));
7272
final BasicHttpEntity entity = new BasicHttpEntity();
7373
entity.setContent(new ByteArrayInputStream(new byte[0]));
7474
response.setEntity(entity);
@@ -82,8 +82,8 @@ public void shouldReleaseLatchOnSuccess() throws Exception {
8282
@Test
8383
public void shouldReleaseLatchOnIOException() throws Exception {
8484
handler = new OAuthAsyncCompletionHandler<>(callback, EXCEPTION_RESPONSE_CONVERTER);
85-
final HttpResponse response =
86-
new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("4", 1, 1), 200, "ok"));
85+
final HttpResponse response
86+
= new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("4", 1, 1), 200, "ok"));
8787
final BasicHttpEntity entity = new BasicHttpEntity();
8888
entity.setContent(new ByteArrayInputStream(new byte[0]));
8989
response.setEntity(entity);
@@ -103,8 +103,8 @@ public void shouldReleaseLatchOnIOException() throws Exception {
103103
@Test
104104
public void shouldReportOAuthException() throws Exception {
105105
handler = new OAuthAsyncCompletionHandler<>(callback, OAUTH_EXCEPTION_RESPONSE_CONVERTER);
106-
final HttpResponse response =
107-
new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("4", 1, 1), 200, "ok"));
106+
final HttpResponse response
107+
= new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("4", 1, 1), 200, "ok"));
108108
final BasicHttpEntity entity = new BasicHttpEntity();
109109
entity.setContent(new ByteArrayInputStream(new byte[0]));
110110
response.setEntity(entity);
@@ -124,8 +124,8 @@ public void shouldReportOAuthException() throws Exception {
124124
@Test
125125
public void shouldReleaseLatchOnCancel() throws Exception {
126126
handler = new OAuthAsyncCompletionHandler<>(callback, ALL_GOOD_RESPONSE_CONVERTER);
127-
final HttpResponse response =
128-
new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("4", 1, 1), 200, "ok"));
127+
final HttpResponse response
128+
= new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("4", 1, 1), 200, "ok"));
129129
final BasicHttpEntity entity = new BasicHttpEntity();
130130
entity.setContent(new ByteArrayInputStream(new byte[0]));
131131
response.setEntity(entity);
@@ -145,8 +145,8 @@ public void shouldReleaseLatchOnCancel() throws Exception {
145145
@Test
146146
public void shouldReleaseLatchOnFailure() throws Exception {
147147
handler = new OAuthAsyncCompletionHandler<>(callback, ALL_GOOD_RESPONSE_CONVERTER);
148-
final HttpResponse response =
149-
new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("4", 1, 1), 200, "ok"));
148+
final HttpResponse response
149+
= new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("4", 1, 1), 200, "ok"));
150150
final BasicHttpEntity entity = new BasicHttpEntity();
151151
entity.setContent(new ByteArrayInputStream(new byte[0]));
152152
response.setEntity(entity);

scribejava-httpclient-ning/src/main/java/com/github/scribejava/httpclient/ning/OAuthAsyncCompletionHandler.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,29 @@ public OAuthAsyncCompletionHandler(OAuthAsyncRequestCallback<T> callback,
2121
}
2222

2323
@Override
24-
public T onCompleted(com.ning.http.client.Response ningResponse) throws IOException {
25-
final Map<String, String> headersMap = new HashMap<>();
26-
27-
for (Map.Entry<String, List<String>> header : ningResponse.getHeaders().entrySet()) {
28-
final StringBuilder value = new StringBuilder();
29-
for (String str : header.getValue()) {
30-
value.append(str);
24+
public T onCompleted(com.ning.http.client.Response ningResponse) {
25+
try {
26+
final Map<String, String> headersMap = new HashMap<>();
27+
28+
for (Map.Entry<String, List<String>> header : ningResponse.getHeaders().entrySet()) {
29+
final StringBuilder value = new StringBuilder();
30+
for (String str : header.getValue()) {
31+
value.append(str);
32+
}
33+
headersMap.put(header.getKey(), value.toString());
3134
}
32-
headersMap.put(header.getKey(), value.toString());
33-
}
3435

35-
final Response response = new Response(ningResponse.getStatusCode(), ningResponse.getStatusText(), headersMap,
36-
ningResponse.getResponseBodyAsStream());
36+
final Response response = new Response(ningResponse.getStatusCode(), ningResponse.getStatusText(),
37+
headersMap, ningResponse.getResponseBodyAsStream());
3738

38-
try {
3939
@SuppressWarnings("unchecked")
4040
final T t = converter == null ? (T) response : converter.convert(response);
4141
if (callback != null) {
4242
callback.onCompleted(t);
4343
}
4444
return t;
45-
} catch (Throwable t) {
46-
onThrowable(t);
45+
} catch (IOException | RuntimeException e) {
46+
onThrowable(e);
4747
return null;
4848
}
4949
}

0 commit comments

Comments
 (0)