1 package org.cacert.gigi;
3 import java.sql.PreparedStatement;
4 import java.sql.ResultSet;
5 import java.sql.SQLException;
7 import org.cacert.gigi.database.DatabaseConnection;
9 public class Domain implements IdCachable {
13 private String suffix;
17 public Domain(int id) throws SQLException {
18 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT memid, domain FROM `domains` WHERE id=? AND deleted IS NULL");
21 ResultSet rs = ps.executeQuery();
23 throw new IllegalArgumentException("Invalid email id " + id);
26 owner = User.getById(rs.getInt(1));
27 suffix = rs.getString(2);
31 public Domain(User owner, String suffix) throws GigiApiException {
37 private static void checkInsert(String suffix) throws GigiApiException {
39 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `domains` WHERE (domain=RIGHT(?,LENGTH(domain)) OR RIGHT(domain,LENGTH(?))=?) AND deleted IS NULL");
40 ps.setString(1, suffix);
41 ps.setString(2, suffix);
42 ps.setString(3, suffix);
43 ResultSet rs = ps.executeQuery();
44 boolean existed = rs.next();
47 throw new GigiApiException("Domain could not be inserted. Domain is already valid.");
49 } catch (SQLException e) {
50 throw new GigiApiException(e);
54 public void insert() throws GigiApiException {
56 throw new GigiApiException("already inserted.");
58 synchronized (Domain.class) {
61 PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `domains` SET memid=?, domain=?");
62 ps.setInt(1, owner.getId());
63 ps.setString(2, suffix);
65 id = DatabaseConnection.lastInsertId(ps);
67 } catch (SQLException e) {
68 throw new GigiApiException(e);
73 public void delete() throws GigiApiException {
75 throw new GigiApiException("not inserted.");
78 PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE `domains` SET deleted=CURRENT_TIMESTAMP WHERE id=?");
81 } catch (SQLException e) {
82 throw new GigiApiException(e);
86 public User getOwner() {
94 public String getSuffix() {
98 public void addPing(String type, String config) throws GigiApiException {
100 PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO pingconfig SET domainid=?, type=?, info=?");
102 ps.setString(2, type);
103 ps.setString(3, config);
105 } catch (SQLException e) {
106 throw new GigiApiException(e);
110 public void verify(String hash) throws GigiApiException {
112 PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE domainPinglog SET state='success' WHERE challenge=? AND configId IN (SELECT id FROM pingconfig WHERE domainId=?)");
113 ps.setString(1, hash);
116 } catch (SQLException e) {
117 throw new GigiApiException(e);
121 public boolean isVerified() {
123 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM domainPinglog INNER JOIN pingconfig ON pingconfig.id=domainPinglog.configId WHERE domainid=? AND state='success'");
125 ResultSet rs = ps.executeQuery();
127 } catch (SQLException e) {
133 public String[][] getPings() throws GigiApiException {
135 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT state, type, info, result FROM domainPinglog INNER JOIN pingconfig ON pingconfig.id=domainPinglog.configid WHERE pingconfig.domainid=? ORDER BY `when` DESC;");
137 ResultSet rs = ps.executeQuery();
139 String[][] contents = new String[rs.getRow()][];
141 for (int i = 0; i < contents.length && rs.next(); i++) {
142 contents[i] = new String[] {
143 rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4)
147 } catch (SQLException e) {
148 throw new GigiApiException(e);
153 private static ObjectCache<Domain> myCache = new ObjectCache<>();
155 public static Domain getById(int id) throws IllegalArgumentException {
156 Domain em = myCache.get(id);
159 synchronized (Domain.class) {
160 myCache.put(em = new Domain(id));
162 } catch (SQLException e1) {
163 throw new IllegalArgumentException(e1);