| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent a7b01f6 commit b2458a8
12 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,6 @@ | |||
| 1 | 1 | [SNAPSHOT] | |
| 2 | 2 | * Add Polar API (https://www.polar.com/) (thanks to https://github.com/vidi42) | |
| 3 | + * make Response accept resources to autoclose and autoclose it (thanks to https://github.com/drei01) | ||
| 3 | 4 | ||
| 4 | 5 | [6.9.0] | |
| 5 | 6 | * Add Xero API (https://www.xero.com/) (thanks to https://github.com/SidneyAllen) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,6 +2,11 @@ | |||
| 2 | 2 | ||
| 3 | 3 | public interface OAuthAsyncRequestCallback<T> { | |
| 4 | 4 | ||
| 5 | + /** | ||
| 6 | + * Implementations of this method should close provided response in case it implements {@link java.io.Closeable} | ||
| 7 | + * | ||
| 8 | + * @param response | ||
| 9 | + */ | ||
| 5 | 10 | void onCompleted(T response); | |
| 6 | 11 | ||
| 7 | 12 | void onThrowable(Throwable t); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -400,6 +400,15 @@ public void setCharset(String charsetName) { | |||
| 400 | 400 | ||
| 401 | 401 | public interface ResponseConverter<T> { | |
| 402 | 402 | ||
| 403 | + /** | ||
| 404 | + * Implementations of this method should close provided Response in case response is not included in the return | ||
| 405 | + * Object of type <T> Then responsibility to close response is in on the | ||
| 406 | + * {@link com.github.scribejava.core.model.OAuthAsyncRequestCallback#onCompleted(java.lang.Object) } | ||
| 407 | + * | ||
| 408 | + * @param response | ||
| 409 | + * @return T | ||
| 410 | + * @throws IOException | ||
| 411 | + */ | ||
| 403 | 412 | T convert(Response response) throws IOException; | |
| 404 | 413 | } | |
| 405 | 414 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,16 +20,20 @@ public class Response implements Closeable { | |||
| 20 | 20 | private final Map<String, String> headers; | |
| 21 | 21 | private String body; | |
| 22 | 22 | private InputStream stream; | |
| 23 | + private Closeable[] closeables; | ||
| 24 | + private boolean closed; | ||
| 23 | 25 | ||
| 24 | 26 | private Response(int code, String message, Map<String, String> headers) { | |
| 25 | 27 | this.code = code; | |
| 26 | 28 | this.message = message; | |
| 27 | 29 | this.headers = headers; | |
| 28 | 30 | } | |
| 29 | 31 | ||
| 30 | - public Response(int code, String message, Map<String, String> headers, InputStream stream) { | ||
| 32 | + public Response(int code, String message, Map<String, String> headers, InputStream stream, | ||
| 33 | + Closeable... closeables) { | ||
| 31 | 34 | this(code, message, headers); | |
| 32 | 35 | this.stream = stream; | |
| 36 | + this.closeables = closeables; | ||
| 33 | 37 | } | |
| 34 | 38 | ||
| 35 | 39 | public Response(int code, String message, Map<String, String> headers, String body) { | |
@@ -124,8 +128,27 @@ public String toString() { | |||
| 124 | 128 | ||
| 125 | 129 | @Override | |
| 126 | 130 | public void close() throws IOException { | |
| 127 | - if (stream != null) { | ||
| 128 | - stream.close(); | ||
| 131 | + if (closed) { | ||
| 132 | + return; | ||
| 129 | 133 | } | |
| 134 | + IOException ioException = null; | ||
| 135 | + if (closeables != null) { | ||
| 136 | + for (Closeable closeable : closeables) { | ||
| 137 | + if (closeable == null) { | ||
| 138 | + continue; | ||
| 139 | + } | ||
| 140 | + try { | ||
| 141 | + closeable.close(); | ||
| 142 | + } catch (IOException ioE) { | ||
| 143 | + if (ioException != null) { | ||
| 144 | + ioException = ioE; | ||
| 145 | + } | ||
| 146 | + } | ||
| 147 | + } | ||
| 148 | + } | ||
| 149 | + if (ioException != null) { | ||
| 150 | + throw ioException; | ||
| 151 | + } | ||
| 152 | + closed = true; | ||
| 130 | 153 | } | |
| 131 | 154 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -61,7 +61,9 @@ public Future<OAuth1RequestToken> getRequestTokenAsync(OAuthAsyncRequestCallback | |||
| 61 | 61 | return execute(request, callback, new OAuthRequest.ResponseConverter<OAuth1RequestToken>() { | |
| 62 | 62 | @Override | |
| 63 | 63 | public OAuth1RequestToken convert(Response response) throws IOException { | |
| 64 | - return getApi().getRequestTokenExtractor().extract(response); | ||
| 64 | + final OAuth1RequestToken token = getApi().getRequestTokenExtractor().extract(response); | ||
| 65 | + response.close(); | ||
| 66 | + return token; | ||
| 65 | 67 | } | |
| 66 | 68 | }); | |
| 67 | 69 | } | |
@@ -130,7 +132,9 @@ public Future<OAuth1AccessToken> getAccessTokenAsync(OAuth1RequestToken requestT | |||
| 130 | 132 | return execute(request, callback, new OAuthRequest.ResponseConverter<OAuth1AccessToken>() { | |
| 131 | 133 | @Override | |
| 132 | 134 | public OAuth1AccessToken convert(Response response) throws IOException { | |
| 133 | - return getApi().getAccessTokenExtractor().extract(response); | ||
| 135 | + final OAuth1AccessToken token = getApi().getAccessTokenExtractor().extract(response); | ||
| 136 | + response.close(); | ||
| 137 | + return token; | ||
| 134 | 138 | } | |
| 135 | 139 | }); | |
| 136 | 140 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -75,7 +75,9 @@ public OAuth2AccessToken convert(Response response) throws IOException { | |||
| 75 | 75 | final String body = response.getBody(); | |
| 76 | 76 | log("response body: %s", body); | |
| 77 | 77 | } | |
| 78 | - return getApi().getAccessTokenExtractor().extract(response); | ||
| 78 | + final OAuth2AccessToken token = getApi().getAccessTokenExtractor().extract(response); | ||
| 79 | + response.close(); | ||
| 80 | + return token; | ||
| 79 | 81 | } | |
| 80 | 82 | }); | |
| 81 | 83 | } | |
@@ -445,6 +447,7 @@ public Future<Void> revokeToken(String tokenToRevoke, OAuthAsyncRequestCallback< | |||
| 445 | 447 | @Override | |
| 446 | 448 | public Void convert(Response response) throws IOException { | |
| 447 | 449 | checkForErrorRevokeToken(response); | |
| 450 | + response.close(); | ||
| 448 | 451 | return null; | |
| 449 | 452 | } | |
| 450 | 453 | }); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -17,6 +17,7 @@ | |||
| 17 | 17 | import com.github.scribejava.core.model.OAuthRequest.ResponseConverter; | |
| 18 | 18 | import com.github.scribejava.core.model.Response; | |
| 19 | 19 | import java.io.IOException; | |
| 20 | + import java.io.InputStream; | ||
| 20 | 21 | import org.apache.http.HttpEntity; | |
| 21 | 22 | ||
| 22 | 23 | public class OAuthAsyncCompletionHandler<T> implements FutureCallback<HttpResponse> { | |
@@ -44,8 +45,9 @@ public void completed(HttpResponse httpResponse) { | |||
| 44 | 45 | final StatusLine statusLine = httpResponse.getStatusLine(); | |
| 45 | 46 | ||
| 46 | 47 | final HttpEntity httpEntity = httpResponse.getEntity(); | |
| 48 | + final InputStream contentStream = httpEntity == null ? null : httpEntity.getContent(); | ||
| 47 | 49 | final Response response = new Response(statusLine.getStatusCode(), statusLine.getReasonPhrase(), headersMap, | |
| 48 | - httpEntity == null ? null : httpEntity.getContent()); | ||
| 50 | + contentStream, contentStream); | ||
| 49 | 51 | ||
| 50 | 52 | @SuppressWarnings("unchecked") | |
| 51 | 53 | final T t = converter == null ? (T) response : converter.convert(response); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -167,6 +167,7 @@ private static class AllGoodResponseConverter implements OAuthRequest.ResponseCo | |||
| 167 | 167 | ||
| 168 | 168 | @Override | |
| 169 | 169 | public String convert(Response response) throws IOException { | |
| 170 | + response.close(); | ||
| 170 | 171 | return "All good"; | |
| 171 | 172 | } | |
| 172 | 173 | } | |
@@ -175,6 +176,7 @@ private static class ExceptionResponseConverter implements OAuthRequest.Response | |||
| 175 | 176 | ||
| 176 | 177 | @Override | |
| 177 | 178 | public String convert(Response response) throws IOException { | |
| 179 | + response.close(); | ||
| 178 | 180 | throw new IOException("Failed to convert"); | |
| 179 | 181 | } | |
| 180 | 182 | } | |
@@ -183,6 +185,7 @@ private static class OAuthExceptionResponseConverter implements OAuthRequest.Res | |||
| 183 | 185 | ||
| 184 | 186 | @Override | |
| 185 | 187 | public String convert(Response response) throws IOException { | |
| 188 | + response.close(); | ||
| 186 | 189 | throw new OAuthException("bad oauth"); | |
| 187 | 190 | } | |
| 188 | 191 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -84,6 +84,7 @@ private static class AllGoodResponseConverter implements OAuthRequest.ResponseCo | |||
| 84 | 84 | ||
| 85 | 85 | @Override | |
| 86 | 86 | public String convert(Response response) throws IOException { | |
| 87 | + response.close(); | ||
| 87 | 88 | return "All good"; | |
| 88 | 89 | } | |
| 89 | 90 | } | |
@@ -92,6 +93,7 @@ private static class OAuthExceptionResponseConverter implements OAuthRequest.Res | |||
| 92 | 93 | ||
| 93 | 94 | @Override | |
| 94 | 95 | public String convert(Response response) throws IOException { | |
| 96 | + response.close(); | ||
| 95 | 97 | throw new OAuthException("bad oauth"); | |
| 96 | 98 | } | |
| 97 | 99 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -38,15 +38,15 @@ public void onResponse(Call call, okhttp3.Response okHttpResponse) { | |||
| 38 | 38 | try { | |
| 39 | 39 | ||
| 40 | 40 | final Response response = OkHttpHttpClient.convertResponse(okHttpResponse); | |
| 41 | - try { | ||
| 41 | + try { | ||
| 42 | 42 | @SuppressWarnings("unchecked") | |
| 43 | 43 | final T t = converter == null ? (T) response : converter.convert(response); | |
| 44 | 44 | okHttpFuture.setResult(t); | |
| 45 | 45 | if (callback != null) { | |
| 46 | 46 | callback.onCompleted(t); | |
| 47 | 47 | } | |
| 48 | - } catch (IOException | RuntimeException e) { | ||
| 49 | - okHttpFuture.setException(e); | ||
| 48 | + } catch (IOException | RuntimeException e) { | ||
| 49 | + okHttpFuture.setException(e); | ||
| 50 | 50 | if (callback != null) { | |
| 51 | 51 | callback.onThrowable(e); | |
| 52 | 52 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments