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);
}
}
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;
}
}
}
}
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;
}
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;
}
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);
}
}
Aggregations