package net.gpedro.integrations.slack;

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 java.net.URLEncoder;

import com.google.gson.JsonObject;

public class SlackApi {

    private String service;

    public SlackApi(String service) {
        if (service == null) {
            throw new IllegalArgumentException(
                    "Missing WebHook URL Configuration @ SlackApi");
        } else
            if (!service.startsWith("https://hooks.slack.com/services/")) {
                throw new IllegalArgumentException(
                        "Invalid Service URL. WebHook URL Format: https://hooks.slack.com/services/{id_1}/{id_2}/{token}");
            }

        this.service = service;
    }

    /**
     * Prepare Message and Send to request
     * 
     * @param message
     */
    public void call(SlackMessage message) {
        if (message != null) {
            this.send(message.prepare());
        }
    }

    /**
     * Send request to WebService
     * 
     * @param message
     * @return String response
     */
    private String send(JsonObject message) {
        URL url;
        HttpURLConnection connection = null;
        try {
            // Create connection
            url = new URL(this.service);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setConnectTimeout(5000);
            connection.setUseCaches(false);
            connection.setDoInput(true);
            connection.setDoOutput(true);

            String payload = "payload="
                    + URLEncoder.encode(message.toString(), "UTF-8");

            // Send request
            DataOutputStream wr = new DataOutputStream(
                    connection.getOutputStream());
            wr.writeBytes(payload);
            wr.flush();
            wr.close();

            // Get Response
            InputStream is = connection.getInputStream();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
            String line;
            StringBuffer response = new StringBuffer();
            while ((line = rd.readLine()) != null) {
                response.append(line);
                response.append('\r');
            }

            System.out.println(response.toString());
            rd.close();
            return response.toString();

        } catch (Exception e) {

            e.printStackTrace();
            return null;

        } finally {

            if (connection != null) {
                connection.disconnect();
            }
        }
    }

}

@@ -33,16 +33,52 @@
 slack;%0A%0A
+import com.google.gson.JsonObject;%0A%0A
 import j
@@ -284,44 +284,8 @@
 r;%0A%0A
-import com.google.gson.JsonObject;%0A%0A
 publ
@@ -2391,61 +2391,8 @@
  %7D%0A%0A
-            System.out.println(response.toString());%0A
     
@@ -2407,24 +2407,24 @@
 rd.close();%0A
+
             
@@ -2447,25 +2447,24 @@
 toString();%0A
-%0A
         %7D ca
@@ -2483,17 +2483,16 @@
 on e) %7B%0A
-%0A
         
@@ -2499,55 +2499,39 @@
     
-e.printStackTrace();%0A            return null
+throw new RuntimeException(e)
 ;%0A
-%0A
     
@@ -2546,17 +2546,16 @@
 nally %7B%0A
-%0A
         
@@ -2649,17 +2649,16 @@
       %7D%0A
+
     %7D%0A
-%0A
 %7D%0A

Throw the error, don't hide it