Search in sources :

Example 1 with FileSystemException

use of org.opendatakit.briefcase.model.FileSystemException in project briefcase by opendatakit.

the class BadXMLFixer method fixBadXML.

public static Document fixBadXML(File xmlFile) throws CannotFixXMLException {
    log.info("Trying to fix the submission {} ", xmlFile.getAbsolutePath());
    try {
        String originalXML = FileUtils.readFileToString(xmlFile, ENCODING);
        String fixedXML = fixXML(originalXML);
        File tempFile = File.createTempFile(xmlFile.getName(), ".fixed.xml");
        FileUtils.writeStringToFile(tempFile, fixedXML, ENCODING);
        return XmlManipulationUtils.parseXml(tempFile);
    } catch (IOException | ParsingException | FileSystemException e) {
        log.error("Cannot fix xml", e);
        throw new CannotFixXMLException("Cannot fix xml", e);
    }
}
Also used : CannotFixXMLException(org.opendatakit.briefcase.model.CannotFixXMLException) FileSystemException(org.opendatakit.briefcase.model.FileSystemException) ParsingException(org.opendatakit.briefcase.model.ParsingException) IOException(java.io.IOException) File(java.io.File)

Example 2 with FileSystemException

use of org.opendatakit.briefcase.model.FileSystemException in project briefcase by opendatakit.

the class ExportToCsv method processInstance.

