summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Wu <peter@lekensteyn.nl>2014-05-01 22:56:06 +0200
committerPeter Wu <peter@lekensteyn.nl>2014-05-01 22:56:06 +0200
commitcb296b188093df4480cb720d3b5f4ab425ac6ec9 (patch)
tree8fae8884cdbd1e2dcf83c67b4771652b3a7ef57c
parentfcff0b671bab64dccc79f24a2818059b72cb577b (diff)
downloadTwitterDataAnalytics-cb296b188093df4480cb720d3b5f4ab425ac6ec9.tar.gz
Don't leak connections
-rw-r--r--src/io/AbstractRequester.java34
-rw-r--r--src/io/BearerRequester.java42
2 files changed, 42 insertions, 34 deletions
diff --git a/src/io/AbstractRequester.java b/src/io/AbstractRequester.java
index 342bba9..ac74533 100644
--- a/src/io/AbstractRequester.java
+++ b/src/io/AbstractRequester.java
@@ -35,23 +35,27 @@ public abstract class AbstractRequester implements Requester {
private JSONObject getJSON(String resource, boolean checkStatusCode)
throws IOException {
HttpURLConnection conn = open(buildUrl(resource));
- preconnect(conn);
- JSONObject resp = getResponseAsJson(conn);
- /* print response to stderr for debugging */
- if (resp.has("errors")) {
- try {
- String errors = resp.get("errors").toString();
- getLogger().info("Request failed: " + errors);
- } catch (JSONException ex) {
+ try {
+ preconnect(conn);
+ JSONObject resp = getResponseAsJson(conn);
+ /* print response to stderr for debugging */
+ if (resp.has("errors")) {
+ try {
+ String errors = resp.get("errors").toString();
+ getLogger().info("Request failed: " + errors);
+ } catch (JSONException ex) {
+ }
}
+ // TODO: what if there is an internal server error? Technically we
+ // should always treat that as "don't know what the server thinks".
+ if (checkStatusCode && conn.getResponseCode() != 200) {
+ // TODO: print more helpful details
+ throw new IOException("Unexpected response code");
+ }
+ return resp;
+ } finally {
+ conn.disconnect();
}
- // TODO: what if there is an internal server error? Technically we
- // should always treat that as "don't know what the server thinks".
- if (checkStatusCode && conn.getResponseCode() != 200) {
- // TODO: print more helpful details
- throw new IOException("Unexpected response code");
- }
- return resp;
}
protected final URL buildUrl(String resource) throws IOException {
diff --git a/src/io/BearerRequester.java b/src/io/BearerRequester.java
index 4cb554b..442f62a 100644
--- a/src/io/BearerRequester.java
+++ b/src/io/BearerRequester.java
@@ -45,27 +45,31 @@ public class BearerRequester extends AbstractRequester {
String postData = "grant_type=client_credentials";
URL url = buildUrl("oauth2/token");
HttpURLConnection conn = open(url);
- conn.setRequestMethod("POST");
- // set request headers
- conn.addRequestProperty("Authorization", "Basic "
- + secrets.getBearerTokenBase64());
- conn.addRequestProperty("Content-Type",
- "application/x-www-form-urlencoded; charset=UTF-8");
- conn.setFixedLengthStreamingMode(postData.length());
- // connect and send request
- conn.setDoOutput(true);
- conn.getOutputStream().write(postData.getBytes(Charsets.UTF_8));
-
try {
- JSONObject resp = getResponseAsJson(conn);
- // TODO: parse resp.errors
- if (!resp.getString("token_type").equals("bearer")) {
- throw new IOException("Expected bearer token type");
+ conn.setRequestMethod("POST");
+ // set request headers
+ conn.addRequestProperty("Authorization", "Basic "
+ + secrets.getBearerTokenBase64());
+ conn.addRequestProperty("Content-Type",
+ "application/x-www-form-urlencoded; charset=UTF-8");
+ conn.setFixedLengthStreamingMode(postData.length());
+ // connect and send request
+ conn.setDoOutput(true);
+ conn.getOutputStream().write(postData.getBytes(Charsets.UTF_8));
+
+ try {
+ JSONObject resp = getResponseAsJson(conn);
+ // TODO: parse resp.errors
+ if (!resp.getString("token_type").equals("bearer")) {
+ throw new IOException("Expected bearer token type");
+ }
+ access_token = resp.getString("access_token");
+ } catch (JSONException ex) {
+ // treat JSON errors as if an I/O error occurred
+ throw new IOException(ex);
}
- access_token = resp.getString("access_token");
- } catch (JSONException ex) {
- // treat JSON errors as if an I/O error occurred
- throw new IOException(ex);
+ } finally {
+ conn.disconnect();
}
}