Search in sources :

Example 1 with DbPortFileNameIterator

use of org.xipki.ca.dbtool.port.DbPortFileNameIterator in project xipki by xipki.

the class OcspCertStoreFromCaDbImporter method importCert.

// method importIssuer0
private void importCert(CertStoreType certstore, Map<Integer, String> profileMap, boolean revokedOnly, List<Integer> caIds, File processLogFile) throws Exception {
    HashAlgo certhashAlgo = getCertHashAlgo(datasource);
    int numProcessedBefore = 0;
    long minId = 1;
    if (processLogFile.exists()) {
        byte[] content = IoUtil.read(processLogFile);
        if (content != null && content.length > 2) {
            String str = new String(content);
            if (str.trim().equalsIgnoreCase(MSG_CERTS_FINISHED)) {
                return;
            }
            StringTokenizer st = new StringTokenizer(str, ":");
            numProcessedBefore = Integer.parseInt(st.nextToken());
            minId = Long.parseLong(st.nextToken());
            minId++;
        }
    }
    deleteCertGreatherThan(minId - 1, LOG);
    final long total = certstore.getCountCerts() - numProcessedBefore;
    final ProcessLog processLog = new ProcessLog(total);
    // all initial values for importLog will be not evaluated, so just any number
    final ProcessLog importLog = new ProcessLog(total);
    System.out.println(importingText() + "certificates from ID " + minId);
    processLog.printHeader();
    PreparedStatement psCert = prepareStatement(SQL_ADD_CERT);
    CaDbEntryType type = CaDbEntryType.CERT;
    DbPortFileNameIterator certsFileIterator = new DbPortFileNameIterator(baseDir + File.separator + type.getDirName() + ".mf");
    try {
        while (certsFileIterator.hasNext()) {
            String certsFile = baseDir + File.separator + type.getDirName() + File.separator + certsFileIterator.next();
            // extract the toId from the filename
            int fromIdx = certsFile.indexOf('-');
            int toIdx = certsFile.indexOf(".zip");
            if (fromIdx != -1 && toIdx != -1) {
                try {
                    long toId = Integer.parseInt(certsFile.substring(fromIdx + 1, toIdx));
                    if (toId < minId) {
                        // try next file
                        continue;
                    }
                } catch (Exception ex) {
                    LOG.warn("invalid file name '{}', but will still be processed", certsFile);
                }
            } else {
                LOG.warn("invalid file name '{}', but will still be processed", certsFile);
            }
            try {
                long lastId = importCert0(certhashAlgo, psCert, certsFile, profileMap, revokedOnly, caIds, minId, processLogFile, processLog, numProcessedBefore, importLog);
                minId = lastId + 1;
            } catch (Exception ex) {
                System.err.println("\ncould not import certificates from file " + certsFile + ".\nplease continue with the option '--resume'");
                LOG.error("Exception", ex);
                throw ex;
            }
        }
    } finally {
        releaseResources(psCert, null);
        certsFileIterator.close();
    }
    processLog.printTrailer();
    DbPorter.echoToFile(MSG_CERTS_FINISHED, processLogFile);
    System.out.println("processed " + processLog.numProcessed() + " and " + importedText() + importLog.numProcessed() + " certificates");
}
Also used : StringTokenizer(java.util.StringTokenizer) HashAlgo(org.xipki.security.HashAlgo) DbPortFileNameIterator(org.xipki.ca.dbtool.port.DbPortFileNameIterator) PreparedStatement(java.sql.PreparedStatement) ProcessLog(org.xipki.common.ProcessLog) InvalidInputException(org.xipki.dbtool.InvalidInputException) SQLException(java.sql.SQLException) DataAccessException(org.xipki.datasource.DataAccessException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) JAXBException(javax.xml.bind.JAXBException)

Example 2 with DbPortFileNameIterator

use of org.xipki.ca.dbtool.port.DbPortFileNameIterator in project xipki by xipki.

the class OcspCertStoreDbImporter method importCert.

