Skip to content

Commit

Permalink
Merge pull request #79 from auth0/fixed-close-response-body
Browse files Browse the repository at this point in the history
Ensure closing the response body after it was parsed
  • Loading branch information
hzalaz authored Mar 2, 2017
2 parents 76e6945 + 4cd830e commit 3c37a6d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
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();
}
}

0 comments on commit 3c37a6d

Please sign in to comment.