diff --git a/auth0/src/main/java/com/auth0/android/request/internal/BaseRequest.java b/auth0/src/main/java/com/auth0/android/request/internal/BaseRequest.java index 4f31b5907..e2c3e0782 100755 --- a/auth0/src/main/java/com/auth0/android/request/internal/BaseRequest.java +++ b/auth0/src/main/java/com/auth0/android/request/internal/BaseRequest.java @@ -43,12 +43,15 @@ import com.squareup.okhttp.Request; import com.squareup.okhttp.RequestBody; import com.squareup.okhttp.Response; +import com.squareup.okhttp.ResponseBody; import java.io.IOException; import java.lang.reflect.Type; import java.util.HashMap; import java.util.Map; +import static com.auth0.android.request.internal.ResponseUtils.closeStream; + abstract class BaseRequest implements ParameterizableRequest, AuthorizableRequest, Callback { private final Map headers; @@ -125,8 +128,9 @@ protected RequestBody buildBody() throws RequestBodyBuildException { protected U parseUnsuccessfulResponse(Response response) { String stringPayload = null; + ResponseBody body = response.body(); try { - stringPayload = response.body().string(); + stringPayload = body.string(); Type mapType = new TypeToken>() { }.getType(); Map mapPayload = gson.fromJson(stringPayload, mapType); @@ -136,6 +140,8 @@ protected U parseUnsuccessfulResponse(Response response) { } catch (IOException e) { final Auth0Exception auth0Exception = new Auth0Exception("Error parsing the server response", e); return errorBuilder.from("Request to " + url.toString() + " failed", auth0Exception); + } finally { + closeStream(body); } } diff --git a/auth0/src/main/java/com/auth0/android/request/internal/ResponseUtils.java b/auth0/src/main/java/com/auth0/android/request/internal/ResponseUtils.java new file mode 100644 index 000000000..6b9ae5e1a --- /dev/null +++ b/auth0/src/main/java/com/auth0/android/request/internal/ResponseUtils.java @@ -0,0 +1,19 @@ +package com.auth0.android.request.internal; + +import java.io.Closeable; +import java.io.IOException; + +class ResponseUtils { + + /** + * Attempts to close a stream. No exception will be thrown if an IOException was raised. + * + * @param closeable the stream to close + */ + static void closeStream(Closeable closeable) { + try { + closeable.close(); + } catch (IOException ignored) { + } + } +} diff --git a/auth0/src/main/java/com/auth0/android/request/internal/SimpleRequest.java b/auth0/src/main/java/com/auth0/android/request/internal/SimpleRequest.java index 67d2b4088..4eefe1c05 100755 --- a/auth0/src/main/java/com/auth0/android/request/internal/SimpleRequest.java +++ b/auth0/src/main/java/com/auth0/android/request/internal/SimpleRequest.java @@ -35,10 +35,13 @@ import com.squareup.okhttp.OkHttpClient; import com.squareup.okhttp.Request; import com.squareup.okhttp.Response; +import com.squareup.okhttp.ResponseBody; import java.io.IOException; import java.io.Reader; +import static com.auth0.android.request.internal.ResponseUtils.closeStream; + class SimpleRequest extends BaseRequest implements ParameterizableRequest, Callback { private final String method; @@ -66,13 +69,16 @@ public void onResponse(Response response) throws IOException { return; } + ResponseBody body = response.body(); try { - Reader charStream = response.body().charStream(); + Reader charStream = body.charStream(); T payload = getAdapter().fromJson(charStream); postOnSuccess(payload); } catch (IOException e) { final Auth0Exception auth0Exception = new Auth0Exception("Failed to parse response to request to " + url, e); postOnFailure(getErrorBuilder().from("Failed to parse a successful response", auth0Exception)); + } finally { + closeStream(body); } } @@ -99,11 +105,14 @@ public T execute() throws Auth0Exception { throw parseUnsuccessfulResponse(response); } + ResponseBody body = response.body(); try { - Reader charStream = response.body().charStream(); + Reader charStream = body.charStream(); return getAdapter().fromJson(charStream); } catch (IOException e) { throw new Auth0Exception("Failed to parse response to request to " + url, e); + } finally { + closeStream(body); } } } diff --git a/auth0/src/test/java/com/auth0/android/request/internal/ResponseUtilsTest.java b/auth0/src/test/java/com/auth0/android/request/internal/ResponseUtilsTest.java new file mode 100644 index 000000000..cb8db7e46 --- /dev/null +++ b/auth0/src/test/java/com/auth0/android/request/internal/ResponseUtilsTest.java @@ -0,0 +1,24 @@ +package com.auth0.android.request.internal; + +import com.squareup.okhttp.ResponseBody; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +public class ResponseUtilsTest { + + @Rule + public ExpectedException exception = ExpectedException.none(); + + @Test + public void shouldCloseBody() throws Exception { + ResponseBody body = mock(ResponseBody.class); + ResponseUtils.closeStream(body); + + verify(body).close(); + } +} \ No newline at end of file