1 package org.cacert.gigi.dbObjects;
3 import java.sql.PreparedStatement;
4 import java.sql.ResultSet;
5 import java.sql.SQLException;
6 import java.util.Collections;
7 import java.util.LinkedList;
10 import org.cacert.gigi.GigiApiException;
11 import org.cacert.gigi.database.DatabaseConnection;
12 import org.cacert.gigi.dbObjects.DomainPingConfiguration.PingType;
14 public class Domain implements IdCachable {
16 public class DomainPingExecution {
24 private String result;
26 private DomainPingConfiguration config;
28 public DomainPingExecution(ResultSet rs) throws SQLException {
29 state = rs.getString(1);
30 type = rs.getString(2);
31 info = rs.getString(3);
32 result = rs.getString(4);
33 config = DomainPingConfiguration.getById(rs.getInt(5));
36 public String getState() {
40 public String getType() {
44 public String getInfo() {
48 public String getResult() {
52 public DomainPingConfiguration getConfig() {
60 private String suffix;
64 private Domain(int id) throws SQLException {
65 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT memid, domain FROM `domains` WHERE id=? AND deleted IS NULL");
68 ResultSet rs = ps.executeQuery();
70 throw new IllegalArgumentException("Invalid domain id " + id);
73 owner = User.getById(rs.getInt(1));
74 suffix = rs.getString(2);
78 public Domain(User owner, String suffix) throws GigiApiException {
84 private static void checkInsert(String suffix) throws GigiApiException {
86 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `domains` WHERE (domain=RIGHT(?,LENGTH(domain)) OR RIGHT(domain,LENGTH(?))=?) AND deleted IS NULL");
87 ps.setString(1, suffix);
88 ps.setString(2, suffix);
89 ps.setString(3, suffix);
90 ResultSet rs = ps.executeQuery();
91 boolean existed = rs.next();
94 throw new GigiApiException("Domain could not be inserted. Domain is already valid.");
96 } catch (SQLException e) {
97 throw new GigiApiException(e);
101 public void insert() throws GigiApiException {
103 throw new GigiApiException("already inserted.");
105 synchronized (Domain.class) {
108 PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `domains` SET memid=?, domain=?");
109 ps.setInt(1, owner.getId());
110 ps.setString(2, suffix);
112 id = DatabaseConnection.lastInsertId(ps);
114 } catch (SQLException e) {
115 throw new GigiApiException(e);
120 public void delete() throws GigiApiException {
122 throw new GigiApiException("not inserted.");
125 PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE `domains` SET deleted=CURRENT_TIMESTAMP WHERE id=?");
128 } catch (SQLException e) {
129 throw new GigiApiException(e);
133 public User getOwner() {
142 public String getSuffix() {
146 private LinkedList<DomainPingConfiguration> configs = null;
148 public List<DomainPingConfiguration> getConfiguredPings() throws GigiApiException {
149 LinkedList<DomainPingConfiguration> configs = this.configs;
150 if (configs == null) {
152 configs = new LinkedList<>();
153 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM pingconfig WHERE domainid=?");
155 ResultSet rs = ps.executeQuery();
157 configs.add(DomainPingConfiguration.getById(rs.getInt(1)));
160 this.configs = configs;
161 } catch (SQLException e) {
162 throw new GigiApiException(e);
166 return Collections.unmodifiableList(configs);
169 public void addPing(PingType type, String config) throws GigiApiException {
171 PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO pingconfig SET domainid=?, type=?, info=?");
173 ps.setString(2, type.toString().toLowerCase());
174 ps.setString(3, config);
177 } catch (SQLException e) {
178 throw new GigiApiException(e);
182 public void verify(String hash) throws GigiApiException {
184 PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE domainPinglog SET state='success' WHERE challenge=? AND configId IN (SELECT id FROM pingconfig WHERE domainId=?)");
185 ps.setString(1, hash);
188 } catch (SQLException e) {
189 throw new GigiApiException(e);
193 public boolean isVerified() {
195 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM domainPinglog INNER JOIN pingconfig ON pingconfig.id=domainPinglog.configId WHERE domainid=? AND state='success'");
197 ResultSet rs = ps.executeQuery();
199 } catch (SQLException e) {
205 public DomainPingExecution[] getPings() throws GigiApiException {
207 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;");
209 ResultSet rs = ps.executeQuery();
211 DomainPingExecution[] contents = new DomainPingExecution[rs.getRow()];
213 for (int i = 0; i < contents.length && rs.next(); i++) {
214 contents[i] = new DomainPingExecution(rs);
217 } catch (SQLException e) {
218 throw new GigiApiException(e);
223 private static ObjectCache<Domain> myCache = new ObjectCache<>();
225 public static synchronized Domain getById(int id) throws IllegalArgumentException {
226 Domain em = myCache.get(id);
229 myCache.put(em = new Domain(id));
230 } catch (SQLException e1) {
231 throw new IllegalArgumentException(e1);