]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/dbObjects/Certificate.java
upd: cleanup in Certificate class
[gigi.git] / src / org / cacert / gigi / dbObjects / Certificate.java
index 7dabe8cc56087cb52ea5e483720190b06db509a1..f15a299bd9c065cfb80ad6537cf90b47cd22f3fb 100644 (file)
@@ -113,7 +113,7 @@ public class Certificate implements IdCachable {
 
     private String serial;
 
-    private String md;
+    private Digest md;
 
     private String csrName;
 
@@ -133,7 +133,7 @@ public class Certificate implements IdCachable {
 
     private CACertificate ca;
 
-    public Certificate(CertificateOwner owner, User actor, HashMap<String, String> dn, String md, String csr, CSRType csrType, CertificateProfile profile, SubjectAlternateName... sans) throws GigiApiException, IOException {
+    public Certificate(CertificateOwner owner, User actor, HashMap<String, String> dn, Digest md, String csr, CSRType csrType, CertificateProfile profile, SubjectAlternateName... sans) throws GigiApiException, IOException {
         if ( !profile.canBeIssuedBy(owner, actor)) {
             throw new GigiApiException("You are not allowed to issue these certificates.");
         }
@@ -151,7 +151,7 @@ public class Certificate implements IdCachable {
         synchronized (Certificate.class) {
 
             GigiPreparedStatement inserter = DatabaseConnection.getInstance().prepare("INSERT INTO certs SET md=?::`mdType`, csr_type=?::`csrType`, crt_name='', memid=?, profile=?");
-            inserter.setString(1, md.toLowerCase());
+            inserter.setString(1, md.toString().toLowerCase());
             inserter.setString(2, csrType.toString());
             inserter.setInt(3, owner.getId());
             inserter.setInt(4, profile.getId());
@@ -191,7 +191,7 @@ public class Certificate implements IdCachable {
     private Certificate(GigiResultSet rs) {
         this.id = rs.getInt("id");
         dnString = rs.getString("subject");
-        md = rs.getString("md");
+        md = Digest.valueOf(rs.getString("md").toUpperCase());
         csrName = rs.getString("csr_name");
         crtName = rs.getString("crt_name");
         owner = CertificateOwner.getById(rs.getInt("memid"));
@@ -334,7 +334,7 @@ public class Certificate implements IdCachable {
         return dnString;
     }
 
-    public String getMessageDigest() {
+    public Digest getMessageDigest() {
         return md;
     }
 
@@ -354,26 +354,21 @@ public class Certificate implements IdCachable {
         if (serial == null || "".equals(serial)) {
             return null;
         }
-        try {
-            String concat = "string_agg(concat('/', `name`, '=', REPLACE(REPLACE(value, '\\\\', '\\\\\\\\'), '/', '\\\\/')), '')";
-            GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT certs.id, " + concat + " as `subject`, `md`, `csr_name`, `crt_name`,`memid`, `profile`, `certs`.`serial` FROM `certs` LEFT JOIN `certAvas` ON `certAvas`.`certId`=`certs`.`id` WHERE `serial`=? GROUP BY `certs`.`id`");
-            ps.setString(1, serial);
-            GigiResultSet rs = ps.executeQuery();
-            if ( !rs.next()) {
-                return null;
-            }
-            int id = rs.getInt(1);
-            Certificate c1 = cache.get(id);
-            if (c1 != null) {
-                return c1;
-            }
-            Certificate certificate = new Certificate(rs);
-            cache.put(certificate);
-            return certificate;
-        } catch (IllegalArgumentException e) {
-
+        String concat = "string_agg(concat('/', `name`, '=', REPLACE(REPLACE(value, '\\\\', '\\\\\\\\'), '/', '\\\\/')), '')";
+        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT certs.id, " + concat + " as `subject`, `md`, `csr_name`, `crt_name`,`memid`, `profile`, `certs`.`serial` FROM `certs` LEFT JOIN `certAvas` ON `certAvas`.`certId`=`certs`.`id` WHERE `serial`=? GROUP BY `certs`.`id`");
+        ps.setString(1, serial);
+        GigiResultSet rs = ps.executeQuery();
+        if ( !rs.next()) {
+            return null;
         }
-        return null;
+        int id = rs.getInt(1);
+        Certificate c1 = cache.get(id);
+        if (c1 != null) {
+            return c1;
+        }
+        Certificate certificate = new Certificate(rs);
+        cache.put(certificate);
+        return certificate;
     }
 
     private static ObjectCache<Certificate> cache = new ObjectCache<>();