multiple configuration for repository

This commit is contained in:
Aldo Mihasi 2023-06-21 12:35:52 +03:00
parent 7510d27d9c
commit 1968334fdb
7 changed files with 220 additions and 190 deletions

View File

@ -51,7 +51,7 @@
<dependency> <dependency>
<groupId>gr.cite.opendmp</groupId> <groupId>gr.cite.opendmp</groupId>
<artifactId>repositorydepositbase</artifactId> <artifactId>repositorydepositbase</artifactId>
<version>1.0.3</version> <version>1.0.4</version>
</dependency> </dependency>
<dependency> <dependency>

View File

@ -7,6 +7,6 @@ public interface ConfigLoader {
List<String> getRelatedIdentifiers(); List<String> getRelatedIdentifiers();
List<String> getAcceptedPidTypes(); List<String> getAcceptedPidTypes();
PidFieldNames getPidFieldNames(); PidFieldNames getPidFieldNames();
byte[] getLogo(); byte[] getLogo(String repositoryId);
ZenodoConfig getZenodoConfig(); List<ZenodoConfig> getZenodoConfig();
} }

View File

@ -1,5 +1,6 @@
package eu.eudat.depositinterface.zenodorepository.config; package eu.eudat.depositinterface.zenodorepository.config;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -22,9 +23,9 @@ public class ConfigLoaderImpl implements ConfigLoader{
private List<String> relatedIdentifiers = new ArrayList<>(); private List<String> relatedIdentifiers = new ArrayList<>();
private List<String> acceptedPidTypes = new ArrayList<>(); private List<String> acceptedPidTypes = new ArrayList<>();
private PidFieldNames pidFieldNames = new PidFieldNames(); private PidFieldNames pidFieldNames = new PidFieldNames();
private ZenodoConfig zenodoConfig; private List<ZenodoConfig> zenodoConfig = new ArrayList<>();
private Environment environment; private final Environment environment;
@Autowired @Autowired
public ConfigLoaderImpl(Environment environment){ public ConfigLoaderImpl(Environment environment){
@ -35,7 +36,7 @@ public class ConfigLoaderImpl implements ConfigLoader{
public List<DOIFunder> getDOIFunders() { public List<DOIFunder> getDOIFunders() {
if (doiFunders == null || doiFunders.isEmpty()) { if (doiFunders == null || doiFunders.isEmpty()) {
try { try {
List<Map<String, Object>> tempdoiFunders = mapper.readValue(getStreamFromPath(environment.getProperty("configuration.doi_funder")), List.class); List<Map<String, Object>> tempdoiFunders = mapper.readValue(getStreamFromPath(environment.getProperty("zenodo_plugin.configuration.doi_funder")), List.class);
doiFunders = tempdoiFunders.stream().map(map -> mapper.convertValue(map, DOIFunder.class) ).collect(Collectors.toList()); doiFunders = tempdoiFunders.stream().map(map -> mapper.convertValue(map, DOIFunder.class) ).collect(Collectors.toList());
} catch (IOException e) { } catch (IOException e) {
logger.error(e.getLocalizedMessage(), e); logger.error(e.getLocalizedMessage(), e);
@ -74,10 +75,10 @@ public class ConfigLoaderImpl implements ConfigLoader{
} }
@Override @Override
public ZenodoConfig getZenodoConfig() { public List<ZenodoConfig> getZenodoConfig() {
if (zenodoConfig == null) { if (zenodoConfig == null || zenodoConfig.isEmpty()) {
try { try {
zenodoConfig = mapper.readValue(getStreamFromPath(environment.getProperty("configuration.zenodo")), ZenodoConfig.class); zenodoConfig = mapper.readValue(getStreamFromPath(environment.getProperty("zenodo_plugin.configuration.zenodo")), new TypeReference<List<ZenodoConfig>>() {});
} catch (IOException e) { } catch (IOException e) {
logger.error(e.getLocalizedMessage(), e); logger.error(e.getLocalizedMessage(), e);
return null; return null;
@ -87,20 +88,29 @@ public class ConfigLoaderImpl implements ConfigLoader{
} }
@Override @Override
public byte[] getLogo() { public byte[] getLogo(String repositoryId) {
String logo = "zenodo.jpg"; if (!zenodoConfig.isEmpty()) {
if(logo != null && !logo.isEmpty()){ ZenodoConfig zenodoConfig = getZenodoConfig().stream().filter(x -> x.getRepositoryId().equals(repositoryId)).findFirst().orElse(null);
InputStream logoStream = getStreamFromPath(logo); if (zenodoConfig != null) {
String logo = zenodoConfig.getLogo();
InputStream logoStream;
if (logo != null && !logo.isEmpty()) {
logoStream = getStreamFromPath(logo);
}
else {
logoStream = getClass().getClassLoader().getResourceAsStream("zenodo.jpg");
}
try { try {
return logoStream.readAllBytes(); return (logoStream != null) ? logoStream.readAllBytes() : null;
} }
catch (IOException e){ catch (IOException e) {
logger.error(e.getMessage(), e); logger.error(e.getMessage(), e);
return null;
} }
} }
return null; return null;
} }
return null;
}
private InputStream getStreamFromPath(String filePath) { private InputStream getStreamFromPath(String filePath) {
try { try {

View File

@ -54,6 +54,8 @@ public class ZenodoConfig {
private String redirectUri; private String redirectUri;
@JsonProperty("hasLogo") @JsonProperty("hasLogo")
private boolean hasLogo; private boolean hasLogo;
@JsonProperty("logo")
private String logo;
public int getDepositType() { public int getDepositType() {
return depositType; return depositType;
@ -132,6 +134,13 @@ public class ZenodoConfig {
this.hasLogo = hasLogo; this.hasLogo = hasLogo;
} }
public String getLogo() {
return logo;
}
public void setLogo(String logo) {
this.logo = logo;
}
public RepositoryDepositConfiguration toRepoConfig() { public RepositoryDepositConfiguration toRepoConfig() {
RepositoryDepositConfiguration config = new RepositoryDepositConfiguration(); RepositoryDepositConfiguration config = new RepositoryDepositConfiguration();
config.setDepositType(this.depositType); config.setDepositType(this.depositType);

View File

@ -24,14 +24,15 @@ import org.springframework.web.client.RestTemplate;
import java.io.*; import java.io.*;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
@Component @Component
public class ZenodoDeposit implements RepositoryDeposit { public class ZenodoDeposit implements RepositoryDeposit {
private static final Logger logger = LoggerFactory.getLogger(ZenodoDeposit.class); private static final Logger logger = LoggerFactory.getLogger(ZenodoDeposit.class);
private static final ObjectMapper objectMapper = new ObjectMapper(); private static final ObjectMapper objectMapper = new ObjectMapper();
private ConfigLoader configLoader; private final ConfigLoader configLoader;
private Environment environment; private final Environment environment;
@Autowired @Autowired
public ZenodoDeposit(ConfigLoader configLoader, Environment environment){ public ZenodoDeposit(ConfigLoader configLoader, Environment environment){
@ -40,11 +41,13 @@ public class ZenodoDeposit implements RepositoryDeposit {
} }
@Override @Override
public String deposit(DMPDepositModel dmpDepositModel, String zenodoToken) throws Exception { public String deposit(String repositoryId, DMPDepositModel dmpDepositModel, String zenodoToken) throws Exception {
RepositoryDepositConfiguration conf = this.getConfiguration(); RepositoryDepositConfiguration conf = this.getConfiguration().stream().filter(x -> x.getRepositoryId().equals(repositoryId)).findFirst().orElse(null);
if(zenodoToken == null){ if(conf != null) {
if (zenodoToken == null) {
zenodoToken = conf.getAccessToken(); zenodoToken = conf.getAccessToken();
} }
@ -70,8 +73,7 @@ public class ZenodoDeposit implements RepositoryDeposit {
String createUrl = zenodoUrl + "deposit/depositions" + "?access_token=" + zenodoToken; String createUrl = zenodoUrl + "deposit/depositions" + "?access_token=" + zenodoToken;
createResponse = restTemplate.postForEntity(createUrl, request, Map.class).getBody(); createResponse = restTemplate.postForEntity(createUrl, request, Map.class).getBody();
links = (LinkedHashMap<String, String>) createResponse.get("links"); links = (LinkedHashMap<String, String>) createResponse.get("links");
} } else {
else {
unpublishedUrl = this.getUnpublishedDOI(zenodoUrl, previousDOI, zenodoToken, dmpDepositModel.getVersion()); unpublishedUrl = this.getUnpublishedDOI(zenodoUrl, previousDOI, zenodoToken, dmpDepositModel.getVersion());
if (unpublishedUrl == null) { if (unpublishedUrl == null) {
//It requires more than one step to create a new version //It requires more than one step to create a new version
@ -113,8 +115,7 @@ public class ZenodoDeposit implements RepositoryDeposit {
restTemplate.delete(latestDraftUrl); restTemplate.delete(latestDraftUrl);
throw e; throw e;
} }
} } else {
else {
String listUrl = zenodoUrl + "deposit/depositions" + "?q=conceptdoi:\"" + previousDOI + "\"&access_token=" + zenodoToken; String listUrl = zenodoUrl + "deposit/depositions" + "?q=conceptdoi:\"" + previousDOI + "\"&access_token=" + zenodoToken;
ResponseEntity<Map[]> listResponses = restTemplate.getForEntity(listUrl, Map[].class); ResponseEntity<Map[]> listResponses = restTemplate.getForEntity(listUrl, Map[].class);
createResponse = listResponses.getBody()[0]; createResponse = listResponses.getBody()[0];
@ -144,7 +145,7 @@ public class ZenodoDeposit implements RepositoryDeposit {
ResponseEntity<byte[]> jsonFile = new ResponseEntity<>(content, responseHeaders, HttpStatus.OK); ResponseEntity<byte[]> jsonFile = new ResponseEntity<>(content, responseHeaders, HttpStatus.OK);
UUID jsonFileUUID = UUID.randomUUID(); UUID jsonFileUUID = UUID.randomUUID();
File tempJsonFile = new File(this.environment.getProperty("storage.temp") + jsonFileUUID + ".json"); File tempJsonFile = new File(this.environment.getProperty("zenodo_plugin.storage.temp") + jsonFileUUID + ".json");
try (FileOutputStream jsonFos = new FileOutputStream(tempJsonFile)) { try (FileOutputStream jsonFos = new FileOutputStream(tempJsonFile)) {
jsonFos.write(jsonFile.getBody()); jsonFos.write(jsonFile.getBody());
jsonFos.flush(); jsonFos.flush();
@ -158,7 +159,7 @@ public class ZenodoDeposit implements RepositoryDeposit {
restTemplate.put(addFileUrl, addFileMapRequest); restTemplate.put(addFileUrl, addFileMapRequest);
Files.deleteIfExists(tempJsonFile.toPath()); Files.deleteIfExists(tempJsonFile.toPath());
if(dmpDepositModel.getSupportingFilesZip() != null) { if (dmpDepositModel.getSupportingFilesZip() != null) {
File supportinFilesZip = dmpDepositModel.getSupportingFilesZip(); File supportinFilesZip = dmpDepositModel.getSupportingFilesZip();
String supportinFilesZipName = dmpDepositModel.getSupportingFilesZip().getName(); String supportinFilesZipName = dmpDepositModel.getSupportingFilesZip().getName();
fileSystemResource = new FileSystemResource(supportinFilesZip); fileSystemResource = new FileSystemResource(supportinFilesZip);
@ -170,8 +171,7 @@ public class ZenodoDeposit implements RepositoryDeposit {
// Third post call to Zenodo to publish the entry and return the DOI. // Third post call to Zenodo to publish the entry and return the DOI.
publishUrl = links.get("publish") + "?access_token=" + zenodoToken; publishUrl = links.get("publish") + "?access_token=" + zenodoToken;
} } else {
else {
publishUrl = unpublishedUrl + "?access_token=" + zenodoToken; publishUrl = unpublishedUrl + "?access_token=" + zenodoToken;
} }
@ -184,6 +184,10 @@ public class ZenodoDeposit implements RepositoryDeposit {
} }
return null;
}
private String publish(String publishUrl){ private String publish(String publishUrl){
RestTemplate restTemplate = new RestTemplate(); RestTemplate restTemplate = new RestTemplate();
Map<String, Object> publishResponce = restTemplate.postForObject(publishUrl, "", Map.class); Map<String, Object> publishResponce = restTemplate.postForObject(publishUrl, "", Map.class);
@ -192,15 +196,17 @@ public class ZenodoDeposit implements RepositoryDeposit {
@Override @Override
public RepositoryDepositConfiguration getConfiguration() { public List<RepositoryDepositConfiguration> getConfiguration() {
ZenodoConfig zenodoConfig = this.configLoader.getZenodoConfig(); List<ZenodoConfig> zenodoConfigs = this.configLoader.getZenodoConfig();
return (zenodoConfig != null) ? zenodoConfig.toRepoConfig() : null; return (zenodoConfigs != null) ? zenodoConfigs.stream().map(ZenodoConfig::toRepoConfig).collect(Collectors.toList()) : null;
} }
@Override @Override
public String authenticate(String code){ public String authenticate(String repositoryId, String code){
RepositoryDepositConfiguration conf = this.getConfiguration(); RepositoryDepositConfiguration conf = this.getConfiguration().stream().filter(x -> x.getRepositoryId().equals(repositoryId)).findFirst().orElse(null);
if(conf != null) {
RestTemplate restTemplate = new RestTemplate(); RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
@ -219,11 +225,11 @@ public class ZenodoDeposit implements RepositoryDeposit {
Map<String, Object> values = restTemplate.postForObject(conf.getRepositoryAccessTokenUrl(), request, Map.class); Map<String, Object> values = restTemplate.postForObject(conf.getRepositoryAccessTokenUrl(), request, Map.class);
//ZenodoResponseToken zenodoResponseToken = new ZenodoResponseToken(); //ZenodoResponseToken zenodoResponseToken = new ZenodoResponseToken();
Map<String, Object> user = (Map<String, Object>) values.get("user"); Map<String, Object> user = (Map<String, Object>) values.get("user");
// zenodoResponseToken.setUserId((String) user.get("id")); // zenodoResponseToken.setUserId((String) user.get("id"));
// zenodoResponseToken.setEmail((String) user.get("email")); // zenodoResponseToken.setEmail((String) user.get("email"));
// zenodoResponseToken.setExpiresIn((Integer) values.get("expires_in")); // zenodoResponseToken.setExpiresIn((Integer) values.get("expires_in"));
// zenodoResponseToken.setAccessToken((String) values.get("access_token")); // zenodoResponseToken.setAccessToken((String) values.get("access_token"));
// zenodoResponseToken.setRefreshToken((String) values.get("refresh_token")); // zenodoResponseToken.setRefreshToken((String) values.get("refresh_token"));
//return zenodoResponseToken; //return zenodoResponseToken;
return (String) values.get("access_token"); return (String) values.get("access_token");
@ -231,16 +237,22 @@ public class ZenodoDeposit implements RepositoryDeposit {
logger.error(ex.getResponseBodyAsString(), ex); logger.error(ex.getResponseBodyAsString(), ex);
} }
return null;
}
return null; return null;
} }
@Override @Override
public String getLogo() { public String getLogo(String repositoryId) {
RepositoryDepositConfiguration conf = this.getConfiguration(); RepositoryDepositConfiguration conf = this.getConfiguration().stream().filter(x -> x.getRepositoryId().equals(repositoryId)).findFirst().orElse(null);
if(conf != null) {
if(conf.isHasLogo()){ if(conf.isHasLogo()){
byte[] logo = this.configLoader.getLogo(); byte[] logo = this.configLoader.getLogo(repositoryId);
return (logo != null && logo.length != 0) ? Base64.getEncoder().encodeToString(logo) : null; return (logo != null && logo.length != 0) ? Base64.getEncoder().encodeToString(logo) : null;
} }
}
return null; return null;
} }

View File

@ -171,7 +171,7 @@ public class DMPToZenodoMapper {
deposit.getMetadata().setPublicationType("datamanagementplan"); deposit.getMetadata().setPublicationType("datamanagementplan");
deposit.getMetadata().setDescription((dmp.getDescription() != null && !dmp.getDescription().isEmpty() ? dmp.getDescription() : "<p></p>")); deposit.getMetadata().setDescription((dmp.getDescription() != null && !dmp.getDescription().isEmpty() ? dmp.getDescription() : "<p></p>"));
deposit.getMetadata().setVersion(String.valueOf(dmp.getVersion())); deposit.getMetadata().setVersion(String.valueOf(dmp.getVersion()));
String zenodoCommunity = environment.getProperty("zenodo.community"); String zenodoCommunity = environment.getProperty("zenodo_plugin.zenodo.community");
if(zenodoCommunity != null && !zenodoCommunity.isEmpty()) { if(zenodoCommunity != null && !zenodoCommunity.isEmpty()) {
ZenodoComunity community = new ZenodoComunity(); ZenodoComunity community = new ZenodoComunity();
community.setIdentifier(zenodoCommunity); community.setIdentifier(zenodoCommunity);
@ -200,11 +200,11 @@ public class DMPToZenodoMapper {
} }
if (dmp.isPublic()) { if (dmp.isPublic()) {
ZenodoRelator relator = new ZenodoRelator(); ZenodoRelator relator = new ZenodoRelator();
relator.setIdentifier(environment.getProperty("zenodo.domain") + "/external/zenodo/" + dmp.getId().toString()); relator.setIdentifier(environment.getProperty("zenodo_plugin.zenodo.domain") + "/external/zenodo/" + dmp.getId().toString());
relator.setRelation("isIdenticalTo"); relator.setRelation("isIdenticalTo");
deposit.getMetadata().getRelatedIdentifiers().add(relator); deposit.getMetadata().getRelatedIdentifiers().add(relator);
} }
String zenodoAffiliation = environment.getProperty("zenodo.affiliation"); String zenodoAffiliation = environment.getProperty("zenodo_plugin.zenodo.affiliation");
List<ZenodoContributor> contributors1 = dmp.getUsers().stream().map(userDMP -> { List<ZenodoContributor> contributors1 = dmp.getUsers().stream().map(userDMP -> {
ZenodoContributor contributor = new ZenodoContributor(); ZenodoContributor contributor = new ZenodoContributor();
contributor.setName(userDMP.getUser().getName()); contributor.setName(userDMP.getUser().getName());

View File

@ -1,7 +1,6 @@
configuration.doi_funder=DOI_Funder.json zenodo_plugin.configuration.doi_funder=${ZENODO_DOI_DUNDER}
configuration.zenodo.logo=${CONFIGURATION_LOGO_ZENODO} zenodo_plugin.storage.temp=${STORAGE_TMP_ZENODO}
storage.temp=${STORAGE_TMP_ZENODO} zenodo_plugin.configuration.zenodo=${CONFIGURATION_ZENODO}
configuration.zenodo=${CONFIGURATION_ZENODO} zenodo_plugin.zenodo.community=${ZENODO_COMMUNITY}
zenodo.community=argos zenodo_plugin.zenodo.affiliation=${ZENODO_AFFILIATION}
zenodo.affiliation=ARGOS zenodo_plugin.zenodo.domain=${ZENODO_OPENDMP_DOMAIN}
zenodo.domain=https://argos.openaire.eu/