]> WPIA git - gigi.git/commitdiff
ADD: A step towards a more friendly SQL API.
authorFelix Dörre <felix@dogcraft.de>
Sat, 13 Sep 2014 23:25:17 +0000 (01:25 +0200)
committerFelix Dörre <felix@dogcraft.de>
Sat, 13 Sep 2014 23:25:17 +0000 (01:25 +0200)
29 files changed:
src/org/cacert/gigi/database/DatabaseConnection.java
src/org/cacert/gigi/database/GigiPreparedStatement.java [new file with mode: 0644]
src/org/cacert/gigi/database/GigiResultSet.java [new file with mode: 0644]
src/org/cacert/gigi/dbObjects/Assurance.java
src/org/cacert/gigi/dbObjects/Certificate.java
src/org/cacert/gigi/dbObjects/CertificateProfile.java
src/org/cacert/gigi/dbObjects/Domain.java
src/org/cacert/gigi/dbObjects/DomainPingConfiguration.java
src/org/cacert/gigi/dbObjects/EmailAddress.java
src/org/cacert/gigi/dbObjects/User.java
src/org/cacert/gigi/email/EmailProvider.java
src/org/cacert/gigi/output/CertificateIterable.java
src/org/cacert/gigi/pages/LoginPage.java
src/org/cacert/gigi/pages/account/MyDetailsForm.java
src/org/cacert/gigi/pages/account/MyListingForm.java
src/org/cacert/gigi/pages/account/certs/CertificateIssueForm.java
src/org/cacert/gigi/pages/account/certs/Certificates.java
src/org/cacert/gigi/pages/main/Signup.java
src/org/cacert/gigi/pages/wot/AssuranceForm.java
src/org/cacert/gigi/pages/wot/AssurePage.java
src/org/cacert/gigi/pages/wot/MyPoints.java
src/org/cacert/gigi/ping/PingerDaemon.java
src/org/cacert/gigi/util/Job.java
src/org/cacert/gigi/util/Notary.java
tests/org/cacert/gigi/TestUserGroupMembership.java
tests/org/cacert/gigi/testUtils/ManagedTest.java
tests/org/cacert/gigi/testUtils/PingTest.java
tests/org/cacert/gigi/util/TestNotary.java
util/org/cacert/gigi/util/SimpleSigner.java

index 21d701cb4bc4048589fcd845040e5660a405912d..abeb78d0d765d7888af97e79a8bd1550e65a869e 100644 (file)
@@ -15,7 +15,7 @@ public class DatabaseConnection {
 
     private Connection c;
 
-    private HashMap<String, PreparedStatement> statements = new HashMap<String, PreparedStatement>();
+    private HashMap<String, GigiPreparedStatement> statements = new HashMap<String, GigiPreparedStatement>();
 
     private static Properties credentials;
 
@@ -44,11 +44,15 @@ public class DatabaseConnection {
         }
     }
 