// method importIssuer0
private void importCert(CertStoreType certstore, File processLogFile) throws Exception {
    int numProcessedBefore = 0;
    long minId = 1;
    if (processLogFile.exists()) {
        byte[] content = IoUtil.read(processLogFile);
        if (content != null && content.length > 2) {
            String str = new String(content);
            if (str.trim().equalsIgnoreCase(MSG_CERTS_FINISHED)) {
                return;
            }
            StringTokenizer st = new StringTokenizer(str, ":");
            numProcessedBefore = Integer.parseInt(st.nextToken());
            minId = Long.parseLong(st.nextToken());
            minId++;
        }
    }
    deleteCertGreatherThan(minId - 1, LOG);
    final long total = certstore.getCountCerts() - numProcessedBefore;
    final ProcessLog processLog = new ProcessLog(total);
    System.out.println(importingText() + "certificates from ID " + minId);
    processLog.printHeader();
    PreparedStatement psCert = prepareStatement(SQL_ADD_CERT);
    OcspDbEntryType type = OcspDbEntryType.CERT;
    DbPortFileNameIterator certsFileIterator = new DbPortFileNameIterator(baseDir + File.separator + type.getDirName() + ".mf");
    try {
        while (certsFileIterator.hasNext()) {
            String certsFile = baseDir + File.separator + type.getDirName() + File.separator + certsFileIterator.next();
            // extract the toId from the filename
            int fromIdx = certsFile.indexOf('-');
            int toIdx = certsFile.indexOf(".zip");
            if (fromIdx != -1 && toIdx != -1) {
                try {
                    long toId = Long.parseLong(certsFile.substring(fromIdx + 1, toIdx));
                    if (toId < minId) {
                        // try next file
                        continue;
                    }
                } catch (Exception ex) {
                    LOG.warn("invalid file name '{}', but will still be processed", certsFile);
                }
            } else {
                LOG.warn("invalid file name '{}', but will still be processed", certsFile);
            }
            try {
                long lastId = importCert0(psCert, certsFile, minId, processLogFile, processLog, numProcessedBefore);
                minId = lastId + 1;
            } catch (Exception ex) {
                System.err.println("\ncould not import certificates from file " + certsFile + ".\nplease continue with the option '--resume'");
                LOG.error("Exception", ex);
                throw ex;
            }
        }
    // end for
    } finally {
        releaseResources(psCert, null);
        certsFileIterator.close();
    }
    processLog.printTrailer();
    echoToFile(MSG_CERTS_FINISHED, processLogFile);
    System.out.println(importedText() + processLog.numProcessed() + " certificates");
}
Also used : StringTokenizer(java.util.StringTokenizer) DbPortFileNameIterator(org.xipki.ca.dbtool.port.DbPortFileNameIterator) PreparedStatement(java.sql.PreparedStatement) ProcessLog(org.xipki.common.ProcessLog) SQLException(java.sql.SQLException) DataAccessException(org.xipki.datasource.DataAccessException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) JAXBException(javax.xml.bind.JAXBException)

Example 3 with DbPortFileNameIterator

use of org.xipki.ca.dbtool.port.DbPortFileNameIterator in project xipki by xipki.

the class CaCertStoreDbImporter method importEntries.

