From acc1229fc60e3db86df4c5f5884d37b364cb8612 Mon Sep 17 00:00:00 2001 From: Baptiste Toulemonde <toulemonde@cines.fr> Date: Wed, 5 Jan 2022 11:27:15 +0100 Subject: [PATCH] fix request to publish --- .../SmartHarvesterMappingController.java | 14 +++++++--- .../mapping/request/PublishedRequest.java | 27 +++++++++++++++++++ .../service/MappingService.java | 15 ++++++++++- 3 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/smartharvester/model/mapping/request/PublishedRequest.java diff --git a/src/main/java/com/smartharvester/controller/SmartHarvesterMappingController.java b/src/main/java/com/smartharvester/controller/SmartHarvesterMappingController.java index 939b55c..01265fb 100644 --- a/src/main/java/com/smartharvester/controller/SmartHarvesterMappingController.java +++ b/src/main/java/com/smartharvester/controller/SmartHarvesterMappingController.java @@ -1,5 +1,6 @@ package com.smartharvester.controller; +import java.net.URI; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @@ -74,22 +75,29 @@ public class SmartHarvesterMappingController { properties = this.mappingService.getValues(url, paths); String dataset = this.mappingService.getDatasetString(catalogId, properties.get(0), fdpUrl); + String locationDataset = null; try { responseEntity = this.mappingService.asyncPostToFdp("/dataset", fdpUrl, dataset, fdpToken); - String location = responseEntity.getHeaders().getLocation().toString(); - datasetId = location.substring(location.indexOf("dataset/") + 8); + locationDataset = responseEntity.getHeaders().getLocation().toString(); + datasetId = locationDataset.substring(locationDataset.indexOf("dataset/") + 8); + HttpStatus statusCode = responseEntity.getStatusCode(); if (distributionPaths.size() > 0) { String distribution = this.mappingService.getDistributionString(datasetId, properties.get(1), fdpUrl); try { - this.mappingService.asyncPostToFdp("/distribution", fdpUrl, distribution, fdpToken); + ResponseEntity<String> distributionResponse = this.mappingService.asyncPostToFdp("/distribution", fdpUrl, distribution, fdpToken); + String locationDistribution = distributionResponse.getHeaders().getLocation().toString(); + if(distributionResponse.getStatusCode().value() == 201) { + this.mappingService.draftToPublished(fdpToken, locationDistribution); + } } catch (HttpStatusCodeException e) { LOGGER.warn(e.getMessage()); } } if (statusCode.value() == 201) { publishedUrl.add(url); + this.mappingService.draftToPublished(fdpToken, locationDataset); } else { notPublishedUrl.add(url); } diff --git a/src/main/java/com/smartharvester/model/mapping/request/PublishedRequest.java b/src/main/java/com/smartharvester/model/mapping/request/PublishedRequest.java new file mode 100644 index 0000000..e204afc --- /dev/null +++ b/src/main/java/com/smartharvester/model/mapping/request/PublishedRequest.java @@ -0,0 +1,27 @@ +package com.smartharvester.model.mapping.request; + +public class PublishedRequest { + + private String current; + + /** + * @return the current + */ + public String getCurrent() { + return current; + } + + /** + * @param current the current to set + */ + public void setCurrent(String current) { + this.current = current; + } + + public PublishedRequest(String current) { + this.current = current; + } + + + +} diff --git a/src/main/java/com/smartharvester/service/MappingService.java b/src/main/java/com/smartharvester/service/MappingService.java index 5a39a67..36f3eb3 100644 --- a/src/main/java/com/smartharvester/service/MappingService.java +++ b/src/main/java/com/smartharvester/service/MappingService.java @@ -24,6 +24,7 @@ import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; @@ -31,6 +32,7 @@ import org.springframework.web.client.HttpClientErrorException.BadRequest; import org.springframework.web.client.RequestCallback; import org.springframework.web.client.RestTemplate; import com.smartharvester.model.mapping.Path; +import com.smartharvester.model.mapping.request.PublishedRequest; @Service public class MappingService { @@ -143,7 +145,7 @@ public class MappingService { .filter(e -> e.getProperty().equals("dct:publisher")).collect(Collectors.toList()).size() == 0) { datasetProperties += "dct:publisher [ a foaf:Agent; foaf:name \"undefined\"];\n"; } - distributionProperties += "dct:hasVersion \"null\";\ndct:publisher [ a foaf:Agent; foaf:name \\\"undefined\\\"];\\n"; + distributionProperties += "dct:hasVersion \"null\";\ndct:publisher [ a foaf:Agent; foaf:name \"undefined\"];\n"; for (Path path : paths) { try { String[] array = path.getPath().split(" : "); @@ -309,5 +311,16 @@ public class MappingService { return this.restTemplate.exchange(fdpUrl + path, HttpMethod.POST, entity, String.class); } + + public void draftToPublished(String fdpToken, String url) { + HttpHeaders headers = new HttpHeaders(); + + headers.setBearerAuth(fdpToken); + headers.setContentType(MediaType.APPLICATION_JSON); + + HttpEntity<PublishedRequest> entity = new HttpEntity<PublishedRequest>(new PublishedRequest("PUBLISHED"), headers); + + this.restTemplate.exchange(url + "/meta/state", HttpMethod.PUT, entity, Void.class); + } } -- GitLab