Skip to content

Commit 3a9ad12

Browse files
Jochen KleinJochen Klein
authored andcommitted
deliver OAuth exceptions from OAuthAsyncCompletionHandler
- apache httpclient
1 parent e46a55c commit 3a9ad12

2 files changed

Lines changed: 67 additions & 33 deletions

File tree

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
package com.github.scribejava.httpclient.apache;
22

3-
import com.github.scribejava.core.model.OAuthAsyncRequestCallback;
4-
import com.github.scribejava.core.model.OAuthRequest.ResponseConverter;
5-
import com.github.scribejava.core.model.Response;
6-
import org.apache.http.Header;
7-
import org.apache.http.HttpResponse;
8-
import org.apache.http.concurrent.FutureCallback;
9-
10-
import java.io.IOException;
113
import java.util.HashMap;
124
import java.util.Map;
135
import java.util.concurrent.CancellationException;
146
import java.util.concurrent.CountDownLatch;
157
import java.util.concurrent.ExecutionException;
168
import java.util.concurrent.TimeUnit;
179
import java.util.concurrent.TimeoutException;
10+
11+
import org.apache.http.Header;
12+
import org.apache.http.HttpResponse;
1813
import org.apache.http.StatusLine;
14+
import org.apache.http.concurrent.FutureCallback;
15+
16+
import com.github.scribejava.core.model.OAuthAsyncRequestCallback;
17+
import com.github.scribejava.core.model.OAuthRequest.ResponseConverter;
18+
import com.github.scribejava.core.model.Response;
1919

