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;
}
}
- 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;
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
--- /dev/null
+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
+
+ }
+}
--- /dev/null
+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
+
+ }
+
+}
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;
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() {
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;
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;
}
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 {
}
- 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");
}
* @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);
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());
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();
}
- public Job revoke() throws SQLException {
+ public Job revoke() {
if (getStatus() != CertificateStatus.ISSUED) {
throw new IllegalStateException();
}
}
- 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.");
}
public String getSerial() {
- try {
- getStatus();
- } catch (SQLException e) {
- e.printStackTrace();
- } // poll changes
+ getStatus();
+ // poll changes
return serial;
}
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 {
}
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);
}
}
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 {
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);
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);
}
}
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.");
}
}
}
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);
}
}
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() {
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;
}
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;
}
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 {
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);
}
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();
}
}
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;
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);
}
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();
}
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.");
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;
}
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;
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;
}
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() {}
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);
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;
}
}
- 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 {
}
}
- 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);
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;
* 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
}
}
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) {
}
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 {
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();
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();
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());
}
- 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();
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<>();
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;
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 {
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;
}
}
- 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;
}
package org.cacert.gigi.output;
-import java.sql.SQLException;
import java.util.Map;
import org.cacert.gigi.dbObjects.Certificate;
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());
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;
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) {
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) {
import java.io.InputStreamReader;
import java.io.PrintWriter;
-import java.sql.SQLException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
} 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;
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);
}
}
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")));
}
@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);
}
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;
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));
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;
} catch (GeneralSecurityException e) {
resp.sendError(404);
return true;
- } catch (SQLException e) {
- resp.sendError(404);
- return true;
}
return true;
vars.put("cert", c.cert());
} catch (GeneralSecurityException e) {
e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
}
certDisplay.output(out, getLanguage(req), vars);
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;
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());
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);
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");
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;
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);
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));
}
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;
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;
@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) {
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);
}
rs.close();
- } catch (SQLException e) {
- e.printStackTrace();
} finally {
- try {
- if (rs != null) {
- rs.close();
- }
- } catch (SQLException e) {
- e.printStackTrace();
+ if (rs != null) {
+ rs.close();
}
}
}
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;
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);
}
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;
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;
@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) {
}
}
- 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");
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;
}
}
- 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) {
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);
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.");
}
}
* 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("")) {
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);
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;
assertThat(u2, is(not(sameInstance(u))));
assertBehavesTtpGroup(u2);
- ResultSet rs = fetchGroupRowsFor(u);
+ GigiResultSet rs = fetchGroupRowsFor(u);
assertTrue(rs.next());
assertEquals(0, rs.getInt("revokedby"));
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"));
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;
}
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;
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;
static {
System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
}
+
/**
* Some password that fullfills the password criteria.
*/
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);
}
throw new Error(e);
} catch (IOException e) {
throw new Error(e);
- } catch (SQLException e) {
- throw new Error(e);
}
}
*/
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;
}
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;
}
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;
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;
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);
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;
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;
}
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);
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"));
}
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);
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (InterruptedException e1) {