Skip to content

Commit

Permalink
Close ResponseBody (closes #195)
Browse files Browse the repository at this point in the history
  • Loading branch information
sowieso-fruehling authored and mhadam committed Jan 2, 2019
1 parent b8c055f commit 08c4323
Showing 1 changed file with 41 additions and 12 deletions.
Expand Up @@ -13,8 +13,7 @@
package com.snowplowanalytics.snowplow.tracker.http;

// Java
import java.util.Iterator;
import java.util.Map;
import java.io.IOException;

// Google
import com.google.common.base.Preconditions;
Expand All @@ -27,6 +26,7 @@
import com.squareup.okhttp.RequestBody;

// Slf4j
import org.apache.http.HttpHeaders;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -96,41 +96,70 @@ public Object getHttpClient() {
* GET request to the configured endpoint.
*
* @param url the URL send
* @return the HttpResponse for the Request
* @return the HttpResponse code for the Request or -1 if exception is caught
*/
public int doGet(String url) {

Response response = null;
int returnValue = -1;

Request request = new Request.Builder().url(url).build();

try {
Response response = httpClient.newCall(request).execute();
return response.code();
response = httpClient.newCall(request).execute();
returnValue = response.code();
} catch (Exception e) {
LOGGER.error("OkHttpClient GET Request failed: {}", e.getMessage());
return -1;
} finally {
closeResponseBody(response);
}

return returnValue;
}


/**
* Attempts to send a group of payloads with a
* POST request to the configured endpoint.
*
* @param url the URL to send to
* @param payload the payload to send
* @return the HttpResponse for the Request
* @return the HttpResponse code for the Request or -1 if exception is caught
*/
public int doPost(String url, String payload) {
Response response = null;
int returnValue = -1;

try {
RequestBody body = RequestBody.create(JSON, payload);
Request request = new Request.Builder()
.url(url)
.addHeader("Content-Type", Constants.POST_CONTENT_TYPE)
.addHeader(HttpHeaders.CONTENT_TYPE, Constants.POST_CONTENT_TYPE)
.post(body)
.build();
Response response = httpClient.newCall(request).execute();
return response.code();
response = httpClient.newCall(request).execute();
returnValue = response.code();
} catch (Exception e) {
LOGGER.error("OkHttpClient POST Request failed: {}", e.getMessage());
return -1;
} finally {
closeResponseBody(response);
}

return returnValue;
}


/**
* Closes response body as required by OkHttpClient documentation
*
* @param response OkHttpClient response
*/
private void closeResponseBody(Response response) {
if (response != null && response.body() != null)
try {
response.body().close();
} catch (IOException e) {
LOGGER.error("OkHttpClient response body closing failed: {}", e.getMessage());
}
}
}
}

0 comments on commit 08c4323

Please sign in to comment.