Search in sources :

Example 6 with FetchItem

use of gov.loc.repository.bagit.domain.FetchItem in project bagit-java by LibraryOfCongress.

the class FetchWriter method writeFetchFile.

/**
 * Write the fetch.txt file to the outputDir with the specified encoding (charsetName)
 *
 * @param itemsToFetch the list of {@link FetchItem}s to write into the fetch.txt
 * @param outputDir the root of the bag
 * @param bagitRootDir the path to the root of the bag
 * @param charsetName the name of the encoding for the file
 *
 * @throws IOException if there was a problem writing a file
 */
public static void writeFetchFile(final List<FetchItem> itemsToFetch, final Path outputDir, final Path bagitRootDir, final Charset charsetName) throws IOException {
    logger.debug(messages.getString("writing_fetch_file_to_path"), outputDir);
    final Path fetchFilePath = outputDir.resolve("fetch.txt");
    for (final FetchItem item : itemsToFetch) {
        final String line = formatFetchLine(item, bagitRootDir);
        logger.debug(messages.getString("writing_line_to_file"), line, fetchFilePath);
        Files.write(fetchFilePath, line.getBytes(charsetName), StandardOpenOption.APPEND, StandardOpenOption.CREATE);
    }
}
Also used : Path(java.nio.file.Path) FetchItem(gov.loc.repository.bagit.domain.FetchItem)

Example 7 with FetchItem

use of gov.loc.repository.bagit.domain.FetchItem in project bagit-java by LibraryOfCongress.

the class FetchWriterTest method testWriteFetchFile.

@Test
public void testWriteFetchFile() throws Exception {
    File rootDir = folder.newFolder();
    Path rootPath = rootDir.toPath();
    File fetch = new File(rootDir, "fetch.txt");
    URL url = new URL("http://localhost:/foo/bar");
    List<FetchItem> itemsToFetch = Arrays.asList(new FetchItem(url, -1l, rootPath.resolve("/data/foo/bar")), new FetchItem(url, 100l, rootPath.resolve("/data/foo/bar")));
    assertFalse(fetch.exists());
    FetchWriter.writeFetchFile(itemsToFetch, Paths.get(rootDir.toURI()), rootPath, StandardCharsets.UTF_8);
    assertTrue(fetch.exists());
}
Also used : Path(java.nio.file.Path) FetchItem(gov.loc.repository.bagit.domain.FetchItem) File(java.io.File) URL(java.net.URL) PrivateConstructorTest(gov.loc.repository.bagit.PrivateConstructorTest) Test(org.junit.Test)

Example 8 with FetchItem

use of gov.loc.repository.bagit.domain.FetchItem in project bagit-java by LibraryOfCongress.

the class FetchReader method readFetch.

/**
 * Reads a fetch.txt file
 *
 * @param fetchFile the specific fetch file
 * @param encoding the encoding to read the file with
 * @param bagRootDir the root directory of the bag
 * @return a list of items to fetch
 *
 * @throws IOException if there is a problem reading a file
 * @throws MaliciousPathException if the path was crafted to point outside the bag directory
 * @throws InvalidBagitFileFormatException if the fetch format does not follow the bagit specification
 */
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
public static List<FetchItem> readFetch(final Path fetchFile, final Charset encoding, final Path bagRootDir) throws IOException, MaliciousPathException, InvalidBagitFileFormatException {
    logger.info(messages.getString("reading_fetch_file"), fetchFile);
    final List<FetchItem> itemsToFetch = new ArrayList<>();
    try (final BufferedReader reader = Files.newBufferedReader(fetchFile, encoding)) {
        String line = reader.readLine();
        String[] parts = null;
        long length = 0;
        URL url = null;
        while (line != null) {
            if (line.matches(FETCH_LINE_REGEX) && !line.matches("\\s*")) {
                parts = line.split("\\s+", 3);
                final Path path = TagFileReader.createFileFromManifest(bagRootDir, parts[2]);
                length = parts[1].equals("-") ? -1 : Long.decode(parts[1]);
                url = new URL(parts[0]);
                logger.debug(messages.getString("read_fetch_file_line"), url, length, parts[2], fetchFile);
                final FetchItem itemToFetch = new FetchItem(url, length, path);
                itemsToFetch.add(itemToFetch);
            } else {
                throw new InvalidBagitFileFormatException(messages.getString("invalid_fetch_file_line_error").replace("{}", line));
            }
            line = reader.readLine();
        }
    }
    return itemsToFetch;
}
Also used : Path(java.nio.file.Path) FetchItem(gov.loc.repository.bagit.domain.FetchItem) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) InvalidBagitFileFormatException(gov.loc.repository.bagit.exceptions.InvalidBagitFileFormatException) URL(java.net.URL)

Example 9 with FetchItem

use of gov.loc.repository.bagit.domain.FetchItem in project bagit-java by LibraryOfCongress.

the class PayloadWriterTest method testWritePayloadFilesMinusFetchFiles.

@Test
public void testWritePayloadFilesMinusFetchFiles() throws IOException, URISyntaxException {
    Path rootDir = Paths.get(getClass().getClassLoader().getResource("bags/v0_97/bag").toURI());
    Path testFile = Paths.get(getClass().getClassLoader().getResource("bags/v0_97/bag/data/dir1/test3.txt").toURI());
    Manifest manifest = new Manifest(StandardSupportedAlgorithms.MD5);
    manifest.getFileToChecksumMap().put(testFile, "someHashValue");
    Set<Manifest> payloadManifests = new HashSet<>();
    payloadManifests.add(manifest);
    File outputDir = folder.newFolder();
    File copiedFile = new File(outputDir, "data/dir1/test3.txt");
    assertFalse(copiedFile.exists() || copiedFile.getParentFile().exists());
    PayloadWriter.writePayloadFiles(payloadManifests, Arrays.asList(new FetchItem(null, null, Paths.get("data/dir1/test3.txt"))), Paths.get(outputDir.toURI()), rootDir.resolve("data"));
    assertFalse(copiedFile.exists() && copiedFile.getParentFile().exists());
}
Also used : Path(java.nio.file.Path) FetchItem(gov.loc.repository.bagit.domain.FetchItem) Manifest(gov.loc.repository.bagit.domain.Manifest) File(java.io.File) HashSet(java.util.HashSet) PrivateConstructorTest(gov.loc.repository.bagit.PrivateConstructorTest) Test(org.junit.Test)

Aggregations

FetchItem (gov.loc.repository.bagit.domain.FetchItem)9 Path (java.nio.file.Path)8 Test (org.junit.Test)7 PrivateConstructorTest (gov.loc.repository.bagit.PrivateConstructorTest)5 URL (java.net.URL)5 File (java.io.File)4 ArrayList (java.util.ArrayList)3 Bag (gov.loc.repository.bagit.domain.Bag)1 Manifest (gov.loc.repository.bagit.domain.Manifest)1 Metadata (gov.loc.repository.bagit.domain.Metadata)1 InvalidBagitFileFormatException (gov.loc.repository.bagit.exceptions.InvalidBagitFileFormatException)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1