Search in sources :

Example 1 with OcspCertsWriter

use of org.xipki.ca.dbtool.xmlio.ocsp.OcspCertsWriter in project xipki by xipki.

the class OcspCertStoreDbExporter method exportCert0.

// method exportCert
private void exportCert0(CertStoreType certstore, File processLogFile, FileOutputStream certsFileOs) throws Exception {
    File certsDir = new File(baseDir, OcspDbEntryType.CERT.getDirName());
    Long minId = null;
    if (processLogFile.exists()) {
        byte[] content = IoUtil.read(processLogFile);
        if (content != null && content.length > 0) {
            minId = Long.parseLong(new String(content).trim());
            minId++;
        }
    }
    if (minId == null) {
        minId = min("CERT", "ID");
    }
    System.out.println(exportingText() + "table CERT from ID " + minId);
    final String coreSql = "ID,SN,IID,LUPDATE,REV,RR,RT,RIT,PN,NAFTER,NBEFORE,HASH,SUBJECT " + "FROM CERT WHERE ID>=?";
    final String certSql = datasource.buildSelectFirstSql(numCertsPerSelect, "ID ASC", coreSql);
    final long maxId = max("CERT", "ID");
    int numProcessedBefore = certstore.getCountCerts();
    final long total = count("CERT") - numProcessedBefore;
    ProcessLog processLog = new ProcessLog(total);
    PreparedStatement certPs = prepareStatement(certSql);
    int sum = 0;
    int numCertInCurrentFile = 0;
    OcspCertsWriter certsInCurrentFile = new OcspCertsWriter();
    File currentCertsZipFile = new File(baseDir, "tmp-certs-" + System.currentTimeMillis() + ".zip");
    ZipOutputStream currentCertsZip = getZipOutputStream(currentCertsZipFile);
    long minCertIdOfCurrentFile = -1;
    long maxCertIdOfCurrentFile = -1;
    processLog.printHeader();
    String sql = null;
    Long id = null;
    try {
        boolean interrupted = false;
        long lastMaxId = minId - 1;
        while (true) {
            if (stopMe.get()) {
                interrupted = true;
                break;
            }
            sql = certSql;
            certPs.setLong(1, lastMaxId + 1);
            ResultSet rs = certPs.executeQuery();
            if (!rs.next()) {
                break;
            }
            do {
                id = rs.getLong("ID");
                if (lastMaxId < id) {
                    lastMaxId = id;
                }
                if (minCertIdOfCurrentFile == -1) {
                    minCertIdOfCurrentFile = id;
                } else if (minCertIdOfCurrentFile > id) {
                    minCertIdOfCurrentFile = id;
                }
                if (maxCertIdOfCurrentFile == -1) {
                    maxCertIdOfCurrentFile = id;
                } else if (maxCertIdOfCurrentFile < id) {
                    maxCertIdOfCurrentFile = id;
                }
                OcspCertType cert = new OcspCertType();
                cert.setId(id);
                cert.setIid(rs.getInt("IID"));
                cert.setSn(rs.getString("SN"));
                cert.setUpdate(rs.getLong("LUPDATE"));
                boolean revoked = rs.getBoolean("REV");
                cert.setRev(revoked);
                if (revoked) {
                    cert.setRr(rs.getInt("RR"));
                    cert.setRt(rs.getLong("RT"));
                    long rit = rs.getLong("RIT");
                    if (rit != 0) {
                        cert.setRit(rit);
                    }
                }
                cert.setProfile(rs.getString("PN"));
                String hash = rs.getString("HASH");
                if (hash != null) {
                    cert.setHash(hash);
                }
                String subject = rs.getString("SUBJECT");
                if (subject != null) {
                    cert.setSubject(subject);
                }
                long nafter = rs.getLong("NAFTER");
                if (nafter != 0) {
                    cert.setNafter(nafter);
                }
                long nbefore = rs.getLong("NBEFORE");
                if (nbefore != 0) {
                    cert.setNbefore(nbefore);
                }
                certsInCurrentFile.add(cert);
                numCertInCurrentFile++;
                sum++;
                if (numCertInCurrentFile == numCertsInBundle) {
                    finalizeZip(currentCertsZip, certsInCurrentFile);
                    String currentCertsFilename = buildFilename("certs_", ".zip", minCertIdOfCurrentFile, maxCertIdOfCurrentFile, maxId);
                    currentCertsZipFile.renameTo(new File(certsDir, currentCertsFilename));
                    writeLine(certsFileOs, currentCertsFilename);
                    certstore.setCountCerts(numProcessedBefore + sum);
                    echoToFile(Long.toString(id), processLogFile);
                    processLog.addNumProcessed(numCertInCurrentFile);
                    processLog.printStatus();
                    // reset
                    certsInCurrentFile = new OcspCertsWriter();
                    numCertInCurrentFile = 0;
                    minCertIdOfCurrentFile = -1;
                    maxCertIdOfCurrentFile = -1;
                    currentCertsZipFile = new File(baseDir, "tmp-certs-" + System.currentTimeMillis() + ".zip");
                    currentCertsZip = getZipOutputStream(currentCertsZipFile);
                }
            // end if
            } while (rs.next());
            rs.close();
        }
        if (interrupted) {
            throw new InterruptedException("interrupted by the user");
        }
        if (numCertInCurrentFile > 0) {
            finalizeZip(currentCertsZip, certsInCurrentFile);
            String currentCertsFilename = buildFilename("certs_", ".zip", minCertIdOfCurrentFile, maxCertIdOfCurrentFile, maxId);
            currentCertsZipFile.renameTo(new File(certsDir, currentCertsFilename));
            writeLine(certsFileOs, currentCertsFilename);
            certstore.setCountCerts(numProcessedBefore + sum);
            if (id != null) {
                echoToFile(Long.toString(id), processLogFile);
            }
            processLog.addNumProcessed(numCertInCurrentFile);
        } else {
            currentCertsZip.close();
            currentCertsZipFile.delete();
        }
    } catch (SQLException ex) {
        throw translate(sql, ex);
    } finally {
        releaseResources(certPs, null);
    }
    processLog.printTrailer();
    // all successful, delete the processLogFile
    processLogFile.delete();
    System.out.println(exportedText() + processLog.numProcessed() + " certificates from tables CERT");
}
Also used : SQLException(java.sql.SQLException) OcspCertType(org.xipki.ca.dbtool.xmlio.ocsp.OcspCertType) PreparedStatement(java.sql.PreparedStatement) ProcessLog(org.xipki.common.ProcessLog) OcspCertsWriter(org.xipki.ca.dbtool.xmlio.ocsp.OcspCertsWriter) ZipOutputStream(java.util.zip.ZipOutputStream) ResultSet(java.sql.ResultSet) File(java.io.File)

Aggregations

File (java.io.File)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 OcspCertType (org.xipki.ca.dbtool.xmlio.ocsp.OcspCertType)1 OcspCertsWriter (org.xipki.ca.dbtool.xmlio.ocsp.OcspCertsWriter)1 ProcessLog (org.xipki.common.ProcessLog)1