Skip to content

Commit

Permalink
error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
rsehr committed Feb 1, 2024
1 parent adb0a1c commit bea655d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,8 @@ private void processFile(String fileUrl, String hash, String targetFolder) throw

CloseableHttpClient httpclient = null;
HttpPost method = null;

String actualHash = "";
Path destination = null;
try {

method = new HttpPost(fileUrl);
Expand All @@ -386,29 +387,28 @@ private void processFile(String fileUrl, String hash, String targetFolder) throw
extension = "." + contentType.substring(contentType.indexOf("/") + 1);
}

Path destination = Paths.get(targetFolder, fileName + extension);
destination = Paths.get(targetFolder, fileName + extension);
StorageProvider.getInstance().createDirectories(destination.getParent());

try (OutputStream out = StorageProvider.getInstance().newOutputStream(destination)) {
entity.writeTo(out);
}

// url is correctly formed, download the file
String actualHash = "";

try (InputStream inputStream = StorageProvider.getInstance().newInputStream(destination)) {
actualHash = calculateHash(inputStream);
}

// check checksum
if (!hash.equals(actualHash)) {
String message = "checksums do not match, the file might be corrupted: " + destination;
// delete the downloaded file
Files.delete(destination);
throw new IOException(message);
}
} catch (Exception e) {
log.error("Unable to connect to url " + fileUrl, e);

}
// check checksum
if (!hash.equals(actualHash)) {
String message = "checksums do not match, the file might be corrupted: " + destination;
// delete the downloaded file
Files.delete(destination);
throw new IOException(message);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.goobi.production.enums.PluginReturnValue;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
Expand Down Expand Up @@ -92,7 +91,7 @@ public void testInit() {
}

// does not work without access to BACH server, enable it locally
@Ignore
// @Ignore
@Test
public void testDownload() throws IOException {
DownloadAndVerifyAssetsStepPlugin plugin = new DownloadAndVerifyAssetsStepPlugin();
Expand Down Expand Up @@ -220,7 +219,7 @@ private void createProcessProperties() {
property.setId(1);
property.setProzess(process);
property.setTitel("AttachmentHashSplitted");
property.setWert("1eae07b41cb3323ab370d3ddd78de440ffe6d581d1d3736c086b8949d24b35da");
property.setWert("1eae07b41cb3323ab370d3ddd78de440ffe6d581d1d3736c086b8949d24b35da1");
props.add(property);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
<!-- Configure here how many times shall be maximally tried before reporting final results. OPTIONAL. DEFAULT 1. -->
<maxTryTimes>3</maxTryTimes>

<authentication>Bearer 123456</authentication>
<authentication>Bearer library_api_testkey_123</authentication>

<downloadUrl>https://example.com/thesis/{meta.ThesisId}/file/{FILEID}</downloadUrl>
<downloadUrl>https://bach.wu.ac.at:12345/thesis-dev/tmg-api/library/thesis/{meta.ThesisId}/file/{FILEID}</downloadUrl>
<!-- This tag accepts the following three attributes:
- @urlProperty: name of the property that holds the URL of the file
- @hashProperty: name of the property that holds the checksum of the file
Expand Down

0 comments on commit bea655d

Please sign in to comment.