2019-02-16 11:04:41 +01:00
package eu.dnetlib.repo.manager.controllers ;
import eu.dnetlib.domain.data.Repository ;
import eu.dnetlib.domain.data.RepositoryInterface ;
2019-11-28 12:25:56 +01:00
import eu.dnetlib.repo.manager.domain.* ;
2022-03-15 12:33:49 +01:00
import eu.dnetlib.repo.manager.domain.dto.RepositoryTerms ;
2021-07-21 13:51:18 +02:00
import eu.dnetlib.repo.manager.domain.dto.User ;
2019-02-16 11:04:41 +01:00
import eu.dnetlib.repo.manager.exception.ResourceNotFoundException ;
2021-07-21 13:51:18 +02:00
import eu.dnetlib.repo.manager.service.RepositoryService ;
import eu.dnetlib.repo.manager.service.security.AuthorizationService ;
import eu.dnetlib.repo.manager.utils.JsonUtils ;
2019-02-16 11:04:41 +01:00
import io.swagger.annotations.Api ;
2020-06-05 10:39:43 +02:00
import org.apache.log4j.Logger ;
2019-02-16 11:04:41 +01:00
import org.json.JSONException ;
2019-12-18 11:04:22 +01:00
import org.mitre.openid.connect.model.OIDCAuthenticationToken ;
2019-02-16 11:04:41 +01:00
import org.springframework.beans.factory.annotation.Autowired ;
2021-07-21 13:51:18 +02:00
import org.springframework.http.HttpStatus ;
2019-02-16 11:04:41 +01:00
import org.springframework.http.MediaType ;
2021-07-21 13:51:18 +02:00
import org.springframework.http.ResponseEntity ;
2019-10-11 13:53:48 +02:00
import org.springframework.security.access.prepost.PostAuthorize ;
2019-02-16 11:04:41 +01:00
import org.springframework.security.access.prepost.PreAuthorize ;
import org.springframework.security.core.Authentication ;
2019-12-18 11:04:22 +01:00
import org.springframework.security.core.context.SecurityContextHolder ;
2019-02-16 11:04:41 +01:00
import org.springframework.web.bind.annotation.* ;
2021-07-21 13:51:18 +02:00
import javax.ws.rs.core.Response ;
2019-02-16 11:04:41 +01:00
import java.io.IOException ;
2022-03-15 12:33:49 +01:00
import java.util.Date ;
2019-02-16 11:04:41 +01:00
import java.util.List ;
import java.util.Map ;
@RestController
2021-07-21 13:51:18 +02:00
@RequestMapping ( value = " /repositories " )
@Api ( description = " Repository API " , tags = { " repositories " } )
2019-02-16 11:04:41 +01:00
public class RepositoryController {
2021-07-21 13:51:18 +02:00
private static final Logger logger = Logger . getLogger ( RepositoryController . class ) ;
private final RepositoryService repositoryService ;
private final AuthorizationService authorizationService ;
2020-06-05 10:39:43 +02:00
2019-02-16 11:04:41 +01:00
@Autowired
2021-07-21 13:51:18 +02:00
RepositoryController ( RepositoryService repositoryService ,
AuthorizationService authorizationService ) {
this . repositoryService = repositoryService ;
this . authorizationService = authorizationService ;
}
2019-02-16 11:04:41 +01:00
2021-07-21 13:51:18 +02:00
@RequestMapping ( value = " /countries " , method = RequestMethod . GET , produces = MediaType . APPLICATION_JSON_VALUE )
2019-02-16 11:04:41 +01:00
@ResponseBody
public Country [ ] getCountries ( ) {
return repositoryService . getCountries ( ) ;
}
@RequestMapping ( value = " /getRepositoriesByCountry/{country}/{mode} " , method = RequestMethod . GET ,
produces = MediaType . APPLICATION_JSON_VALUE )
@ResponseBody
public List < RepositorySnippet > getRepositoriesByCountry ( @PathVariable ( " country " ) String country ,
@PathVariable ( " mode " ) String mode ,
2021-07-21 13:51:18 +02:00
@RequestParam ( value = " managed " , required = false ) Boolean managed ) throws JSONException , IOException {
2019-02-16 11:04:41 +01:00
return repositoryService . getRepositoriesByCountry ( country , mode , managed ) ;
}
2021-07-21 13:51:18 +02:00
@RequestMapping ( value = " /snippets/user " , method = RequestMethod . GET ,
2019-02-16 11:04:41 +01:00
produces = MediaType . APPLICATION_JSON_VALUE )
@ResponseBody
2021-07-21 13:51:18 +02:00
@PreAuthorize ( " hasAuthority('REGISTERED_USER') " )
public List < RepositorySnippet > getRepositoriesSnippetsOfUser ( ) throws Exception {
2022-03-15 12:33:49 +01:00
return repositoryService . getRepositoriesSnippetsOfUser ( " 0 " , " 100 " ) ; // FIXME
}
@RequestMapping ( value = " /terms " , method = RequestMethod . POST ,
produces = MediaType . APPLICATION_JSON_VALUE )
@ResponseBody
@PreAuthorize ( " hasAuthority('REGISTERED_USER') " )
public void updateRepositoriesTerms ( @RequestBody List < RepositoryTerms > repositoriesTerms ) throws Exception {
Date date = new Date ( ) ;
if ( repositoriesTerms ! = null ) {
for ( RepositoryTerms terms : repositoriesTerms ) {
Repository repository = repositoryService . getRepositoryById ( terms . getId ( ) ) ;
repository . setConsentTermsOfUse ( terms . getConsentTermsOfUse ( ) ) ;
repository . setFullTextDownload ( terms . getFullTextDownload ( ) ) ;
repository . setConsentTermsOfUseDate ( date ) ;
repositoryService . updateRepository ( repository , SecurityContextHolder . getContext ( ) . getAuthentication ( ) ) ;
}
}
2019-02-16 11:04:41 +01:00
}
2021-07-21 13:51:18 +02:00
@RequestMapping ( value = " /searchRegisteredRepositories/{page}/{size} " , method = RequestMethod . GET ,
2019-07-19 11:31:23 +02:00
produces = MediaType . APPLICATION_JSON_VALUE )
@ResponseBody
2021-07-21 13:51:18 +02:00
@PreAuthorize ( " hasAuthority('SUPER_ADMINISTRATOR') or hasAuthority('CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR') " )
public List < RepositorySnippet > searchRegisteredRepositories ( @RequestParam ( name = " country " , required = false ) String country ,
@RequestParam ( name = " typology " , required = false ) String typology ,
@RequestParam ( name = " englishName " , required = false ) String englishName ,
@RequestParam ( name = " officialName " , required = false ) String officialName ,
2019-07-19 11:31:23 +02:00
@RequestParam ( " requestSortBy " ) String requestSortBy ,
@RequestParam ( " order " ) String order ,
@PathVariable ( " page " ) int page ,
@PathVariable ( " size " ) int pageSize ) throws Exception {
return repositoryService . searchRegisteredRepositories ( country , typology , englishName , officialName , requestSortBy , order , page , pageSize ) ;
}
2019-02-16 11:04:41 +01:00
@RequestMapping ( value = " /getRepositoryById/{id} " , method = RequestMethod . GET ,
produces = MediaType . APPLICATION_JSON_VALUE )
@ResponseBody
2021-07-28 14:28:58 +02:00
@PostAuthorize ( " hasAnyAuthority('SUPER_ADMINISTRATOR', 'CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR') or @authorizationService.isMemberOf(#id) or (returnObject.registeredBy=='null' and hasAuthority('REGISTERED_USER')) " )
2021-07-21 13:51:18 +02:00
public Repository getRepositoryById ( @PathVariable ( " id " ) String id ) throws JSONException , ResourceNotFoundException {
2019-11-08 13:56:47 +01:00
Repository repo = repositoryService . getRepositoryById ( id ) ;
2020-06-05 10:39:43 +02:00
2021-07-21 13:51:18 +02:00
if ( repo ! = null )
logger . info ( " Returning repository " + repo . getId ( ) + " registered by " + repo . getRegisteredBy ( ) ) ;
else
logger . info ( " Requested repository " + id + " not found " ) ;
2019-11-08 13:56:47 +01:00
return repo ;
2019-02-16 11:04:41 +01:00
}
@RequestMapping ( value = " /getRepositoryAggregations/{id} " , method = RequestMethod . GET ,
produces = MediaType . APPLICATION_JSON_VALUE )
@ResponseBody
public List < AggregationDetails > getRepositoryAggregations ( @PathVariable ( " id " ) String id ) throws JSONException {
2021-07-21 13:51:18 +02:00
return repositoryService . getRepositoryAggregations ( id , 0 , 20 ) ;
2019-02-16 11:04:41 +01:00
}
@RequestMapping ( value = " /getRepositoryAggregationsByYear/{id} " , method = RequestMethod . GET ,
produces = MediaType . APPLICATION_JSON_VALUE )
@ResponseBody
public Map < String , List < AggregationDetails > > getRepositoryAggregationsByYear ( @PathVariable ( " id " ) String id ) throws JSONException {
return repositoryService . getRepositoryAggregationsByYear ( id ) ;
}
@RequestMapping ( value = " /getRepositoriesByName/{name:.+}/{page}/{size}/ " , method = RequestMethod . GET ,
produces = MediaType . APPLICATION_JSON_VALUE )
@ResponseBody
public List < Repository > getRepositoriesByName ( @PathVariable ( " name " ) String name ,
@PathVariable ( " page " ) String page ,
@PathVariable ( " size " ) String size ) throws JSONException {
return repositoryService . getRepositoriesByName ( name , page , size ) ;
}
@RequestMapping ( value = " /getRepositoryInterface/{id} " , method = RequestMethod . GET ,
produces = MediaType . APPLICATION_JSON_VALUE )
@ResponseBody
2021-07-21 13:51:18 +02:00
@PostAuthorize ( " hasAuthority('SUPER_ADMINISTRATOR') or hasAuthority('CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR') or @authorizationService.isMemberOf(#id) or (@repositoryService.getRepositoryById(#id).registeredBy=='null' and hasAuthority('REGISTERED_USER')) " )
2019-02-16 11:04:41 +01:00
public List < RepositoryInterface > getRepositoryInterface ( @PathVariable ( " id " ) String id ) throws JSONException {
return repositoryService . getRepositoryInterface ( id ) ;
}
@RequestMapping ( value = " /addRepository " , method = RequestMethod . POST ,
consumes = MediaType . APPLICATION_JSON_VALUE )
@ResponseBody
2021-07-21 13:51:18 +02:00
// @PreAuthorize("hasAuthority('SUPER_ADMINISTRATOR') or hasAuthority('CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR') or (hasAuthority(@authorizationService.convertRepoIdToRoleId(#repository.id)) or hasAuthority(@authorizationService.convertRepoIdToRoleId(returnObject.id)))")
@PreAuthorize ( " hasAuthority('SUPER_ADMINISTRATOR') or hasAuthority('CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR') or hasAuthority('REGISTERED_USER') " )
2019-02-16 11:04:41 +01:00
public Repository addRepository ( @RequestParam ( " datatype " ) String datatype ,
@RequestBody Repository repository ) throws Exception {
return repositoryService . addRepository ( datatype , repository ) ;
}
@RequestMapping ( value = " /getDnetCountries " , method = RequestMethod . GET ,
produces = MediaType . APPLICATION_JSON_VALUE )
@ResponseBody
2021-07-21 13:51:18 +02:00
public List < String > getDnetCountries ( ) {
2019-02-16 11:04:41 +01:00
return repositoryService . getDnetCountries ( ) ;
}
@RequestMapping ( value = " /getTypologies " , method = RequestMethod . GET ,
produces = MediaType . APPLICATION_JSON_VALUE )
@ResponseBody
2021-07-21 13:51:18 +02:00
public List < String > getTypologies ( ) {
2019-02-16 11:04:41 +01:00
return repositoryService . getTypologies ( ) ;
}
@RequestMapping ( value = " /getTimezones " , method = RequestMethod . GET ,
produces = MediaType . APPLICATION_JSON_VALUE )
@ResponseBody
2021-07-21 13:51:18 +02:00
public List < Timezone > getTimezones ( ) {
2019-02-16 11:04:41 +01:00
return repositoryService . getTimezones ( ) ;
}
@RequestMapping ( value = " /updateRepository " , method = RequestMethod . POST ,
consumes = MediaType . APPLICATION_JSON_VALUE )
@ResponseBody
2021-10-22 13:32:44 +02:00
@PreAuthorize ( " hasAuthority('SUPER_ADMINISTRATOR') or hasAuthority('CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR') or @authorizationService.isMemberOfInterface(#repository.id) " )
2021-07-21 13:51:18 +02:00
public Repository updateRepository ( @RequestBody Repository repository , Authentication authentication ) throws Exception {
2019-02-16 11:04:41 +01:00
return repositoryService . updateRepository ( repository , authentication ) ;
}
@RequestMapping ( value = " /deleteInterface/ " , method = RequestMethod . DELETE )
2021-10-22 13:32:44 +02:00
@PreAuthorize ( " hasAuthority('SUPER_ADMINISTRATOR') or hasAuthority('CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR') or @authorizationService.isMemberOfInterface(#id) " )
2021-07-21 13:51:18 +02:00
public void deleteRepositoryInterface ( @RequestParam ( " id " ) String id ,
@RequestParam ( " registeredBy " ) String registeredBy ) {
2019-02-16 11:04:41 +01:00
repositoryService . deleteRepositoryInterface ( id , registeredBy ) ;
}
@RequestMapping ( value = " /addInterface " , method = RequestMethod . POST ,
consumes = MediaType . APPLICATION_JSON_VALUE )
@ResponseBody
2021-07-21 13:51:18 +02:00
@PreAuthorize ( " hasAuthority('SUPER_ADMINISTRATOR') or hasAuthority('CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR') or @authorizationService.isMemberOf(#id) " )
2019-02-16 11:04:41 +01:00
public RepositoryInterface addRepositoryInterface ( @RequestParam ( " datatype " ) String datatype ,
2021-10-22 13:32:44 +02:00
@RequestParam ( " repoId " ) String id ,
2019-02-16 11:04:41 +01:00
@RequestParam ( " registeredBy " ) String registeredBy ,
2020-07-30 13:49:24 +02:00
@RequestParam ( value = " comment " , required = false ) String comment ,
2020-01-10 13:56:27 +01:00
@RequestBody RepositoryInterface repositoryInterface ) throws Exception {
2021-07-21 13:51:18 +02:00
return repositoryService . addRepositoryInterface ( datatype , id , registeredBy , comment , repositoryInterface ) ;
2019-02-16 11:04:41 +01:00
}
2021-10-22 13:32:44 +02:00
@RequestMapping ( value = " /updateRepositoryInterface " , method = RequestMethod . POST ,
consumes = MediaType . APPLICATION_JSON_VALUE )
@ResponseBody
@PreAuthorize ( " hasAuthority('SUPER_ADMINISTRATOR') or hasAuthority('CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR') or @authorizationService.isMemberOf(#id) " )
public RepositoryInterface updateRepositoryInterface ( @RequestParam ( " repoId " ) String id ,
@RequestParam ( " registeredBy " ) String registeredBy ,
@RequestParam ( value = " comment " , required = false ) String comment ,
@RequestBody RepositoryInterface repositoryInterface ) throws Exception {
return repositoryService . updateRepositoryInterface ( id , registeredBy , comment , repositoryInterface ) ;
}
2021-07-21 13:51:18 +02:00
@RequestMapping ( value = " /getUrlsOfUserRepos/{page}/{size}/ " , method = RequestMethod . GET ,
2019-02-16 11:04:41 +01:00
produces = MediaType . APPLICATION_JSON_VALUE )
@ResponseBody
2021-07-21 13:51:18 +02:00
@PreAuthorize ( " hasAuthority('REGISTERED_USER') " )
public List < String > getUrlsOfUserRepos ( @PathVariable ( " page " ) String page , @PathVariable ( " size " ) String size ) {
2020-01-10 13:56:27 +01:00
return repositoryService . getUrlsOfUserRepos ( ( ( OIDCAuthenticationToken ) SecurityContextHolder . getContext ( ) . getAuthentication ( ) ) . getUserInfo ( ) . getEmail ( ) , page , size ) ;
2019-02-16 11:04:41 +01:00
}
2021-07-21 13:51:18 +02:00
@RequestMapping ( value = " /getDatasourceVocabularies/{mode} " , method = RequestMethod . GET ,
2019-02-16 11:04:41 +01:00
produces = MediaType . APPLICATION_JSON_VALUE )
@ResponseBody
public List < String > getDatasourceVocabularies ( @PathVariable ( " mode " ) String mode ) {
2021-07-21 13:51:18 +02:00
return repositoryService . getDatasourceVocabularies ( mode ) ;
2019-02-16 11:04:41 +01:00
}
2021-07-21 13:51:18 +02:00
@RequestMapping ( value = " /getCompatibilityClasses/{mode} " , method = RequestMethod . GET ,
2019-02-16 11:04:41 +01:00
produces = MediaType . APPLICATION_JSON_VALUE )
@ResponseBody
2021-07-21 13:51:18 +02:00
public Map < String , String > getCompatibilityClasses ( @PathVariable ( " mode " ) String mode ) {
2019-02-16 11:04:41 +01:00
return repositoryService . getCompatibilityClasses ( mode ) ;
}
2021-07-21 13:51:18 +02:00
@RequestMapping ( value = " /getDatasourceClasses/{mode} " , method = RequestMethod . GET ,
2019-02-16 11:04:41 +01:00
produces = MediaType . APPLICATION_JSON_VALUE )
@ResponseBody
2021-07-21 13:51:18 +02:00
public Map < String , String > getDatasourceClasses ( @PathVariable ( " mode " ) String mode ) {
return repositoryService . getDatasourceClasses ( mode ) ;
2019-02-16 11:04:41 +01:00
}
2021-07-21 13:51:18 +02:00
@RequestMapping ( value = " /getMetricsInfoForRepository/{id} " , method = RequestMethod . GET ,
2019-02-16 11:04:41 +01:00
produces = MediaType . APPLICATION_JSON_VALUE )
@ResponseBody
2021-07-21 13:51:18 +02:00
public MetricsInfo getMetricsInfoForRepository ( @PathVariable ( " id " ) String id ) throws RepositoryServiceException {
return repositoryService . getMetricsInfoForRepository ( id ) ;
2019-02-16 11:04:41 +01:00
}
2021-07-21 13:51:18 +02:00
@RequestMapping ( value = " /getListLatestUpdate/{mode} " , method = RequestMethod . GET ,
2019-02-16 11:04:41 +01:00
produces = MediaType . APPLICATION_JSON_VALUE )
@ResponseBody
2021-07-21 13:51:18 +02:00
public Map < String , String > getListLatestUpdate ( @PathVariable ( " mode " ) String mode ) throws JSONException {
2019-02-16 11:04:41 +01:00
return repositoryService . getListLatestUpdate ( mode ) ;
}
2021-07-21 13:51:18 +02:00
///////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////
/ * *
* Get all the admins of the repository
* /
@RequestMapping ( method = RequestMethod . GET , path = " {id}/admins " )
@PreAuthorize ( " hasAnyAuthority('SUPER_ADMINISTRATOR', 'CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR') or @authorizationService.isMemberOf(#id) " )
public ResponseEntity < List < User > > getAdminsOfARepo ( @PathVariable ( " id " ) String id ) {
return new ResponseEntity < > ( authorizationService . getAdminsOfRepo ( id ) , HttpStatus . OK ) ;
}
/ * *
* Subscribe to repo by email
* /
@RequestMapping ( method = RequestMethod . POST , path = " {id}/admins " )
@PreAuthorize ( " hasAnyAuthority('SUPER_ADMINISTRATOR', 'CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR') or @authorizationService.isMemberOf(#id) " )
public Response subscribeByEmail ( @PathVariable ( " id " ) String id , @RequestBody String email ) throws ResourceNotFoundException {
authorizationService . addAdmin ( id , email ) ;
return Response . status ( HttpStatus . OK . value ( ) ) . entity ( JsonUtils . createResponse ( " Role has been assigned " ) . toString ( ) ) . type ( javax . ws . rs . core . MediaType . APPLICATION_JSON ) . build ( ) ;
}
/ * *
* Unsubscribe from repo by email
* /
@RequestMapping ( method = RequestMethod . DELETE , path = " {id}/admins/{email:.+} " )
@PreAuthorize ( " hasAnyAuthority('SUPER_ADMINISTRATOR', 'CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR') or @authorizationService.isMemberOf(#id) " )
public ResponseEntity < Void > unsubscribeByEmail ( @PathVariable ( " id " ) String id , @PathVariable ( " email " ) String email ) throws ResourceNotFoundException {
authorizationService . removeAdmin ( id , email ) ;
return new ResponseEntity < > ( HttpStatus . NO_CONTENT ) ;
2019-02-16 11:04:41 +01:00
}
}