-    public PreparedStatement prepare(String query) throws SQLException {
+    public GigiPreparedStatement prepare(String query) {
         ensureOpen();
-        PreparedStatement statement = statements.get(query);
+        GigiPreparedStatement statement = statements.get(query);
         if (statement == null) {
-            statement = c.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
+            try {
+                statement = new GigiPreparedStatement(c.prepareStatement(query, Statement.RETURN_GENERATED_KEYS));
+            } catch (SQLException e) {
+                throw new Error(e);
+            }
             statements.put(query, statement);
         }
         return statement;
@@ -71,14 +75,6 @@ public class DatabaseConnection {
         lastAction = System.currentTimeMillis();
     }
 
-    public static int lastInsertId(PreparedStatement query) throws SQLException {
-        ResultSet rs = query.getGeneratedKeys();
-        rs.next();
-        int id = rs.getInt(1);
-        rs.close();
-        return id;
-    }
-
     private static ThreadLocal<DatabaseConnection> instances = new ThreadLocal<DatabaseConnection>() {
 
         @Override
diff --git a/src/org/cacert/gigi/database/GigiPreparedStatement.java b/src/org/cacert/gigi/database/GigiPreparedStatement.java
new file mode 100644 (file)
index 0000000..81e5f4e
--- /dev/null
@@ -0,0 +1,106 @@
+package org.cacert.gigi.database;
+
+import java.sql.Date;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Timestamp;
+
+public class GigiPreparedStatement {
+
+    PreparedStatement target;
+
+    public GigiPreparedStatement(PreparedStatement preparedStatement) {
+        target = preparedStatement;
+    }
+
+    public GigiResultSet executeQuery() {
+        try {
+            return new GigiResultSet(target.executeQuery());
+        } catch (SQLException e) {
+            handleSQL(e);
+            throw new Error(e);
+        }
+    }
+
+    public int executeUpdate() {
+        try {
+            return target.executeUpdate();
+        } catch (SQLException e) {
+            handleSQL(e);
+            throw new Error(e);
+        }
+    }
+
+    public boolean execute() {
+        try {
+            return target.execute();
+        } catch (SQLException e) {
+            handleSQL(e);
+            throw new Error(e);
+        }
+    }
+
+    public void setInt(int parameterIndex, int x) {
+        try {
+            target.setInt(parameterIndex, x);
+        } catch (SQLException e) {
+            handleSQL(e);
+            throw new Error(e);
+        }
+    }
+
+    public void setString(int parameterIndex, String x) {
+        try {
+            target.setString(parameterIndex, x);
+        } catch (SQLException e) {
+            handleSQL(e);
+            throw new Error(e);
+        }
+    }
+
+    public void setDate(int parameterIndex, Date x) {
+        try {
+            target.setDate(parameterIndex, x);
+        } catch (SQLException e) {
+            handleSQL(e);
+            throw new Error(e);
+        }
+    }
+
+    public void setTimestamp(int parameterIndex, Timestamp x) {
+        try {
+            target.setTimestamp(parameterIndex, x);
+        } catch (SQLException e) {
+            handleSQL(e);
+            throw new Error(e);
+        }
+    }
+
+    public int lastInsertId() {
+        try {
+            ResultSet rs = target.getGeneratedKeys();
+            rs.next();
+            int id = rs.getInt(1);
+            rs.close();
+            return id;
+        } catch (SQLException e) {
+            handleSQL(e);
+            throw new Error(e);
+        }
+    }
+
+    public void setBoolean(int parameterIndex, boolean x) {
+        try {
+            target.setBoolean(parameterIndex, x);
+        } catch (SQLException e) {
+            handleSQL(e);
+            throw new Error(e);
+        }
+    }
+
+    private void handleSQL(SQLException e) {
+        // TODO Auto-generated method stub
+
+    }
+}
diff --git a/src/org/cacert/gigi/database/GigiResultSet.java b/src/org/cacert/gigi/database/GigiResultSet.java
new file mode 100644 (file)
index 0000000..9dc83bd
--- /dev/null
@@ -0,0 +1,176 @@
+package org.cacert.gigi.database;
+
+import java.sql.Date;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Time;
+import java.sql.Timestamp;
+
+public class GigiResultSet {
+
+    ResultSet target;
+
+    public GigiResultSet(ResultSet target) {
+        this.target = target;
+    }
+
+    public String getString(int columnIndex) {
+        try {
+            return target.getString(columnIndex);
+        } catch (SQLException e) {
+            handleSQL(e);
+            throw new Error(e);
+        }
+    }
+
+    public boolean getBoolean(int columnIndex) {
+        try {
+            return target.getBoolean(columnIndex);
+        } catch (SQLException e) {
+            handleSQL(e);
+            throw new Error(e);
+        }
+    }
+
+    public int getInt(int columnIndex) {
+        try {
+            return target.getInt(columnIndex);
+        } catch (SQLException e) {
+            handleSQL(e);
+            throw new Error(e);
+        }
+    }
+
+    public Date getDate(int columnIndex) {
+        try {
+            return target.getDate(columnIndex);
+        } catch (SQLException e) {
+            handleSQL(e);
+            throw new Error(e);
+        }
+    }
+
+    public Timestamp getTimestamp(int columnIndex) {
+        try {
+            return target.getTimestamp(columnIndex);
+        } catch (SQLException e) {
+            handleSQL(e);
+            throw new Error(e);
+        }
+    }
+
+    public Time getTime(int columnIndex) {
+        try {
+            return target.getTime(columnIndex);
+        } catch (SQLException e) {
+            handleSQL(e);
+            throw new Error(e);
+        }
+    }
+
+    public String getString(String columnLabel) {
+        try {
+            return target.getString(columnLabel);
+        } catch (SQLException e) {
+            handleSQL(e);
+            throw new Error(e);
+        }
+    }
+
+    public boolean getBoolean(String columnLabel) {
+        try {
+            return target.getBoolean(columnLabel);
+        } catch (SQLException e) {
+            handleSQL(e);
+            throw new Error(e);
+        }
+    }
+
+    public int getInt(String columnLabel) {
+        try {
+            return target.getInt(columnLabel);
+        } catch (SQLException e) {
+            handleSQL(e);
+            throw new Error(e);
+        }
+    }
+
+    public Date getDate(String columnLabel) {
+        try {
+            return target.getDate(columnLabel);
+        } catch (SQLException e) {
+            handleSQL(e);
+            throw new Error(e);
+        }
+    }
+
+    public Timestamp getTimestamp(String columnLabel) {
+        try {
+            return target.getTimestamp(columnLabel);
+        } catch (SQLException e) {
+            handleSQL(e);
+            throw new Error(e);
+        }
+    }
+
+    public Time getTime(String columnLabel) {
+        try {
+            return target.getTime(columnLabel);
+        } catch (SQLException e) {
+            handleSQL(e);
+            throw new Error(e);
+        }
+    }
+
+    public boolean next() {
+        try {
+            return target.next();
+        } catch (SQLException e) {
+            handleSQL(e);
+            throw new Error(e);
+        }
+    }
+
+    public int getRow() {
+        try {
+            return target.getRow();
+        } catch (SQLException e) {
+            handleSQL(e);
+            throw new Error(e);
+        }
+    }
+
+    public void beforeFirst() {
+        try {
+            target.beforeFirst();
+        } catch (SQLException e) {
+            handleSQL(e);
+            throw new Error(e);
+        }
+    }
+
+    public void last() {
+        try {
+            target.last();
+        } catch (SQLException e) {
+            handleSQL(e);
+            throw new Error(e);
+        }
+    }
+
+    public void close() {
+        try {
+            target.close();
+        } catch (SQLException e) {
+            handleSQL(e);
+            throw new Error(e);
+        }
+
+    }
+
+    private void handleSQL(SQLException e) {
+        // TODO Auto-generated method stub
+
+    }
+
+}
index b1c9ccd507c79fc4981ea7d364ff8280e67a7f79..d4ebe333089cbcdc2ca86774b75bd20802535574 100644 (file)
@@ -1,10 +1,9 @@
 package org.cacert.gigi.dbObjects;
 
-import java.sql.ResultSet;
-import java.sql.SQLException;
-
+import org.cacert.gigi.database.GigiResultSet;
 
 public class Assurance {
+
     private int id;
 
     private User from;
@@ -19,15 +18,15 @@ public class Assurance {
 
     private String date;
 
-    public Assurance(ResultSet result) throws SQLException {
+    public Assurance(GigiResultSet res) {
         super();
-        this.id = result.getInt("id");
-        this.from = User.getById(result.getInt("from"));
-        this.to = User.getById(result.getInt("to"));
-        this.location = result.getString("location");
-        this.method = result.getString("method");
-        this.points = result.getInt("points");
-        this.date = result.getString("date");
+        this.id = res.getInt("id");
+        this.from = User.getById(res.getInt("from"));
+        this.to = User.getById(res.getInt("to"));
+        this.location = res.getString("location");
+        this.method = res.getString("method");
+        this.points = res.getInt("points");
+        this.date = res.getString("date");
     }
 
     public User getFrom() {
index 59d0aded9b329c9396c5690d5690c5d84f1b3f3e..756a70fdff3c592a0d23413aafb021220b8fbdfe 100644 (file)
@@ -9,9 +9,6 @@ import java.security.GeneralSecurityException;
 import java.security.cert.CertificateFactory;
 import java.security.cert.X509Certificate;
 import java.sql.Date;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.LinkedList;
@@ -19,6 +16,8 @@ import java.util.List;
 
 import org.cacert.gigi.GigiApiException;
 import org.cacert.gigi.database.DatabaseConnection;
+import org.cacert.gigi.database.GigiPreparedStatement;
+import org.cacert.gigi.database.GigiResultSet;
 import org.cacert.gigi.util.Job;
 import org.cacert.gigi.util.KeyStorage;
 import org.cacert.gigi.util.Notary;
@@ -140,35 +139,31 @@ public class Certificate {
     }
 
     private Certificate(String serial) {
-        try {
-            PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id,subject, md, csr_name, crt_name,memid, profile FROM `certs` WHERE serial=?");
-            ps.setString(1, serial);
-            ResultSet rs = ps.executeQuery();
-            if ( !rs.next()) {
-                throw new IllegalArgumentException("Invalid mid " + serial);
-            }
-            this.id = rs.getInt(1);
-            dn = rs.getString(2);
-            md = rs.getString(3);
-            csrName = rs.getString(4);
-            crtName = rs.getString(5);
-            ownerId = rs.getInt(6);
-            profile = CertificateProfile.getById(rs.getInt(7));
-            this.serial = serial;
-
-            PreparedStatement ps2 = DatabaseConnection.getInstance().prepare("SELECT contents, type FROM `subjectAlternativeNames` WHERE certId=?");
-            ps2.setInt(1, id);
-            ResultSet rs2 = ps2.executeQuery();
-            sans = new LinkedList<>();
-            while (rs2.next()) {
-                sans.add(new SubjectAlternateName(SANType.valueOf(rs2.getString("type").toUpperCase()), rs2.getString("contents")));
-            }
-            rs2.close();
-
-            rs.close();
-        } catch (SQLException e) {
-            e.printStackTrace();
+        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id,subject, md, csr_name, crt_name,memid, profile FROM `certs` WHERE serial=?");
+        ps.setString(1, serial);
+        GigiResultSet rs = ps.executeQuery();
+        if ( !rs.next()) {
+            throw new IllegalArgumentException("Invalid mid " + serial);
+        }
+        this.id = rs.getInt(1);
+        dn = rs.getString(2);
+        md = rs.getString(3);
+        csrName = rs.getString(4);
+        crtName = rs.getString(5);
+        ownerId = rs.getInt(6);
+        profile = CertificateProfile.getById(rs.getInt(7));
+        this.serial = serial;
+
+        GigiPreparedStatement ps2 = DatabaseConnection.getInstance().prepare("SELECT contents, type FROM `subjectAlternativeNames` WHERE certId=?");
+        ps2.setInt(1, id);
+        GigiResultSet rs2 = ps2.executeQuery();
+        sans = new LinkedList<>();
+        while (rs2.next()) {
+            sans.add(new SubjectAlternateName(SANType.valueOf(rs2.getString("type").toUpperCase()), rs2.getString("contents")));
         }
+        rs2.close();
+
+        rs.close();
     }
 
     public enum CertificateStatus {
@@ -198,13 +193,13 @@ public class Certificate {
 
     }
 
-    public CertificateStatus getStatus() throws SQLException {
+    public CertificateStatus getStatus() {
         if (id == 0) {
             return CertificateStatus.DRAFT;
         }
-        PreparedStatement searcher = DatabaseConnection.getInstance().prepare("SELECT crt_name, created, revoked, serial FROM certs WHERE id=?");
+        GigiPreparedStatement searcher = DatabaseConnection.getInstance().prepare("SELECT crt_name, created, revoked, serial FROM certs WHERE id=?");
         searcher.setInt(1, id);
-        ResultSet rs = searcher.executeQuery();
+        GigiResultSet rs = searcher.executeQuery();
         if ( !rs.next()) {
             throw new IllegalStateException("Certificate not in Database");
         }
@@ -231,25 +226,23 @@ public class Certificate {
      * @return A job which can be used to monitor the progress of this task.
      * @throws IOException
      *             for problems with writing the CSR/SPKAC
-     * @throws SQLException
-     *             for problems with writing to the DB
      * @throws GigiApiException
      *             if the period is bogus
      */
-    public Job issue(Date start, String period) throws IOException, SQLException, GigiApiException {
+    public Job issue(Date start, String period) throws IOException, GigiApiException {
         if (getStatus() != CertificateStatus.DRAFT) {
             throw new IllegalStateException();
         }
         Notary.writeUserAgreement(ownerId, "CCA", "issue certificate", "", true, 0);
 
-        PreparedStatement inserter = DatabaseConnection.getInstance().prepare("INSERT INTO certs SET md=?, subject=?, csr_type=?, crt_name='', memid=?, profile=?");
+        GigiPreparedStatement inserter = DatabaseConnection.getInstance().prepare("INSERT INTO certs SET md=?, subject=?, csr_type=?, crt_name='', memid=?, profile=?");
         inserter.setString(1, md);
         inserter.setString(2, dn);
         inserter.setString(3, csrType.toString());
         inserter.setInt(4, ownerId);
         inserter.setInt(5, profile.getId());
         inserter.execute();
-        id = DatabaseConnection.lastInsertId(inserter);
+        id = inserter.lastInsertId();
         File csrFile = KeyStorage.locateCsr(id);
         csrName = csrFile.getPath();
         FileOutputStream fos = new FileOutputStream(csrFile);
@@ -257,7 +250,7 @@ public class Certificate {
         fos.close();
 
         // TODO draft to insert SANs
-        PreparedStatement san = DatabaseConnection.getInstance().prepare("INSERT INTO subjectAlternativeNames SET certId=?, contents=?, type=?");
+        GigiPreparedStatement san = DatabaseConnection.getInstance().prepare("INSERT INTO subjectAlternativeNames SET certId=?, contents=?, type=?");
         for (SubjectAlternateName subjectAlternateName : sans) {
             san.setInt(1, id);
             san.setString(2, subjectAlternateName.getName());
@@ -265,7 +258,7 @@ public class Certificate {
             san.execute();
         }
 
-        PreparedStatement updater = DatabaseConnection.getInstance().prepare("UPDATE certs SET csr_name=? WHERE id=?");
+        GigiPreparedStatement updater = DatabaseConnection.getInstance().prepare("UPDATE certs SET csr_name=? WHERE id=?");
         updater.setString(1, csrName);
         updater.setInt(2, id);
         updater.execute();
@@ -273,7 +266,7 @@ public class Certificate {
 
     }
 
-    public Job revoke() throws SQLException {
+    public Job revoke() {
         if (getStatus() != CertificateStatus.ISSUED) {
             throw new IllegalStateException();
         }
@@ -281,7 +274,7 @@ public class Certificate {
 
     }
 
-    public X509Certificate cert() throws IOException, GeneralSecurityException, SQLException {
+    public X509Certificate cert() throws IOException, GeneralSecurityException {
         CertificateStatus status = getStatus();
         if (status != CertificateStatus.ISSUED) {
             throw new IllegalStateException(status + " is not wanted here.");
@@ -309,11 +302,8 @@ public class Certificate {
     }
 
     public String getSerial() {
-        try {
-            getStatus();
-        } catch (SQLException e) {
-            e.printStackTrace();
-        } // poll changes
+        getStatus();
+        // poll changes
         return serial;
     }
 
index e771cd12750bcfcfd87ad46f521403c23cce2321..26634a664dcde4e71b62cf792223e215d5498e2f 100644 (file)
@@ -1,11 +1,10 @@
 package org.cacert.gigi.dbObjects;
 
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
 import java.util.HashMap;
 
 import org.cacert.gigi.database.DatabaseConnection;
+import org.cacert.gigi.database.GigiPreparedStatement;
+import org.cacert.gigi.database.GigiResultSet;
 
 public class CertificateProfile {
 
@@ -38,16 +37,12 @@ public class CertificateProfile {
     }
 
     static {
-        try {
-            PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id, keyname, name FROM `profiles`");
-            ResultSet rs = ps.executeQuery();
-            while (rs.next()) {
-                CertificateProfile cp = new CertificateProfile(rs.getInt("id"), rs.getString("keyName"), rs.getString("name"));
-                byId.put(cp.getId(), cp);
-                byName.put(cp.getKeyName(), cp);
-            }
-        } catch (SQLException e) {
-            e.printStackTrace();
+        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id, keyname, name FROM `profiles`");
+        GigiResultSet rs = ps.executeQuery();
+        while (rs.next()) {
+            CertificateProfile cp = new CertificateProfile(rs.getInt("id"), rs.getString("keyName"), rs.getString("name"));
+            byId.put(cp.getId(), cp);
+            byName.put(cp.getKeyName(), cp);
         }
 
     }
index 8d70c56cce82a7c7a065b179bd58392ef28eafac..13c8436cdd8fd63919d89a947b6b39cda65fb896 100644 (file)
@@ -1,14 +1,13 @@
 package org.cacert.gigi.dbObjects;
 
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
 import java.util.Collections;
 import java.util.LinkedList;
 import java.util.List;
 
 import org.cacert.gigi.GigiApiException;
 import org.cacert.gigi.database.DatabaseConnection;
+import org.cacert.gigi.database.GigiPreparedStatement;
+import org.cacert.gigi.database.GigiResultSet;
 import org.cacert.gigi.dbObjects.DomainPingConfiguration.PingType;
 
 public class Domain implements IdCachable {
@@ -25,7 +24,7 @@ public class Domain implements IdCachable {
 
         private DomainPingConfiguration config;
 
-        public DomainPingExecution(ResultSet rs) throws SQLException {
+        public DomainPingExecution(GigiResultSet rs) {
             state = rs.getString(1);
             type = rs.getString(2);
             info = rs.getString(3);
@@ -61,11 +60,11 @@ public class Domain implements IdCachable {
 
     private int id;
 
-    private Domain(int id) throws SQLException {
-        PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT memid, domain FROM `domains` WHERE id=? AND deleted IS NULL");
+    private Domain(int id) {
+        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT memid, domain FROM `domains` WHERE id=? AND deleted IS NULL");
         ps.setInt(1, id);
 
-        ResultSet rs = ps.executeQuery();
+        GigiResultSet rs = ps.executeQuery();
         if ( !rs.next()) {
             throw new IllegalArgumentException("Invalid domain id " + id);
         }
@@ -82,19 +81,15 @@ public class Domain implements IdCachable {
     }
 
     private static void checkInsert(String suffix) throws GigiApiException {
-        try {
-            PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `domains` WHERE (domain=RIGHT(?,LENGTH(domain))  OR RIGHT(domain,LENGTH(?))=?) AND deleted IS NULL");
-            ps.setString(1, suffix);
-            ps.setString(2, suffix);
-            ps.setString(3, suffix);
-            ResultSet rs = ps.executeQuery();
-            boolean existed = rs.next();
-            rs.close();
-            if (existed) {
-                throw new GigiApiException("Domain could not be inserted. Domain is already valid.");
-            }
-        } catch (SQLException e) {
-            throw new GigiApiException(e);
+        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `domains` WHERE (domain=RIGHT(?,LENGTH(domain))  OR RIGHT(domain,LENGTH(?))=?) AND deleted IS NULL");
+        ps.setString(1, suffix);
+        ps.setString(2, suffix);
+        ps.setString(3, suffix);
+        GigiResultSet rs = ps.executeQuery();
+        boolean existed = rs.next();
+        rs.close();
+        if (existed) {
+            throw new GigiApiException("Domain could not be inserted. Domain is already valid.");
         }
     }
 
@@ -104,16 +99,12 @@ public class Domain implements IdCachable {
         }
         synchronized (Domain.class) {
             checkInsert(suffix);
-            try {
-                PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `domains` SET memid=?, domain=?");
-                ps.setInt(1, owner.getId());
-                ps.setString(2, suffix);
-                ps.execute();
-                id = DatabaseConnection.lastInsertId(ps);
-                myCache.put(this);
-            } catch (SQLException e) {
-                throw new GigiApiException(e);
-            }
+            GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `domains` SET memid=?, domain=?");
+            ps.setInt(1, owner.getId());
+            ps.setString(2, suffix);
+            ps.execute();
+            id = ps.lastInsertId();
+            myCache.put(this);
         }
     }
 
@@ -121,13 +112,9 @@ public class Domain implements IdCachable {
         if (id == 0) {
             throw new GigiApiException("not inserted.");
         }
-        try {
-            PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE `domains` SET deleted=CURRENT_TIMESTAMP WHERE id=?");
-            ps.setInt(1, id);
-            ps.execute();
-        } catch (SQLException e) {
-            throw new GigiApiException(e);
-        }
+        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE `domains` SET deleted=CURRENT_TIMESTAMP WHERE id=?");
+        ps.setInt(1, id);
+        ps.execute();
     }
 
     public User getOwner() {
@@ -148,75 +135,54 @@ public class Domain implements IdCachable {
     public List<DomainPingConfiguration> getConfiguredPings() throws GigiApiException {
         LinkedList<DomainPingConfiguration> configs = this.configs;
         if (configs == null) {
-            try {
-                configs = new LinkedList<>();
-                PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM pingconfig WHERE domainid=?");
-                ps.setInt(1, id);
-                ResultSet rs = ps.executeQuery();
-                while (rs.next()) {
-                    configs.add(DomainPingConfiguration.getById(rs.getInt(1)));
-                }
-                rs.close();
-                this.configs = configs;
-            } catch (SQLException e) {
-                throw new GigiApiException(e);
+            configs = new LinkedList<>();
+            GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM pingconfig WHERE domainid=?");
+            ps.setInt(1, id);
+            GigiResultSet rs = ps.executeQuery();
+            while (rs.next()) {
+                configs.add(DomainPingConfiguration.getById(rs.getInt(1)));
             }
+            rs.close();
+            this.configs = configs;
 
         }
         return Collections.unmodifiableList(configs);
     }
 
     public void addPing(PingType 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.toString().toLowerCase());
-            ps.setString(3, config);
-            ps.execute();
-            configs = null;
-        } catch (SQLException e) {
-            throw new GigiApiException(e);
-        }
+        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO pingconfig SET domainid=?, type=?, info=?");
+        ps.setInt(1, id);
+        ps.setString(2, type.toString().toLowerCase());
+        ps.setString(3, config);
+        ps.execute();
+        configs = null;
     }
 
     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);
-        }
+        GigiPreparedStatement 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();
     }
 
     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;
+        GigiPreparedStatement 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);
+        GigiResultSet rs = ps.executeQuery();
+        return rs.next();
     }
 
     public DomainPingExecution[] getPings() throws GigiApiException {
-        try {
-            PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT state, type, info, result, configId 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();
-            DomainPingExecution[] contents = new DomainPingExecution[rs.getRow()];
-            rs.beforeFirst();
-            for (int i = 0; i < contents.length && rs.next(); i++) {
-                contents[i] = new DomainPingExecution(rs);
-            }
-            return contents;
-        } catch (SQLException e) {
-            throw new GigiApiException(e);
+        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT state, type, info, result, configId FROM domainPinglog INNER JOIN pingconfig ON pingconfig.id=domainPinglog.configid WHERE pingconfig.domainid=? ORDER BY `when` DESC;");
+        ps.setInt(1, id);
+        GigiResultSet rs = ps.executeQuery();
+        rs.last();
+        DomainPingExecution[] contents = new DomainPingExecution[rs.getRow()];
+        rs.beforeFirst();
+        for (int i = 0; i < contents.length && rs.next(); i++) {
+            contents[i] = new DomainPingExecution(rs);
         }
+        return contents;
 
     }
 
@@ -225,11 +191,7 @@ public class Domain implements IdCachable {
     public static synchronized Domain getById(int id) throws IllegalArgumentException {
         Domain em = myCache.get(id);
         if (em == null) {
-            try {
-                myCache.put(em = new Domain(id));
-            } catch (SQLException e1) {
-                throw new IllegalArgumentException(e1);
-            }
+            myCache.put(em = new Domain(id));
         }
         return em;
     }
index f5dd2a2c03cfda86ecca5be7b17aafd2e196fa71..1146cca0c5c8b0b451c1f75c334cff0f71e87a91 100644 (file)
@@ -1,10 +1,8 @@
 package org.cacert.gigi.dbObjects;
 
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-
 import org.cacert.gigi.database.DatabaseConnection;
+import org.cacert.gigi.database.GigiPreparedStatement;
+import org.cacert.gigi.database.GigiResultSet;
 
 public class DomainPingConfiguration implements IdCachable {
 
@@ -20,11 +18,11 @@ public class DomainPingConfiguration implements IdCachable {
 
     private String info;
 
-    private DomainPingConfiguration(int id) throws SQLException {
-        PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id, domainid, type, info FROM pingconfig WHERE id=?");
+    private DomainPingConfiguration(int id) {
+        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id, domainid, type, info FROM pingconfig WHERE id=?");
         ps.setInt(1, id);
 
-        ResultSet rs = ps.executeQuery();
+        GigiResultSet rs = ps.executeQuery();
         if ( !rs.next()) {
             throw new IllegalArgumentException("Invalid pingconfig id " + id);
         }
@@ -56,23 +54,15 @@ public class DomainPingConfiguration implements IdCachable {
     public static synchronized DomainPingConfiguration getById(int id) {
         DomainPingConfiguration res = cache.get(id);
         if (res == null) {
-            try {
-                cache.put(res = new DomainPingConfiguration(id));
-            } catch (SQLException e) {
-                throw new IllegalArgumentException(e);
-            }
+            cache.put(res = new DomainPingConfiguration(id));
         }
         return res;
     }
 
     public void requestReping() {
-        try {
-            PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE pingconfig set reping='y' WHERE id=?");
-            ps.setInt(1, id);
-            ps.execute();
-        } catch (SQLException e) {
-            e.printStackTrace();
-        }
+        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE pingconfig set reping='y' WHERE id=?");
+        ps.setInt(1, id);
+        ps.execute();
     }
 
 }
index 1191952f7e236ee93c1ab09fe8facfd3f8d5bfe5..f4678fbf4ef894e9aab8b773c68555195b980214 100644 (file)
@@ -1,12 +1,10 @@
 package org.cacert.gigi.dbObjects;
 
 import java.io.IOException;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-
 import org.cacert.gigi.GigiApiException;
 import org.cacert.gigi.database.DatabaseConnection;
+import org.cacert.gigi.database.GigiPreparedStatement;
+import org.cacert.gigi.database.GigiResultSet;
 import org.cacert.gigi.email.EmailProvider;
 import org.cacert.gigi.email.MailProbe;
 import org.cacert.gigi.localisation.Language;
@@ -22,11 +20,11 @@ public class EmailAddress implements IdCachable {
 
     private String hash = null;
 
-    private EmailAddress(int id) throws SQLException {
-        PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT memid, email, hash FROM `emails` WHERE id=? AND deleted=0");
+    private EmailAddress(int id) {
+        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT memid, email, hash FROM `emails` WHERE id=? AND deleted=0");
         ps.setInt(1, id);
 
-        ResultSet rs = ps.executeQuery();
+        GigiResultSet rs = ps.executeQuery();
         if ( !rs.next()) {
             throw new IllegalArgumentException("Invalid email id " + id);
         }
@@ -51,18 +49,16 @@ public class EmailAddress implements IdCachable {
             throw new IllegalStateException("already inserted.");
         }
         try {
-            PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `emails` SET memid=?, hash=?, email=?");
+            GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `emails` SET memid=?, hash=?, email=?");
             ps.setInt(1, owner.getId());
             ps.setString(2, hash);
             ps.setString(3, address);
             synchronized (EmailAddress.class) {
                 ps.execute();
-                id = DatabaseConnection.lastInsertId(ps);
+                id = ps.lastInsertId();
                 myCache.put(this);
             }
             MailProbe.sendMailProbe(l, "email", id, hash, address);
-        } catch (SQLException e) {
-            e.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
         }
@@ -78,22 +74,17 @@ public class EmailAddress implements IdCachable {
 
     public synchronized void verify(String hash) throws GigiApiException {
         if (this.hash.equals(hash)) {
-
-            try {
-                PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE `emails` SET hash='' WHERE id=?");
-                ps.setInt(1, id);
-                ps.execute();
-                hash = "";
-
-                // Verify user with that primary email
-                PreparedStatement ps2 = DatabaseConnection.getInstance().prepare("update `users` set `verified`='1' where `id`=? and `email`=? and `verified`='0'");
-                ps2.setInt(1, owner.getId());
-                ps2.setString(2, address);
-                ps2.execute();
-                this.hash = "";
-            } catch (SQLException e) {
-                throw new GigiApiException(e);
-            }
+            GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE `emails` SET hash='' WHERE id=?");
+            ps.setInt(1, id);
+            ps.execute();
+            hash = "";
+
+            // Verify user with that primary email
+            GigiPreparedStatement ps2 = DatabaseConnection.getInstance().prepare("update `users` set `verified`='1' where `id`=? and `email`=? and `verified`='0'");
+            ps2.setInt(1, owner.getId());
+            ps2.setString(2, address);
+            ps2.execute();
+            this.hash = "";
 
         } else {
             throw new GigiApiException("Email verification hash is invalid.");
@@ -109,11 +100,7 @@ public class EmailAddress implements IdCachable {
     public static synchronized EmailAddress getById(int id) throws IllegalArgumentException {
         EmailAddress em = myCache.get(id);
         if (em == null) {
-            try {
-                myCache.put(em = new EmailAddress(id));
-            } catch (SQLException e1) {
-                throw new IllegalArgumentException(e1);
-            }
+            myCache.put(em = new EmailAddress(id));
         }
         return em;
     }
index c07db48cb298fd5b7cf5da2470da3dcf6e494386..6de799aeacb4e7a5461e1482b112ebf3bff59643 100644 (file)
@@ -1,9 +1,6 @@
 package org.cacert.gigi.dbObjects;
 
 import java.sql.Date;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
 import java.util.Calendar;
 import java.util.Collections;
 import java.util.HashSet;
@@ -12,6 +9,8 @@ import java.util.Set;
 
 import org.cacert.gigi.GigiApiException;
 import org.cacert.gigi.database.DatabaseConnection;
+import org.cacert.gigi.database.GigiPreparedStatement;
+import org.cacert.gigi.database.GigiResultSet;
 import org.cacert.gigi.localisation.Language;
 import org.cacert.gigi.util.Notary;
 import org.cacert.gigi.util.PasswordHash;
@@ -39,32 +38,28 @@ public class User implements IdCachable {
     }
 
     private void updateName(int id) {
-        try {
-            PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `fname`, `lname`,`mname`, `suffix`, `dob`, `email`, `language` FROM `users` WHERE id=?");
-            ps.setInt(1, id);
-            ResultSet rs = ps.executeQuery();
-            if (rs.next()) {
-                name = new Name(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4));
-                dob = rs.getDate(5);
-                email = rs.getString(6);
-                String localeStr = rs.getString(7);
-                if (localeStr == null || localeStr.equals("")) {
-                    locale = Locale.getDefault();
-                } else {
-                    locale = Language.getLocaleFromString(localeStr);
-                }
-            }
-            rs.close();
-            PreparedStatement psg = DatabaseConnection.getInstance().prepare("SELECT permission FROM user_groups WHERE user=? AND deleted is NULL");
-            psg.setInt(1, id);
-            ResultSet rs2 = psg.executeQuery();
-            while (rs2.next()) {
-                groups.add(Group.getByString(rs2.getString(1)));
+        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `fname`, `lname`,`mname`, `suffix`, `dob`, `email`, `language` FROM `users` WHERE id=?");
+        ps.setInt(1, id);
+        GigiResultSet rs = ps.executeQuery();
+        if (rs.next()) {
+            name = new Name(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4));
+            dob = rs.getDate(5);
+            email = rs.getString(6);
+            String localeStr = rs.getString(7);
+            if (localeStr == null || localeStr.equals("")) {
+                locale = Locale.getDefault();
+            } else {
+                locale = Language.getLocaleFromString(localeStr);
             }
-            rs2.close();
-        } catch (SQLException e) {
-            e.printStackTrace();
         }
+        rs.close();
+        GigiPreparedStatement psg = DatabaseConnection.getInstance().prepare("SELECT permission FROM user_groups WHERE user=? AND deleted is NULL");
+        psg.setInt(1, id);
+        GigiResultSet rs2 = psg.executeQuery();
+        while (rs2.next()) {
+            groups.add(Group.getByString(rs2.getString(1)));
+        }
+        rs2.close();
     }
 
     public User() {}
@@ -129,11 +124,11 @@ public class User implements IdCachable {
         this.name.lname = lname;
     }
 
-    public void insert(String password) throws SQLException {
+    public void insert(String password) {
         if (id != 0) {
             throw new Error("refusing to insert");
         }
-        PreparedStatement query = DatabaseConnection.getInstance().prepare("insert into `users` set `email`=?, `password`=?, " + "`fname`=?, `mname`=?, `lname`=?, " + "`suffix`=?, `dob`=?, `created`=NOW(), locked=0, `language`=?");
+        GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("insert into `users` set `email`=?, `password`=?, " + "`fname`=?, `mname`=?, `lname`=?, " + "`suffix`=?, `dob`=?, `created`=NOW(), locked=0, `language`=?");
         query.setString(1, email);
         query.setString(2, PasswordHash.hash(password));
         query.setString(3, name.fname);
@@ -144,36 +139,32 @@ public class User implements IdCachable {
         query.setString(8, locale.toString());
         synchronized (User.class) {
             query.execute();
-            id = DatabaseConnection.lastInsertId(query);
+            id = query.lastInsertId();
             myCache.put(this);
         }
     }
 
     public void changePassword(String oldPass, String newPass) throws GigiApiException {
-        try {
-            PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `password` FROM users WHERE id=?");
-            ps.setInt(1, id);
-            ResultSet rs = ps.executeQuery();
-            if ( !rs.next()) {
-                throw new GigiApiException("User not found... very bad.");
-            }
-            if ( !PasswordHash.verifyHash(oldPass, rs.getString(1))) {
-                throw new GigiApiException("Old password does not match.");
-            }
-            rs.close();
-            PasswordStrengthChecker.assertStrongPassword(newPass, this);
-            ps = DatabaseConnection.getInstance().prepare("UPDATE users SET `password`=? WHERE id=?");
-            ps.setString(1, PasswordHash.hash(newPass));
-            ps.setInt(2, id);
-            if (ps.executeUpdate() != 1) {
-                throw new GigiApiException("Password update failed.");
-            }
-        } catch (SQLException e) {
-            throw new GigiApiException(e);
+        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `password` FROM users WHERE id=?");
+        ps.setInt(1, id);
+        GigiResultSet rs = ps.executeQuery();
+        if ( !rs.next()) {
+            throw new GigiApiException("User not found... very bad.");
+        }
+        if ( !PasswordHash.verifyHash(oldPass, rs.getString(1))) {
+            throw new GigiApiException("Old password does not match.");
+        }
+        rs.close();
+        PasswordStrengthChecker.assertStrongPassword(newPass, this);
+        ps = DatabaseConnection.getInstance().prepare("UPDATE users SET `password`=? WHERE id=?");
+        ps.setString(1, PasswordHash.hash(newPass));
+        ps.setInt(2, id);
+        if (ps.executeUpdate() != 1) {
+            throw new GigiApiException("Password update failed.");
         }
     }
 
-    public boolean canAssure() throws SQLException {
+    public boolean canAssure() {
         if ( !isOfAge(14)) { // PoJAM
             return false;
         }
@@ -185,10 +176,10 @@ public class User implements IdCachable {
 
     }
 
-    public boolean hasPassedCATS() throws SQLException {
-        PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `cats_passed` where `user_id`=?");
+    public boolean hasPassedCATS() {
+        GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `cats_passed` where `user_id`=?");
         query.setInt(1, id);
-        ResultSet rs = query.executeQuery();
+        GigiResultSet rs = query.executeQuery();
         if (rs.next()) {
             return true;
         } else {
@@ -196,10 +187,10 @@ public class User implements IdCachable {
         }
     }
 
-    public int getAssurancePoints() throws SQLException {
-        PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT sum(points) FROM `notary` where `to`=? AND `deleted`=0");
+    public int getAssurancePoints() {
+        GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT sum(points) FROM `notary` where `to`=? AND `deleted`=0");
         query.setInt(1, id);
-        ResultSet rs = query.executeQuery();
+        GigiResultSet rs = query.executeQuery();
         int points = 0;
         if (rs.next()) {
             points = rs.getInt(1);
@@ -208,10 +199,10 @@ public class User implements IdCachable {
         return points;
     }
 
-    public int getExperiencePoints() throws SQLException {
-        PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT count(*) FROM `notary` where `from`=? AND `deleted`=0");
+    public int getExperiencePoints() {
+        GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT count(*) FROM `notary` where `from`=? AND `deleted`=0");
         query.setInt(1, id);
-        ResultSet rs = query.executeQuery();
+        GigiResultSet rs = query.executeQuery();
         int points = 0;
         if (rs.next()) {
             points = rs.getInt(1) * 2;
@@ -238,10 +229,9 @@ public class User implements IdCachable {
      * Gets the maximum allowed points NOW. Note that an assurance needs to
      * re-check PoJam as it has taken place in the past.
      * 
-     * @return the maximal points
-     * @throws SQLException
+     * @return the maximal points @
      */
-    public int getMaxAssurePoints() throws SQLException {
+    public int getMaxAssurePoints() {
         if ( !isOfAge(18)) {
             return 10; // PoJAM
         }
@@ -279,75 +269,60 @@ public class User implements IdCachable {
     }
 
     public EmailAddress[] getEmails() {
-        try {
-            PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM emails WHERE memid=? AND deleted=0");
-            ps.setInt(1, id);
-            ResultSet rs = ps.executeQuery();
-            rs.last();
-            int count = rs.getRow();
-            EmailAddress[] data = new EmailAddress[count];
-            rs.beforeFirst();
-            for (int i = 0; i < data.length; i++) {
-                if ( !rs.next()) {
-                    throw new Error("Internal sql api violation.");
-                }
-                data[i] = EmailAddress.getById(rs.getInt(1));
+        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM emails WHERE memid=? AND deleted=0");
+        ps.setInt(1, id);
+        GigiResultSet rs = ps.executeQuery();
+        rs.last();
+        int count = rs.getRow();
+        EmailAddress[] data = new EmailAddress[count];
+        rs.beforeFirst();
+        for (int i = 0; i < data.length; i++) {
+            if ( !rs.next()) {
+                throw new Error("Internal sql api violation.");
             }
-            rs.close();
-            return data;
-        } catch (SQLException e) {
-            e.printStackTrace();
+            data[i] = EmailAddress.getById(rs.getInt(1));
         }
+        rs.close();
+        return data;
 
-        return null;
     }
 
     public Domain[] getDomains() {
-        try {
-            PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM domains WHERE memid=? AND deleted IS NULL");
-            ps.setInt(1, id);
-            ResultSet rs = ps.executeQuery();
-            rs.last();
-            int count = rs.getRow();
-            Domain[] data = new Domain[count];
-            rs.beforeFirst();
-            for (int i = 0; i < data.length; i++) {
-                if ( !rs.next()) {
-                    throw new Error("Internal sql api violation.");
-                }
-                data[i] = Domain.getById(rs.getInt(1));
+        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM domains WHERE memid=? AND deleted IS NULL");
+        ps.setInt(1, id);
+        GigiResultSet rs = ps.executeQuery();
+        rs.last();
+        int count = rs.getRow();
+        Domain[] data = new Domain[count];
+        rs.beforeFirst();
+        for (int i = 0; i < data.length; i++) {
+            if ( !rs.next()) {
+                throw new Error("Internal sql api violation.");
             }
-            rs.close();
-            return data;
-        } catch (SQLException e) {
-            e.printStackTrace();
+            data[i] = Domain.getById(rs.getInt(1));
         }
+        rs.close();
+        return data;
 
-        return null;
     }
 
     public Certificate[] getCertificates() {
-        try {
-            PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT serial FROM certs WHERE memid=? AND revoked=0");
-            ps.setInt(1, id);
-            ResultSet rs = ps.executeQuery();
-            rs.last();
-            int count = rs.getRow();
-            Certificate[] data = new Certificate[count];
-            rs.beforeFirst();
-            for (int i = 0; i < data.length; i++) {
-                if ( !rs.next()) {
-                    throw new Error("Internal sql api violation.");
-                }
-                data[i] = Certificate.getBySerial(rs.getString(1));
+        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT serial FROM certs WHERE memid=? AND revoked=0");
+        ps.setInt(1, id);
+        GigiResultSet rs = ps.executeQuery();
+        rs.last();
+        int count = rs.getRow();
+        Certificate[] data = new Certificate[count];
+        rs.beforeFirst();
+        for (int i = 0; i < data.length; i++) {
+            if ( !rs.next()) {
+                throw new Error("Internal sql api violation.");
             }
-            rs.close();
-            return data;
-        } catch (SQLException e) {
-            e.printStackTrace();
+            data[i] = Certificate.getBySerial(rs.getString(1));
         }
+        rs.close();
+        return data;
 
-        return null;
     }
 
     public boolean isValidDomain(String domainname) {
@@ -374,25 +349,21 @@ public class User implements IdCachable {
     }
 
     public void updateDefaultEmail(EmailAddress newMail) throws GigiApiException {
-        try {
-            EmailAddress[] adrs = getEmails();
-            for (int i = 0; i < adrs.length; i++) {
-                if (adrs[i].getAddress().equals(newMail.getAddress())) {
-                    if ( !adrs[i].isVerified()) {
-                        throw new GigiApiException("Email not verified.");
-                    }
-                    PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE users SET email=? WHERE id=?");
-                    ps.setString(1, newMail.getAddress());
-                    ps.setInt(2, getId());
-                    ps.execute();
-                    email = newMail.getAddress();
-                    return;
+        EmailAddress[] adrs = getEmails();
+        for (int i = 0; i < adrs.length; i++) {
+            if (adrs[i].getAddress().equals(newMail.getAddress())) {
+                if ( !adrs[i].isVerified()) {
+                    throw new GigiApiException("Email not verified.");
                 }
+                GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE users SET email=? WHERE id=?");
+                ps.setString(1, newMail.getAddress());
+                ps.setInt(2, getId());
+                ps.execute();
+                email = newMail.getAddress();
+                return;
             }
-            throw new GigiApiException("Given address not an address of the user.");
-        } catch (SQLException e) {
-            throw new GigiApiException(e);
         }
+        throw new GigiApiException("Given address not an address of the user.");
     }
 
     public void deleteEmail(EmailAddress mail) throws GigiApiException {
@@ -402,26 +373,21 @@ public class User implements IdCachable {
         EmailAddress[] emails = getEmails();
         for (int i = 0; i < emails.length; i++) {
             if (emails[i].getId() == mail.getId()) {
-                try {
-                    PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE emails SET deleted=? WHERE id=?");
-                    ps.setDate(1, new Date(System.currentTimeMillis()));
-                    ps.setInt(2, mail.getId());
-                    ps.execute();
-                } catch (SQLException e) {
-                    e.printStackTrace();
-                    throw new GigiApiException(e);
-                }
+                GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE emails SET deleted=? WHERE id=?");
+                ps.setDate(1, new Date(System.currentTimeMillis()));
+                ps.setInt(2, mail.getId());
+                ps.execute();
                 return;
             }
         }
         throw new GigiApiException("Email not one of user's email addresses.");
     }
 
-    public Assurance[] getReceivedAssurances() throws SQLException {
+    public Assurance[] getReceivedAssurances() {
         if (receivedAssurances == null) {
-            PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT * FROM notary WHERE `to`=? AND deleted=0");
+            GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT * FROM notary WHERE `to`=? AND deleted=0");
             query.setInt(1, getId());
-            ResultSet res = query.executeQuery();
+            GigiResultSet res = query.executeQuery();
             res.last();
             Assurance[] assurances = new Assurance[res.getRow()];
             res.beforeFirst();
@@ -435,11 +401,11 @@ public class User implements IdCachable {
         return receivedAssurances;
     }
 
-    public Assurance[] getMadeAssurances() throws SQLException {
+    public Assurance[] getMadeAssurances() {
         if (madeAssurances == null) {
-            PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT * FROM notary WHERE `from`=? AND deleted=0");
+            GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT * FROM notary WHERE `from`=? AND deleted=0");
             query.setInt(1, getId());
-            ResultSet res = query.executeQuery();
+            GigiResultSet res = query.executeQuery();
             res.last();
             Assurance[] assurances = new Assurance[res.getRow()];
             res.beforeFirst();
@@ -461,12 +427,12 @@ public class User implements IdCachable {
         receivedAssurances = null;
     }
 
-    public void updateUserData() throws SQLException, GigiApiException {
+    public void updateUserData() throws GigiApiException {
         synchronized (Notary.class) {
             if (getAssurancePoints() != 0) {
                 throw new GigiApiException("No change after assurance allowed.");
             }
-            PreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET fname=?, lname=?, mname=?, suffix=?, dob=? WHERE id=?");
+            GigiPreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET fname=?, lname=?, mname=?, suffix=?, dob=? WHERE id=?");
             update.setString(1, getFname());
             update.setString(2, getLname());
             update.setString(3, getMname());
@@ -486,31 +452,31 @@ public class User implements IdCachable {
 
     }
 
-    public boolean wantsDirectoryListing() throws SQLException {
-        PreparedStatement get = DatabaseConnection.getInstance().prepare("SELECT listme FROM users WHERE id=?");
+    public boolean wantsDirectoryListing() {
+        GigiPreparedStatement get = DatabaseConnection.getInstance().prepare("SELECT listme FROM users WHERE id=?");
         get.setInt(1, getId());
-        ResultSet exec = get.executeQuery();
+        GigiResultSet exec = get.executeQuery();
         exec.next();
         return exec.getBoolean("listme");
     }
 
-    public String getContactInformation() throws SQLException {
-        PreparedStatement get = DatabaseConnection.getInstance().prepare("SELECT contactinfo FROM users WHERE id=?");
+    public String getContactInformation() {
+        GigiPreparedStatement get = DatabaseConnection.getInstance().prepare("SELECT contactinfo FROM users WHERE id=?");
         get.setInt(1, getId());
-        ResultSet exec = get.executeQuery();
+        GigiResultSet exec = get.executeQuery();
         exec.next();
         return exec.getString("contactinfo");
     }
 
-    public void setDirectoryListing(boolean on) throws SQLException {
-        PreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET listme = ? WHERE id = ?");
+    public void setDirectoryListing(boolean on) {
+        GigiPreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET listme = ? WHERE id = ?");
         update.setBoolean(1, on);
         update.setInt(2, getId());
         update.executeUpdate();
     }
 
-    public void setContactInformation(String contactInfo) throws SQLException {
-        PreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET contactinfo = ? WHERE id = ?");
+    public void setContactInformation(String contactInfo) {
+        GigiPreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET contactinfo = ? WHERE id = ?");
         update.setString(1, contactInfo);
         update.setInt(2, getId());
         update.executeUpdate();
@@ -526,28 +492,20 @@ public class User implements IdCachable {
 
     public void grantGroup(User granter, Group toGrant) throws GigiApiException {
         groups.add(toGrant);
-        try {
-            PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO user_groups SET user=?, permission=?, grantedby=?");
-            ps.setInt(1, getId());
-            ps.setString(2, toGrant.getDatabaseName());
-            ps.setInt(3, granter.getId());
-            ps.execute();
-        } catch (SQLException e) {
-            throw new GigiApiException(e);
-        }
+        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO user_groups SET user=?, permission=?, grantedby=?");
+        ps.setInt(1, getId());
+        ps.setString(2, toGrant.getDatabaseName());
+        ps.setInt(3, granter.getId());
+        ps.execute();
     }
 
     public void revokeGroup(User revoker, Group toRevoke) throws GigiApiException {
         groups.remove(toRevoke);
-        try {
-            PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE user_groups SET deleted=CURRENT_TIMESTAMP, revokedby=? WHERE deleted is NULL AND permission=? AND user=?");
-            ps.setInt(1, revoker.getId());
-            ps.setString(2, toRevoke.getDatabaseName());
-            ps.setInt(3, getId());
-            ps.execute();
-        } catch (SQLException e) {
-            throw new GigiApiException(e);
-        }
+        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE user_groups SET deleted=CURRENT_TIMESTAMP, revokedby=? WHERE deleted is NULL AND permission=? AND user=?");
+        ps.setInt(1, revoker.getId());
+        ps.setString(2, toRevoke.getDatabaseName());
+        ps.setInt(3, getId());
+        ps.execute();
     }
 
     private static ObjectCache<User> myCache = new ObjectCache<>();
index 8e1881332101ae4bb5630d71a8c05115f23f9e8b..3e6d3493acc317c4272ec14a845b1cb19f0d7f39 100644 (file)
@@ -10,8 +10,6 @@ import java.security.Key;
 import java.security.PrivateKey;
 import java.security.cert.Certificate;
 import java.security.cert.X509Certificate;
-import java.sql.PreparedStatement;
-import java.sql.SQLException;
 import java.util.Properties;
 import java.util.regex.Pattern;
 
@@ -19,6 +17,7 @@ import javax.naming.NamingException;
 
 import org.cacert.gigi.crypto.SMIME;
 import org.cacert.gigi.database.DatabaseConnection;
+import org.cacert.gigi.database.GigiPreparedStatement;
 import org.cacert.gigi.util.DNSUtil;
 
 public abstract class EmailProvider {
@@ -110,15 +109,11 @@ public abstract class EmailProvider {
                     pw.print("QUIT\r\n");
                     pw.flush();
 
-                    try {
-                        PreparedStatement statmt = DatabaseConnection.getInstance().prepare("insert into `pinglog` set `when`=NOW(), `email`=?, `result`=?, `uid`=?");
-                        statmt.setString(1, address);
-                        statmt.setString(2, line);
-                        statmt.setInt(3, forUid);
-                        statmt.execute();
-                    } catch (SQLException e) {
-                        e.printStackTrace();
-                    }
+                    GigiPreparedStatement statmt = DatabaseConnection.getInstance().prepare("insert into `pinglog` set `when`=NOW(), `email`=?, `result`=?, `uid`=?");
+                    statmt.setString(1, address);
+                    statmt.setString(2, line);
+                    statmt.setInt(3, forUid);
+                    statmt.execute();
 
                     if (line == null || !line.startsWith("250")) {
                         return line;
@@ -129,15 +124,11 @@ public abstract class EmailProvider {
 
             }
         }
-        try {
-            PreparedStatement statmt = DatabaseConnection.getInstance().prepare("insert into `pinglog` set `when`=NOW(), `email`=?, `result`=?, `uid`=?");
-            statmt.setString(1, address);
-            statmt.setString(2, "Failed to make a connection to the mail server");
-            statmt.setInt(3, forUid);
-            statmt.execute();
-        } catch (SQLException e) {
-            e.printStackTrace();
-        }
+        GigiPreparedStatement statmt = DatabaseConnection.getInstance().prepare("insert into `pinglog` set `when`=NOW(), `email`=?, `result`=?, `uid`=?");
+        statmt.setString(1, address);
+        statmt.setString(2, "Failed to make a connection to the mail server");
+        statmt.setInt(3, forUid);
+        statmt.execute();
         return FAIL;
     }
 
index ae359869362a7a8580ca47fa88b3f921ba837deb..261fef196245897d08abf9988398873460771c27 100644 (file)
@@ -1,6 +1,5 @@
 package org.cacert.gigi.output;
 
-import java.sql.SQLException;
 import java.util.Map;
 
 import org.cacert.gigi.dbObjects.Certificate;
@@ -23,12 +22,7 @@ public class CertificateIterable implements IterableDataset {
             return false;
         }
         Certificate c = certificates[i++];
-        try {
-            vars.put("state", l.getTranslation(c.getStatus().toString().toLowerCase()));
-        } catch (SQLException e) {
-            vars.put("state", "Failed");
-            e.printStackTrace();
-        }
+        vars.put("state", l.getTranslation(c.getStatus().toString().toLowerCase()));
         vars.put("CN", c.getDistinguishedName());
         vars.put("serial", c.getSerial());
         vars.put("digest", c.getMessageDigest());
index 0a498c587112dcaa34cf0166094889b606c3c728..4a405d84de8f4ecc7e733d4d9530a09ddd89c9d8 100644 (file)
@@ -4,15 +4,13 @@ import static org.cacert.gigi.Gigi.*;
 
 import java.io.IOException;
 import java.security.cert.X509Certificate;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpSession;
 
 import org.cacert.gigi.database.DatabaseConnection;
+import org.cacert.gigi.database.GigiPreparedStatement;
+import org.cacert.gigi.database.GigiResultSet;
 import org.cacert.gigi.dbObjects.User;
 import org.cacert.gigi.localisation.Language;
 import org.cacert.gigi.util.PasswordHash;
@@ -66,19 +64,15 @@ public class LoginPage extends Page {
     private void tryAuthWithUnpw(HttpServletRequest req) {
         String un = req.getParameter("username");
         String pw = req.getParameter("password");
-        try {
-            PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `password`, `id` FROM `users` WHERE `email`=? AND locked='0' AND verified='1'");
-            ps.setString(1, un);
-            ResultSet rs = ps.executeQuery();
-            if (rs.next()) {
-                if (PasswordHash.verifyHash(pw, rs.getString(1))) {
-                    loginSession(req, User.getById(rs.getInt(2)));
-                }
+        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `password`, `id` FROM `users` WHERE `email`=? AND locked='0' AND verified='1'");
+        ps.setString(1, un);
+        GigiResultSet rs = ps.executeQuery();
+        if (rs.next()) {
+            if (PasswordHash.verifyHash(pw, rs.getString(1))) {
+                loginSession(req, User.getById(rs.getInt(2)));
             }
-            rs.close();
-        } catch (SQLException e) {
-            e.printStackTrace();
         }
+        rs.close();
     }
 
     public static User getUser(HttpServletRequest req) {
@@ -87,17 +81,13 @@ public class LoginPage extends Page {
 
     private void tryAuthWithCertificate(HttpServletRequest req, X509Certificate x509Certificate) {
         String serial = x509Certificate.getSerialNumber().toString(16).toUpperCase();
-        try {
-            PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `memid` FROM `certs` WHERE `serial`=? AND `disablelogin`='0' AND `revoked` = " + "'0000-00-00 00:00:00'");
-            ps.setString(1, serial);
-            ResultSet rs = ps.executeQuery();
-            if (rs.next()) {
-                loginSession(req, User.getById(rs.getInt(1)));
-            }
-            rs.close();
-        } catch (SQLException e) {
-            e.printStackTrace();
+        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `memid` FROM `certs` WHERE `serial`=? AND `disablelogin`='0' AND `revoked` = " + "'0000-00-00 00:00:00'");
+        ps.setString(1, serial);
+        GigiResultSet rs = ps.executeQuery();
+        if (rs.next()) {
+            loginSession(req, User.getById(rs.getInt(1)));
         }
+        rs.close();
     }
 
     private void loginSession(HttpServletRequest req, User user) {
index 848f349069bc21458b160c46721611c733107b93..4f85ab687f32f4809e000c080ea3c244825d9078 100644 (file)
@@ -2,7 +2,6 @@ package org.cacert.gigi.pages.account;
 
 import java.io.InputStreamReader;
 import java.io.PrintWriter;
-import java.sql.SQLException;
 import java.util.Map;
 
 import javax.servlet.http.HttpServletRequest;
@@ -56,9 +55,6 @@ public class MyDetailsForm extends Form {
             } else {
                 throw new GigiApiException("No change after assurance allowed.");
             }
-        } catch (SQLException e) {
-            new GigiApiException(e).format(out, Page.getLanguage(req));
-            return false;
         } catch (GigiApiException e) {
             e.format(out, Page.getLanguage(req));
             return false;
@@ -76,16 +72,12 @@ public class MyDetailsForm extends Form {
         vars.put("lname", HTMLEncoder.encodeHTML(target.getLname()));
         vars.put("suffix", target.getSuffix() == null ? "" : HTMLEncoder.encodeHTML(target.getSuffix()));
         vars.put("details", "");
-        try {
-            if (target.getAssurancePoints() == 0) {
-                vars.put("DoB", ds);
-                templ.output(out, l, vars);
-            } else {
-                vars.put("DoB", DateSelector.getDateFormat().format(target.getDob()));
-                assured.output(out, l, vars);
-            }
-        } catch (SQLException e) {
-            e.printStackTrace();
+        if (target.getAssurancePoints() == 0) {
+            vars.put("DoB", ds);
+            templ.output(out, l, vars);
+        } else {
+            vars.put("DoB", DateSelector.getDateFormat().format(target.getDob()));
+            assured.output(out, l, vars);
         }
     }
 
index e9fa960292c2766c56d36d2c02a2148afc0a9c27..c1a6f4244067dcd6baeb2a2d80a92cf2912a5ecc 100644 (file)
@@ -2,23 +2,20 @@ package org.cacert.gigi.pages.account;
 
 import java.io.InputStreamReader;
 import java.io.PrintWriter;
-import java.sql.SQLException;
 import java.util.Map;
 
 import javax.servlet.http.HttpServletRequest;
 
-import org.cacert.gigi.GigiApiException;
 import org.cacert.gigi.dbObjects.User;
 import org.cacert.gigi.localisation.Language;
 import org.cacert.gigi.output.Form;
 import org.cacert.gigi.output.template.Template;
-import org.cacert.gigi.pages.Page;
 
 public class MyListingForm extends Form {
 
     private static Template template;
-    
-    static{
+
+    static {
         template = new Template(new InputStreamReader(MyListingForm.class.getResourceAsStream("MyListingForm.templ")));
     }
 
@@ -32,33 +29,23 @@ public class MyListingForm extends Form {
     @Override
     public boolean submit(PrintWriter out, HttpServletRequest req) {
         if (req.getParameter("listme") != null && req.getParameter("contactinfo") != null) {
-            try {
-                target.setDirectoryListing( !req.getParameter("listme").equals("0"));
-                target.setContactInformation(req.getParameter("contactinfo"));
-                return true;
-            } catch (SQLException e) {
-                new GigiApiException(e).format(out, Page.getLanguage(req));
-                e.printStackTrace();
-                return false;
-            }
+            target.setDirectoryListing( !req.getParameter("listme").equals("0"));
+            target.setContactInformation(req.getParameter("contactinfo"));
+            return true;
         }
         return false;
     }
 
     @Override
     protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
-        try {
-            if (target.wantsDirectoryListing()) {
-                vars.put("selected", "selected");
-                vars.put("notSelected", "");
-                vars.put("activeInfo", target.getContactInformation());
-            } else {
-                vars.put("selected", "");
-                vars.put("notSelected", "selected");
-                vars.put("activeInfo", "");
-            }
-        } catch (SQLException e) {
-            new GigiApiException(e).format(out, l);
+        if (target.wantsDirectoryListing()) {
+            vars.put("selected", "selected");
+            vars.put("notSelected", "");
+            vars.put("activeInfo", target.getContactInformation());
+        } else {
+            vars.put("selected", "");
+            vars.put("notSelected", "selected");
+            vars.put("activeInfo", "");
         }
         template.output(out, l, vars);
     }
index 9d48b712be8ab176644bbab06722f9bec5408863..e848854fa19d7b50b9558d44f5acb8e6b3652283 100644 (file)
@@ -7,7 +7,6 @@ import java.security.PublicKey;
 import java.security.interfaces.DSAPublicKey;
 import java.security.interfaces.ECPublicKey;
 import java.security.interfaces.RSAPublicKey;
-import java.sql.SQLException;
 import java.util.Base64;
 import java.util.HashMap;
 import java.util.LinkedHashSet;
@@ -313,8 +312,6 @@ public class CertificateIssueForm extends Form {
                 throw new GigiApiException("Certificate Request format is invalid.");
             } catch (InterruptedException e) {
                 e.printStackTrace();
-            } catch (SQLException e) {
-                throw new GigiApiException(e);
             }
         } catch (GigiApiException e) {
             e.format(out, Page.getLanguage(req));
index 423ec72dd9cdad9d3c29259ea26373c4529460ad..0c0282227f7dbf71eb65f7dab3c759ddfcc7bbda 100644 (file)
@@ -5,7 +5,6 @@ import java.io.PrintWriter;
 import java.net.URLEncoder;
 import java.security.GeneralSecurityException;
 import java.security.cert.X509Certificate;
-import java.sql.SQLException;
 import java.util.HashMap;
 
 import javax.servlet.ServletOutputStream;
@@ -77,9 +76,6 @@ public class Certificates extends Page {
         } catch (GeneralSecurityException e) {
             resp.sendError(404);
             return true;
-        } catch (SQLException e) {
-            resp.sendError(404);
-            return true;
         }
 
         return true;
@@ -106,8 +102,6 @@ public class Certificates extends Page {
                 vars.put("cert", c.cert());
             } catch (GeneralSecurityException e) {
                 e.printStackTrace();
-            } catch (SQLException e) {
-                e.printStackTrace();
             }
             certDisplay.output(out, getLanguage(req), vars);
 
index 55b56b0a57ecc7c41c28589043a20e9127c1d0d2..6f2bf6efd0fabec27c30c4b474d8888ad0b1e807 100644 (file)
@@ -3,18 +3,16 @@ package org.cacert.gigi.pages.main;
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.sql.Date;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
 import java.sql.SQLException;
-import java.util.Enumeration;
 import java.util.HashMap;
-import java.util.Locale;
 import java.util.Map;
 
 import javax.servlet.http.HttpServletRequest;
 
 import org.cacert.gigi.GigiApiException;
 import org.cacert.gigi.database.DatabaseConnection;
+import org.cacert.gigi.database.GigiPreparedStatement;
+import org.cacert.gigi.database.GigiResultSet;
 import org.cacert.gigi.dbObjects.EmailAddress;
 import org.cacert.gigi.dbObjects.User;
 import org.cacert.gigi.email.EmailProvider;
@@ -121,31 +119,26 @@ public class Signup extends Form {
         if (isFailed(out)) {
             return false;
         }
-        try {
-            PreparedStatement q1 = DatabaseConnection.getInstance().prepare("select * from `emails` where `email`=? and `deleted`=0");
-            PreparedStatement q2 = DatabaseConnection.getInstance().prepare("select * from `users` where `email`=? and `deleted`=0");
-            q1.setString(1, buildup.getEmail());
-            q2.setString(1, buildup.getEmail());
-            ResultSet r1 = q1.executeQuery();
-            ResultSet r2 = q2.executeQuery();
-            if (r1.next() || r2.next()) {
-                outputError(out, req, "This email address is currently valid in the system.");
-            }
-            r1.close();
-            r2.close();
-            PreparedStatement q3 = DatabaseConnection.getInstance().prepare("select `domain` from `baddomains` where `domain`=RIGHT(?, LENGTH(`domain`))");
-            q3.setString(1, buildup.getEmail());
-
-            ResultSet r3 = q3.executeQuery();
-            if (r3.next()) {
-                String domain = r3.getString(1);
-                outputError(out, req, "We don't allow signups from people using email addresses from %s", domain);
-            }
-            r3.close();
-        } catch (SQLException e) {
-            e.printStackTrace();
-            outputError(out, req, "an internal error happened");
-        }
+        GigiPreparedStatement q1 = DatabaseConnection.getInstance().prepare("select * from `emails` where `email`=? and `deleted`=0");
+        GigiPreparedStatement q2 = DatabaseConnection.getInstance().prepare("select * from `users` where `email`=? and `deleted`=0");
+        q1.setString(1, buildup.getEmail());
+        q2.setString(1, buildup.getEmail());
+        GigiResultSet r1 = q1.executeQuery();
+        GigiResultSet r2 = q2.executeQuery();
+        if (r1.next() || r2.next()) {
+            outputError(out, req, "This email address is currently valid in the system.");
+        }
+        r1.close();
+        r2.close();
+        GigiPreparedStatement q3 = DatabaseConnection.getInstance().prepare("select `domain` from `baddomains` where `domain`=RIGHT(?, LENGTH(`domain`))");
+        q3.setString(1, buildup.getEmail());
+
+        GigiResultSet r3 = q3.executeQuery();
+        if (r3.next()) {
+            String domain = r3.getString(1);
+            outputError(out, req, "We don't allow signups from people using email addresses from %s", domain);
+        }
+        r3.close();
         String mailResult = EmailProvider.FAIL;
         try {
             mailResult = EmailProvider.getInstance().checkEmailServer(0, buildup.getEmail());
@@ -178,7 +171,6 @@ public class Signup extends Form {
     private void run(HttpServletRequest req, String password) throws SQLException {
         try {
             DatabaseConnection.getInstance().beginTransaction();
-            Enumeration<Locale> locales = req.getLocales();
             buildup.setPreferredLocale(Page.getLanguage(req).getLocale());
             buildup.setDob(myDoB.getDate());
             buildup.insert(password);
@@ -186,7 +178,7 @@ public class Signup extends Form {
             EmailAddress ea = new EmailAddress(buildup, buildup.getEmail());
             ea.insert(Page.getLanguage(req));
 
-            PreparedStatement ps = DatabaseConnection.getInstance().prepare("insert into `alerts` set `memid`=?," + " `general`=?, `country`=?, `regional`=?, `radius`=?");
+            GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("insert into `alerts` set `memid`=?," + " `general`=?, `country`=?, `regional`=?, `radius`=?");
             ps.setInt(1, memid);
             ps.setString(2, general ? "1" : "0");
             ps.setString(3, country ? "1" : "0");
index 81743c90214bc306fc8e501dc7eac5d3eac765bd..38b4ae0654acc1497ceccccedf5c47ad4a644269 100644 (file)
@@ -1,7 +1,6 @@
 package org.cacert.gigi.pages.wot;
 
 import java.io.PrintWriter;
-import java.sql.SQLException;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.HashMap;
@@ -48,11 +47,7 @@ public class AssuranceForm extends Form {
         res.putAll(vars);
         res.put("nameExplicit", assuree.getName());
         res.put("name", assuree.getName().toString());
-        try {
-            res.put("maxpoints", assuree.getMaxAssurePoints());
-        } catch (SQLException e) {
-            e.printStackTrace();
-        }
+        res.put("maxpoints", assuree.getMaxAssurePoints());
         res.put("dob", sdf.format(assuree.getDob()));
         res.put("dobFmt2", sdf2.format(assuree.getDob()));
         templ.output(out, l, res);
@@ -82,8 +77,6 @@ public class AssuranceForm extends Form {
         try {
             Notary.assure(Page.getUser(req), assuree, assureeName, dob, pointsI, req.getParameter("location"), req.getParameter("date"));
             return true;
-        } catch (SQLException e) {
-            e.printStackTrace();
         } catch (GigiApiException e) {
             e.format(out, Page.getLanguage(req));
         }
index e1822e3beb1dc91cd817fb6de78e7ad710c9562b..aa99a2fbb7e992278bea88d93a4becdc587352d9 100644 (file)
@@ -2,9 +2,6 @@ package org.cacert.gigi.pages.wot;
 
 import java.io.IOException;
 import java.io.PrintWriter;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
 import java.util.HashMap;
 
 import javax.servlet.http.HttpServletRequest;
@@ -12,6 +9,8 @@ import javax.servlet.http.HttpServletResponse;
 
 import org.cacert.gigi.GigiApiException;
 import org.cacert.gigi.database.DatabaseConnection;
+import org.cacert.gigi.database.GigiPreparedStatement;
+import org.cacert.gigi.database.GigiResultSet;
 import org.cacert.gigi.dbObjects.User;
 import org.cacert.gigi.output.DateSelector;
 import org.cacert.gigi.output.Form;
@@ -46,12 +45,7 @@ public class AssurePage extends Page {
 
     @Override
     public boolean isPermitted(User u) {
-        try {
-            return u != null && u.canAssure();
-        } catch (SQLException e) {
-            e.printStackTrace();
-            return false;
-        }
+        return u != null && u.canAssure();
     }
 
     private void outputForm(HttpServletRequest req, PrintWriter out, AssuranceForm form) {
@@ -79,9 +73,9 @@ public class AssurePage extends Page {
             return;
         }
 
-        ResultSet rs = null;
+        GigiResultSet rs = null;
         try {
-            PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id, verified FROM users WHERE email=? AND dob=? AND deleted=0");
+            GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id, verified FROM users WHERE email=? AND dob=? AND deleted=0");
             ps.setString(1, req.getParameter("email"));
             String day = req.getParameter("year") + "-" + req.getParameter("month") + "-" + req.getParameter("day");
             ps.setString(2, day);
@@ -110,15 +104,9 @@ public class AssurePage extends Page {
             }
 
             rs.close();
-        } catch (SQLException e) {
-            e.printStackTrace();
         } finally {
-            try {
-                if (rs != null) {
-                    rs.close();
-                }
-            } catch (SQLException e) {
-                e.printStackTrace();
+            if (rs != null) {
+                rs.close();
             }
         }
     }
index 7bb4ce6cf6ff7066db79bddb88f198d26deb43c5..ffe68986b85fd6b46a3d1d18fac444cdd2a36911 100644 (file)
@@ -1,13 +1,11 @@
 package org.cacert.gigi.pages.wot;
 
 import java.io.IOException;
-import java.sql.SQLException;
 import java.util.HashMap;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
-import org.cacert.gigi.GigiApiException;
 import org.cacert.gigi.dbObjects.User;
 import org.cacert.gigi.output.AssurancesDisplay;
 import org.cacert.gigi.pages.Page;
@@ -29,14 +27,9 @@ public class MyPoints extends Page {
         HashMap<String, Object> vars = new HashMap<String, Object>();
         vars.put("pointlist", myDisplay);
         vars.put("madelist", toOtherDisplay);
-        try {
-            User user = getUser(req);
-            vars.put("asArr", user.getReceivedAssurances());
-            vars.put("otherAsArr", user.getMadeAssurances());
-        } catch (SQLException e) {
-            new GigiApiException(e).format(resp.getWriter(), getLanguage(req));
-            return;
-        }
+        User user = getUser(req);
+        vars.put("asArr", user.getReceivedAssurances());
+        vars.put("otherAsArr", user.getMadeAssurances());
         getDefaultTemplate().output(resp.getWriter(), getLanguage(req), vars);
     }
 
index 60c1ee2eec9ae21b6a065ae33516e52e1172a79a..2a3167438851969d613ea8575ebd4d5ae0850c72 100644 (file)
@@ -1,11 +1,11 @@
 package org.cacert.gigi.ping;
 
 import java.security.KeyStore;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
 import java.util.HashMap;
+
 import org.cacert.gigi.database.DatabaseConnection;
+import org.cacert.gigi.database.GigiPreparedStatement;
+import org.cacert.gigi.database.GigiResultSet;
 import org.cacert.gigi.dbObjects.Domain;
 import org.cacert.gigi.dbObjects.User;
 import org.cacert.gigi.util.RandomToken;
@@ -14,11 +14,11 @@ public class PingerDaemon extends Thread {
 
     HashMap<String, DomainPinger> pingers = new HashMap<>();
 
-    private PreparedStatement searchNeededPings;
+    private GigiPreparedStatement searchNeededPings;
 
-    private PreparedStatement enterPingResult;
+    private GigiPreparedStatement enterPingResult;
 
-    private PreparedStatement updatePingStatus;
+    private GigiPreparedStatement updatePingStatus;
 
     private KeyStore truststore;
 
@@ -28,23 +28,16 @@ public class PingerDaemon extends Thread {
 
     @Override
     public void run() {
-        try {
-            searchNeededPings = DatabaseConnection.getInstance().prepare("SELECT pingconfig.*, domains.domain, domains.memid FROM pingconfig LEFT JOIN domainPinglog ON domainPinglog.configId=pingconfig.id INNER JOIN domains ON domains.id=pingconfig.domainid WHERE ( pingconfig.reping='y' OR domainPinglog.configId IS NULL) AND domains.deleted IS NULL GROUP BY pingconfig.id");
-            enterPingResult = DatabaseConnection.getInstance().prepare("INSERT INTO domainPinglog SET configId=?, state=?, result=?, challenge=?");
-            updatePingStatus = DatabaseConnection.getInstance().prepare("UPDATE pingconfig SET reping='n' WHERE id=?");
-            pingers.put("email", new EmailPinger());
-            pingers.put("ssl", new SSLPinger(truststore));
-            pingers.put("http", new HTTPFetch());
-            pingers.put("dns", new DNSPinger());
-        } catch (SQLException e) {
-            e.printStackTrace();
-        }
+        searchNeededPings = DatabaseConnection.getInstance().prepare("SELECT pingconfig.*, domains.domain, domains.memid FROM pingconfig LEFT JOIN domainPinglog ON domainPinglog.configId=pingconfig.id INNER JOIN domains ON domains.id=pingconfig.domainid WHERE ( pingconfig.reping='y' OR domainPinglog.configId IS NULL) AND domains.deleted IS NULL GROUP BY pingconfig.id");
+        enterPingResult = DatabaseConnection.getInstance().prepare("INSERT INTO domainPinglog SET configId=?, state=?, result=?, challenge=?");
+        updatePingStatus = DatabaseConnection.getInstance().prepare("UPDATE pingconfig SET reping='n' WHERE id=?");
+        pingers.put("email", new EmailPinger());
+        pingers.put("ssl", new SSLPinger(truststore));
+        pingers.put("http", new HTTPFetch());
+        pingers.put("dns", new DNSPinger());
+
         while (true) {
-            try {
-                execute();
-            } catch (SQLException e) {
-                e.printStackTrace();
-            }
+            execute();
             try {
                 Thread.sleep(5000);
             } catch (InterruptedException e) {
@@ -52,9 +45,9 @@ public class PingerDaemon extends Thread {
         }
     }
 
-    private void execute() throws SQLException {
+    private void execute() {
 
-        ResultSet rs = searchNeededPings.executeQuery();
+        GigiResultSet rs = searchNeededPings.executeQuery();
         while (rs.next()) {
             String type = rs.getString("type");
             String config = rs.getString("info");
index e10a44d87c5b013ac43bdaea6a8787e0201554e6..cb484cf068c500cd8fc582bd03f320e058093995 100644 (file)
@@ -1,12 +1,10 @@
 package org.cacert.gigi.util;
 
 import java.sql.Date;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-
 import org.cacert.gigi.GigiApiException;
 import org.cacert.gigi.database.DatabaseConnection;
+import org.cacert.gigi.database.GigiPreparedStatement;
+import org.cacert.gigi.database.GigiResultSet;
 import org.cacert.gigi.dbObjects.Certificate;
 import org.cacert.gigi.output.CertificateValiditySelector;
 
@@ -32,31 +30,31 @@ public class Job {
         }
     }
 
-    public static Job sign(Certificate targetId, Date start, String period) throws SQLException, GigiApiException {
+    public static Job sign(Certificate targetId, Date start, String period) throws GigiApiException {
         CertificateValiditySelector.checkValidityLength(period);
-        PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `jobs` SET targetId=?, task=?, executeFrom=?, executeTo=?");
+        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `jobs` SET targetId=?, task=?, executeFrom=?, executeTo=?");
         ps.setInt(1, targetId.getId());
         ps.setString(2, JobType.SIGN.getName());
         ps.setDate(3, start);
         ps.setString(4, period);
         ps.execute();
-        return new Job(DatabaseConnection.lastInsertId(ps));
+        return new Job(ps.lastInsertId());
     }
 
-    public static Job revoke(Certificate targetId) throws SQLException {
+    public static Job revoke(Certificate targetId) {
 
-        PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `jobs` SET targetId=?, task=?");
+        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `jobs` SET targetId=?, task=?");
         ps.setInt(1, targetId.getId());
         ps.setString(2, JobType.REVOKE.getName());
         ps.execute();
-        return new Job(DatabaseConnection.lastInsertId(ps));
+        return new Job(ps.lastInsertId());
     }
 
-    public boolean waitFor(int max) throws SQLException, InterruptedException {
+    public boolean waitFor(int max) throws InterruptedException {
         long start = System.currentTimeMillis();
-        PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `jobs` WHERE id=? AND state='open'");
+        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `jobs` WHERE id=? AND state='open'");
         ps.setInt(1, id);
-        ResultSet rs = ps.executeQuery();
+        GigiResultSet rs = ps.executeQuery();
         while (rs.next()) {
             rs.close();
             if (max != 0 && System.currentTimeMillis() - start > max) {
index 184ca8bc44f4a8036c83ef73933b3f2c47e47047..d1f92c801caedab6526f07595b582cd02f4b51f0 100644 (file)
@@ -1,21 +1,20 @@
 package org.cacert.gigi.util;
 
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
 import java.text.ParseException;
 import java.util.Date;
 
 import org.cacert.gigi.GigiApiException;
 import org.cacert.gigi.database.DatabaseConnection;
+import org.cacert.gigi.database.GigiPreparedStatement;
+import org.cacert.gigi.database.GigiResultSet;
 import org.cacert.gigi.dbObjects.Name;
 import org.cacert.gigi.dbObjects.User;
 import org.cacert.gigi.output.DateSelector;
 
 public class Notary {
 
-    public static void writeUserAgreement(int memid, String document, String method, String comment, boolean active, int secmemid) throws SQLException {
-        PreparedStatement q = DatabaseConnection.getInstance().prepare("insert into `user_agreements` set `memid`=?, `secmemid`=?," + " `document`=?,`date`=NOW(), `active`=?,`method`=?,`comment`=?");
+    public static void writeUserAgreement(int memid, String document, String method, String comment, boolean active, int secmemid) {
+        GigiPreparedStatement q = DatabaseConnection.getInstance().prepare("insert into `user_agreements` set `memid`=?, `secmemid`=?," + " `document`=?,`date`=NOW(), `active`=?,`method`=?,`comment`=?");
         q.setInt(1, memid);
         q.setInt(2, secmemid);
         q.setString(3, document);
@@ -29,21 +28,17 @@ public class Notary {
         if (assurer.getId() == target.getId()) {
             throw new GigiApiException("You cannot assure yourself.");
         }
-        try {
-            PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `notary` where `to`=? and `from`=? AND `deleted`=0");
-            ps.setInt(1, target.getId());
-            ps.setInt(2, assurer.getId());
-            ResultSet rs = ps.executeQuery();
-            if (rs.next()) {
-                rs.close();
-                throw new GigiApiException("You have already assured this member.");
-            }
+        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `notary` where `to`=? and `from`=? AND `deleted`=0");
+        ps.setInt(1, target.getId());
+        ps.setInt(2, assurer.getId());
+        GigiResultSet rs = ps.executeQuery();
+        if (rs.next()) {
             rs.close();
-            if ( !assurer.canAssure()) {
-                throw new GigiApiException("You are not an assurer.");
-            }
-        } catch (SQLException e) {
-            throw new GigiApiException(e);
+            throw new GigiApiException("You have already assured this member.");
+        }
+        rs.close();
+        if ( !assurer.canAssure()) {
+            throw new GigiApiException("You are not an assurer.");
         }
     }
 
@@ -66,12 +61,10 @@ public class Notary {
      *            the location where the assurance took place
      * @param date
      *            the date when the assurance took place
-     * @throws SQLException
-     *             if SQL goes wrong
      * @throws GigiApiException
      *             if the assurance fails (for various reasons)
      */
-    public synchronized static void assure(User assurer, User assuree, Name assureeName, Date dob, int awarded, String location, String date) throws SQLException, GigiApiException {
+    public synchronized static void assure(User assurer, User assuree, Name assureeName, Date dob, int awarded, String location, String date) throws GigiApiException {
         GigiApiException gae = new GigiApiException();
 
         if (date == null || date.equals("")) {
@@ -109,7 +102,7 @@ public class Notary {
             throw gae;
         }
 
-        PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?");
+        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?");
         ps.setInt(1, assurer.getId());
         ps.setInt(2, assuree.getId());
         ps.setInt(3, awarded);
index fa670721d2315862eb20608047e68f23eb120a17..fbccba9c640b25bea4ddea720ac913c75a0fdb1f 100644 (file)
@@ -3,14 +3,14 @@ package org.cacert.gigi;
 import static org.hamcrest.CoreMatchers.*;
 import static org.junit.Assert.*;
 
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashSet;
 
 import org.cacert.gigi.database.DatabaseConnection;
+import org.cacert.gigi.database.GigiPreparedStatement;
+import org.cacert.gigi.database.GigiResultSet;
 import org.cacert.gigi.dbObjects.Group;
 import org.cacert.gigi.dbObjects.ObjectCache;
 import org.cacert.gigi.dbObjects.User;
@@ -39,7 +39,7 @@ public class TestUserGroupMembership extends ManagedTest {
         assertThat(u2, is(not(sameInstance(u))));
         assertBehavesTtpGroup(u2);
 
-        ResultSet rs = fetchGroupRowsFor(u);
+        GigiResultSet rs = fetchGroupRowsFor(u);
 
         assertTrue(rs.next());
         assertEquals(0, rs.getInt("revokedby"));
@@ -69,7 +69,7 @@ public class TestUserGroupMembership extends ManagedTest {
         assertThat(u2, is(not(sameInstance(u))));
         assertBehavesEmpty(u);
 
-        ResultSet rs = fetchGroupRowsFor(u);
+        GigiResultSet rs = fetchGroupRowsFor(u);
         assertTrue(rs.next());
         assertEquals(granter.getId(), rs.getInt("revokedby"));
         assertEquals(granter.getId(), rs.getInt("grantedby"));
@@ -81,10 +81,10 @@ public class TestUserGroupMembership extends ManagedTest {
         assertFalse(rs.next());
     }
 
-    private ResultSet fetchGroupRowsFor(User u) throws SQLException {
-        PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT * FROM user_groups WHERE user=?");
+    private GigiResultSet fetchGroupRowsFor(User u) throws SQLException {
+        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT * FROM user_groups WHERE user=?");
         ps.setInt(1, u.getId());
-        ResultSet rs = ps.executeQuery();
+        GigiResultSet rs = ps.executeQuery();
         return rs;
     }
 
index 570d1636fab0b7775e3f74aa81c789023795aa7c..0b1fb4e09f9446ffcb722b634b69378daa51df1a 100644 (file)
@@ -31,8 +31,6 @@ import java.security.Principal;
 import java.security.PrivateKey;
 import java.security.Signature;
 import java.security.cert.X509Certificate;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.Locale;
 import java.util.Properties;
@@ -47,6 +45,8 @@ import javax.net.ssl.X509KeyManager;
 import org.cacert.gigi.DevelLauncher;
 import org.cacert.gigi.GigiApiException;
 import org.cacert.gigi.database.DatabaseConnection;
+import org.cacert.gigi.database.GigiPreparedStatement;
+import org.cacert.gigi.database.GigiResultSet;
 import org.cacert.gigi.dbObjects.EmailAddress;
 import org.cacert.gigi.dbObjects.ObjectCache;
 import org.cacert.gigi.dbObjects.User;
@@ -71,6 +71,7 @@ public class ManagedTest {
     static {
         System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
     }
+
     /**
      * Some password that fullfills the password criteria.
      */
@@ -290,10 +291,10 @@ public class ManagedTest {
             String[] parts = verifyLink.split("\\?");
             URL u = new URL("https://" + getServerName() + "/verify?" + parts[1]);
             u.openStream().close();
-            ;
-            PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM users where email=?");
+
+            GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM users where email=?");
             ps.setString(1, email);
-            ResultSet rs = ps.executeQuery();
+            GigiResultSet rs = ps.executeQuery();
             if (rs.next()) {
                 return rs.getInt(1);
             }
@@ -302,8 +303,6 @@ public class ManagedTest {
             throw new Error(e);
         } catch (IOException e) {
             throw new Error(e);
-        } catch (SQLException e) {
-            throw new Error(e);
         }
     }
 
@@ -323,19 +322,14 @@ public class ManagedTest {
      */
     public static int createAssuranceUser(String firstName, String lastName, String email, String password) {
         int uid = createVerifiedUser(firstName, lastName, email, password);
-        try {
-            PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `cats_passed` SET `user_id`=?, `variant_id`=?");
-            ps.setInt(1, uid);
-            ps.setInt(2, 0);
-            ps.execute();
-            ps = DatabaseConnection.getInstance().prepare("INSERT INTO `notary` SET `from`=?, `to`=?, points='100'");
-            ps.setInt(1, uid);
-            ps.setInt(2, uid);
-            ps.execute();
-
-        } catch (SQLException e) {
-            throw new Error(e);
-        }
+        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `cats_passed` SET `user_id`=?, `variant_id`=?");
+        ps.setInt(1, uid);
+        ps.setInt(2, 0);
+        ps.execute();
+        ps = DatabaseConnection.getInstance().prepare("INSERT INTO `notary` SET `from`=?, `to`=?, points='100'");
+        ps.setInt(1, uid);
+        ps.setInt(2, uid);
+        ps.execute();
         return uid;
     }
 
index 9007ec25811b5d4044bf3220a6ad95fecc35a480..5095d680690f8eb776c92125aaf4471aee071356 100644 (file)
@@ -7,13 +7,13 @@ import java.net.HttpURLConnection;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLConnection;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 import org.cacert.gigi.database.DatabaseConnection;
+import org.cacert.gigi.database.GigiPreparedStatement;
+import org.cacert.gigi.database.GigiResultSet;
 import org.cacert.gigi.pages.account.domain.DomainOverview;
 import org.junit.Before;
 
@@ -26,10 +26,10 @@ public abstract class PingTest extends ClientTest {
     }
 
     protected void waitForPings(int count) throws SQLException, InterruptedException {
-        PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT COUNT(*) FROM domainPinglog");
+        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT COUNT(*) FROM domainPinglog");
         long start = System.currentTimeMillis();
         while (System.currentTimeMillis() - start < 10000) {
-            ResultSet rs = ps.executeQuery();
+            GigiResultSet rs = ps.executeQuery();
             rs.next();
             if (rs.getInt(1) >= count) {
                 break;
index 930f8dec8131bbe5929e7653df418502bee93593..b9dccd8e61321825665225992402ded8df4a9ef7 100644 (file)
@@ -1,11 +1,11 @@
 package org.cacert.gigi.util;
 
-import java.sql.PreparedStatement;
 import java.sql.SQLException;
 import java.util.Date;
 
 import org.cacert.gigi.GigiApiException;
 import org.cacert.gigi.database.DatabaseConnection;
+import org.cacert.gigi.database.GigiPreparedStatement;
 import org.cacert.gigi.dbObjects.User;
 import org.cacert.gigi.output.DateSelector;
 import org.cacert.gigi.testUtils.ManagedTest;
@@ -64,7 +64,7 @@ public class TestNotary extends ManagedTest {
             users[i] = User.getById(id);
         }
         int id = createAssuranceUser("fn", "ln", createUniqueName() + "@email.org", TEST_PASSWORD);
-        PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE users SET dob=TIMESTAMPADD(YEAR,-14,NOW()) WHERE id=?");
+        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE users SET dob=TIMESTAMPADD(YEAR,-14,NOW()) WHERE id=?");
         ps.setInt(1, id);
         ps.execute();
         User assurer = User.getById(id);
index 3b2a40fdf87cbd0c3d258d8c6a92430cb74d7403..c98a58ce0033e78ad108a09420bae11c21fb5fc2 100644 (file)
@@ -13,8 +13,6 @@ import java.security.GeneralSecurityException;
 import java.security.cert.CertificateFactory;
 import java.security.cert.X509Certificate;
 import java.util.Date;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Timestamp;
 import java.text.ParseException;
@@ -24,24 +22,26 @@ import java.util.Properties;
 import java.util.TimeZone;
 
 import org.cacert.gigi.database.DatabaseConnection;
+import org.cacert.gigi.database.GigiPreparedStatement;
+import org.cacert.gigi.database.GigiResultSet;
 import org.cacert.gigi.dbObjects.Certificate.CSRType;
 import org.cacert.gigi.output.DateSelector;
 
 public class SimpleSigner {
 
-    private static PreparedStatement warnMail;
+    private static GigiPreparedStatement warnMail;
 
-    private static PreparedStatement updateMail;
+    private static GigiPreparedStatement updateMail;
 
-    private static PreparedStatement readyCerts;
+    private static GigiPreparedStatement readyCerts;
 
-    private static PreparedStatement getSANSs;
+    private static GigiPreparedStatement getSANSs;
 
-    private static PreparedStatement revoke;
+    private static GigiPreparedStatement revoke;
 
-    private static PreparedStatement revokeCompleted;
+    private static GigiPreparedStatement revokeCompleted;
 
-    private static PreparedStatement finishJob;
+    private static GigiPreparedStatement finishJob;
 
     private static boolean running = true;
 
@@ -128,7 +128,7 @@ public class SimpleSigner {
     }
 
     private static void revokeCertificates() throws SQLException, IOException, InterruptedException {
-        ResultSet rs = revoke.executeQuery();
+        GigiResultSet rs = revoke.executeQuery();
         boolean worked = false;
         while (rs.next()) {
             int id = rs.getInt(1);
@@ -188,7 +188,7 @@ public class SimpleSigner {
     private static int counter = 0;
 
     private static void signCertificates() throws SQLException {
-        ResultSet rs = readyCerts.executeQuery();
+        GigiResultSet rs = readyCerts.executeQuery();
 
         Calendar c = Calendar.getInstance();
         c.setTimeZone(TimeZone.getTimeZone("UTC"));
@@ -229,7 +229,7 @@ public class SimpleSigner {
                 }
 
                 getSANSs.setInt(1, id);
-                ResultSet san = getSANSs.executeQuery();
+                GigiResultSet san = getSANSs.executeQuery();
 
                 File f = new File("keys", "SANFile" + System.currentTimeMillis() + (counter++) + ".cfg");
                 PrintWriter cfg = new PrintWriter(f);
@@ -318,8 +318,6 @@ public class SimpleSigner {
                 e.printStackTrace();
             } catch (IOException e) {
                 e.printStackTrace();
-            } catch (SQLException e) {
-                e.printStackTrace();
             } catch (ParseException e) {
                 e.printStackTrace();
             } catch (InterruptedException e1) {