// method importDeltaCRLCache
private Exception importEntries(CaDbEntryType type, CertStoreType certstore, File processLogFile, Integer numProcessedInLastProcess, Long idProcessedInLastProcess) {
    String tablesText = (CaDbEntryType.CERT == type) ? "tables CERT and CRAW" : "table " + type.getTableName();
    try {
        int numProcessedBefore = 0;
        long minId = 1;
        if (idProcessedInLastProcess != null) {
            minId = idProcessedInLastProcess + 1;
            numProcessedBefore = numProcessedInLastProcess;
        }
        deleteFromTableWithLargerId(type.getTableName(), "ID", minId - 1, LOG);
        if (type == CaDbEntryType.CERT) {
            deleteFromTableWithLargerId("CRAW", "CID", minId - 1, LOG);
        }
        final long total;
        String[] sqls;
        switch(type) {
            case CERT:
                total = certstore.getCountCerts();
                sqls = new String[] { SQL_ADD_CERT, SQL_ADD_CRAW };
                break;
            case CRL:
                total = certstore.getCountCrls();
                sqls = new String[] { SQL_ADD_CRL };
                break;
            case REQUEST:
                total = certstore.getCountRequests();
                sqls = new String[] { SQL_ADD_REQUEST };
                break;
            case REQCERT:
                total = certstore.getCountReqCerts();
                sqls = new String[] { SQL_ADD_REQCERT };
                break;
            default:
                throw new RuntimeException("unsupported DbEntryType " + type);
        }
        final long remainingTotal = total - numProcessedBefore;
        final ProcessLog processLog = new ProcessLog(remainingTotal);
        System.out.println(importingText() + "entries to " + tablesText + " from ID " + minId);
        processLog.printHeader();
        DbPortFileNameIterator entriesFileIterator = null;
        PreparedStatement[] statements = null;
        try {
            entriesFileIterator = new DbPortFileNameIterator(baseDir + File.separator + type.getDirName() + ".mf");
            statements = new PreparedStatement[sqls.length];
            for (int i = 0; i < sqls.length; i++) {
                statements[i] = prepareStatement(sqls[i]);
            }
            while (entriesFileIterator.hasNext()) {
                String entriesFile = baseDir + File.separator + type.getDirName() + File.separator + entriesFileIterator.next();
                // extract the toId from the filename
                int fromIdx = entriesFile.indexOf('-');
                int toIdx = entriesFile.indexOf(".zip");
                if (fromIdx != -1 && toIdx != -1) {
                    try {
                        long toId = Integer.parseInt(entriesFile.substring(fromIdx + 1, toIdx));
                        if (toId < minId) {
                            // try next file
                            continue;
                        }
                    } catch (Exception ex) {
                        LOG.warn("invalid file name '{}', but will still be processed", entriesFile);
                    }
                } else {
                    LOG.warn("invalid file name '{}', but will still be processed", entriesFile);
                }
                try {
                    long lastId = importEntries(type, entriesFile, minId, processLogFile, processLog, numProcessedBefore, statements, sqls);
                    minId = lastId + 1;
                } catch (Exception ex) {
                    System.err.println("\ncould not import entries from file " + entriesFile + ".\nplease continue with the option '--resume'");
                    LOG.error("Exception", ex);
                    return ex;
                }
            }
        // end for
        } finally {
            if (statements != null) {
                for (PreparedStatement stmt : statements) {
                    if (stmt != null) {
                        releaseResources(stmt, null);
                    }
                }
            }
            if (entriesFileIterator != null) {
                entriesFileIterator.close();
            }
        }
        processLog.printTrailer();
        echoToFile(type + ":" + (numProcessedBefore + processLog.numProcessed()) + ":-1", processLogFile);
        System.out.println(importedText() + processLog.numProcessed() + " entries");
        return null;
    } catch (Exception ex) {
        System.err.println("\nimporting " + tablesText + " has been cancelled due to error,\n" + "please continue with the option '--resume'");
        LOG.error("Exception", ex);
        return ex;
    }
}
Also used : DbPortFileNameIterator(org.xipki.ca.dbtool.port.DbPortFileNameIterator) PreparedStatement(java.sql.PreparedStatement) DEROctetString(org.bouncycastle.asn1.DEROctetString) ProcessLog(org.xipki.common.ProcessLog) XMLStreamException(javax.xml.stream.XMLStreamException) DataAccessException(org.xipki.datasource.DataAccessException) JAXBException(javax.xml.bind.JAXBException) InvalidDataObjectException(org.xipki.ca.dbtool.xmlio.InvalidDataObjectException) CRLException(java.security.cert.CRLException) SQLException(java.sql.SQLException) CertificateException(java.security.cert.CertificateException)

Aggregations

CertificateException (java.security.cert.CertificateException)3 PreparedStatement (java.sql.PreparedStatement)3 SQLException (java.sql.SQLException)3 JAXBException (javax.xml.bind.JAXBException)3 DbPortFileNameIterator (org.xipki.ca.dbtool.port.DbPortFileNameIterator)3 ProcessLog (org.xipki.common.ProcessLog)3 DataAccessException (org.xipki.datasource.DataAccessException)3 IOException (java.io.IOException)2 StringTokenizer (java.util.StringTokenizer)2 CRLException (java.security.cert.CRLException)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 DEROctetString (org.bouncycastle.asn1.DEROctetString)1 InvalidDataObjectException (org.xipki.ca.dbtool.xmlio.InvalidDataObjectException)1 InvalidInputException (org.xipki.dbtool.InvalidInputException)1 HashAlgo (org.xipki.security.HashAlgo)1