Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -54,15 +56,11 @@ public class AuditPacker {
/**
* The directory where the temporary files are stored.
*/
private final File directory;
private final Path directory;
/**
* Map between the contributor id and the reached preservation sequence number.
*/
private final Map<String, Long> seqNumsReached = new HashMap<>();
/**
* Whether the output stream should be appended to the file.
*/
private static final boolean APPEND = true;

private long packedAuditCount = 0;

Expand All @@ -76,7 +74,7 @@ public class AuditPacker {
public AuditPacker(AuditTrailStore store, AuditTrailPreservation settings, String collectionID) {
this.store = store;
this.collectionID = collectionID;
this.directory = FileUtils.retrieveDirectory(settings.getAuditTrailPreservationTemporaryDirectory());
this.directory = FileUtils.retrieveDirectory(settings.getAuditTrailPreservationTemporaryDirectory()).toPath();
this.contributors.addAll(SettingsUtils.getAuditContributorsForCollection(collectionID));

initializeReachedSequenceNumbers();
Expand Down Expand Up @@ -108,23 +106,18 @@ private void initializeReachedSequenceNumbers() {
*
* @return A compressed file with all the audit trails.
*/
public synchronized File createNewPackage() {
public synchronized Path createNewPackage() throws IOException {
resetPackedAuditCount();
File container = new File(directory, collectionID + "-audit-trails-" + System.currentTimeMillis());
Path auditTrailsFile = directory.resolve(collectionID + "-audit-trails-" + System.currentTimeMillis());
try {
if (container.createNewFile()) {
packContributors(container);
return createCompressedFile(container);
}
Files.createFile(auditTrailsFile);
packContributors(auditTrailsFile);
return createCompressedFile(auditTrailsFile);
} catch (IOException e) {
throw new IllegalStateException("Cannot package the newest audit trails.", e);
} finally {
// cleaning up.
if (container.exists()) {
FileUtils.delete(container);
}
Files.deleteIfExists(auditTrailsFile);
}
return null;
}

/**
Expand All @@ -141,18 +134,14 @@ private void resetPackedAuditCount() {
* @param container The file where the audit trails should be written.
* @throws IOException If writing to the file somehow fails.
*/
private void packContributors(File container) throws IOException {
PrintWriter writer = null;
try {
writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(container, APPEND), StandardCharsets.UTF_8));
private void packContributors(Path container) throws IOException {
try (OutputStream os = Files.newOutputStream(container, StandardOpenOption.APPEND);
OutputStreamWriter osw = new OutputStreamWriter(os, StandardCharsets.UTF_8);
PrintWriter writer = new PrintWriter(osw)) {

for (String contributor : contributors) {
packContributor(contributor, writer);
}
} finally {
if (writer != null) {
writer.flush();
writer.close();
}
}
}

Expand Down Expand Up @@ -204,8 +193,8 @@ private void packContributor(String contributorID, PrintWriter writer) {
* @return The compressed file.
* @throws IOException If anything goes wrong.
*/
private File createCompressedFile(File fileToCompress) throws IOException {
File zippedFile = new File(directory, fileToCompress.getName() + ".zip");
private Path createCompressedFile(Path fileToCompress) throws IOException {
Path zippedFile = directory.resolve(fileToCompress.getFileName() + ".zip");
FileUtils.zipFile(fileToCompress, zippedFile);
return zippedFile;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ public class LocalAuditTrailPreserver implements AuditTrailPreserver {
private final Logger log = LoggerFactory.getLogger(getClass());
private final AuditTrailStore store;
private final BlockingPutFileClient client;
private Timer timer;
private AuditPreservationTimerTask preservationTask = null;
private final Map<String, AuditPacker> auditPackers = new HashMap<>();
private final AuditTrailPreservation preservationSettings;
private final Settings settings;
private final FileExchange exchange;
private Timer timer;
private AuditPreservationTimerTask preservationTask = null;
private long preservedAuditCount = 0;

/**
Expand Down Expand Up @@ -161,7 +161,7 @@ private void resetPreservedAuditCount() {
private synchronized void performAuditTrailPreservation(String collectionID) {
try {
AuditPacker auditPacker = auditPackers.get(collectionID);
File auditPackage = auditPacker.createNewPackage();
File auditPackage = auditPacker.createNewPackage().toFile();

if (auditPacker.getPackedAuditCount() > 0) {
URL url = uploadFile(auditPackage);
Expand All @@ -177,7 +177,8 @@ private synchronized void performAuditTrailPreservation(String collectionID) {

preservedAuditCount += auditPacker.getPackedAuditCount();
} else {
log.info("No new audit trails to preserve. No preservation file uploaded.");
log.info("No new audit trails to preserve for collection '{}'. No preservation file uploaded.",
collectionID);
}

log.debug("Cleanup of the audit trail package.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.List;
import java.util.Map;

Expand All @@ -34,7 +35,7 @@ public void setup() {
}

@Test
public void testCreateNewPackage() {
public void testCreateNewPackage() throws IOException {
AuditPacker packer = new AuditPacker(store, preservationSettings, collectionID);
Map<String, Long> seqNumsReached = packer.getSequenceNumbersReached();
assertEquals(seqNumsReached.entrySet().size(), 3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Enumeration;
import java.util.Objects;
import java.util.zip.ZipEntry;
Expand Down Expand Up @@ -255,6 +258,7 @@ public static void writeStreamToFile(InputStream in, File f) throws IOException
}

/**
* TODO remove? - only used in migration tests
* Unzip a zipFile into a directory. This will create subdirectories
* as needed.
*
Expand Down Expand Up @@ -303,28 +307,22 @@ public static void unzip(File zipFile, File toDir) throws IOException {
/**
* Zips a file.
*
* @param inputFile The file to zip.
* @param outputFile The file where the compressed content will be placed.
* @param inputFile The file to zip.
* @param outputZipFile The file where the compressed content will be placed.
* @throws IOException If any error occurs while writing the stream to a file
*/
public static void zipFile(File inputFile, File outputFile) throws IOException {
ArgumentValidator.checkNotNull(inputFile, "File zipFile");
ArgumentValidator.checkNotNull(outputFile, "File toDir");
ArgumentValidator.checkTrue(inputFile.canRead(), "can't read '" + inputFile + "'");

byte[] buffer = new byte[BYTE_ARRAY_SIZE];
int bytesRead;

try (InputStream inputStream = new FileInputStream(inputFile);
ZipOutputStream outStream = new ZipOutputStream(new FileOutputStream(outputFile))) {

ZipEntry entry = new ZipEntry(inputFile.getPath());
outStream.putNextEntry(entry);

while ((bytesRead = inputStream.read(buffer)) > 0) {
outStream.write(buffer, 0, bytesRead);
}
outStream.flush();
public static void zipFile(Path inputFile, Path outputZipFile) throws IOException {
ArgumentValidator.checkNotNull(inputFile, "Path inputFile");
ArgumentValidator.checkNotNull(outputZipFile, "Path outputZipFile");
ArgumentValidator.checkTrue(Files.isReadable(inputFile), "Can't read '" + inputFile + "'");

try (OutputStream os = Files.newOutputStream(outputZipFile);
ZipOutputStream zs = new ZipOutputStream(os)) {

ZipEntry zipEntry = new ZipEntry(inputFile.getFileName().toString());
zs.putNextEntry(zipEntry);
Files.copy(inputFile, zs);
zs.closeEntry();
}
}

Expand Down