diff --git a/pom.xml b/pom.xml index 4554a4f..b563a73 100644 --- a/pom.xml +++ b/pom.xml @@ -115,12 +115,8 @@ eu.dnetlib uoa-clients - [2.0.0-SNAPSHOT, 3.0.0) + [2.0.0-SNAPSHOT,) - - log4j - log4j - javax.servlet javax.servlet-api @@ -135,23 +131,11 @@ eu.dnetlib dnet-runtime 1.0.2-SNAPSHOT - - - log4j - log4j - - eu.dnetlib uoa-hcm [2.0.0-SNAPSHOT, 3.0.0) - - - log4j - log4j - - eu.dnetlib @@ -320,13 +304,7 @@ org.eurocris openaire-cris-validator - 1.0.1-SNAPSHOT - - - - - - + 2.0.2-SNAPSHOT diff --git a/src/main/java/eu/dnetlib/repo/manager/controllers/UserRoleController.java b/src/main/java/eu/dnetlib/repo/manager/controllers/UserRoleController.java index 9f4d8b7..ee13ee2 100644 --- a/src/main/java/eu/dnetlib/repo/manager/controllers/UserRoleController.java +++ b/src/main/java/eu/dnetlib/repo/manager/controllers/UserRoleController.java @@ -1,108 +1,104 @@ -//package eu.dnetlib.repo.manager.controllers; -// -//import eu.dnetlib.repo.manager.domain.dto.Role; -//import eu.dnetlib.repo.manager.service.aai.registry.AaiRegistryService; -//import eu.dnetlib.repo.manager.service.security.AuthoritiesUpdater; -//import eu.dnetlib.repo.manager.service.security.AuthorizationService; -//import eu.dnetlib.repo.manager.service.security.RoleMappingService; -//import eu.dnetlib.repo.manager.utils.JsonUtils; -//import io.swagger.annotations.ApiOperation; -//import org.springframework.beans.factory.annotation.Autowired; -//import org.springframework.http.HttpStatus; -//import org.springframework.http.ResponseEntity; -//import org.springframework.security.access.prepost.PreAuthorize; -//import org.springframework.web.bind.annotation.*; -// -//import javax.ws.rs.core.MediaType; -//import javax.ws.rs.core.Response; -//import java.util.Collection; -// -////@RestController -////@RequestMapping(value = "/role-management") -////@Api(description = "Role Management", value = "role-management") -//public class UserRoleController { -// -// private final AaiRegistryService aaiRegistryService; -// private final AuthoritiesUpdater authoritiesUpdater; -// private final RoleMappingService roleMappingService; -// private final AuthorizationService authorizationService; -// -// @Autowired -// UserRoleController(AaiRegistryService aaiRegistryService, -// AuthoritiesUpdater authoritiesUpdater, -// RoleMappingService roleMappingService, -// AuthorizationService authorizationService) { -// this.aaiRegistryService = aaiRegistryService; -// this.authoritiesUpdater = authoritiesUpdater; -// this.roleMappingService = roleMappingService; -// this.authorizationService = authorizationService; -// } -// -// /** -// * Get the role with the given id. -// **/ -// @RequestMapping(method = RequestMethod.GET, path = "/role/{id}") -//// @PreAuthorize("hasAnyAuthority('REGISTERED_USER', 'SUPER_ADMINISTRATOR', 'CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR')") -// public Response getRole(@RequestParam(value = "type", defaultValue = "datasource") String type, @PathVariable("id") String id) { -// int roleId = aaiRegistryService.getCouId(type, id); -// return Response.status(HttpStatus.OK.value()).entity(JsonUtils.createResponse("Role id is: " + roleId).toString()).type(MediaType.APPLICATION_JSON).build(); -// } -// -// /** -// * Create a new role with the given name and description. -// **/ -// @RequestMapping(method = RequestMethod.POST, path = "/role") -// @PreAuthorize("hasAnyAuthority('SUPER_ADMINISTRATOR')") -// public Response createRole(@RequestBody Role role) { -// aaiRegistryService.createRole(role); -// return Response.status(HttpStatus.OK.value()).entity(JsonUtils.createResponse("Role has been created").toString()).type(MediaType.APPLICATION_JSON).build(); -// } -// -// /** -// * Subscribe to a type(Community, etc.) with id(ee, egi, etc.) -// */ -// @ApiOperation(value = "subscribe") -// @RequestMapping(method = RequestMethod.POST, path = "/subscribe/{type}/{id}") -// @PreAuthorize("hasAnyAuthority('SUPER_ADMINISTRATOR', 'CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR')") -// public Response subscribe(@PathVariable("type") String type, @PathVariable("id") String id) { -// Integer coPersonId = aaiRegistryService.getCoPersonIdByIdentifier(); -// if (coPersonId == null) { -// coPersonId = aaiRegistryService.getCoPersonIdsByEmail(); -// } -// Integer couId = aaiRegistryService.getCouId(type, id); -// if (couId != null) { -// aaiRegistryService.assignMemberRole(coPersonId, couId); -// -// // Add role to current authorities -// authoritiesUpdater.addRole(roleMappingService.convertRepoIdToAuthority(id)); -// -// return Response.status(HttpStatus.OK.value()).entity(JsonUtils.createResponse("Role has been assigned").toString()).type(MediaType.APPLICATION_JSON).build(); -// } else { -// return Response.status(HttpStatus.NOT_FOUND.value()).entity(JsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build(); -// } -// } -// ///////////////////////////////////////////////////////////////////////////////////////////// -// ///////////////////////////////////////////////////////////////////////////////////////////// -// -// @RequestMapping(method = RequestMethod.GET, path = "/users/couid/{id}") -// @PreAuthorize("hasAnyAuthority('SUPER_ADMINISTRATOR', 'CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR')") -// public ResponseEntity getUsersByCouId(@PathVariable("id") Integer id) { -//// calls.getUserByCoId() -// return ResponseEntity.ok(aaiRegistryService.getUsersByCouId(id).toString()); -// } -// -// -// @RequestMapping(method = RequestMethod.GET, path = "/users/{email}/roles") -// @PreAuthorize("hasAnyAuthority('SUPER_ADMINISTRATOR', 'CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR') or hasAuthority('REGISTERED_USER') and authentication.userInfo.email==#email") -// public ResponseEntity> getRolesByEmail(@PathVariable("email") String email) { -// return ResponseEntity.ok(authorizationService.getUserRolesByEmail(email)); -// } -// -// -// @RequestMapping(method = RequestMethod.GET, path = "/user/roles/my") -// @PreAuthorize("hasAuthority('REGISTERED_USER')") -// public ResponseEntity> getRoleNames() { -// return ResponseEntity.ok(authorizationService.getUserRoles()); -// } -// -//} +package eu.dnetlib.repo.manager.controllers; + +import eu.dnetlib.repo.manager.domain.dto.Role; +import eu.dnetlib.repo.manager.service.aai.registry.AaiRegistryService; +import eu.dnetlib.repo.manager.service.security.AuthoritiesUpdater; +import eu.dnetlib.repo.manager.service.security.AuthorizationService; +import eu.dnetlib.repo.manager.service.security.RoleMappingService; +import eu.dnetlib.repo.manager.utils.JsonUtils; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.web.bind.annotation.*; + +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import java.util.Collection; + +//@RestController +//@RequestMapping(value = "/role-management") +//@Api(description = "Role Management", value = "role-management") +public class UserRoleController { + + private final AaiRegistryService aaiRegistryService; + private final AuthoritiesUpdater authoritiesUpdater; + private final RoleMappingService roleMappingService; + private final AuthorizationService authorizationService; + + UserRoleController(AaiRegistryService aaiRegistryService, + AuthoritiesUpdater authoritiesUpdater, + RoleMappingService roleMappingService, + AuthorizationService authorizationService) { + this.aaiRegistryService = aaiRegistryService; + this.authoritiesUpdater = authoritiesUpdater; + this.roleMappingService = roleMappingService; + this.authorizationService = authorizationService; + } + + /** + * Get the role with the given id. + **/ + @RequestMapping(method = RequestMethod.GET, path = "/role/{id}") +// @PreAuthorize("hasAnyAuthority('REGISTERED_USER', 'SUPER_ADMINISTRATOR', 'CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR')") + public Response getRole(@RequestParam(value = "type", defaultValue = "datasource") String type, @PathVariable("id") String id) { + int roleId = aaiRegistryService.getCouId(type, id); + return Response.status(HttpStatus.OK.value()).entity(JsonUtils.createResponse("Role id is: " + roleId).toString()).type(MediaType.APPLICATION_JSON).build(); + } + + /** + * Create a new role with the given name and description. + **/ + @RequestMapping(method = RequestMethod.POST, path = "/role") + @PreAuthorize("hasAnyAuthority('SUPER_ADMINISTRATOR')") + public Response createRole(@RequestBody Role role) { + aaiRegistryService.createRole(role); + return Response.status(HttpStatus.OK.value()).entity(JsonUtils.createResponse("Role has been created").toString()).type(MediaType.APPLICATION_JSON).build(); + } + + /** + * Subscribe to a type(Community, etc.) with id(ee, egi, etc.) + */ + @RequestMapping(method = RequestMethod.POST, path = "/subscribe/{type}/{id}") + @PreAuthorize("hasAnyAuthority('SUPER_ADMINISTRATOR', 'CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR')") + public Response subscribe(@PathVariable("type") String type, @PathVariable("id") String id) { + Integer coPersonId = aaiRegistryService.getCoPersonIdByIdentifier(); + if (coPersonId == null) { + coPersonId = aaiRegistryService.getCoPersonIdsByEmail().get(0); + } + Integer couId = aaiRegistryService.getCouId(type, id); + if (couId != null) { + aaiRegistryService.assignMemberRole(coPersonId, couId); + + // Add role to current authorities + authoritiesUpdater.addRole(roleMappingService.repositoryIdToAuthority(id)); + + return Response.status(HttpStatus.OK.value()).entity(JsonUtils.createResponse("Role has been assigned").toString()).type(MediaType.APPLICATION_JSON).build(); + } else { + return Response.status(HttpStatus.NOT_FOUND.value()).entity(JsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build(); + } + } + ///////////////////////////////////////////////////////////////////////////////////////////// + ///////////////////////////////////////////////////////////////////////////////////////////// + + @RequestMapping(method = RequestMethod.GET, path = "/users/couid/{id}") + @PreAuthorize("hasAnyAuthority('SUPER_ADMINISTRATOR', 'CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR')") + public ResponseEntity getUsersByCouId(@PathVariable("id") Integer id) { +// calls.getUserByCoId() + return ResponseEntity.ok(aaiRegistryService.getUsersByCouId(id).toString()); + } + + + @RequestMapping(method = RequestMethod.GET, path = "/users/{email}/roles") + @PreAuthorize("hasAnyAuthority('SUPER_ADMINISTRATOR', 'CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR') or hasAuthority('REGISTERED_USER') and authentication.userInfo.email==#email") + public ResponseEntity> getRolesByEmail(@PathVariable("email") String email) { + return ResponseEntity.ok(authorizationService.getUserRolesByEmail(email)); + } + + + @RequestMapping(method = RequestMethod.GET, path = "/user/roles/my") + @PreAuthorize("hasAuthority('REGISTERED_USER')") + public ResponseEntity> getRoleNames() { + return ResponseEntity.ok(authorizationService.getUserRoles()); + } + +} diff --git a/src/main/java/eu/dnetlib/repo/manager/domain/Term.java b/src/main/java/eu/dnetlib/repo/manager/domain/Term.java index 63e3c48..0fa39a1 100644 --- a/src/main/java/eu/dnetlib/repo/manager/domain/Term.java +++ b/src/main/java/eu/dnetlib/repo/manager/domain/Term.java @@ -1,6 +1,8 @@ package eu.dnetlib.repo.manager.domain; +import java.util.ArrayList; +import java.util.List; /** * Created by panagiotis on 15/1/2018. @@ -11,6 +13,9 @@ public class Term { private String nativeName; private String encoding; private String code; + private List synonyms = new ArrayList<>(); + + public Term() {} public Term(String englishName, String nativeName, String encoding, String code) { this.englishName = englishName; @@ -19,6 +24,14 @@ public class Term { this.code = code; } + public Term(String englishName, String nativeName, String encoding, String code, List synonyms) { + this.englishName = englishName; + this.nativeName = nativeName; + this.encoding = encoding; + this.code = code; + this.synonyms = synonyms; + } + public String getEnglishName() { return englishName; } @@ -50,4 +63,12 @@ public class Term { public void setCode(String code) { this.code = code; } + + public List getSynonyms() { + return synonyms; + } + + public void setSynonyms(List synonyms) { + this.synonyms = synonyms; + } } diff --git a/src/main/java/eu/dnetlib/repo/manager/service/BrokerServiceImpl.java b/src/main/java/eu/dnetlib/repo/manager/service/BrokerServiceImpl.java index de2d52e..e6dbc5d 100644 --- a/src/main/java/eu/dnetlib/repo/manager/service/BrokerServiceImpl.java +++ b/src/main/java/eu/dnetlib/repo/manager/service/BrokerServiceImpl.java @@ -34,8 +34,7 @@ import java.util.Map; @Service("brokerService") public class BrokerServiceImpl implements BrokerService { - @Autowired - private RepositoryServiceImpl repoAPI; + @Value("${services.provide.broker.url}:${services.provide.broker.port}/${services.provide.broker.api}${services.provide.broker.openaire}") private String openairePath; @Value("${services.provide.broker.url}:${services.provide.broker.port}/${services.provide.broker.api}") @@ -45,13 +44,19 @@ public class BrokerServiceImpl implements BrokerService { private static final Logger logger = LoggerFactory.getLogger(BrokerServiceImpl.class); - @Autowired - RestTemplate restTemplate; + private final RepositoryService repoAPI; + private final RestTemplate restTemplate; private HttpHeaders httpHeaders; private final HashMap topics = new HashMap<>(); + public BrokerServiceImpl(RepositoryService repositoryService, RestTemplate restTemplate) { + logger.info("init broker service"); + this.repoAPI = repositoryService; + this.restTemplate = restTemplate; + } + @PostConstruct private void initDnetTopicsMap() { diff --git a/src/main/java/eu/dnetlib/repo/manager/service/RepositoryServiceImpl.java b/src/main/java/eu/dnetlib/repo/manager/service/RepositoryServiceImpl.java index c8e3366..e4d6a44 100644 --- a/src/main/java/eu/dnetlib/repo/manager/service/RepositoryServiceImpl.java +++ b/src/main/java/eu/dnetlib/repo/manager/service/RepositoryServiceImpl.java @@ -106,38 +106,42 @@ public class RepositoryServiceImpl implements RepositoryService { private void init() { logger.debug("Initialization method of repository api! Updated version!"); - for (String key : this.getVocabulary("dnet:datasource_typologies").getAsMap().keySet()) { - if (key.contains("aggregator")) { - dataSourceClass.putIfAbsent("aggregator", new ArrayList<>()); - dataSourceClass.get("aggregator").add(key); - } else if (key.contains("crissystem")) { - dataSourceClass.putIfAbsent("dris", new ArrayList<>()); - dataSourceClass.get("dris").add(key); - } else if (key.contains("pubsrepository::journal")) { // do not change order --> - dataSourceClass.putIfAbsent("journal", Collections.singletonList("pubsrepository::journal")); - } else if (key.contains("pubsrepository")) { // do not change order <-- - dataSourceClass.putIfAbsent("opendoar", new ArrayList<>()); - dataSourceClass.get("opendoar").add(key); - } else if (key.contains("datarepository")) { - dataSourceClass.putIfAbsent("re3data", Collections.singletonList("datarepository::unknown")); + try { + for (String key : this.getVocabulary("dnet:datasource_typologies").getAsMap().keySet()) { + if (key.contains("aggregator")) { + dataSourceClass.putIfAbsent("aggregator", new ArrayList<>()); + dataSourceClass.get("aggregator").add(key); + } else if (key.contains("crissystem")) { + dataSourceClass.putIfAbsent("dris", new ArrayList<>()); + dataSourceClass.get("dris").add(key); + } else if (key.contains("pubsrepository::journal")) { // do not change order --> + dataSourceClass.putIfAbsent("journal", Collections.singletonList("pubsrepository::journal")); + } else if (key.contains("pubsrepository")) { // do not change order <-- + dataSourceClass.putIfAbsent("opendoar", new ArrayList<>()); + dataSourceClass.get("opendoar").add(key); + } else if (key.contains("datarepository")) { + dataSourceClass.putIfAbsent("re3data", Collections.singletonList("datarepository::unknown")); + } } - } - for (Map.Entry> entry : dataSourceClass.entrySet()) { - entry.getValue().forEach(v -> invertedDataSourceClass.put(v, entry.getKey())); - } + for (Map.Entry> entry : dataSourceClass.entrySet()) { + entry.getValue().forEach(v -> invertedDataSourceClass.put(v, entry.getKey())); + } - httpHeaders = new HttpHeaders(); - httpHeaders.setContentType(MediaType.APPLICATION_JSON); + httpHeaders = new HttpHeaders(); + httpHeaders.setContentType(MediaType.APPLICATION_JSON); - for (String vocName : vocabularyNames) { - vocabularyMap.put(vocName, vocabularyLoader.getVocabulary(vocName, Locale.ENGLISH, Locale.ROOT)); - } + for (String vocName : vocabularyNames) { + this.getVocabulary(vocName); + } - Country[] countries = getCountries(); - for (Country c : countries) { - countriesMap.put(c.getName(), c.getCode()); - inverseCountriesMap.put(c.getCode(), c.getName()); + Country[] countries = getCountries(); + for (Country c : countries) { + countriesMap.put(c.getName(), c.getCode()); + inverseCountriesMap.put(c.getCode(), c.getName()); + } + } catch (Exception e) { + logger.error("Could not retrieve vocabularies", e); } } diff --git a/src/main/java/eu/dnetlib/repo/manager/service/vocabulary/DnetVocabulary.java b/src/main/java/eu/dnetlib/repo/manager/service/vocabulary/DnetVocabulary.java new file mode 100644 index 0000000..524be7e --- /dev/null +++ b/src/main/java/eu/dnetlib/repo/manager/service/vocabulary/DnetVocabulary.java @@ -0,0 +1,69 @@ +package eu.dnetlib.repo.manager.service.vocabulary; + +import eu.dnetlib.repo.manager.domain.Term; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; + +public class DnetVocabulary { + private String id; + private String name; + private String description; + private String code; + private List terms = new ArrayList<>(); + + public DnetVocabulary() { + } + + public static eu.dnetlib.domain.enabling.Vocabulary toVocabulary(DnetVocabulary vocabulary) { + Map nameMap = new TreeMap<>(); + Map encodingMap = new TreeMap<>(); + for (Term term : vocabulary.getTerms()) { + nameMap.put(term.getEnglishName(), term.getCode()); + encodingMap.put(term.getCode(), term.getEnglishName()); + } + return new eu.dnetlib.domain.enabling.Vocabulary(vocabulary.getName(), nameMap, encodingMap); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public List getTerms() { + return terms; + } + + public void setTerms(List terms) { + this.terms = terms; + } +} diff --git a/src/main/java/eu/dnetlib/repo/manager/service/vocabulary/JsonApiVocabularyLoader.java b/src/main/java/eu/dnetlib/repo/manager/service/vocabulary/JsonApiVocabularyLoader.java new file mode 100644 index 0000000..0de196e --- /dev/null +++ b/src/main/java/eu/dnetlib/repo/manager/service/vocabulary/JsonApiVocabularyLoader.java @@ -0,0 +1,37 @@ +package eu.dnetlib.repo.manager.service.vocabulary; + +import eu.dnetlib.domain.enabling.Vocabulary; +import gr.uoa.di.driver.enabling.vocabulary.VocabularyLoader; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Primary; +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; + +import java.util.Locale; + +@Service +@Primary +public class JsonApiVocabularyLoader implements VocabularyLoader { + + private static final Logger logger = LoggerFactory.getLogger(JsonApiVocabularyLoader.class); + private static final String url = "%s/provision/mvc/vocabularies/%s.json"; + + private final String endpoint; + private final RestTemplate restTemplate = new RestTemplate(); + + public JsonApiVocabularyLoader(@Value("${services.openaireServicesBaseUrl}") String endpoint) { + this.endpoint = endpoint; + } + + @Override + public Vocabulary getVocabulary(String vocabularyName, Locale locale, Locale defaultLocale) { + DnetVocabulary voc = restTemplate.getForObject(String.format(url, endpoint, vocabularyName), DnetVocabulary.class); + if (voc == null) { + logger.error("Error retrieving vocabulary '{}'", vocabularyName); + return null; + } + return DnetVocabulary.toVocabulary(voc); + } +} diff --git a/src/main/java/eu/dnetlib/repo/manager/utils/CrisValidatorUtils.java b/src/main/java/eu/dnetlib/repo/manager/utils/CrisValidatorUtils.java index 9d47bf4..3674cd0 100644 --- a/src/main/java/eu/dnetlib/repo/manager/utils/CrisValidatorUtils.java +++ b/src/main/java/eu/dnetlib/repo/manager/utils/CrisValidatorUtils.java @@ -59,6 +59,9 @@ public class CrisValidatorUtils { public static List crisResultsToJobEntries(Job crisJob) { List jobResultEntries = new ArrayList<>(); + if (crisJob.getRuleResults() == null) { + crisJob.setRuleResults(new ArrayList<>()); + } for (RuleResults ruleResults : crisJob.getRuleResults()) { Rule rule = ruleResults.getRule(); JobResultEntry jobResultEntry = new JobResultEntry();