package com.flowdock.plugins.confluence;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

import com.atlassian.confluence.json.json.JsonObject;

public class FlowdockConnection {
	public static void sendApiMessage(JsonObject params, String apiKey) {
		if (params == null || apiKey == null) {
			return;
		}

		try {
			URL apiUrl = getApiUrl(URLEncoder.encode(apiKey, "UTF-8"));
			postData(apiUrl, params);
		} catch (MalformedURLException mue) {

		} catch (IOException ioe) {

		}
	}

	private static void postData(URL apiUrl, JsonObject params) throws IOException {
		HttpURLConnection conn = (HttpURLConnection) apiUrl.openConnection();
		conn.setRequestMethod("POST");
		conn.setUseCaches(false);
		conn.setDoOutput(true);
		conn.setDoInput(true);
		conn.setRequestProperty("Content-Type", "application/json");
		conn.setRequestProperty("User-Agent", "Flowdock Confluence Plugin");

		OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
		out.write(params.serialize());
		out.close();

		BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
		while (in.readLine() != null);
		in.close();

		conn.connect();
	}

	private static URL getApiUrl(String encodedApiKey) throws MalformedURLException {
		return new URL("https://www.flowdock.com/jari/confluence/messages/?tokens=" + encodedApiKey);
	}
}

@@ -1159,24 +1159,34 @@
 tream());%0A%09%09
+try %7B%0A%09%09  
 out.write(pa
@@ -1206,16 +1206,32 @@
 e());%0A%09%09
+%7D finally %7B%0A%09%09  
 out.clos
@@ -1230,24 +1230,28 @@
 out.close();
+%0A%09%09%7D
 %0A%0A%09%09Buffered
@@ -1328,16 +1328,25 @@
 am()));%0A
+%09%09try %7B%0A%09
 %09%09while 
@@ -1370,16 +1370,31 @@
  null);%0A
+%09%09%7D finally %7B%0A%09
 %09%09in.clo
@@ -1403,26 +1403,11 @@
 ();%0A
-%0A
 %09%09
-conn.connect();
+%7D
 %0A%09%7D%0A

Remove redundant connect call and ensure streams closing