first asset created

This commit is contained in:
gezvf
2017-09-14 12:27:02 +02:00
parent 9d7d3b6776
commit 5354283f07
4 changed files with 214 additions and 48 deletions
@@ -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================================================");
}
}
@@ -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,9 +14,15 @@ 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
@@ -24,38 +32,95 @@ public class MamMigrationSelective implements CommandLineRunner {
private static final Logger log = LoggerFactory.getLogger(MamMigrationSelective.class);
@Value("${mam.auth}")
private String mamAuth;
private String mamAuth;
static int[] selection = {71048,70778,70746/*,70142,70361,63924,71263,63201,56942,57641,70745,70440,70340,70288,71725*/};
@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 {
public void run(String... strings) throws Exception {
RestTemplate restTemplate = new RestTemplate();
restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()));
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
interceptors.add(new LoggingRequestInterceptor());
restTemplate.setInterceptors(interceptors);
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<String> entity = new HttpEntity<String>("Parameters", headers);
adamHeaders = createAdamHeaders();
mamHeaders = createMamHeaders();
HttpEntity<String> entity = new HttpEntity<String>("", 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<String> entity = new HttpEntity<String>("", adamHeaders);
ResponseEntity<String> 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<WriteRecord> httpEntity = new HttpEntity<WriteRecord>(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<String> entity = new HttpEntity<String>("", 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);
}
}
@@ -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<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
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<String>("Parameters", headers);
ResponseEntity<String> r1 = restTemplate.exchange("https://edam-q.bayer.com:2015/record/f46147866c6f49f690ffa7900097b861", HttpMethod.GET, entity, String.class);
ResponseEntity<String> 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<ReadRecord> r2 = restTemplate.exchange("https://edam-q.bayer.com:2015/record/f46147866c6f49f690ffa7900097b861", HttpMethod.GET, entity, ReadRecord.class);
log.info(r2.getBody().toString());
HttpEntity<String> newRecord = new HttpEntity<String>("{\"contentType\": \"Record\"}", headers);
URI location = restTemplate.postForLocation("https://edam-q.bayer.com:2015/records", newRecord);
log.info("Location: " + location);
HttpEntity<String> newRecordString = new HttpEntity<String>("{'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<String> r3 = restTemplate.exchange("https://edam-q.bayer.com:2015/records", HttpMethod.POST, newRecord)
HttpEntity<WriteRecord> newRecord = new HttpEntity<WriteRecord>(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<String> 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<String>("Parameters", headers);
entity = new HttpEntity<String>("", headers);
ResponseEntity<Entry> 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[]) {
@@ -1,5 +1,9 @@
package com.bayer.edam.migration.adam;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class WriteRecord {
@@ -7,35 +11,68 @@ public class WriteRecord {
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 List<Classification> addOrUpdate = new ArrayList<Classification>();
}
public Classifications() {
public static class LocalizedValue {
public String languageId = "00000000000000000000000000000000";
public String value;
public List<String> values = new ArrayList<String>();
public LocalizedValue(String value) {
this.value = value;
}
public LocalizedValue(List<String> values) {
this.values = values;
}
}
public static class Field {
public String id;
public List<LocalizedValue> localizedValues = new ArrayList<LocalizedValue>();
public Field() {
public Field(String id) {
this.id = id;
}
}
public static class Fields {
public Field[] addOrUpdate;
public Fields() {
}
public List<Field> addOrUpdate = new ArrayList<Field>();
}
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);
}
}