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
@@ -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<String> entity = new HttpEntity<String>("Parameters", headers);
public void run(String... strings) throws Exception {
restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()));
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
interceptors.add(new LoggingRequestInterceptor());
restTemplate.setInterceptors(interceptors);
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);
}
}