Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure closing the response body after it was parsed #79

Merged
merged 4 commits into from
Mar 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, U extends Auth0Exception> implements ParameterizableRequest<T, U>, AuthorizableRequest<T, U>, Callback {

private final Map<String, String> headers;
Expand Down Expand Up @@ -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<Map<String, Object>>() {
}.getType();
Map<String, Object> mapPayload = gson.fromJson(stringPayload, mapType);
Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, U extends Auth0Exception> extends BaseRequest<T, U> implements ParameterizableRequest<T, U>, Callback {

private final String method;
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}