hardened EntryDownloader
This commit is contained in:
@@ -20,6 +20,7 @@ import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.client.HttpClientErrorException;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import com.bayer.edam.migration.mam.Entry;
|
||||
@@ -65,6 +66,12 @@ public class EntryDownloader implements CommandLineRunner {
|
||||
String entryJson = restTemplate.exchange(mamHost + "/rest/migration/entry/{id}", HttpMethod.GET, entity, String.class, entrylist.entries[i].mediaAssistantId).getBody();
|
||||
Entry entry = jsonMapper.readValue(entryJson, Entry.class);
|
||||
|
||||
File targetDir = new File("E:\\" + entry.id);
|
||||
if (targetDir.exists()) {
|
||||
log.info("Skip " + entry.id + ", target directory already present");
|
||||
continue;
|
||||
}
|
||||
|
||||
List<Download> downloadSelection = new ArrayList<Download>();
|
||||
|
||||
// Select download: Download original only. If original is not available, download largest file
|
||||
@@ -86,7 +93,7 @@ public class EntryDownloader implements CommandLineRunner {
|
||||
if (selectedDownload != null)
|
||||
downloadSelection.add(selectedDownload);
|
||||
|
||||
// TODO extract, if zip --> postponed
|
||||
// TODO extract, if zip --> postponed until upload
|
||||
|
||||
for (Download download : entry.downloadListPublicFolder) {
|
||||
downloadSelection.add(download);
|
||||
@@ -108,7 +115,7 @@ public class EntryDownloader implements CommandLineRunner {
|
||||
|
||||
log.info(entry.id + separator + entry.mediaAssistantId + separator + entry.stateId + separator + aggregatedFilesize + separator + entryJson);
|
||||
|
||||
new File("E:\\" + entry.id).mkdir();
|
||||
targetDir.mkdir();
|
||||
|
||||
for (Download download : cleanedDownloads) {
|
||||
String filenameToUse;
|
||||
@@ -117,18 +124,25 @@ public class EntryDownloader implements CommandLineRunner {
|
||||
else
|
||||
filenameToUse = download.fileName;
|
||||
|
||||
ResponseEntity<byte[]> response = restTemplate.exchange(download.href, HttpMethod.GET, entity, byte[].class);
|
||||
try {
|
||||
ResponseEntity<byte[]> response = restTemplate.exchange(download.href, HttpMethod.GET, entity, byte[].class);
|
||||
|
||||
if (response.getStatusCode() == HttpStatus.OK) {
|
||||
Files.write(Paths.get("E:\\" + entry.id + "\\" + filenameToUse), response.getBody());
|
||||
}
|
||||
else {
|
||||
log.error(entry.id + ", " + response.getStatusCodeValue() + ", " + download.href);
|
||||
}
|
||||
}
|
||||
catch (HttpClientErrorException hcee) {
|
||||
log.error(hcee.getMessage());
|
||||
hcee.printStackTrace();
|
||||
}
|
||||
|
||||
if (response.getStatusCode() == HttpStatus.OK) {
|
||||
Files.write(Paths.get("E:\\" + entry.id + "\\" + filenameToUse), response.getBody());
|
||||
}
|
||||
else {
|
||||
log.error(entry.id + ", " + response.getStatusCodeValue() + ", " + download.href);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IOException ioe) {
|
||||
log.error(ioe.toString());
|
||||
log.error(ioe.getMessage());
|
||||
ioe.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user