From 1e2a0c6c675014fded348008ab545af8cd74cd2c Mon Sep 17 00:00:00 2001 From: gexce Date: Tue, 26 Sep 2017 09:12:57 +0200 Subject: [PATCH] =?UTF-8?q?l=C3=A4uft?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bayer/edam/migration/EntryDownloader.java | 53 +++++++++++-------- 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/AH MAM Migration/src/com/bayer/edam/migration/EntryDownloader.java b/AH MAM Migration/src/com/bayer/edam/migration/EntryDownloader.java index 71c8bb0..3007140 100644 --- a/AH MAM Migration/src/com/bayer/edam/migration/EntryDownloader.java +++ b/AH MAM Migration/src/com/bayer/edam/migration/EntryDownloader.java @@ -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 entity = new HttpEntity("", 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 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(); } } }