package de.bornemisza.maintenance.task;

import javax.ejb.Stateless;

import org.javalite.http.Get;
import org.javalite.http.Http;
import org.javalite.http.HttpException;

import de.bornemisza.rest.HttpConnection;

@Stateless
public class HealthChecks {

    public boolean isCouchDbReady(HttpConnection conn) {
        Http http = conn.getHttp();
        Get get = http.get("", 200, 1000);
        try {
            if (get.responseCode() != 200) {
                return false;
            }
        }
        catch (HttpException ex) {
            return false;
        }
        String expected = "{\"db_name\":\"" + conn.getDatabaseName() + "\"";
        return get.text().startsWith(expected);
    }

    public String getCouchDbStatus(HttpConnection conn) {
        Http http = conn.getHttp();
        Get get = http.get("");
        try {
            int responseCode = get.responseCode();
            if (responseCode != 200) {
                return "Response Code: " + responseCode;
            }
        }
        catch (HttpException ex) {
            Throwable t = ex.getCause();
            return t == null ? ex.toString() : t.toString();
        }
        String expected = "{\"db_name\":\"" + conn.getDatabaseName() + "\"";
        String body = get.text();
        if (body.startsWith(expected)) {
            return "OK";
        }
        else return body;
    }

}

@@ -34,16 +34,50 @@
 .task;%0A%0A
+import java.util.logging.Logger;%0A%0A
 import j
@@ -687,22 +687,21 @@
         
-return
+if (!
  get.tex
@@ -724,16 +724,175 @@
 xpected)
+) %7B%0A            Logger.getAnonymousLogger().warning(%22Health Check unexpected Response: %22 + get.text());%0A            return false;%0A        %7D%0A        return true
 ;%0A    %7D%0A

improve logging