private boolean processInstance(File instanceDir) {
    File submission = new File(instanceDir, "submission.xml");
    if (!submission.exists() || !submission.isFile()) {
        EventBus.publish(new ExportProgressEvent("Submission not found for instance directory: " + instanceDir.getPath(), briefcaseLfd));
        return false;
    }
    processedInstances++;
    EventBus.publish(new ExportProgressEvent("Processing instance: " + instanceDir.getName(), briefcaseLfd));
    EventBus.publish(new ExportProgressPercentageEvent((processedInstances * 100.0) / totalInstances, briefcaseLfd));
    // If we are encrypted, be sure the temporary directory
    // that will hold the unencrypted files is created.
    // If we aren't encrypted, the temporary directory
    // is the same as the instance directory.
    File unEncryptedDir;
    if (briefcaseLfd.isFileEncryptedForm()) {
        // create the temp directory that will hold the unencrypted
        // files. Do this in the outputDir so that the briefcase storage location
        // can be a read-only network mount. issue 676.
        Path path;
        try {
            path = Files.createTempDirectory(Paths.get(outputDir.toURI()), ".temp");
        } catch (IOException e) {
            String msg = "Unable to create temp directory.";
            log.error(msg, e);
            EventBus.publish(new ExportProgressEvent(msg + " Cause : " + e.toString(), briefcaseLfd));
            return false;
        }
        unEncryptedDir = path.toFile();
    } else {
        unEncryptedDir = instanceDir;
    }
    // parse the xml document (this is the manifest if encrypted)...
    Document doc;
    boolean isValidated = false;
    try {
        doc = XmlManipulationUtils.parseXml(submission);
    } catch (ParsingException | FileSystemException e) {
        log.error("Error parsing submission", e);
        EventBus.publish(new ExportProgressEvent(("Error parsing submission " + instanceDir.getName()) + " Cause: " + e.toString(), briefcaseLfd));
        return false;
    }
    String submissionDate = null;
    // extract the submissionDate, if present, from the attributes
    // of the root element of the submission or submission manifest (if encrypted).
    submissionDate = doc.getRootElement().getAttributeValue(null, "submissionDate");
    if (submissionDate == null || submissionDate.length() == 0) {
        submissionDate = null;
    } else {
        Date theDate = WebUtils.parseDate(submissionDate);
        DateFormat formatter = DateFormat.getDateTimeInstance();
        submissionDate = formatter.format(theDate);
        // just return true to skip records out of range
        if (startDate != null && theDate.before(startDate)) {
            log.info("Submission date is before specified, skipping: " + instanceDir.getName());
            return true;
        }
        if (endDate != null && theDate.after(endDate)) {
            log.info("Submission date is after specified, skipping: " + instanceDir.getName());
            return true;
        }
        // don't export records without dates if either date is set
        if ((startDate != null || endDate != null) && submissionDate == null) {
            log.info("No submission date found, skipping: " + instanceDir.getName());
            return true;
        }
    }
    // failure.
    try {
        if (briefcaseLfd.isFileEncryptedForm()) {
            // NOTE: this changes the value of 'doc'
            try {
                FileSystemUtils.DecryptOutcome outcome = FileSystemUtils.decryptAndValidateSubmission(doc, briefcaseLfd.getPrivateKey(), instanceDir, unEncryptedDir);
                doc = outcome.submission;
                isValidated = outcome.isValidated;
            } catch (ParsingException | CryptoException | FileSystemException e) {
                // Was unable to parse file or decrypt file or a file system error occurred
                // Hence skip this instance
                EventBus.publish(new ExportProgressEvent("Error decrypting submission " + instanceDir.getName() + " Cause: " + e.toString() + " skipping....", briefcaseLfd));
                log.info("Error decrypting submission " + instanceDir.getName() + " Cause: " + e.toString());
                // update total number of files skipped
                totalFilesSkipped++;
                return true;
            }
        }
        String instanceId = null;
        String base64EncryptedFieldKey = null;
        // find an instanceId to use...
        try {
            FormInstanceMetadata sim = XmlManipulationUtils.getFormInstanceMetadata(doc.getRootElement());
            instanceId = sim.instanceId;
            base64EncryptedFieldKey = sim.base64EncryptedFieldKey;
        } catch (ParsingException e) {
            log.error("Could not extract metadata from submission", e);
            EventBus.publish(new ExportProgressEvent("Could not extract metadata from submission " + submission.getAbsolutePath(), briefcaseLfd));
            return false;
        }
        if (instanceId == null || instanceId.length() == 0) {
            // if we have no instanceID, and there isn't any in the file,
            // use the checksum as the id.
            // NOTE: encrypted submissions always have instanceIDs.
            // This is for legacy non-OpenRosa forms.
            long checksum;
            try {
                checksum = FileUtils.checksumCRC32(submission);
            } catch (IOException e1) {
                String msg = "Failed during computing of crc";
                log.error(msg, e1);
                EventBus.publish(new ExportProgressEvent(msg + ": " + e1.getMessage(), briefcaseLfd));
                return false;
            }
            instanceId = "crc32:" + Long.toString(checksum);
        }
        if (terminationFuture.isCancelled()) {
            EventBus.publish(new ExportProgressEvent("Aborted", briefcaseLfd));
            return false;
        }
        EncryptionInformation ei = null;
        if (base64EncryptedFieldKey != null) {
            try {
                ei = new EncryptionInformation(base64EncryptedFieldKey, instanceId, briefcaseLfd.getPrivateKey());
            } catch (CryptoException e) {
                log.error("Error establishing field decryption", e);
                EventBus.publish(new ExportProgressEvent("Error establishing field decryption for submission " + instanceDir.getName() + " Cause: " + e.toString(), briefcaseLfd));
                return false;
            }
        }
        // emit the csv record...
        try {
            OutputStreamWriter osw = fileMap.get(briefcaseLfd.getSubmissionElement());
            emitString(osw, true, submissionDate);
            emitSubmissionCsv(osw, ei, doc.getRootElement(), briefcaseLfd.getSubmissionElement(), briefcaseLfd.getSubmissionElement(), false, instanceId, unEncryptedDir);
            emitString(osw, false, instanceId);
            if (briefcaseLfd.isFileEncryptedForm()) {
                emitString(osw, false, Boolean.toString(isValidated));
                if (!isValidated) {
                    EventBus.publish(new ExportProgressEvent("Decrypted submission " + instanceDir.getName() + " may be missing attachments and could not be validated.", briefcaseLfd));
                }
            }
            osw.append("\n");
            return true;
        } catch (IOException e) {
            String msg = "Failed writing csv";
            log.error(msg, e);
            EventBus.publish(new ExportProgressEvent(msg + ": " + e.getMessage(), briefcaseLfd));
            return false;
        }
    } finally {
        if (briefcaseLfd.isFileEncryptedForm()) {
            // destroy the temp directory and its contents...
            try {
                FileUtils.deleteDirectory(unEncryptedDir);
            } catch (IOException e) {
                String msg = "Unable to remove decrypted files";
                log.error(msg, e);
                EventBus.publish(new ExportProgressEvent(msg + ": " + e.getMessage(), briefcaseLfd));
                return false;
            }
        }
    }
}
Also used : Path(java.nio.file.Path) FormInstanceMetadata(org.opendatakit.briefcase.util.XmlManipulationUtils.FormInstanceMetadata) IOException(java.io.IOException) Document(org.kxml2.kdom.Document) ExportProgressEvent(org.opendatakit.briefcase.model.ExportProgressEvent) Date(java.util.Date) LocalDate(java.time.LocalDate) FileSystemException(org.opendatakit.briefcase.model.FileSystemException) ParsingException(org.opendatakit.briefcase.model.ParsingException) DateFormat(java.text.DateFormat) ExportProgressPercentageEvent(org.opendatakit.briefcase.model.ExportProgressPercentageEvent) OutputStreamWriter(java.io.OutputStreamWriter) CryptoException(org.opendatakit.briefcase.model.CryptoException) File(java.io.File)

