]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/Domain.java
ADD: output pinglog, test DNS and email ping.
[gigi.git] / src / org / cacert / gigi / Domain.java
index 538081d8d37143f3e924c2579d57162894d9996c..21fe668edda1d638beb90287e769758c8b1f860e 100644 (file)
@@ -3,7 +3,6 @@ package org.cacert.gigi;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
-
 import org.cacert.gigi.database.DatabaseConnection;
 
 public class Domain {
@@ -104,4 +103,58 @@ public class Domain {
         }
     }
 
+    public void addPing(String type, String config) throws GigiApiException {
+        try {
+            PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO pingconfig SET domainid=?, type=?, info=?");
+            ps.setInt(1, id);
+            ps.setString(2, type);
+            ps.setString(3, config);
+            ps.execute();
+        } catch (SQLException e) {
+            throw new GigiApiException(e);
+        }
+    }
+
+    public void verify(String hash) throws GigiApiException {
+        try {
+            PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE domainPinglog SET state='success' WHERE challenge=? AND configId IN (SELECT id FROM pingconfig WHERE domainId=?)");
+            ps.setString(1, hash);
+            ps.setInt(2, id);
+            ps.executeUpdate();
+        } catch (SQLException e) {
+            throw new GigiApiException(e);
+        }
+    }
+
+    public boolean isVerified() {
+        try {
+            PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM domainPinglog INNER JOIN pingconfig ON pingconfig.id=domainPinglog.configId WHERE domainid=? AND state='success'");
+            ps.setInt(1, id);
+            ResultSet rs = ps.executeQuery();
+            return rs.next();
+        } catch (SQLException e) {
+            e.printStackTrace();
+        }
+        return false;
+    }
+
+    public String[][] getPings() throws GigiApiException {
+        try {
+            PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT state, type, info, result FROM domainPinglog INNER JOIN pingconfig ON pingconfig.id=domainPinglog.configid WHERE pingconfig.domainid=? ORDER BY `when` DESC;");
+            ps.setInt(1, id);
+            ResultSet rs = ps.executeQuery();
+            rs.last();
+            String[][] contents = new String[rs.getRow()][];
+            rs.beforeFirst();
+            for (int i = 0; i < contents.length && rs.next(); i++) {
+                contents[i] = new String[] {
+                        rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4)
+                };
+            }
+            return contents;
+        } catch (SQLException e) {
+            throw new GigiApiException(e);
+        }
+
+    }
 }