1 package org.cacert.gigi.dbObjects;
3 import java.io.IOException;
4 import java.io.ObjectInputStream;
5 import java.io.ObjectOutputStream;
6 import java.io.ObjectStreamException;
7 import java.io.Serializable;
8 import java.util.LinkedList;
11 import org.cacert.gigi.GigiApiException;
12 import org.cacert.gigi.database.GigiPreparedStatement;
13 import org.cacert.gigi.database.GigiResultSet;
15 public abstract class CertificateOwner implements IdCachable, Serializable {
17 private static final long serialVersionUID = -672580485730247314L;
19 private static final ObjectCache<CertificateOwner> myCache = new ObjectCache<>();
23 protected CertificateOwner(int id) {
27 protected CertificateOwner() {
28 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `certOwners` DEFAULT VALUES")) {
30 id = ps.lastInsertId();
39 public static synchronized CertificateOwner getById(int id) {
40 CertificateOwner u = myCache.get(id);
42 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT *, `users`.`id` AS uid, `organisations`.`id` AS oid FROM `certOwners` LEFT JOIN `users` ON `users`.`id`=`certOwners`.`id` LEFT JOIN `organisations` ON `organisations`.`id` = `certOwners`.`id` WHERE `certOwners`.`id`=? AND `deleted` is null")) {
44 try (GigiResultSet rs = ps.executeQuery()) {
48 if (rs.getString("uid") != null) {
49 myCache.put(u = new User(rs));
50 } else if (rs.getString("oid") != null) {
51 myCache.put(u = new Organisation(rs));
53 System.err.print("Malformed cert owner: " + id);
55 } catch (GigiApiException e) {
63 public Domain[] getDomains() {
64 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `id` FROM `domains` WHERE `memid`=? AND `deleted` IS NULL")) {
65 ps.setInt(1, getId());
67 try (GigiResultSet rs = ps.executeQuery()) {
68 LinkedList<Domain> data = new LinkedList<Domain>();
71 data.add(Domain.getById(rs.getInt(1)));
74 return data.toArray(new Domain[0]);
79 public Certificate[] getCertificates(boolean includeRevoked) {
80 try (GigiPreparedStatement ps = new GigiPreparedStatement(includeRevoked ? "SELECT id FROM certs WHERE memid=?" : "SELECT id FROM certs WHERE memid=? AND revoked IS NULL")) {
81 ps.setInt(1, getId());
83 GigiResultSet rs = ps.executeQuery();
84 LinkedList<Certificate> data = new LinkedList<Certificate>();
87 data.add(Certificate.getById(rs.getInt(1)));
90 return data.toArray(new Certificate[0]);
94 public boolean isValidDomain(String domainname) {
95 for (Domain d : getDomains()) {
96 String sfx = d.getSuffix();
97 if (domainname.equals(sfx) || domainname.endsWith("." + sfx)) {
98 return d.isVerified();
105 public abstract boolean isValidEmail(String email);
107 public void delete() {
108 try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `certOwners` SET `deleted`=NOW() WHERE `id`=?")) {
109 ps.setInt(1, getId());
112 myCache.remove(this);
115 public String[] getAdminLog() {
116 try (GigiPreparedStatement prep = new GigiPreparedStatement("SELECT `when`, type, information FROM `adminLog` WHERE uid=? ORDER BY `when` ASC")) {
117 prep.setInt(1, getId());
118 GigiResultSet res = prep.executeQuery();
119 List<String> entries = new LinkedList<String>();
122 entries.add(res.getString(2) + " (" + res.getString(3) + ")");
124 return entries.toArray(new String[0]);
128 public static CertificateOwner getByEnabledSerial(String serial) {
129 try (GigiPreparedStatement prep = new GigiPreparedStatement("SELECT `memid` FROM `certs` INNER JOIN `logincerts` ON `logincerts`.`id`=`certs`.`id` WHERE serial=? AND `revoked` is NULL")) {
130 prep.setString(1, serial.toLowerCase());
131 GigiResultSet res = prep.executeQuery();
133 return getById(res.getInt(1));
139 private void writeObject(ObjectOutputStream oos) throws IOException {
140 oos.writeLong(getId());
143 private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
144 id = (int) ois.readLong();
147 protected Object readResolve() throws ObjectStreamException {
149 * Returning the Object by looking up its ID in the cache.
151 * @see http://www.javalobby.org/java/forums/t17491.html
152 * @see http://www.jguru.com/faq/view.jsp?EID=44039
153 * @see http://thecodersbreakfast.net/
154 * ?post/2011/05/12/Serialization-and-magic-methods
156 CertificateOwner co = getById(this.getId());
159 throw new Error("Unknown Certificate Owner");