diff --git a/solidify-controller/src/main/java/ch/unige/solidify/config/SolidifyProperties.java b/solidify-controller/src/main/java/ch/unige/solidify/config/SolidifyProperties.java index 7f8392bacbf4c077892536c018ede8bce4a2697b..e3d29e4c0e335449b2f8ec987b3a2a471c45683d 100644 --- a/solidify-controller/src/main/java/ch/unige/solidify/config/SolidifyProperties.java +++ b/solidify-controller/src/main/java/ch/unige/solidify/config/SolidifyProperties.java @@ -9,12 +9,12 @@ * it under the terms of the GNU General Public License as * published by the Free Software Foundation, either version 2 of the * License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public * License along with this program. If not, see * <http://www.gnu.org/licenses/gpl-2.0.html>. @@ -35,6 +35,7 @@ import org.springframework.validation.annotation.Validated; import jakarta.validation.Valid; +import ch.unige.solidify.model.OrcidWebsite; import ch.unige.solidify.service.GitInfoProperties; import ch.unige.solidify.util.StringTool; @@ -199,6 +200,8 @@ public class SolidifyProperties { private String scope = "/authenticate"; private String apiBaseUrl = "https://api.orcid.org/v3.0/"; + private OrcidWebsite[] websites = {}; + public String getClientId() { return this.clientId; } @@ -254,6 +257,14 @@ public class SolidifyProperties { public void setScope(String scope) { this.scope = scope; } + + public OrcidWebsite[] getWebsites() { + return this.websites; + } + + public void setWebsites(OrcidWebsite[] websites) { + this.websites = websites; + } } public static class Sitemap { diff --git a/solidify-model/src/main/java/ch/unige/solidify/model/OrcidWebsite.java b/solidify-model/src/main/java/ch/unige/solidify/model/OrcidWebsite.java new file mode 100644 index 0000000000000000000000000000000000000000..d0c35c4b976e87d341f7cd6a01a63f2360d6d529 --- /dev/null +++ b/solidify-model/src/main/java/ch/unige/solidify/model/OrcidWebsite.java @@ -0,0 +1,48 @@ +/*- + * %%---------------------------------------------------------------------------------------------- + * Solidify Framework - Solidify Model - OrcidWebsite.java + * SPDX-License-Identifier: GPL-2.0-or-later + * %----------------------------------------------------------------------------------------------% + * Copyright (C) 2017 - 2024 University of Geneva + * %----------------------------------------------------------------------------------------------% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-2.0.html>. + * ----------------------------------------------------------------------------------------------%% + */ + +package ch.unige.solidify.model; + +import java.util.ArrayList; +import java.util.List; + +public class OrcidWebsite { + private List<OrcidWebsiteLabel> labels = new ArrayList<>(); + + /** + * URL used to check if the ORCID exists on the website + */ + private String checkUrl; + + public List<OrcidWebsiteLabel> getLabels() { + return this.labels; + } + + public String getCheckUrl() { + return this.checkUrl; + } + + public void setCheckUrl(String checkUrl) { + this.checkUrl = checkUrl; + } +} diff --git a/solidify-model/src/main/java/ch/unige/solidify/model/OrcidWebsiteLabel.java b/solidify-model/src/main/java/ch/unige/solidify/model/OrcidWebsiteLabel.java new file mode 100644 index 0000000000000000000000000000000000000000..efbb6ef61aec3e6798472143059cc2a9fc0de6b7 --- /dev/null +++ b/solidify-model/src/main/java/ch/unige/solidify/model/OrcidWebsiteLabel.java @@ -0,0 +1,36 @@ +/*- + * %%---------------------------------------------------------------------------------------------- + * Solidify Framework - Solidify Model - OrcidWebsiteLabel.java + * SPDX-License-Identifier: GPL-2.0-or-later + * %----------------------------------------------------------------------------------------------% + * Copyright (C) 2017 - 2024 University of Geneva + * %----------------------------------------------------------------------------------------------% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-2.0.html>. + * ----------------------------------------------------------------------------------------------%% + */ + +package ch.unige.solidify.model; + +public class OrcidWebsiteLabel extends Label { + private String url; + + public String getUrl() { + return this.url; + } + + public void setUrl(String url) { + this.url = url; + } +} diff --git a/solidify-model/src/main/java/ch/unige/solidify/rest/ActionName.java b/solidify-model/src/main/java/ch/unige/solidify/rest/ActionName.java index 78a0081715aaa6295d1208f12fcbaf116f4490f8..f2f6e779e631d5a4c8d5bf894eedf4465316b61e 100644 --- a/solidify-model/src/main/java/ch/unige/solidify/rest/ActionName.java +++ b/solidify-model/src/main/java/ch/unige/solidify/rest/ActionName.java @@ -9,12 +9,12 @@ * it under the terms of the GNU General Public License as * published by the Free Software Foundation, either version 2 of the * License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public * License along with this program. If not, see * <http://www.gnu.org/licenses/gpl-2.0.html>. @@ -70,6 +70,7 @@ public class ActionName { public static final String SORT = "sort"; public static final String START = "start"; public static final String START_ORCID_AUTH = "start-orcid-auth"; + public static final String ORCID_EXTERNAL_WEBSITES = "external-websites"; public static final String STATUS = "status"; public static final String UPLOAD = "upload"; public static final String UPLOAD_ARCHIVE = "upload-archive"; diff --git a/solidify-orcid/src/main/java/ch/unige/solidify/controller/OrcidController.java b/solidify-orcid/src/main/java/ch/unige/solidify/controller/OrcidController.java index 14640df544077798e6d77cc35c783959a85ca486..10844e8a1679a9ab205cb214fd4371b543652ab8 100644 --- a/solidify-orcid/src/main/java/ch/unige/solidify/controller/OrcidController.java +++ b/solidify-orcid/src/main/java/ch/unige/solidify/controller/OrcidController.java @@ -23,11 +23,15 @@ package ch.unige.solidify.controller; +import java.util.List; + import org.json.JSONObject; import org.springframework.context.annotation.Profile; +import org.springframework.http.HttpEntity; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @@ -35,10 +39,12 @@ import org.springframework.web.bind.annotation.RestController; import com.fasterxml.jackson.core.JsonProcessingException; import ch.unige.solidify.SolidifyConstants; +import ch.unige.solidify.model.OrcidWebsiteDTO; import ch.unige.solidify.rest.ActionName; import ch.unige.solidify.security.EveryonePermissions; import ch.unige.solidify.security.UserPermissions; import ch.unige.solidify.service.OrcidOAuthService; +import ch.unige.solidify.service.OrcidWebsitesService; import ch.unige.solidify.util.StringTool; @RestController @@ -48,9 +54,11 @@ public class OrcidController { private static final String LOCATION_HEADER = "Location"; private final OrcidOAuthService orcidOAuthService; + private final OrcidWebsitesService externalWebsiteService; - public OrcidController(OrcidOAuthService orcidOAuthService) { + public OrcidController(OrcidOAuthService orcidOAuthService, OrcidWebsitesService externalWebsiteService) { this.orcidOAuthService = orcidOAuthService; + this.externalWebsiteService = externalWebsiteService; } @EveryonePermissions @@ -75,4 +83,11 @@ public class OrcidController { final JSONObject jsonObject = this.orcidOAuthService.startOrcidAuth(originUrl); return ResponseEntity.ok().body(jsonObject.toString()); } + + @EveryonePermissions + @GetMapping("/{orcid}/" + ActionName.ORCID_EXTERNAL_WEBSITES) + public HttpEntity<List<OrcidWebsiteDTO>> getExternalWebsitesByOrcid(@PathVariable String orcid) { + List<OrcidWebsiteDTO> orcidWebsites = this.externalWebsiteService.getAvailableWebsitesForOrcid(orcid); + return new ResponseEntity<>(orcidWebsites, HttpStatus.OK); + } } diff --git a/solidify-orcid/src/main/java/ch/unige/solidify/model/OrcidWebsiteDTO.java b/solidify-orcid/src/main/java/ch/unige/solidify/model/OrcidWebsiteDTO.java new file mode 100644 index 0000000000000000000000000000000000000000..753b67bae80a7b223a7ec5a00011b5e2d5b2cc46 --- /dev/null +++ b/solidify-orcid/src/main/java/ch/unige/solidify/model/OrcidWebsiteDTO.java @@ -0,0 +1,68 @@ +/*- + * %%---------------------------------------------------------------------------------------------- + * Solidify Framework - Solidify ORCID - OrcidWebsiteDTO.java + * SPDX-License-Identifier: GPL-2.0-or-later + * %----------------------------------------------------------------------------------------------% + * Copyright (C) 2017 - 2024 University of Geneva + * %----------------------------------------------------------------------------------------------% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-2.0.html>. + * ----------------------------------------------------------------------------------------------%% + */ +package ch.unige.solidify.model; + +import java.util.ArrayList; +import java.util.List; + +public class OrcidWebsiteDTO { + + private List<I18nLink> i18nWebsites = new ArrayList<>(); + + public OrcidWebsiteDTO(OrcidWebsite orcidWebsite, String orcid) { + for (OrcidWebsiteLabel orcidWebsiteLabel : orcidWebsite.getLabels()) { + String url = orcidWebsiteLabel.getUrl().replace("{orcid}", orcid); + String languageCode = orcidWebsiteLabel.getLanguage().getCode(); + String text = orcidWebsiteLabel.getText(); + this.i18nWebsites.add(new I18nLink(url, languageCode, text)); + } + } + + public List<I18nLink> getLinks() { + return this.i18nWebsites; + } + + private class I18nLink { + private String url; + private String languageCode; + private String text; + + public I18nLink(String url, String languageCode, String text) { + this.url = url; + this.languageCode = languageCode; + this.text = text; + } + + public String getUrl() { + return this.url; + } + + public String getLanguageCode() { + return this.languageCode; + } + + public String getText() { + return this.text; + } + } +} diff --git a/solidify-orcid/src/main/java/ch/unige/solidify/service/OrcidUrlChecker.java b/solidify-orcid/src/main/java/ch/unige/solidify/service/OrcidUrlChecker.java new file mode 100644 index 0000000000000000000000000000000000000000..dd45fb020b05a7e82733eb9e6f0c92d450c76dc1 --- /dev/null +++ b/solidify-orcid/src/main/java/ch/unige/solidify/service/OrcidUrlChecker.java @@ -0,0 +1,49 @@ +/*- + * %%---------------------------------------------------------------------------------------------- + * Solidify Framework - Solidify ORCID - OrcidUrlChecker.java + * SPDX-License-Identifier: GPL-2.0-or-later + * %----------------------------------------------------------------------------------------------% + * Copyright (C) 2017 - 2024 University of Geneva + * %----------------------------------------------------------------------------------------------% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-2.0.html>. + * ----------------------------------------------------------------------------------------------%% + */ +package ch.unige.solidify.service; + +import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.web.client.RestTemplate; + +/** + * This class only checks that the checkURL exists for a given ORCID + */ +public class OrcidUrlChecker extends OrcidWebsiteChecker { + + public OrcidUrlChecker(String checkUrl) { + super(checkUrl); + } + + @Override + public boolean checkOrcidExists(String orcid) { + try { + String url = this.getCheckUrl(orcid); + final RestTemplateBuilder restTemplateBuilder = new RestTemplateBuilder(); + final RestTemplate client = restTemplateBuilder.rootUri(url).build(); + client.headForHeaders(url); + return true; + } catch (Exception e) { + return false; + } + } +} diff --git a/solidify-orcid/src/main/java/ch/unige/solidify/service/OrcidWebsiteChecker.java b/solidify-orcid/src/main/java/ch/unige/solidify/service/OrcidWebsiteChecker.java new file mode 100644 index 0000000000000000000000000000000000000000..7afc056a910aeb3cd79ce140c5b82e24954830b1 --- /dev/null +++ b/solidify-orcid/src/main/java/ch/unige/solidify/service/OrcidWebsiteChecker.java @@ -0,0 +1,37 @@ +/*- + * %%---------------------------------------------------------------------------------------------- + * Solidify Framework - Solidify ORCID - OrcidWebsiteChecker.java + * SPDX-License-Identifier: GPL-2.0-or-later + * %----------------------------------------------------------------------------------------------% + * Copyright (C) 2017 - 2024 University of Geneva + * %----------------------------------------------------------------------------------------------% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-2.0.html>. + * ----------------------------------------------------------------------------------------------%% + */ +package ch.unige.solidify.service; + +public abstract class OrcidWebsiteChecker { + protected final String checkUrl; + + public OrcidWebsiteChecker(String checkUrl) { + this.checkUrl = checkUrl; + } + + public abstract boolean checkOrcidExists(String orcid); + + protected String getCheckUrl(String orcid) { + return this.checkUrl.replace("{orcid}", orcid); + } +} diff --git a/solidify-orcid/src/main/java/ch/unige/solidify/service/OrcidWebsitesService.java b/solidify-orcid/src/main/java/ch/unige/solidify/service/OrcidWebsitesService.java new file mode 100644 index 0000000000000000000000000000000000000000..30519228bdfe636ac28adb04df01e2e5376f2274 --- /dev/null +++ b/solidify-orcid/src/main/java/ch/unige/solidify/service/OrcidWebsitesService.java @@ -0,0 +1,72 @@ +/*- + * %%---------------------------------------------------------------------------------------------- + * Solidify Framework - Solidify ORCID - OrcidWebsitesService.java + * SPDX-License-Identifier: GPL-2.0-or-later + * %----------------------------------------------------------------------------------------------% + * Copyright (C) 2017 - 2024 University of Geneva + * %----------------------------------------------------------------------------------------------% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-2.0.html>. + * ----------------------------------------------------------------------------------------------%% + */ +package ch.unige.solidify.service; + +import java.util.ArrayList; +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Service; + +import ch.unige.solidify.config.SolidifyProperties; +import ch.unige.solidify.controller.OrcidController; +import ch.unige.solidify.model.OrcidWebsite; +import ch.unige.solidify.model.OrcidWebsiteDTO; + +@Service +@ConditionalOnBean(OrcidController.class) +public class OrcidWebsitesService { + + private static final Logger log = LoggerFactory.getLogger(OrcidWebsitesService.class); + + private final static String CACHE_NAME = "SolidifyOrcidExternalWebsites"; + + private final List<OrcidWebsite> externalWebsites; + + public OrcidWebsitesService(SolidifyProperties solidifyProperties) { + this.externalWebsites = List.of(solidifyProperties.getOrcid().getWebsites()); + } + + @Cacheable(CACHE_NAME) + public List<OrcidWebsiteDTO> getAvailableWebsitesForOrcid(String orcid) { + List<OrcidWebsiteDTO> availableOrcidWebsites = new ArrayList<>(); + for (OrcidWebsite orcidWebsite : this.externalWebsites) { + OrcidWebsiteChecker checker = new OrcidUrlChecker(orcidWebsite.getCheckUrl()); + if (checker.checkOrcidExists(orcid)) { + availableOrcidWebsites.add(new OrcidWebsiteDTO(orcidWebsite, orcid)); + } + } + return availableOrcidWebsites; + } + + @Scheduled(fixedDelayString = "${solidify.orcid.externalWebsitesRefreshDelay:600000}") + @CacheEvict(value = CACHE_NAME, allEntries = true) + public void evictCache() { + log.debug("All ORCID external websites evicted from cache"); + } +}