From 5354283f073add5f4e3e86eb349e0410314cdbd1 Mon Sep 17 00:00:00 2001 From: gezvf Date: Thu, 14 Sep 2017 12:27:02 +0200 Subject: [PATCH] first asset created --- .../migration/LoggingRequestInterceptor.java | 35 ++++++ .../edam/migration/MamMigrationSelective.java | 119 ++++++++++++++---- .../MediaAssistantMigrationSimple.java | 47 +++++-- .../edam/migration/adam/WriteRecord.java | 61 +++++++-- 4 files changed, 214 insertions(+), 48 deletions(-) create mode 100644 AH MAM Migration/src/com/bayer/edam/migration/LoggingRequestInterceptor.java diff --git a/AH MAM Migration/src/com/bayer/edam/migration/LoggingRequestInterceptor.java b/AH MAM Migration/src/com/bayer/edam/migration/LoggingRequestInterceptor.java new file mode 100644 index 0000000..0359496 --- /dev/null +++ b/AH MAM Migration/src/com/bayer/edam/migration/LoggingRequestInterceptor.java @@ -0,0 +1,35 @@ +package com.bayer.edam.migration; + +import java.io.IOException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.HttpRequest; +import org.springframework.http.client.ClientHttpRequestExecution; +import org.springframework.http.client.ClientHttpRequestInterceptor; +import org.springframework.http.client.ClientHttpResponse; +import org.springframework.stereotype.Component; + +@Component +public class LoggingRequestInterceptor implements ClientHttpRequestInterceptor { + + final static Logger log = LoggerFactory.getLogger(LoggingRequestInterceptor.class); + + @Override + public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) + throws IOException { + traceRequest(request, body); + ClientHttpResponse response = execution.execute(request, body); + return response; + } + + private void traceRequest(HttpRequest request, byte[] body) throws IOException { + log.debug("===========================request begin================================================"); + log.debug("URI : {}", request.getURI()); + log.debug("Method : {}", request.getMethod()); + log.debug("Headers : {}", request.getHeaders()); + log.debug("Request body: {}", new String(body, "UTF-8")); + log.debug("==========================request end================================================"); + } + +} \ No newline at end of file diff --git a/AH MAM Migration/src/com/bayer/edam/migration/MamMigrationSelective.java b/AH MAM Migration/src/com/bayer/edam/migration/MamMigrationSelective.java index c295db3..486ad19 100644 --- a/AH MAM Migration/src/com/bayer/edam/migration/MamMigrationSelective.java +++ b/AH MAM Migration/src/com/bayer/edam/migration/MamMigrationSelective.java @@ -1,6 +1,8 @@ package com.bayer.edam.migration; -import java.util.Collections; +import java.net.URI; +import java.util.ArrayList; +import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -12,50 +14,113 @@ 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.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.http.client.BufferingClientHttpRequestFactory; +import org.springframework.http.client.ClientHttpRequestInterceptor; +import org.springframework.http.client.SimpleClientHttpRequestFactory; +import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.RestTemplate; +import com.bayer.edam.migration.adam.Token; +import com.bayer.edam.migration.adam.WriteRecord; import com.bayer.edam.migration.mam.Entry; @SpringBootApplication @PropertySource("file:secret.properties") public class MamMigrationSelective implements CommandLineRunner { - + private static final Logger log = LoggerFactory.getLogger(MamMigrationSelective.class); - + @Value("${mam.auth}") - private String mamAuth; - - static int[] selection = {71048,70778,70746/*,70142,70361,63924,71263,63201,56942,57641,70745,70440,70340,70288,71725*/}; - + private String mamAuth; + + @Value("${adam.auth}") + private String adamAuth; + + static int[] selection = { 71048, /* 70778, 70746 ,70142,70361,63924,71263,63201,56942,57641,70745,70440,70340,70288,71725 */ }; + + RestTemplate restTemplate; + HttpHeaders adamHeaders, mamHeaders; + @Override - public void run(String... strings) throws Exception { - - RestTemplate restTemplate = new RestTemplate(); - - HttpHeaders headers = new HttpHeaders(); - headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); - headers.add("Authorization", "Basic " + mamAuth); - headers.add("From", "migration@bayer.com"); - headers.add("Content-Type", "application/json"); - HttpEntity entity = new HttpEntity("Parameters", headers); + public void run(String... strings) throws Exception { + + restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory())); + List interceptors = new ArrayList(); + interceptors.add(new LoggingRequestInterceptor()); + restTemplate.setInterceptors(interceptors); + + adamHeaders = createAdamHeaders(); + mamHeaders = createMamHeaders(); + HttpEntity entity = new HttpEntity("", mamHeaders); for (int id : selection) { Entry entry = restTemplate .exchange("https://service.media-assistant.animalhealth.bayer.com/rest/migration/entry/{id}", HttpMethod.GET, entity, Entry.class, id) .getBody(); - migrate(entry); - } + postToAdam(entry); + } + + invalidateAdamToken(); } - - void migrate(Entry entry) { + + void invalidateAdamToken() { + HttpEntity entity = new HttpEntity("", adamHeaders); + ResponseEntity response = restTemplate.exchange("https://edam-q.bayer.com:2015/auth", HttpMethod.DELETE, + entity, String.class); + log.info("Logged out from adam API: " + response.toString()); + } + + void postToAdam(Entry entry) { log.info("Migrating entry " + entry.id); + WriteRecord newRecord = new WriteRecord(); + + HttpEntity httpEntity = new HttpEntity(newRecord, adamHeaders); + try { + URI location = restTemplate.postForLocation("https://edam-q.bayer.com:2015/records", httpEntity, WriteRecord.class); + log.info("Location: " + location); + } + catch (HttpClientErrorException hcee) { + log.error(hcee.getStatusCode().toString()); + log.error(hcee.getResponseBodyAsString()); + } } - - public static void main(String args[]) { - SpringApplication.run(MamMigrationSelective.class, args); - } - + + private HttpHeaders createMamHeaders() { + HttpHeaders headers = new HttpHeaders(); + headers.add("Accept", "application/json"); + headers.add("Authorization", "Basic " + mamAuth); + headers.add("Content-Type", "application/json"); + headers.add("From", "migration@bayer.com"); + + return headers; + } + + private HttpHeaders createAdamHeaders() { + HttpHeaders headers = new HttpHeaders(); + headers.add("Registration", "ADAMBAYER"); + headers.add("API-VERSION", "1"); + headers.add("Accept", "application/hal+json"); + headers.add("Content-Type", "application/json"); + headers.add("Authorization", "Basic " + adamAuth); + HttpEntity entity = new HttpEntity("", headers); + Token token = restTemplate.exchange("https://edam-q.bayer.com:2015/auth", HttpMethod.GET, entity, Token.class) + .getBody(); + + // replace auth header + headers.remove("Authorization"); + headers.add("Authorization", "Token " + token.getToken()); + + // replace accept-charset header (in order to declutter log) + headers.add("Accept-Charset", "us-ascii, utf-16, utf-8"); + + return headers; + } + + public static void main(String args[]) { + SpringApplication.run(MamMigrationSelective.class, args); + } + } diff --git a/AH MAM Migration/src/com/bayer/edam/migration/MediaAssistantMigrationSimple.java b/AH MAM Migration/src/com/bayer/edam/migration/MediaAssistantMigrationSimple.java index dab37e5..b6487e4 100644 --- a/AH MAM Migration/src/com/bayer/edam/migration/MediaAssistantMigrationSimple.java +++ b/AH MAM Migration/src/com/bayer/edam/migration/MediaAssistantMigrationSimple.java @@ -4,7 +4,9 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URI; +import java.util.ArrayList; import java.util.Collections; +import java.util.List; import java.util.Properties; import org.slf4j.Logger; @@ -14,10 +16,15 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; +import org.springframework.http.client.BufferingClientHttpRequestFactory; +import org.springframework.http.client.ClientHttpRequestInterceptor; +import org.springframework.http.client.SimpleClientHttpRequestFactory; +import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.RestTemplate; import com.bayer.edam.migration.adam.ReadRecord; import com.bayer.edam.migration.adam.Token; +import com.bayer.edam.migration.adam.WriteRecord; import com.bayer.edam.migration.mam.Entry; public class MediaAssistantMigrationSimple { @@ -36,7 +43,10 @@ public class MediaAssistantMigrationSimple { - RestTemplate restTemplate = new RestTemplate(); + RestTemplate restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory())); + List interceptors = new ArrayList(); + interceptors.add(new LoggingRequestInterceptor()); + restTemplate.setInterceptors(interceptors); HttpHeaders headers = new HttpHeaders(); headers.add("Registration", "ADAMBAYER"); @@ -54,20 +64,40 @@ public class MediaAssistantMigrationSimple { headers.add("Content-Type", "application/json"); entity = new HttpEntity("Parameters", headers); - ResponseEntity r1 = restTemplate.exchange("https://edam-q.bayer.com:2015/record/f46147866c6f49f690ffa7900097b861", HttpMethod.GET, entity, String.class); + ResponseEntity r1 = restTemplate.exchange("https://edam-q.bayer.com:2015/record/f5bccf48-6695-4255-8d70-a7e5008e1720", HttpMethod.GET, entity, String.class); + log.info(r1.getBody().toString()); + r1 = restTemplate.exchange("https://edam-q.bayer.com:2015/record/f5bccf48-6695-4255-8d70-a7e5008e1720/fields", HttpMethod.GET, entity, String.class); + log.info(r1.getBody().toString()); + r1 = restTemplate.exchange("https://edam-q.bayer.com:2015/record/f5bccf48-6695-4255-8d70-a7e5008e1720/classifications", HttpMethod.GET, entity, String.class); log.info(r1.getBody().toString()); ResponseEntity r2 = restTemplate.exchange("https://edam-q.bayer.com:2015/record/f46147866c6f49f690ffa7900097b861", HttpMethod.GET, entity, ReadRecord.class); log.info(r2.getBody().toString()); - HttpEntity newRecord = new HttpEntity("{\"contentType\": \"Record\"}", headers); - URI location = restTemplate.postForLocation("https://edam-q.bayer.com:2015/records", newRecord); - log.info("Location: " + location); + HttpEntity newRecordString = new HttpEntity("{'contentType': 'Record'}", headers); -// log.info("Entity: " + entity.getBody()); + try { + URI location = restTemplate.postForLocation("https://edam-q.bayer.com:2015/records", newRecordString); + log.info("Location: " + location); + } + catch (HttpClientErrorException hcee) { + log.error(hcee.getStatusCode().toString()); + log.error(hcee.getResponseBodyAsString()); + } -// ResponseEntity r3 = restTemplate.exchange("https://edam-q.bayer.com:2015/records", HttpMethod.POST, newRecord) + HttpEntity newRecord = new HttpEntity(new WriteRecord(), headers); + try { + URI location = restTemplate.postForLocation("https://edam-q.bayer.com:2015/records", newRecord, WriteRecord.class); + log.info("Location: " + location); + } + catch (HttpClientErrorException hcee) { + log.error(hcee.getStatusCode().toString()); + log.error(hcee.getResponseBodyAsString()); + } + +// ResponseEntity r3 = restTemplate.exchange("https://edam-q.bayer.com:2015/records", HttpMethod.POST, newRecord, String.class); +// log.info(r3.getBody()); headers = new HttpHeaders(); @@ -83,10 +113,9 @@ public class MediaAssistantMigrationSimple { headers.add("Authorization", "Basic " + prop.getProperty("mam.auth")); headers.add("From", "migration@bayer.com"); headers.add("Content-Type", "application/json"); - entity = new HttpEntity("Parameters", headers); + entity = new HttpEntity("", headers); ResponseEntity m1 = restTemplate.exchange("https://service.media-assistant.animalhealth.bayer.com/rest/migration/entry/71948", HttpMethod.GET, entity, Entry.class); log.info(m1.toString()); - } public static void main(String args[]) { diff --git a/AH MAM Migration/src/com/bayer/edam/migration/adam/WriteRecord.java b/AH MAM Migration/src/com/bayer/edam/migration/adam/WriteRecord.java index 8dac444..8acb749 100644 --- a/AH MAM Migration/src/com/bayer/edam/migration/adam/WriteRecord.java +++ b/AH MAM Migration/src/com/bayer/edam/migration/adam/WriteRecord.java @@ -1,41 +1,78 @@ package com.bayer.edam.migration.adam; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + public class WriteRecord { public static class Classification { public String id; public Integer sortIndex; - - public Classification() { + + public Classification(String id, Integer sortIndex) { + this.id = id; + this.sortIndex = sortIndex; } } public static class Classifications { - public Classification[] addOrUpdate; - - public Classifications() { + public List addOrUpdate = new ArrayList(); + } + + public static class LocalizedValue { + public String languageId = "00000000000000000000000000000000"; + public String value; + public List values = new ArrayList(); + + public LocalizedValue(String value) { + this.value = value; + } + + public LocalizedValue(List values) { + this.values = values; } } public static class Field { public String id; + public List localizedValues = new ArrayList(); - public Field() { + public Field(String id) { + this.id = id; } } public static class Fields { - public Field[] addOrUpdate; - - public Fields() { - } + public List addOrUpdate = new ArrayList(); } public String contentType = "Record"; - public Classifications classifications; - + public Classifications classifications = new Classifications(); + public Fields fields = new Fields(); + public WriteRecord() { + // XXX some default values + classifications.addOrUpdate.add(new Classification("dff7666b-326b-4e1c-912c-a7b4006c8500", 1)); // Global + classifications.addOrUpdate.add(new Classification("8de5ef0e-3c6d-451c-b82b-a7b4006c954c", 1)); // Bayer AH + classifications.addOrUpdate.add(new Classification("306e050a-dd58-46ad-93a7-a73100a2547f", 1)); // CAP + classifications.addOrUpdate.add(new Classification("7c13e620-7eb0-4c12-9dfb-a7b4008f19b1", 1)); // Other + classifications.addOrUpdate.add(new Classification("dfee7cb7-fffc-41cb-9ee9-a68a00d4f0c3", 1)); // Draft + classifications.addOrUpdate.add(new Classification("6b95a635-40e9-4782-ad5f-a73100a2bb94", 1)); // Unrestricted + + Field field = new Field("31f337fa-add6-4e25-a672-a68a00d4ff3b"); // Title + field.localizedValues.add(new LocalizedValue("Happy Doggy")); + fields.addOrUpdate.add(field); + + field = new Field("5400d446-9d95-4158-ac06-a6dd00d06a12"); // Project Lead + field.localizedValues.add(new LocalizedValue(Collections.singletonList("21cfce6116484269a351a71300e1fed0"))); + fields.addOrUpdate.add(field); + + field = new Field("de3f83cb50e747228bfea731010bc27c"); // License Holder + field.localizedValues.add(new LocalizedValue(Collections.singletonList("4b82f989fae54a309e17a7470073c36b"))); + fields.addOrUpdate.add(field); + } }