package org.cacert.gigi.dbObjects;
import java.io.IOException;
+import java.io.InputStream;
import java.net.IDN;
import java.util.Arrays;
import java.util.Collections;
import org.cacert.gigi.dbObjects.DomainPingConfiguration.PingType;
import org.cacert.gigi.util.PublicSuffixes;
-public class Domain implements IdCachable {
+public class Domain implements IdCachable, Verifyable {
public class DomainPingExecution {
private int id;
private static final Set<String> IDNEnabledTLDs;
+
static {
Properties CPS = new Properties();
- try {
- CPS.load(Domain.class.getResourceAsStream("CPS.properties"));
+ try (InputStream resourceAsStream = Domain.class.getResourceAsStream("CPS.properties")) {
+ CPS.load(resourceAsStream);
IDNEnabledTLDs = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(CPS.getProperty("IDN-enabled").split(","))));
} catch (IOException e) {
throw new Error(e);
}
private Domain(int id) {
- GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT memid, domain FROM `domains` WHERE id=? AND deleted IS NULL");
+ GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `memid`, `domain` FROM `domains` WHERE `id`=? AND `deleted` IS NULL");
ps.setInt(1, id);
GigiResultSet rs = ps.executeQuery();
}
public Domain(User owner, String suffix) throws GigiApiException {
- checkCertifyableDomain(suffix, owner.isInGroup(Group.getByString("codesign")));
+ checkCertifyableDomain(suffix, owner.isInGroup(Group.CODESIGNING));
this.owner = owner;
this.suffix = suffix;
}
private static void checkInsert(String suffix) throws GigiApiException {
- GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `domains` WHERE (domain=? OR (CONCAT('.', domain)=RIGHT(?,LENGTH(domain)+1) OR RIGHT(domain,LENGTH(?)+1)=CONCAT('.',?))) AND deleted IS NULL");
+ GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `domains` WHERE (`domain`=? OR (CONCAT('.', `domain`)=RIGHT(?,LENGTH(`domain`)+1) OR RIGHT(`domain`,LENGTH(?)+1)=CONCAT('.',?))) AND `deleted` IS NULL");
ps.setString(1, suffix);
ps.setString(2, suffix);
ps.setString(3, suffix);
}
public void insert() throws GigiApiException {
- if (id != 0) {
- throw new GigiApiException("already inserted.");
- }
synchronized (Domain.class) {
+ if (id != 0) {
+ throw new GigiApiException("already inserted.");
+ }
checkInsert(suffix);
GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `domains` SET memid=?, domain=?");
ps.setInt(1, owner.getId());
if (id == 0) {
throw new GigiApiException("not inserted.");
}
- GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE `domains` SET deleted=CURRENT_TIMESTAMP WHERE id=?");
+ GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE `domains` SET `deleted`=CURRENT_TIMESTAMP WHERE `id`=?");
ps.setInt(1, id);
ps.execute();
}
}
public void addPing(PingType type, String config) throws GigiApiException {
- GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO pingconfig SET domainid=?, type=?, info=?");
+ GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `pingconfig` SET `domainid`=?, `type`=?::`pingType`, `info`=?");
ps.setInt(1, id);
ps.setString(2, type.toString().toLowerCase());
ps.setString(3, config);
configs = null;
}
- public void verify(String hash) throws GigiApiException {
- GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE domainPinglog SET state='success' WHERE challenge=? AND configId IN (SELECT id FROM pingconfig WHERE domainId=?)");
+ public synchronized void verify(String hash) throws GigiApiException {
+ 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() {
- GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM domainPinglog INNER JOIN pingconfig ON pingconfig.id=domainPinglog.configId WHERE domainid=? AND state='success'");
+ 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 {
- 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;");
+ GigiPreparedStatement ps = DatabaseConnection.getInstance().prepareScrollable("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();
}
- private static ObjectCache<Domain> myCache = new ObjectCache<>();
+ private static final ObjectCache<Domain> myCache = new ObjectCache<>();
public static synchronized Domain getById(int id) throws IllegalArgumentException {
Domain em = myCache.get(id);
return em;
}
+ public static int searchUserIdByDomain(String domain) {
+ GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `memid` FROM `domains` WHERE `domain` = ?");
+ ps.setString(1, domain);
+ GigiResultSet res = ps.executeQuery();
+ if (res.next()) {
+ return res.getInt(1);
+ } else {
+ return -1;
+ }
+ }
+
}