This commit is contained in:
gexce
2017-09-26 09:12:57 +02:00
parent 2f055ba36d
commit 1e2a0c6c67
@@ -1,9 +1,13 @@
package com.bayer.edam.migration;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
@@ -18,9 +22,6 @@ import org.springframework.context.annotation.PropertySource;
import org.springframework.http.HttpEntity;
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;
@@ -36,6 +37,12 @@ public class EntryDownloader implements CommandLineRunner {
@Value("${mam.auth}")
private String mamAuth;
@Value("${mam.user}")
private String mamUser;
@Value("${mam.password}")
private String mamPassword;
static final String mamHost = "https://service.media-assistant.animalhealth.bayer.com";
@@ -46,6 +53,19 @@ public class EntryDownloader implements CommandLineRunner {
private ObjectMapper jsonMapper = new ObjectMapper();
private RestTemplate restTemplate = new RestTemplate();
public static class MamAuthenticator extends Authenticator {
private final PasswordAuthentication auth;
public MamAuthenticator(String mamUser, String mamPassword) {
auth = new PasswordAuthentication(mamUser, mamPassword.toCharArray());
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return auth;
}
}
@Override
public void run(String... strings) {
@@ -56,6 +76,8 @@ public class EntryDownloader implements CommandLineRunner {
headers.add("From", "migration@bayer.com");
HttpEntity<String> entity = new HttpEntity<String>("", headers);
Authenticator.setDefault(new MamAuthenticator(mamUser, mamPassword));
int lastPage = 1;
for (int pageNum = 1; pageNum <= lastPage; pageNum++) {
Entrylist entrylist = restTemplate.exchange(mamHost + "/rest/migration/entrylist?pagenumber={pagenum}&pagesize=500", HttpMethod.GET, entity, Entrylist.class, pageNum).getBody();
@@ -126,26 +148,15 @@ public class EntryDownloader implements CommandLineRunner {
else
filenameToUse = download.fileName;
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("ID " + entry.id + ": " + hcee.getMessage());
hcee.printStackTrace();
}
URL fileLink = new URL(download.href);
ReadableByteChannel rbc = Channels.newChannel(fileLink.openStream());
FileOutputStream fos = new FileOutputStream("E:\\" + entry.id + "\\" + filenameToUse);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
}
}
catch (IOException ioe) {
log.error(ioe.getMessage());
ioe.printStackTrace();
}
}
}