2020
public class OAuthAsyncCompletionHandler<T> implements FutureCallback<HttpResponse> {
2121

2222
private final OAuthAsyncRequestCallback<T> callback;
2323
private final ResponseConverter<T> converter;
2424
private final CountDownLatch latch;
2525
private T result;
26-
private Exception exception;
26+
private Throwable exception;
2727

2828
public OAuthAsyncCompletionHandler(OAuthAsyncRequestCallback<T> callback, ResponseConverter<T> converter) {
2929
this.callback = callback;
@@ -41,16 +41,17 @@ public void completed(HttpResponse httpResponse) {
4141

4242
final StatusLine statusLine = httpResponse.getStatusLine();
4343

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

4748
@SuppressWarnings("unchecked")
4849
final T t = converter == null ? (T) response : converter.convert(response);
4950
result = t;
5051
if (callback != null) {
5152
callback.onCompleted(result);
5253
}
53-
} catch (IOException e) {
54+
} catch (Throwable e) {
5455
exception = e;
5556
if (callback != null) {
5657
callback.onThrowable(e);

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

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
package com.github.scribejava.httpclient.apache;
22

3-
import com.github.scribejava.core.model.OAuthAsyncRequestCallback;
4-
import com.github.scribejava.core.model.OAuthRequest;
5-
import com.github.scribejava.core.model.Response;
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.Assert.assertNull;
6+
import static org.junit.Assert.assertTrue;
7+
import static org.junit.Assert.fail;
8+
9+
import java.io.ByteArrayInputStream;
10+
import java.io.IOException;
11+
import java.util.concurrent.CancellationException;
12+
import java.util.concurrent.ExecutionException;
13+
614
import org.apache.http.HttpResponse;
715
import org.apache.http.ProtocolVersion;
816
import org.apache.http.entity.BasicHttpEntity;
@@ -11,21 +19,17 @@
1119
import org.junit.Before;
1220
import org.junit.Test;
1321

14-
import java.io.ByteArrayInputStream;
15-
import java.io.IOException;
16-
import java.util.concurrent.CancellationException;
17-
import java.util.concurrent.ExecutionException;
18-
19-
import static org.junit.Assert.assertEquals;
20-
import static org.junit.Assert.assertNotNull;
21-
import static org.junit.Assert.assertNull;
22-
import static org.junit.Assert.assertTrue;
23-
import static org.junit.Assert.fail;
22+
import com.github.scribejava.core.exceptions.OAuthException;
23+
import com.github.scribejava.core.model.OAuthAsyncRequestCallback;
24+
import com.github.scribejava.core.model.OAuthRequest;
25+
import com.github.scribejava.core.model.Response;
2426

2527
public class OauthAsyncCompletionHandlerTest {
2628

2729
private static final AllGoodResponseConverter ALL_GOOD_RESPONSE_CONVERTER = new AllGoodResponseConverter();
2830
private static final ExceptionResponseConverter EXCEPTION_RESPONSE_CONVERTER = new ExceptionResponseConverter();
31+
private static final OAuthExceptionResponseConverter OAUTH_EXCEPTION_RESPONSE_CONVERTER =
32+
new OAuthExceptionResponseConverter();
2933

3034
private OAuthAsyncCompletionHandler<String> handler;
3135
private TestCallback callback;
@@ -63,8 +67,8 @@ public void setUp() {
6367
@Test
6468
public void shouldReleaseLatchOnSuccess() throws Exception {
6569
handler = new OAuthAsyncCompletionHandler<>(callback, ALL_GOOD_RESPONSE_CONVERTER);
66-
final HttpResponse response = new BasicHttpResponse(new BasicStatusLine(
67-
new ProtocolVersion("4", 1, 1), 200, "ok"));
70+
final HttpResponse response =
71+
new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("4", 1, 1), 200, "ok"));
6872
final BasicHttpEntity entity = new BasicHttpEntity();
6973
entity.setContent(new ByteArrayInputStream(new byte[0]));
7074
response.setEntity(entity);
@@ -78,8 +82,8 @@ public void shouldReleaseLatchOnSuccess() throws Exception {
7882
@Test
7983
public void shouldReleaseLatchOnIOException() throws Exception {
8084
handler = new OAuthAsyncCompletionHandler<>(callback, EXCEPTION_RESPONSE_CONVERTER);
81-
final HttpResponse response = new BasicHttpResponse(new BasicStatusLine(
82-
new ProtocolVersion("4", 1, 1), 200, "ok"));
85+
final HttpResponse response =
86+
new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("4", 1, 1), 200, "ok"));
8387
final BasicHttpEntity entity = new BasicHttpEntity();
8488
entity.setContent(new ByteArrayInputStream(new byte[0]));
8589
response.setEntity(entity);
@@ -96,11 +100,32 @@ public void shouldReleaseLatchOnIOException() throws Exception {
96100
}
97101
}
98102

103+
@Test
104+
public void shouldReportOAuthException() throws Exception {
105+
handler = new OAuthAsyncCompletionHandler<>(callback, OAUTH_EXCEPTION_RESPONSE_CONVERTER);
106+
final HttpResponse response =
107+
new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("4", 1, 1), 200, "ok"));
108+
final BasicHttpEntity entity = new BasicHttpEntity();
109+
entity.setContent(new ByteArrayInputStream(new byte[0]));
110+
response.setEntity(entity);
111+
handler.completed(response);
112+
assertNull(callback.getResponse());
113+
assertNotNull(callback.getThrowable());
114+
assertTrue(callback.getThrowable() instanceof OAuthException);
115+
// verify latch is released
116+
try {
117+
handler.getResult();
118+
fail();
119+
} catch (ExecutionException expected) {
120+
// expected
121+
}
122+
}
123+
99124
@Test
100125
public void shouldReleaseLatchOnCancel() throws Exception {
101126
handler = new OAuthAsyncCompletionHandler<>(callback, ALL_GOOD_RESPONSE_CONVERTER);
102-
final HttpResponse response = new BasicHttpResponse(new BasicStatusLine(
103-
new ProtocolVersion("4", 1, 1), 200, "ok"));
127+
final HttpResponse response =
128+
new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("4", 1, 1), 200, "ok"));
104129
final BasicHttpEntity entity = new BasicHttpEntity();
105130
entity.setContent(new ByteArrayInputStream(new byte[0]));
106131
response.setEntity(entity);
@@ -120,8 +145,8 @@ public void shouldReleaseLatchOnCancel() throws Exception {
120145
@Test
121146
public void shouldReleaseLatchOnFailure() throws Exception {
122147
handler = new OAuthAsyncCompletionHandler<>(callback, ALL_GOOD_RESPONSE_CONVERTER);
123-
final HttpResponse response = new BasicHttpResponse(new BasicStatusLine(
124-
new ProtocolVersion("4", 1, 1), 200, "ok"));
148+
final HttpResponse response =
149+
new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("4", 1, 1), 200, "ok"));
125150
final BasicHttpEntity entity = new BasicHttpEntity();
126151
entity.setContent(new ByteArrayInputStream(new byte[0]));
127152
response.setEntity(entity);
@@ -153,4 +178,12 @@ public String convert(Response response) throws IOException {
153178
throw new IOException("Failed to convert");
154179
}
155180
}
181+
182+
private static class OAuthExceptionResponseConverter implements OAuthRequest.ResponseConverter<String> {
183+
184+
@Override
185+
public String convert(Response response) throws IOException {
186+
throw new OAuthException("bad oauth");
187+
}
188+
}
156189
}

0 commit comments

Comments
 (0)