X-Git-Url: https://code.wpia.club/?p=gigi.git;a=blobdiff_plain;f=src%2Fclub%2Fwpia%2Fgigi%2FdbObjects%2FCertificate.java;h=d5679029c474d51b819049fe0e4083e419375ac3;hp=bd1e7744f7bd939185fe5516dfc0920e24e96e0c;hb=c4c60e1b9446e5ab69b8431ce71a2fbe11d47ef5;hpb=8822b2ffdda7eb4109415a7867388a321ea6b56f diff --git a/src/club/wpia/gigi/dbObjects/Certificate.java b/src/club/wpia/gigi/dbObjects/Certificate.java index bd1e7744..d5679029 100644 --- a/src/club/wpia/gigi/dbObjects/Certificate.java +++ b/src/club/wpia/gigi/dbObjects/Certificate.java @@ -47,6 +47,15 @@ public class Certificate implements IdCachable { } } + public enum AttachmentType implements DBEnum { + CSR, CRT; + + @Override + public String getDBName() { + return toString(); + } + } + public enum SANType implements DBEnum { EMAIL("email"), DNS("DNS"); @@ -553,4 +562,35 @@ public class Certificate implements IdCachable { } return certs; } + + public void addAttachment(AttachmentType tp, String data) throws GigiApiException { + if (getAttachment(tp) != null) { + throw new GigiApiException("Cannot override attachment"); + } + if (data == null) { + throw new GigiApiException("Attachment must not be null"); + } + try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `certificateAttachment` SET `certid`=?, `type`=?::`certificateAttachmentType`, `content`=?")) { + ps.setInt(1, getId()); + ps.setEnum(2, tp); + ps.setString(3, data); + ps.execute(); + } + } + + public String getAttachment(AttachmentType tp) throws GigiApiException { + try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `content` FROM `certificateAttachment` WHERE `certid`=? AND `type`=?::`certificateAttachmentType`")) { + ps.setInt(1, getId()); + ps.setEnum(2, tp); + GigiResultSet rs = ps.executeQuery(); + if ( !rs.next()) { + return null; + } + String s = rs.getString(1); + if (rs.next()) { + throw new GigiApiException("Invalid database state"); + } + return s; + } + } }