Search in sources :

Example 1 with OcspCertType

use of org.xipki.ca.dbtool.xmlio.ocsp.OcspCertType 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)

Example 2 with OcspCertType

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

the class OcspCertStoreDbImporter method importCert0.

// method importCert
private long importCert0(PreparedStatement psCert, String certsZipFile, long minId, File processLogFile, ProcessLog processLog, int numProcessedInLastProcess) throws Exception {
    ZipFile zipFile = new ZipFile(new File(certsZipFile));
    ZipEntry certsXmlEntry = zipFile.getEntry("certs.xml");
    OcspCertsReader certs;
    try {
        certs = new OcspCertsReader(zipFile.getInputStream(certsXmlEntry));
    } catch (Exception ex) {
        try {
            zipFile.close();
        } catch (Exception e2) {
            LOG.error("could not close ZIP file {}: {}", certsZipFile, e2.getMessage());
            LOG.debug("could not close ZIP file " + certsZipFile, e2);
        }
        throw ex;
    }
    disableAutoCommit();
    try {
        int numEntriesInBatch = 0;
        long lastSuccessfulCertId = 0;
        while (certs.hasNext()) {
            if (stopMe.get()) {
                throw new InterruptedException("interrupted by the user");
            }
            OcspCertType cert = (OcspCertType) certs.next();
            long id = cert.getId();
            if (id < minId) {
                continue;
            }
            numEntriesInBatch++;
            // cert
            try {
                int idx = 1;
                psCert.setLong(idx++, id);
                psCert.setInt(idx++, cert.getIid());
                psCert.setString(idx++, cert.getSn());
                psCert.setLong(idx++, cert.getUpdate());
                psCert.setLong(idx++, cert.getNbefore());
                psCert.setLong(idx++, cert.getNafter());
                setBoolean(psCert, idx++, cert.getRev().booleanValue());
                setInt(psCert, idx++, cert.getRr());
                setLong(psCert, idx++, cert.getRt());
                setLong(psCert, idx++, cert.getRit());
                psCert.setString(idx++, cert.getProfile());
                psCert.setString(idx++, cert.getHash());
                psCert.setString(idx++, cert.getSubject());
                psCert.addBatch();
            } catch (SQLException ex) {
                throw translate(SQL_ADD_CERT, ex);
            }
            boolean isLastBlock = !certs.hasNext();
            if (numEntriesInBatch > 0 && (numEntriesInBatch % this.numCertsPerCommit == 0 || isLastBlock)) {
                if (evaulateOnly) {
                    psCert.clearBatch();
                } else {
                    try {
                        psCert.executeBatch();
                        commit("(commit import cert to OCSP)");
                    } catch (Throwable th) {
                        rollback();
                        deleteCertGreatherThan(lastSuccessfulCertId, LOG);
                        if (th instanceof SQLException) {
                            throw translate(SQL_ADD_CERT, (SQLException) th);
                        } else if (th instanceof Exception) {
                            throw (Exception) th;
                        } else {
                            throw new Exception(th);
                        }
                    }
                }
                lastSuccessfulCertId = id;
                processLog.addNumProcessed(numEntriesInBatch);
                numEntriesInBatch = 0;
                echoToFile((numProcessedInLastProcess + processLog.numProcessed()) + ":" + lastSuccessfulCertId, processLogFile);
                processLog.printStatus();
            }
        }
        return lastSuccessfulCertId;
    } finally {
        recoverAutoCommit();
        zipFile.close();
    }
}
Also used : ZipFile(java.util.zip.ZipFile) SQLException(java.sql.SQLException) ZipEntry(java.util.zip.ZipEntry) OcspCertType(org.xipki.ca.dbtool.xmlio.ocsp.OcspCertType) ZipFile(java.util.zip.ZipFile) File(java.io.File) OcspCertsReader(org.xipki.ca.dbtool.xmlio.ocsp.OcspCertsReader) SQLException(java.sql.SQLException) DataAccessException(org.xipki.datasource.DataAccessException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) JAXBException(javax.xml.bind.JAXBException)

Aggregations

File (java.io.File)2 SQLException (java.sql.SQLException)2 OcspCertType (org.xipki.ca.dbtool.xmlio.ocsp.OcspCertType)2 IOException (java.io.IOException)1 CertificateException (java.security.cert.CertificateException)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 ZipEntry (java.util.zip.ZipEntry)1 ZipFile (java.util.zip.ZipFile)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 JAXBException (javax.xml.bind.JAXBException)1 OcspCertsReader (org.xipki.ca.dbtool.xmlio.ocsp.OcspCertsReader)1 OcspCertsWriter (org.xipki.ca.dbtool.xmlio.ocsp.OcspCertsWriter)1 ProcessLog (org.xipki.common.ProcessLog)1 DataAccessException (org.xipki.datasource.DataAccessException)1