This is an automated email from the git hooks/post-receive script. New commit to branch feature/6688 in repository tutti. See http://git.codelutin.com/tutti.git commit 573a659a2d577d4ad823b97e8fb00e9a1021e8be Author: Tony CHEMIT <chemit@codelutin.com> Date: Tue Feb 24 20:43:01 2015 +0100 review howto validate the layout of an archive --- .../genericformat/GenericFormatArchive.java | 124 ++++++++------------- ...GenericFormatArchiveInvalidLayoutException.java | 19 ++++ 2 files changed, 68 insertions(+), 75 deletions(-) diff --git a/tutti-service/src/main/java/fr/ifremer/tutti/service/genericformat/GenericFormatArchive.java b/tutti-service/src/main/java/fr/ifremer/tutti/service/genericformat/GenericFormatArchive.java index 0e60317..3ea7e5f 100644 --- a/tutti-service/src/main/java/fr/ifremer/tutti/service/genericformat/GenericFormatArchive.java +++ b/tutti-service/src/main/java/fr/ifremer/tutti/service/genericformat/GenericFormatArchive.java @@ -6,7 +6,6 @@ import fr.ifremer.tutti.persistence.ProgressionModel; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.commons.vfs2.FileObject; -import org.nuiton.jaxx.application.ApplicationBusinessException; import org.nuiton.jaxx.application.ApplicationIOUtil; import org.nuiton.jaxx.application.ApplicationTechnicalException; @@ -84,7 +83,7 @@ public class GenericFormatArchive implements Serializable { private final File archiveFile; - private final Path workingDirectory; + private final File workingDirectory; private final ArchiveMode archiveMode; @@ -94,7 +93,7 @@ public class GenericFormatArchive implements Serializable { try { - Path workingDirectory = Files.createTempDirectory(tempDirectory.toPath(), "genericImport"); + File workingDirectory = Files.createTempDirectory(tempDirectory.toPath(), "genericImport").toFile(); GenericFormatArchive archive = new GenericFormatArchive(ArchiveMode.IMPORT, archiveFile, workingDirectory); return archive; @@ -108,7 +107,7 @@ public class GenericFormatArchive implements Serializable { try { - Path workingDirectory = Files.createTempDirectory(tempDirectory.toPath(), "genericExport"); + File workingDirectory = Files.createTempDirectory(tempDirectory.toPath(), "genericExport").toFile(); GenericFormatArchive archive = new GenericFormatArchive(ArchiveMode.EXPORT, archiveFile, workingDirectory); return archive; @@ -120,12 +119,12 @@ public class GenericFormatArchive implements Serializable { public static GenericFormatArchive forExportFromWorkingDirectory(File archiveFile, File workingDirectory) { - GenericFormatArchive archive = new GenericFormatArchive(ArchiveMode.EXPORT, archiveFile, workingDirectory.toPath()); + GenericFormatArchive archive = new GenericFormatArchive(ArchiveMode.EXPORT, archiveFile, workingDirectory); return archive; } - public Path getWorkingDirectoryPath() { + public File getWorkingDirectoryPath() { return workingDirectory; } @@ -209,29 +208,56 @@ public class GenericFormatArchive implements Serializable { return getPath(ArchiveFilePath.DATA_MARINE_LITTER); } - public int getNbCruisesFromFile() { + public void validateArchiveLayout() throws GenericFormatArchiveInvalidLayoutException { - Path path = getSurveyPath(); - try (BufferedReader bufferedReader = Files.newBufferedReader(path, Charset.forName("UTF-8"))) { + List<String> errors = new ArrayList<>(); - try (LineNumberReader lineNumberReader = new LineNumberReader(bufferedReader)) { - lineNumberReader.skip(Long.MAX_VALUE); - int result = lineNumberReader.getLineNumber() - 1; - if (result == -1) { - result = 0; + try (ZipFile zipFile = new ZipFile(archiveFile)) { + + for (ArchiveFilePath archiveFilePath : ArchiveFilePath.values()) { + + if (archiveFilePath.isMandatory()) { + String zipEntryPath = getZipEntryPath(archiveFilePath); + + if (log.isDebugEnabled()) { + log.debug("Check mandatory entry " + zipEntryPath + " exists."); + } + + ZipEntry zipEntry = zipFile.getEntry(zipEntryPath); + + if (zipEntry == null) { + + if (log.isErrorEnabled()) { + log.error("Mandatory entry " + zipEntryPath + " not found."); + } + errors.add(t("tutti.service.genericFormat.importError.missArchiveFile", archiveFilePath.getFilename())); + + } else { + + if (log.isInfoEnabled()) { + log.info("Mandatory entry " + zipEntryPath + " found."); + } + } } - return result; + } } catch (IOException e) { - throw new ApplicationTechnicalException("Could not read survey.csv file from archive " + archiveFile, e); + + throw new ApplicationTechnicalException("Could not open zip file: " + archiveFile, e); } - } + if (!errors.isEmpty()) { - public int getNbOperationsFromFile() { + String message = t("tutti.service.genericFormat.importError.archiveNotSane", Joiner.on("\n").join(errors)); + + throw new GenericFormatArchiveInvalidLayoutException(message); - Path path = getOperationPath(); + } + + } + + public int countImportLines(Path path) { try (BufferedReader bufferedReader = Files.newBufferedReader(path, Charset.forName("UTF-8"))) { @@ -245,7 +271,7 @@ public class GenericFormatArchive implements Serializable { } } catch (IOException e) { - throw new ApplicationTechnicalException("Could not read operation.csv file from archive " + archiveFile, e); + throw new ApplicationTechnicalException("Could not read " + path.toFile().getName() + " file from archive " + archiveFile, e); } } @@ -258,7 +284,7 @@ public class GenericFormatArchive implements Serializable { } - ApplicationIOUtil.zip(workingDirectory.toFile(), archiveFile, t("tutti.service.genericFormat.export.zip.error", archiveFile)); + ApplicationIOUtil.zip(workingDirectory, archiveFile, t("tutti.service.genericFormat.export.zip.error", archiveFile)); } @@ -266,7 +292,7 @@ public class GenericFormatArchive implements Serializable { String filename = archiveFilePath.getFilename(); - Path file = workingDirectory.resolve(filename); + Path file = workingDirectory.toPath().resolve(filename); if (isImport() && Files.notExists(file) && !missingPaths.contains(archiveFilePath)) { @@ -311,7 +337,7 @@ public class GenericFormatArchive implements Serializable { protected final EnumSet<ArchiveFilePath> missingPaths; - protected GenericFormatArchive(ArchiveMode archiveMode, File archiveFile, Path workingDirectory) { + protected GenericFormatArchive(ArchiveMode archiveMode, File archiveFile, File workingDirectory) { this.archiveFile = archiveFile; this.workingDirectory = workingDirectory; @@ -328,8 +354,6 @@ public class GenericFormatArchive implements Serializable { log.info("Zip entries prefix path: " + prefixPath); } - checkArchiveLayout(); - missingPaths = getMissingPaths(); } else { @@ -363,56 +387,6 @@ public class GenericFormatArchive implements Serializable { } - protected void checkArchiveLayout() { - - List<String> errors = new ArrayList<>(); - - try (ZipFile zipFile = new ZipFile(archiveFile)) { - - for (ArchiveFilePath archiveFilePath : ArchiveFilePath.values()) { - - if (archiveFilePath.isMandatory()) { - String zipEntryPath = getZipEntryPath(archiveFilePath); - - if (log.isDebugEnabled()) { - log.debug("Check mandatory entry " + zipEntryPath + " exists."); - } - - ZipEntry zipEntry = zipFile.getEntry(zipEntryPath); - - if (zipEntry == null) { - - if (log.isErrorEnabled()) { - log.error("Mandatory entry " + zipEntryPath + " not found."); - } - errors.add(t("tutti.service.genericFormat.importError.missArchiveFile", archiveFilePath.getFilename())); - - } else { - - if (log.isInfoEnabled()) { - log.info("Mandatory entry " + zipEntryPath + " found."); - } - } - } - - } - - } catch (IOException e) { - - throw new ApplicationTechnicalException("Could not open zip file: " + archiveFile, e); - } - - if (!errors.isEmpty()) { - - String message = t("tutti.service.genericFormat.importError.archiveNotSane", Joiner.on("\n").join(errors)); - - throw new ApplicationBusinessException(message); - - } - - } - - protected EnumSet<ArchiveFilePath> getMissingPaths() { EnumSet<ArchiveFilePath> result = EnumSet.noneOf(ArchiveFilePath.class); diff --git a/tutti-service/src/main/java/fr/ifremer/tutti/service/genericformat/GenericFormatArchiveInvalidLayoutException.java b/tutti-service/src/main/java/fr/ifremer/tutti/service/genericformat/GenericFormatArchiveInvalidLayoutException.java new file mode 100644 index 0000000..f2c4a22 --- /dev/null +++ b/tutti-service/src/main/java/fr/ifremer/tutti/service/genericformat/GenericFormatArchiveInvalidLayoutException.java @@ -0,0 +1,19 @@ +package fr.ifremer.tutti.service.genericformat; + +import org.nuiton.jaxx.application.ApplicationBusinessException; + +/** + * Created on 2/24/15. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 3.14 + */ +public class GenericFormatArchiveInvalidLayoutException extends ApplicationBusinessException { + + private static final long serialVersionUID = 1L; + + public GenericFormatArchiveInvalidLayoutException(String message) { + super(message); + } + +} -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.