X Tutup
Skip to content

Commit b2458a8

Browse files
committed
make Response accept resources to autoclose and autoclose it (thanks to https://github.com/drei01)
1 parent a7b01f6 commit b2458a8

File tree

12 files changed

+69
-12
lines changed

12 files changed

+69
-12
lines changed

changelog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[SNAPSHOT]
22
* 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)
34

45
[6.9.0]
56
* Add Xero API (https://www.xero.com/) (thanks to https://github.com/SidneyAllen)

scribejava-core/src/main/java/com/github/scribejava/core/model/OAuthAsyncRequestCallback.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
public interface OAuthAsyncRequestCallback<T> {
44

5+
/**
6+
* Implementations of this method should close provided response in case it implements {@link java.io.Closeable}
7+
*
8+
* @param response
9+
*/
510
void onCompleted(T response);
611

712
void onThrowable(Throwable t);

scribejava-core/src/main/java/com/github/scribejava/core/model/OAuthRequest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,15 @@ public void setCharset(String charsetName) {
400400

401401
public interface ResponseConverter<T> {
402402

403+
/**
404+
* Implementations of this method should close provided Response in case response is not included in the return
405+
* Object of type &lt;T&gt; 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+
*/
403412
T convert(Response response) throws IOException;
404413
}
405414

scribejava-core/src/main/java/com/github/scribejava/core/model/Response.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,20 @@ public class Response implements Closeable {
2020
private final Map<String, String> headers;
2121
private String body;
2222
private InputStream stream;
23+
private Closeable[] closeables;
24+
private boolean closed;
2325

2426
private Response(int code, String message, Map<String, String> headers) {
2527
this.code = code;
2628
this.message = message;
2729
this.headers = headers;
2830
}
2931

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) {
3134
this(code, message, headers);
3235
this.stream = stream;
36+
this.closeables = closeables;
3337
}
3438

3539
public Response(int code, String message, Map<String, String> headers, String body) {
@@ -124,8 +128,27 @@ public String toString() {
124128

125129
@Override
126130
public void close() throws IOException {
127-
if (stream != null) {
128-
stream.close();
131+
if (closed) {
132+
return;
129133
}
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;
130153
}
131154
}

scribejava-core/src/main/java/com/github/scribejava/core/oauth/OAuth10aService.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ public Future<OAuth1RequestToken> getRequestTokenAsync(OAuthAsyncRequestCallback
6161
return execute(request, callback, new OAuthRequest.ResponseConverter<OAuth1RequestToken>() {
6262
@Override
6363
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;
6567
}
6668
});
6769
}
@@ -130,7 +132,9 @@ public Future<OAuth1AccessToken> getAccessTokenAsync(OAuth1RequestToken requestT
130132
return execute(request, callback, new OAuthRequest.ResponseConverter<OAuth1AccessToken>() {
131133
@Override
132134
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;
134138
}
135139
});
136140
}

scribejava-core/src/main/java/com/github/scribejava/core/oauth/OAuth20Service.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ public OAuth2AccessToken convert(Response response) throws IOException {
7575
final String body = response.getBody();
7676
log("response body: %s", body);
7777
}
78-
return getApi().getAccessTokenExtractor().extract(response);
78+
final OAuth2AccessToken token = getApi().getAccessTokenExtractor().extract(response);
79+
response.close();
80+
return token;
7981
}
8082
});
8183
}
@@ -445,6 +447,7 @@ public Future<Void> revokeToken(String tokenToRevoke, OAuthAsyncRequestCallback<
445447
@Override
446448
public Void convert(Response response) throws IOException {
447449
checkForErrorRevokeToken(response);
450+
response.close();
448451
return null;
449452
}
450453
});

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.github.scribejava.core.model.OAuthRequest.ResponseConverter;
1818
import com.github.scribejava.core.model.Response;
1919
import java.io.IOException;
20+
import java.io.InputStream;
2021
import org.apache.http.HttpEntity;
2122

2223
public class OAuthAsyncCompletionHandler<T> implements FutureCallback<HttpResponse> {
@@ -44,8 +45,9 @@ public void completed(HttpResponse httpResponse) {
4445
final StatusLine statusLine = httpResponse.getStatusLine();
4546

4647
final HttpEntity httpEntity = httpResponse.getEntity();
48+
final InputStream contentStream = httpEntity == null ? null : httpEntity.getContent();
4749
final Response response = new Response(statusLine.getStatusCode(), statusLine.getReasonPhrase(), headersMap,
48-
httpEntity == null ? null : httpEntity.getContent());
50+
contentStream, contentStream);
4951

5052
@SuppressWarnings("unchecked")
5153
final T t = converter == null ? (T) response : converter.convert(response);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ private static class AllGoodResponseConverter implements OAuthRequest.ResponseCo
167167

168168
@Override
169169
public String convert(Response response) throws IOException {
170+
response.close();
170171
return "All good";
171172
}
172173
}
@@ -175,6 +176,7 @@ private static class ExceptionResponseConverter implements OAuthRequest.Response
175176

176177
@Override
177178
public String convert(Response response) throws IOException {
179+
response.close();
178180
throw new IOException("Failed to convert");
179181
}
180182
}
@@ -183,6 +185,7 @@ private static class OAuthExceptionResponseConverter implements OAuthRequest.Res
183185

184186
@Override
185187
public String convert(Response response) throws IOException {
188+
response.close();
186189
throw new OAuthException("bad oauth");
187190
}
188191
}

scribejava-httpclient-ning/src/test/java/com/github/scribejava/httpclient/ning/OAuthAsyncCompletionHandlerTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ private static class AllGoodResponseConverter implements OAuthRequest.ResponseCo
8484

8585
@Override
8686
public String convert(Response response) throws IOException {
87+
response.close();
8788
return "All good";
8889
}
8990
}
@@ -92,6 +93,7 @@ private static class OAuthExceptionResponseConverter implements OAuthRequest.Res
9293

9394
@Override
9495
public String convert(Response response) throws IOException {
96+
response.close();
9597
throw new OAuthException("bad oauth");
9698
}
9799
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ public void onResponse(Call call, okhttp3.Response okHttpResponse) {
3838
try {
3939

4040
final Response response = OkHttpHttpClient.convertResponse(okHttpResponse);
41-
try {
41+
try {
4242
@SuppressWarnings("unchecked")
4343
final T t = converter == null ? (T) response : converter.convert(response);
4444
okHttpFuture.setResult(t);
4545
if (callback != null) {
4646
callback.onCompleted(t);
4747
}
48-
} catch (IOException | RuntimeException e) {
49-
okHttpFuture.setException(e);
48+
} catch (IOException | RuntimeException e) {
49+
okHttpFuture.setException(e);
5050
if (callback != null) {
5151
callback.onThrowable(e);
5252
}

0 commit comments

Comments
 (0)
X Tutup