package com.namelessmc.NamelessAPI;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class NamelessAPI {

	/**
	 * Checks if a web API connection can be established
	 * @return An exception if the connection was unsuccessful, null if the connection was successful.
	 */
	public static NamelessException checkWebAPIConnection(URL url) {
		try {
			URL apiConnection = new URL(url + "/checkConnection");

			HttpURLConnection connection = (HttpURLConnection) apiConnection.openConnection();
			connection.setRequestMethod("POST");
			connection.setRequestProperty("Content-Length", Integer.toString(0));
			connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			connection.setDoOutput(true);
			connection.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");

			// Initialise output stream
			DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
			// Write request
			outputStream.writeBytes("");

			// Initialise input stream
			InputStream inputStream = connection.getInputStream();

			// Handle response
			BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
			StringBuilder responseBuilder = new StringBuilder();

			String responseString;
			while ((responseString = streamReader.readLine()) != null)
				responseBuilder.append(responseString);

			JsonObject response = new JsonObject();
			JsonParser parser = new JsonParser();

			response = parser.parse(responseBuilder.toString()).getAsJsonObject();

			String errorMessage;
			
			if (response.has("success")
					|| response.get("message").getAsString().equalsIgnoreCase("Invalid API method")) {
				errorMessage = null;
			} else if (response.has("error")) {
				errorMessage = response.get("message").getAsString();
			} else {
				errorMessage = "Unknown error";
			}

			// Close output/input stream
			outputStream.flush();
			outputStream.close();
			inputStream.close();

			// Disconnect
			connection.disconnect();
			
			if (errorMessage == null) {
				//Error message == null - connection successful
				return null;
			} else {
				//Connection unsuccessful
				return new NamelessException(errorMessage);
			}

		} catch (Exception e) {
			return new NamelessException(e);
		}
	}

}
@@ -211,16 +211,89 @@
 net.URL;
+%0Aimport java.net.URLConnection;%0A%0Aimport javax.net.ssl.HttpsURLConnection;
 %0A%0Aimport
@@ -623,16 +623,75 @@
 %09%09try %7B%0A
+%09%09%09boolean https = url.toString().startsWith(%22https%22);%0A%09%09%09%0A
 %09%09%09URL a
@@ -744,16 +744,70 @@
 %22);%0A
+%09%09%09
 %0A
+%09%09%09URLConnection connection;%0A%09%09%09%0A%09%09%09if (https) %7B%0A%09
 %09%09%09Http
+s
 URLC
@@ -808,33 +808,222 @@
 psURLConnection 
-c
+httpsConnection = (HttpsURLConnection) apiConnection.openConnection();%0A%09%09%09%09httpsConnection.setRequestMethod(%22POST%22);%0A%09%09%09%09connection = httpsConnection;%0A%09%09%09%7D else %7B%0A%09%09%09%09HttpURLConnection httpC
 onnection = (Htt
@@ -1069,25 +1069,30 @@
 ction();%0A%09%09%09
-c
+%09httpC
 onnection.se
@@ -1111,24 +1111,66 @@
 od(%22POST%22);%0A
+%09%09%09%09connection = httpConnection;%0A%09%09%09%7D%0A%09%09%09%0A
 %09%09%09connectio
@@ -1449,24 +1449,25 @@
 NT 5.0)%22);%0A%0A
+%0A
 %09%09%09// Initia
@@ -2630,24 +2630,63 @@
 sconnect%0A%09%09%09
+if (https) %7B%0A%09%09%09%09((HttpsURLConnection) 
 connection.d
@@ -2683,16 +2683,17 @@
 nnection
+)
 .disconn
@@ -2699,16 +2699,84 @@
 nect();%0A
+%09%09%09%7D else %7B%0A%09%09%09%09((HttpURLConnection) connection).disconnect();%0A%09%09%09%7D%0A
 %09%09%09%0A%09%09%09i

Add support for HTTPS