Search in sources :

Example 1 with DbiXmlWriter

use of org.xipki.ca.dbtool.xmlio.DbiXmlWriter in project xipki by xipki.

the class CaCertStoreDbExporter method exportEntries.

private void exportEntries(CaDbEntryType type, CertStoreType certstore, File processLogFile, FileOutputStream filenameListOs, Long idProcessedInLastProcess) throws Exception {
    final int numEntriesPerSelect = Math.max(1, Math.round(type.getSqlBatchFactor() * numCertsPerSelect));
    final int numEntriesPerZip = Math.max(1, Math.round(type.getSqlBatchFactor() * numCertsInBundle));
    final File entriesDir = new File(baseDir, type.getDirName());
    final String tableName = type.getTableName();
    int numProcessedBefore;
    String coreSql;
    switch(type) {
        case CERT:
            numProcessedBefore = certstore.getCountCerts();
            coreSql = "ID,SN,CA_ID,PID,RID,ART,RTYPE,TID,UID,EE,LUPDATE,REV,RR,RT,RIT,FP_RS," + "REQ_SUBJECT,CERT FROM CERT INNER JOIN CRAW ON CERT.ID>=? AND CERT.ID=CRAW.CID";
            break;
        case CRL:
            numProcessedBefore = certstore.getCountCrls();
            coreSql = "ID,CA_ID,CRL FROM CRL WHERE ID>=?";
            break;
        case REQUEST:
            numProcessedBefore = certstore.getCountRequests();
            coreSql = "ID,LUPDATE,DATA FROM REQUEST WHERE ID>=?";
            break;
        case REQCERT:
            numProcessedBefore = certstore.getCountReqCerts();
            coreSql = "ID,RID,CID FROM REQCERT WHERE ID>=?";
            break;
        default:
            throw new RuntimeException("unknown CaDbEntryType " + type);
    }
    Long minId = (idProcessedInLastProcess != null) ? idProcessedInLastProcess + 1 : min(tableName, "ID");
    String tablesText = (CaDbEntryType.CERT == type) ? "tables " + tableName + " and CRAW" : "table " + type.getTableName();
    System.out.println(exportingText() + tablesText + " from ID " + minId);
    final long maxId = max(tableName, "ID");
    long total = count(tableName) - numProcessedBefore;
    if (total < 1) {
        // to avoid exception
        total = 1;
    }
    String sql = datasource.buildSelectFirstSql(numEntriesPerSelect, "ID ASC", coreSql);
    DbiXmlWriter entriesInCurrentFile = createWriter(type);
    PreparedStatement ps = prepareStatement(sql.toString());
    int numEntriesInCurrentFile = 0;
    int sum = 0;
    File currentEntriesZipFile = new File(baseDir, "tmp-" + type.getDirName() + "-" + System.currentTimeMillis() + ".zip");
    ZipOutputStream currentEntriesZip = getZipOutputStream(currentEntriesZipFile);
    long minIdOfCurrentFile = -1;
    long maxIdOfCurrentFile = -1;
    ProcessLog processLog = new ProcessLog(total);
    processLog.printHeader();
    try {
        Long id = null;
        boolean interrupted = false;
        long lastMaxId = minId - 1;
        while (true) {
            if (stopMe.get()) {
                interrupted = true;
                break;
            }
            ps.setLong(1, lastMaxId + 1);
            ResultSet rs = ps.executeQuery();
            // no entries anymore
            if (!rs.next()) {
                break;
            }
            do {
                id = rs.getLong("ID");
                if (lastMaxId < id) {
                    lastMaxId = id;
                }
                if (minIdOfCurrentFile == -1) {
                    minIdOfCurrentFile = id;
                } else if (minIdOfCurrentFile > id) {
                    minIdOfCurrentFile = id;
                }
                if (maxIdOfCurrentFile == -1) {
                    maxIdOfCurrentFile = id;
                } else if (maxIdOfCurrentFile < id) {
                    maxIdOfCurrentFile = id;
                }
                if (CaDbEntryType.CERT == type) {
                    String b64Cert = rs.getString("CERT");
                    byte[] certBytes = Base64.decodeFast(b64Cert);
                    String sha1 = HashAlgo.SHA1.hexHash(certBytes);
                    String certFileName = sha1 + ".der";
                    if (!evaulateOnly) {
                        ZipEntry certZipEntry = new ZipEntry(certFileName);
                        currentEntriesZip.putNextEntry(certZipEntry);
                        try {
                            currentEntriesZip.write(certBytes);
                        } finally {
                            currentEntriesZip.closeEntry();
                        }
                    }
                    CertType cert = new CertType();
                    cert.setId(id);
                    cert.setArt(rs.getInt("ART"));
                    cert.setCaId(rs.getInt("CA_ID"));
                    cert.setEe(rs.getBoolean("EE"));
                    cert.setFile(certFileName);
                    long fpReqSubject = rs.getLong("FP_RS");
                    if (fpReqSubject != 0) {
                        cert.setFpRs(fpReqSubject);
                        cert.setRs(rs.getString("REQ_SUBJECT"));
                    }
                    cert.setPid(rs.getInt("PID"));
                    cert.setReqType(rs.getInt("RTYPE"));
                    cert.setRid(rs.getInt("RID"));
                    cert.setSn(rs.getString("SN"));
                    String str = rs.getString("TID");
                    if (StringUtil.isNotBlank(str)) {
                        cert.setTid(str);
                    }
                    int userId = rs.getInt("UID");
                    if (userId != 0) {
                        cert.setUid(userId);
                    }
                    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 revInvTime = rs.getLong("RIT");
                        if (revInvTime != 0) {
                            cert.setRit(revInvTime);
                        }
                    }
                    ((CertsWriter) entriesInCurrentFile).add(cert);
                } else if (CaDbEntryType.CRL == type) {
                    String b64Crl = rs.getString("CRL");
                    byte[] crlBytes = Base64.decodeFast(b64Crl);
                    X509CRL x509Crl = null;
                    try {
                        x509Crl = X509Util.parseCrl(crlBytes);
                    } catch (CRLException ex) {
                        LogUtil.error(LOG, ex, "could not parse CRL with id " + id);
                        throw ex;
                    } catch (Exception ex) {
                        LogUtil.error(LOG, ex, "could not parse CRL with id " + id);
                        throw new CRLException(ex.getMessage(), ex);
                    }
                    byte[] octetString = x509Crl.getExtensionValue(Extension.cRLNumber.getId());
                    if (octetString == null) {
                        LOG.warn("CRL without CRL number, ignore it");
                        continue;
                    }
                    String sha1 = HashAlgo.SHA1.hexHash(crlBytes);
                    final String crlFilename = sha1 + ".crl";
                    if (!evaulateOnly) {
                        ZipEntry certZipEntry = new ZipEntry(crlFilename);
                        currentEntriesZip.putNextEntry(certZipEntry);
                        try {
                            currentEntriesZip.write(crlBytes);
                        } finally {
                            currentEntriesZip.closeEntry();
                        }
                    }
                    CrlType crl = new CrlType();
                    crl.setId(id);
                    crl.setCaId(rs.getInt("CA_ID"));
                    byte[] extnValue = DEROctetString.getInstance(octetString).getOctets();
                    BigInteger crlNumber = ASN1Integer.getInstance(extnValue).getPositiveValue();
                    crl.setCrlNo(crlNumber.toString());
                    crl.setFile(crlFilename);
                    ((CrlsWriter) entriesInCurrentFile).add(crl);
                } else if (CaDbEntryType.REQUEST == type) {
                    long update = rs.getLong("LUPDATE");
                    String b64Data = rs.getString("DATA");
                    byte[] dataBytes = Base64.decodeFast(b64Data);
                    String sha1 = HashAlgo.SHA1.hexHash(dataBytes);
                    final String dataFilename = sha1 + ".req";
                    if (!evaulateOnly) {
                        ZipEntry certZipEntry = new ZipEntry(dataFilename);
                        currentEntriesZip.putNextEntry(certZipEntry);
                        try {
                            currentEntriesZip.write(dataBytes);
                        } finally {
                            currentEntriesZip.closeEntry();
                        }
                    }
                    RequestType entry = new RequestType();
                    entry.setId(id);
                    entry.setUpdate(update);
                    entry.setFile(dataFilename);
                    ((RequestsWriter) entriesInCurrentFile).add(entry);
                } else if (CaDbEntryType.REQCERT == type) {
                    long cid = rs.getLong("CID");
                    long rid = rs.getLong("RID");
                    RequestCertType entry = new RequestCertType();
                    entry.setId(id);
                    entry.setCid(cid);
                    entry.setRid(rid);
                    ((RequestCertsWriter) entriesInCurrentFile).add(entry);
                } else {
                    throw new RuntimeException("unknown CaDbEntryType " + type);
                }
                numEntriesInCurrentFile++;
                sum++;
                if (numEntriesInCurrentFile == numEntriesPerZip) {
                    String currentEntriesFilename = buildFilename(type.getDirName() + "_", ".zip", minIdOfCurrentFile, maxIdOfCurrentFile, maxId);
                    finalizeZip(currentEntriesZip, "overview.xml", entriesInCurrentFile);
                    currentEntriesZipFile.renameTo(new File(entriesDir, currentEntriesFilename));
                    writeLine(filenameListOs, currentEntriesFilename);
                    setCount(type, certstore, numProcessedBefore + sum);
                    echoToFile(tableName + ":" + Long.toString(id), processLogFile);
                    processLog.addNumProcessed(numEntriesInCurrentFile);
                    processLog.printStatus();
                    // reset
                    entriesInCurrentFile = createWriter(type);
                    numEntriesInCurrentFile = 0;
                    minIdOfCurrentFile = -1;
                    maxIdOfCurrentFile = -1;
                    currentEntriesZipFile = new File(baseDir, "tmp-" + type.getDirName() + "-" + System.currentTimeMillis() + ".zip");
                    currentEntriesZip = getZipOutputStream(currentEntriesZipFile);
                }
            } while (rs.next());
            rs.close();
        }
        if (interrupted) {
            currentEntriesZip.close();
            throw new InterruptedException("interrupted by the user");
        }
        if (numEntriesInCurrentFile > 0) {
            finalizeZip(currentEntriesZip, "overview.xml", entriesInCurrentFile);
            String currentEntriesFilename = buildFilename(type.getDirName() + "_", ".zip", minIdOfCurrentFile, maxIdOfCurrentFile, maxId);
            currentEntriesZipFile.renameTo(new File(entriesDir, currentEntriesFilename));
            writeLine(filenameListOs, currentEntriesFilename);
            setCount(type, certstore, numProcessedBefore + sum);
            if (id != null) {
                echoToFile(Long.toString(id), processLogFile);
            }
            processLog.addNumProcessed(numEntriesInCurrentFile);
        } else {
            currentEntriesZip.close();
            currentEntriesZipFile.delete();
        }
    } catch (SQLException ex) {
        throw translate(null, ex);
    } finally {
        releaseResources(ps, null);
    }
    // end try
    processLog.printTrailer();
    // all successful, delete the processLogFile
    processLogFile.delete();
    System.out.println(exportedText() + sum + " entries from " + tablesText);
}
Also used : X509CRL(java.security.cert.X509CRL) SQLException(java.sql.SQLException) ZipEntry(java.util.zip.ZipEntry) RequestCertType(org.xipki.ca.dbtool.xmlio.ca.RequestCertType) CertType(org.xipki.ca.dbtool.xmlio.ca.CertType) DEROctetString(org.bouncycastle.asn1.DEROctetString) ProcessLog(org.xipki.common.ProcessLog) DbiXmlWriter(org.xipki.ca.dbtool.xmlio.DbiXmlWriter) ResultSet(java.sql.ResultSet) CRLException(java.security.cert.CRLException) PreparedStatement(java.sql.PreparedStatement) RequestCertType(org.xipki.ca.dbtool.xmlio.ca.RequestCertType) XMLStreamException(javax.xml.stream.XMLStreamException) DataAccessException(org.xipki.datasource.DataAccessException) JAXBException(javax.xml.bind.JAXBException) CRLException(java.security.cert.CRLException) InvalidInputException(org.xipki.dbtool.InvalidInputException) SQLException(java.sql.SQLException) IOException(java.io.IOException) CertsWriter(org.xipki.ca.dbtool.xmlio.ca.CertsWriter) RequestCertsWriter(org.xipki.ca.dbtool.xmlio.ca.RequestCertsWriter) RequestsWriter(org.xipki.ca.dbtool.xmlio.ca.RequestsWriter) CrlType(org.xipki.ca.dbtool.xmlio.ca.CrlType) ZipOutputStream(java.util.zip.ZipOutputStream) BigInteger(java.math.BigInteger) File(java.io.File) RequestType(org.xipki.ca.dbtool.xmlio.ca.RequestType)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 BigInteger (java.math.BigInteger)1 CRLException (java.security.cert.CRLException)1 X509CRL (java.security.cert.X509CRL)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ZipEntry (java.util.zip.ZipEntry)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 JAXBException (javax.xml.bind.JAXBException)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 DEROctetString (org.bouncycastle.asn1.DEROctetString)1 DbiXmlWriter (org.xipki.ca.dbtool.xmlio.DbiXmlWriter)1 CertType (org.xipki.ca.dbtool.xmlio.ca.CertType)1 CertsWriter (org.xipki.ca.dbtool.xmlio.ca.CertsWriter)1 CrlType (org.xipki.ca.dbtool.xmlio.ca.CrlType)1 RequestCertType (org.xipki.ca.dbtool.xmlio.ca.RequestCertType)1 RequestCertsWriter (org.xipki.ca.dbtool.xmlio.ca.RequestCertsWriter)1 RequestType (org.xipki.ca.dbtool.xmlio.ca.RequestType)1