Skip to content

Commit 7a8ca94

Browse files
committed
Merge branch 'master' of https://github.com/jochen314/scribejava into jochen314-master
2 parents b867f07 + b1afe2f commit 7a8ca94

13 files changed

Lines changed: 607 additions & 51 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.IOException;
44
import java.util.regex.Matcher;
55
import java.util.regex.Pattern;
6+
67
import com.github.scribejava.core.exceptions.OAuthException;
78
import com.github.scribejava.core.model.OAuth2AccessToken;
89
import com.github.scribejava.core.model.Response;
@@ -37,6 +38,9 @@ public static OAuth2AccessTokenExtractor instance() {
3738
*/
3839
@Override
3940
public OAuth2AccessToken extract(Response response) throws IOException {
41+
if (response.getCode() != 200) {
42+
throw new OAuthException("Response code not 200 but " + response.getCode());
43+
}
4044
final String body = response.getBody();
4145
Preconditions.checkEmptyString(body,
4246
"Response body is incorrect. Can't extract a token from an empty string");

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 (IOException e) {
82+
} catch (Throwable 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 Exception exception;
14+
private final Throwable exception;
1515
private final V response;
1616

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

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

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

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ public void shouldExtractTokenFromResponseWithManyParameters() throws IOExceptio
5858
assertEquals("foo1234", extracted.getAccessToken());
5959
}
6060

61+
@Test(expected = OAuthException.class)
62+
public void shouldThrowExceptionIfErrorResponse() throws IOException {
63+
final String response = "";
64+
this.extractor.extract(error(response));
65+
}
66+
6167
@Test(expected = OAuthException.class)
6268
public void shouldThrowExceptionIfTokenIsAbsent() throws IOException {
6369
final String response = "&expires=5108";
@@ -78,4 +84,8 @@ public void shouldThrowExceptionIfResponseIsEmptyString() throws IOException {
7884
private static Response ok(String body) {
7985
return new Response(200, /* message */ null, /* headers */ Collections.<String, String>emptyMap(), body);
8086
}
87+
88+
private static Response error(final String body) {
89+
return new Response(400, /* message */ null, /* headers */ Collections.<String, String>emptyMap(), body);
90+
}
8191
}

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
}

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,17 @@ public T onCompleted(com.ning.http.client.Response ningResponse) throws IOExcept
3535
final Response response = new Response(ningResponse.getStatusCode(), ningResponse.getStatusText(), headersMap,
3636
ningResponse.getResponseBodyAsStream());
3737

38-
@SuppressWarnings("unchecked")
39-
final T t = converter == null ? (T) response : converter.convert(response);
40-
if (callback != null) {
41-
callback.onCompleted(t);
38+
try {
39+
@SuppressWarnings("unchecked")
40+
final T t = converter == null ? (T) response : converter.convert(response);
41+
if (callback != null) {
42+
callback.onCompleted(t);
43+
}
44+
return t;
45+
} catch (Throwable t) {
46+
onThrowable(t);
47+
return null;
4248
}
43-
return t;
4449
}
4550

4651
@Override
@@ -49,4 +54,4 @@ public void onThrowable(Throwable t) {
4954
callback.onThrowable(t);
5055
}
5156
}
52-
};
57+
}

0 commit comments

Comments
 (0)