]> WPIA git - gigi.git/blobdiff - src/club/wpia/gigi/dbObjects/DomainPingConfiguration.java
chg: revoke certificates if repeated ping failed
[gigi.git] / src / club / wpia / gigi / dbObjects / DomainPingConfiguration.java
index bdaad75880a5acb1e0c9dd09428a9be7597df19e..59db66cda5e1623f3401537b0d208ae05c175584 100644 (file)
@@ -1,5 +1,6 @@
 package club.wpia.gigi.dbObjects;
 
+import java.sql.Timestamp;
 import java.util.Date;
 
 import club.wpia.gigi.Gigi;
@@ -92,4 +93,47 @@ public class DomainPingConfiguration implements IdCachable {
         }
         throw new GigiApiException(SprintfCommand.createSimple("Reping is only allowed after {0} minutes, yours end at {1}.", REPING_MINIMUM_DELAY / 60 / 1000, new Date(lastExecution.getTime() + REPING_MINIMUM_DELAY)));
     }
+
+    /**
+     * Return true when there was a last execution and it succeeded.
+     * 
+     * @return if this ping is currently valid.
+     */
+    public boolean isValid() {
+        try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT state='success' AS bool from `domainPinglog` WHERE `configId`=? ORDER BY `when` DESC LIMIT 1")) {
+            ps.setInt(1, id);
+            GigiResultSet rs = ps.executeQuery();
+            if ( !rs.next()) {
+                return false;
+            }
+            return rs.getBoolean(1);
+        }
+    }
+
+    /**
+     * Return true when this ping has not been successful within the last 2
+     * weeks.
+     * 
+     * @param time
+     *            the point in time for which the determination is carried out.
+     * @return the value for this ping.
+     */
+    public boolean isStrictlyInvalid(Date time) {
+        Date lastSuccess = getLastSuccess();
+        if (lastSuccess.getTime() == 0) {
+            // never a successful ping
+            return true;
+        }
+        try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `when` AS stamp from `domainPinglog` WHERE `configId`=? AND state='failed' AND `when` > ? ORDER BY `when` ASC LIMIT 1")) {
+            ps.setInt(1, id);
+            ps.setTimestamp(2, new Timestamp(lastSuccess.getTime()));
+            GigiResultSet rs = ps.executeQuery();
+            if (rs.next()) {
+                Date turnedInvalid = new Date(rs.getTimestamp("stamp").getTime());
+                // turned invalid older than 2 weeks ago
+                return turnedInvalid.getTime() < time.getTime() - 2L * 7 * 24 * 60 * 60 * 1000;
+            }
+            return false;
+        }
+    }
 }