package de.christianscheb.partyprojector.app.httpclient;

import java.io.*;
import java.net.*;

public class WebApiClient {

    private String baseUrl;

    public WebApiClient(String baseUrl) throws WebApiClientException {
        this.baseUrl = baseUrl;
        if (baseUrl == null) {
            throw new WebApiClientException("You need to configure a server URL");
        }
    }

    public void sendMessage(String message) throws WebApiClientException {
        String url = baseUrl + "message";
        URL urlObj;
        try {
            urlObj = new URL(url);
        } catch (MalformedURLException e) {
            throw new WebApiClientException("Could not connect to " + url, e);
        }

        String urlParameters;
        try {
            urlParameters = "message=" + URLEncoder.encode(message, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new WebApiClientException("Could not encode message", e);
        }

        HttpURLConnection urlConnection = null;
        try {
            urlConnection = (HttpURLConnection) urlObj.openConnection();
            urlConnection.setRequestMethod("POST");
            urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            urlConnection.setRequestProperty("Content-Length", Integer.toString(urlParameters.getBytes().length));
            urlConnection.setUseCaches(false);
            urlConnection.setDoInput(true);
            urlConnection.setDoOutput(true);

            //Send request
            DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream());
            wr.writeBytes(urlParameters);
            wr.flush();
            wr.close();

            //Get Response
            InputStream is = urlConnection.getInputStream();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
            String response = getResponse(rd);
        } catch (ProtocolException e) {
            throw new WebApiClientException("Could not execute HTTP request", e);
        } catch (IOException e) {
            throw new WebApiClientException("Could not execute HTTP request", e);
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
        }
    }

    private String getResponse(BufferedReader rd) throws IOException {
        String line;
        StringBuilder response = new StringBuilder();
        while ((line = rd.readLine()) != null) {
            response.append(line);
            response.append('\r');
        }
        rd.close();
        return response.toString();
    }
}

@@ -51,16 +51,76 @@
 lient;%0A%0A
+import org.json.JSONException;%0Aimport org.json.JSONObject;%0A%0A
 import j
@@ -1945,22 +1945,26 @@
         
-String
+JSONObject
  respons
@@ -1967,16 +1967,31 @@
 ponse = 
+new JSONObject(
 getRespo
@@ -1997,18 +1997,194 @@
 onse(rd)
+)
 ;%0A
+            boolean isSuccess = response.getBoolean(%22success%22);%0A            if (!isSuccess) %7B%0A                throw new WebApiClientException(%22Request failed%22);%0A            %7D%0A
         
@@ -2405,32 +2405,138 @@
 P request%22, e);%0A
+        %7D catch (JSONException e) %7B%0A            throw new WebApiClientException(%22Malformed response%22, e);%0A
         %7D finall

Validate response