Example 3 with FileSystemException

use of org.opendatakit.briefcase.model.FileSystemException in project briefcase by opendatakit.

the class FileSystemUtils method getFormDirectory.

public static File getFormDirectory(String formName) throws FileSystemException {
    // clean up friendly form name...
    String rootName = asFilesystemSafeName(formName);
    File formPath = new File(getFormsFolder(), rootName);
    if (!formPath.exists() && !formPath.mkdirs()) {
        throw new FileSystemException("unable to create directory: " + formPath.getAbsolutePath());
    }
    return formPath;
}
Also used : FileSystemException(org.opendatakit.briefcase.model.FileSystemException) File(java.io.File)

Example 4 with FileSystemException

use of org.opendatakit.briefcase.model.FileSystemException in project briefcase by opendatakit.

the class FileSystemUtils method getFormSubmissionDirectories.

public static Set<File> getFormSubmissionDirectories(File formDirectory) {
    Set<File> files = new TreeSet<>();
    File formInstancesDir;
    try {
        formInstancesDir = getFormInstancesDirectory(formDirectory);
    } catch (FileSystemException e) {
        log.error("failed to get submission directory", e);
        return files;
    }
    File[] briefcaseInstances = formInstancesDir.listFiles();
    if (briefcaseInstances != null) {
        for (File briefcaseInstance : briefcaseInstances) {
            if (!briefcaseInstance.isDirectory() || briefcaseInstance.getName().startsWith(".")) {
                log.warn("skipping non-directory or dot-file in form instances subdirectory");
                continue;
            }
            files.add(briefcaseInstance);
        }
    }
    return files;
}
Also used : FileSystemException(org.opendatakit.briefcase.model.FileSystemException) TreeSet(java.util.TreeSet) File(java.io.File)

Example 5 with FileSystemException

use of org.opendatakit.briefcase.model.FileSystemException in project briefcase by opendatakit.

the class StorageLocation method assertBriefcaseStorageLocationParentFolder.

void assertBriefcaseStorageLocationParentFolder(File pathname) throws FileSystemException {
    File folder = new File(pathname, StorageLocation.BRIEFCASE_DIR);
    if (!folder.exists()) {
        if (!folder.mkdir()) {
            throw new FileSystemException("Unable to create " + StorageLocation.BRIEFCASE_DIR);
        }
    }
    File forms = new File(folder, FORMS_DIR);
    if (!forms.exists()) {
        if (!forms.mkdir()) {
            throw new FileSystemException("Unable to create " + FORMS_DIR);
        }
    }
    File f = new File(folder, README_TXT);
    if (!f.exists()) {
        try {
            if (!f.createNewFile()) {
                throw new FileSystemException("Unable to create " + README_TXT);
            }
        } catch (IOException e) {
            String msg = "Unable to create " + README_TXT;
            log.error(msg, e);
            throw new FileSystemException(msg);
        }
    }
    try {
        OutputStreamWriter fout = new OutputStreamWriter(new FileOutputStream(f, false), "UTF-8");
        fout.write(MessageStrings.README_CONTENTS);
        fout.close();
    } catch (IOException e) {
        String msg = "Unable to write " + README_TXT;
        log.error(msg, e);
        throw new FileSystemException(msg);
    }
}
Also used : FileSystemException(org.opendatakit.briefcase.model.FileSystemException) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) File(java.io.File)

Aggregations

File (java.io.File)14 FileSystemException (org.opendatakit.briefcase.model.FileSystemException)14 IOException (java.io.IOException)9 ParsingException (org.opendatakit.briefcase.model.ParsingException)7 SQLException (java.sql.SQLException)4 BriefcaseFormDefinition (org.opendatakit.briefcase.model.BriefcaseFormDefinition)4 FormStatusEvent (org.opendatakit.briefcase.model.FormStatusEvent)4 OutputStreamWriter (java.io.OutputStreamWriter)3 Document (org.kxml2.kdom.Document)3 FormStatus (org.opendatakit.briefcase.model.FormStatus)3 LocalDate (java.time.LocalDate)2 Date (java.util.Date)2 CannotFixXMLException (org.opendatakit.briefcase.model.CannotFixXMLException)2 CryptoException (org.opendatakit.briefcase.model.CryptoException)2 ExportProgressEvent (org.opendatakit.briefcase.model.ExportProgressEvent)2 OdkCollectFormDefinition (org.opendatakit.briefcase.model.OdkCollectFormDefinition)2 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1