1 package org.cacert.gigi.dbObjects;
3 import java.util.Collections;
4 import java.util.LinkedList;
7 import org.cacert.gigi.GigiApiException;
8 import org.cacert.gigi.database.GigiPreparedStatement;
9 import org.cacert.gigi.database.GigiResultSet;
10 import org.cacert.gigi.util.DomainAssessment;
12 public class Domain implements IdCachable, Verifyable {
14 private CertificateOwner owner;
16 private String suffix;
20 private Domain(int id) {
21 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `memid`, `domain` FROM `domains` WHERE `id`=? AND `deleted` IS NULL")) {
24 GigiResultSet rs = ps.executeQuery();
26 throw new IllegalArgumentException("Invalid domain id " + id);
29 owner = CertificateOwner.getById(rs.getInt(1));
30 suffix = rs.getString(2);
34 public Domain(User actor, CertificateOwner owner, String suffix) throws GigiApiException {
35 suffix = suffix.toLowerCase();
36 synchronized (Domain.class) {
37 DomainAssessment.checkCertifiableDomain(suffix, actor.isInGroup(Group.CODESIGNING), true);
44 private static void checkInsert(String suffix) throws GigiApiException {
45 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT 1 FROM `domains` WHERE (`domain`=? OR (CONCAT('.', `domain`)=RIGHT(?,LENGTH(`domain`)+1) OR RIGHT(`domain`,LENGTH(?)+1)=CONCAT('.',?))) AND `deleted` IS NULL")) {
46 ps.setString(1, suffix);
47 ps.setString(2, suffix);
48 ps.setString(3, suffix);
49 ps.setString(4, suffix);
50 GigiResultSet rs = ps.executeQuery();
51 boolean existed = rs.next();
54 throw new GigiApiException("Domain could not be inserted. Domain is already known to the system.");
59 private void insert() throws GigiApiException {
61 throw new GigiApiException("already inserted.");
64 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `domains` SET memid=?, domain=?")) {
65 ps.setInt(1, owner.getId());
66 ps.setString(2, suffix);
68 id = ps.lastInsertId();
73 public void delete() throws GigiApiException {
75 throw new GigiApiException("not inserted.");
77 try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `domains` SET `deleted`=CURRENT_TIMESTAMP WHERE `id`=?")) {
83 public CertificateOwner getOwner() {
92 public String getSuffix() {
96 private LinkedList<DomainPingConfiguration> configs = null;
98 public List<DomainPingConfiguration> getConfiguredPings() throws GigiApiException {
99 LinkedList<DomainPingConfiguration> configs = this.configs;
100 if (configs == null) {
101 configs = new LinkedList<>();
102 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT id FROM pingconfig WHERE domainid=? AND `deleted` IS NULL")) {
104 GigiResultSet rs = ps.executeQuery();
106 configs.add(DomainPingConfiguration.getById(rs.getInt(1)));
109 this.configs = configs;
112 return Collections.unmodifiableList(configs);
115 public void addPing(DomainPingType type, String config) throws GigiApiException {
116 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `pingconfig` SET `domainid`=?, `type`=?::`pingType`, `info`=?")) {
118 ps.setString(2, type.toString().toLowerCase());
119 ps.setString(3, config);
125 public void clearPings() throws GigiApiException {
126 try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `pingconfig` SET `deleted`=CURRENT_TIMESTAMP WHERE `deleted` is NULL AND `domainid`=?")) {
133 public synchronized void verify(String hash) throws GigiApiException {
134 try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `domainPinglog` SET `state`='success' WHERE `challenge`=? AND `state`='open' AND `configId` IN (SELECT `id` FROM `pingconfig` WHERE `domainid`=? AND `type`='email')")) {
135 ps.setString(1, hash);
141 public boolean isVerified() {
142 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT 1 FROM `domainPinglog` INNER JOIN `pingconfig` ON `pingconfig`.`id`=`domainPinglog`.`configId` WHERE `domainid`=? AND `state`='success'")) {
144 GigiResultSet rs = ps.executeQuery();
149 public DomainPingExecution[] getPings() throws GigiApiException {
150 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `state`, `type`, `info`, `result`, `configId`, `when` FROM `domainPinglog` INNER JOIN `pingconfig` ON `pingconfig`.`id`=`domainPinglog`.`configId` WHERE `pingconfig`.`domainid`=? ORDER BY `when` DESC;", true)) {
152 GigiResultSet rs = ps.executeQuery();
154 DomainPingExecution[] contents = new DomainPingExecution[rs.getRow()];
156 for (int i = 0; i < contents.length && rs.next(); i++) {
157 contents[i] = new DomainPingExecution(rs);
164 private static final ObjectCache<Domain> myCache = new ObjectCache<>();
166 public static synchronized Domain getById(int id) throws IllegalArgumentException {
167 Domain em = myCache.get(id);
169 myCache.put(em = new Domain(id));
174 public static Domain searchUserIdByDomain(String domain) {
175 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `id` FROM `domains` WHERE `domain` = ?")) {
176 ps.setString(1, domain);
177 GigiResultSet res = ps.executeQuery();
179 return getById(res.getInt(1));