From cb296b188093df4480cb720d3b5f4ab425ac6ec9 Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Thu, 1 May 2014 22:56:06 +0200 Subject: Don't leak connections --- src/io/AbstractRequester.java | 34 +++++++++++++++++++--------------- src/io/BearerRequester.java | 42 +++++++++++++++++++++++------------------- 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(); } } -- cgit v1.2.1