Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensures that external resources are not exported by default. #158

Merged
merged 2 commits into from
Feb 3, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 7 additions & 2 deletions src/main/java/org/fcrepo/importexport/exporter/Exporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import java.io.OutputStream;
import java.io.Reader;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -388,7 +389,7 @@ private void exportBinary(final URI uri, final List<URI> describedby, final bool
throws FcrepoOperationFailedException, IOException {

if (!config.isIncludeBinaries()) {
logger.info("Skipping {}", uri);
logger.debug("Skipping: {} -> binaries are not included in this export configuration", uri);
return;
}

Expand All @@ -402,8 +403,12 @@ private void exportBinary(final URI uri, final List<URI> describedby, final bool
final File file = external ? fileForExternalResources(uri, null, null, config.getBaseDirectory()) :
fileForBinary(uri, null, null, config.getBaseDirectory());

//only retrieve content of external resources when retrieve external flag is enabled
//otherwise write a zero length file.
final InputStream is = external && !config.retrieveExternal() ?
IOUtils.toInputStream("", Charset.defaultCharset()) : response.getBody();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not seeing this InputStream being closed anywhere. Is it happening?

logger.info("Exporting binary: {}", uri);
writeResponse(uri, response.getBody(), describedby, file);
writeResponse(uri, is, describedby, file);
writeHeadersFile(response, getHeadersFile(file));
exportLogger.info("export {} to {}", uri, file.getAbsolutePath());
successCount.incrementAndGet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ public void testExportBag() throws Exception {
bagArgs.setMode("export");
bagArgs.setBaseDirectory(basedir);
bagArgs.setIncludeBinaries(true);
bagArgs.setRetrieveExternal(true);
bagArgs.setPredicates(predicates);
bagArgs.setRdfLanguage("application/ld+json");
bagArgs.setResource(resource3);
Expand Down Expand Up @@ -297,6 +298,7 @@ public void testExportBeyondTheRepositoryBag() throws IOException {
config.setMode("export");
config.setBaseDirectory(exportDirectory);
config.setIncludeBinaries(true);
config.setRetrieveExternal(true);
config.setPredicates(predicates);
config.setRdfLanguage("application/ld+json");
config.setResource(resource3);
Expand Down
33 changes: 33 additions & 0 deletions src/test/java/org/fcrepo/importexport/integration/ExporterIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,39 @@ public void testExportRetrieveExternal() throws Exception {
assertEquals("content", readFileToString(externalFile, "UTF-8"));
}

@Test
public void testExportRetrieveExternalDisabled() throws Exception {
// Create an external content resource pointing at another repository resource
final URI binaryURI = URI.create(serverAddress + UUID.randomUUID());
createBody(binaryURI, "content", "text/plain");
final Map<String,String> headers = new HashMap<>();
headers.put("Link", "<" + binaryURI.toString() + ">;" +
" rel=\"http://fedora.info/definitions/fcrepo#ExternalContent\"; " +
" handling=\"redirect\"; " +
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For completeness, may as well add another test for proxy.

" type=\"text/plain\"");

createBody(url, new ByteArrayInputStream("".getBytes()), "text/plain", headers);

// Run an export process
final Config config = new Config();
config.setMode("export");
config.setBaseDirectory(TARGET_DIR);
config.setResource(url);
config.setIncludeBinaries(true);
config.setRetrieveExternal(false);
config.setUsername(USERNAME);
config.setPassword(PASSWORD);

final Exporter exporter = new Exporter(config, clientBuilder);
exporter.run();

// Verify
final File externalFile = new File(TARGET_DIR, url.getPath() + EXTERNAL_RESOURCE_EXTENSION);
assertTrue(externalFile.exists());
assertEquals("File length should be 0", 0, externalFile.length());

}

@Test
public void testExportCustomPredicate() throws Exception {
final String[] predicates = new String[]{ "http://example.org/custom" };
Expand Down
20 changes: 14 additions & 6 deletions src/test/java/org/fcrepo/importexport/integration/RoundtripIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public void testRoundtripWithIgnoreVersions() throws Exception {
final FcrepoResponse memento = post(timemapURI);
assertEquals(SC_CREATED, memento.getStatusCode());
final URI mementoURI = memento.getLocation();
roundtrip(uri, new ArrayList<URI>(), true, true, false);
roundtrip(uri, new ArrayList<URI>(), true, true, false, false);
//timemap will be there because it is created automatically
assertEquals(SC_OK, get(timemapURI).getStatusCode());
//the memento however should not have been imported.
Expand Down Expand Up @@ -390,7 +390,7 @@ public void testRoundtripExcludedBinaries() throws Exception {
assertTrue(exists(res1));
assertTrue(exists(file1));

final Config config = roundtrip(res1, Collections.singletonList(file1), true, false, true);
final Config config = roundtrip(res1, Collections.singletonList(file1), true, false, true, false);

// verify that files exist and contain expected content
final File exportDir = config.getBaseDirectory();
Expand Down Expand Up @@ -498,7 +498,7 @@ public void testRoundtripExternalProxy() throws Exception {

final String contentLength = get(file1).getHeaderValue("Content-Length");
assertEquals("Unexpected Content-Length value", contentLength, binaryFileLength + "");
final Config config = roundtrip(URI.create(baseURI), true);
final Config config = roundtrip(URI.create(baseURI), true,true);

// verify that files exist and contain expected content
final File exportDir = config.getBaseDirectory();
Expand Down Expand Up @@ -648,12 +648,19 @@ private Literal longLiteral(final String longString) {
return createTypedLiteral(longString, XSDlong);
}

private Config roundtrip(final URI uri, final boolean reset) throws FcrepoOperationFailedException {
return roundtrip(uri, new ArrayList<URI>(), true, true, true);
private Config roundtrip(final URI uri, final boolean reset)
throws FcrepoOperationFailedException {
return roundtrip(uri, new ArrayList<URI>(), true, true, true, false);
}

private Config roundtrip(final URI uri, final boolean reset, final boolean retrieveExternal)
throws FcrepoOperationFailedException {
return roundtrip(uri, new ArrayList<URI>(), true, true, true, retrieveExternal);
}

private Config roundtrip(final URI uri, final List<URI> relatedResources,
final boolean reset, final boolean includeBinary, final boolean includeVersions)
final boolean reset, final boolean includeBinary, final boolean includeVersions,
final boolean retrieveExternal)
throws FcrepoOperationFailedException {
// export resources
final Config config = new Config();
Expand All @@ -662,6 +669,7 @@ private Config roundtrip(final URI uri, final List<URI> relatedResources,
config.setIncludeBinaries(includeBinary);
config.setIncludeMembership(true);
config.setResource(uri);
config.setRetrieveExternal(retrieveExternal);
config.setPredicates(new String[]{ CONTAINS.toString() });
config.setRdfExtension(DEFAULT_RDF_EXT);
config.setRdfLanguage(DEFAULT_RDF_LANG);
Expand Down